On 03/06/2014 11:46 PM, Crístian Viana wrote:
Am 06-03-2014 12:12, schrieb Sheldon:
AFAK, seems two types has no string representation, one is unicode and another is object derived from nothing.
The statement if not isinstance(value, unicode), which is in the sample code above, makes sure that we will not try to convert a unicode object to string (only those who have a different type).

And an object derived from nothing also has a string representation. Take a look at this example:

>>> class X():
...     def x(self):
...             pass
...
>>> a = X()
>>> print str(a)
<__main__.X instance at 0x7f8fe13d4b00>
>>> print "this is a string: %s." % a
this is a string: <__main__.X instance at 0x7f8fe13d4b00>.

You forgot one. The a follow string format will also do string representation.

In [2]: class a(): pass
In [9]: def toU1(value):
   ...:     if not isinstance(value, unicode):
   ...:         value = unicode(str(value), 'utf-8')
   ...:     return u"fǒǒ = %s" % value
   ...:

In [10]: def toU2(value):
   ....:     if isinstance(value, str):
   ....:         value = unicode(str(value), 'utf-8')
   ....:     return u"fǒǒ = %s" % value
   ....:

In [11]: print toU1(a)
fǒǒ = __main__.a

In [12]: print toU2(a)
fǒǒ = __main__.a

In [13]: print toU1("fǒǒ")
fǒǒ = fǒǒ

In [14]: print toU2("fǒǒ")
fǒǒ = fǒǒ

In [15]: print toU1(u"fǒǒ")
fǒǒ = fǒǒ

In [16]: print toU2(u"fǒǒ")
fǒǒ = fǒǒ


if one developer define a class like this, it should tell the ptyhon this a unicode file.
In [17]: class afǒǒ(): pass
We have discuss this problem before, kimchi do not like this.

In this case, ptyhon take a string are unicode, it should OK.
Not try.
 


But I'm not worry about it.
IMO, no one will not pass this instance of object to  KimchiException as args.
IMO, we should never trust that the users/developers will pass the correct parameters to our code. Eventually, someone will forget that, and then we will have one more bug ;)
your example make me do not need to worry about this. 

>>> class X():
...     def x(self):
...             pass
...
>>> a = X()
>>> print str(a)
<__main__.X instance at 0x7f8fe13d4b00>
>>> print "this is a string: %s." % a
this is a string: <__main__.X instance at 0x7f8fe13d4b00>.

In [20]: print a
<__main__.X instance at 0x206c440>

In [21]: print u"%s" % a
<__main__.X instance at 0x206c440>

-- 
Thanks and best regards!

Sheldon Feng(冯少合)<shaohef@linux.vnet.ibm.com>
IBM Linux Technology Center