[ 
https://issues.apache.org/jira/browse/LIBCLOUD-306?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16368072#comment-16368072
 ] 

Connor Osborn edited comment on LIBCLOUD-306 at 2/17/18 2:13 AM:
-----------------------------------------------------------------

Ran into this issue, and it got the better of my curiosity. The cause is that 
libcloud doesn't have python 2 compatible {{__repr__}} methods_._ In python 2 
the {{repr}} builtin expects a __{{repr__}} method which returns an ascii 
string (or something coercible to an ascii string).  It's required because 
{{repr}} will translate the result of {{X.repr_()}} to ascii.


 In the case of 
[Node.__repr|https://github.com/apache/libcloud/blob/408caa9c95a197578baede0a5a1476e9201486e7/libcloud/compute/base.py#L260-L266]__,
 if the node's name contains characters outside of ascii, then whenever 
{{repr(node)}} is called the unicode string returned from {{repr}} will fail to 
be encoded as ascii.

It looks like in python 3 the opposite is true, {{repr}} should return unicode 
strings.

The builtin {{str}} has all the same caveats. 

This can also easily be replicated by stubbing out a simple class:
{code:java}
class Foo:
    def __repr__(self):
        return u'\xe0'

> repr(Foo())
> str(Foo())
{code}


was (Author: cdosborn):
Ran into this issue, and it got the better of my curiosity. The cause is that 
libcloud doesn't have python 2 compatible \{{__repr__}} methods. In python 2, 
the \{{__repr__}} builtin expects a \{{repr}} method which returns an ascii 
string (or something coercible to an ascii string).  It's required because 
\{{repr}} will translate the result of \{{X.repr_()}} to ascii.

In the case of 
[Node._repr|https://github.com/apache/libcloud/blob/408caa9c95a197578baede0a5a1476e9201486e7/libcloud/compute/base.py#L260-L266|https://github.com/apache/libcloud/blob/408caa9c95a197578baede0a5a1476e9201486e7/libcloud/compute/base.py#L260-L266].],
 if the node's name contains characters outside of ascii, then whenever 
\{{repr}}(node) is called the unicode string returned from \{{repr}} will fail 
to be encoded as ascii.

It looks like in python 3 the opposite is true, \{{__repr__}} should return 
unicode strings.

The builtin \{{str}} has all the same caveats.

This can also easily be replicated by stubbing out a simple class:

class Foo:
 def __repr__(self):
 return u'\xe0'

> repr(Foo())
> str(Foo())

> XML response with utf-8 charset break thing with python 2
> ---------------------------------------------------------
>
>                 Key: LIBCLOUD-306
>                 URL: https://issues.apache.org/jira/browse/LIBCLOUD-306
>             Project: Libcloud
>          Issue Type: Bug
>         Environment: ubuntu 12.04, python 2.7, opennebula 3.8
>            Reporter: Guillaume ZITTA
>            Priority: Critical
>         Attachments: debug.log, test_utf.py, utf-8_python2.patch, 
> utf-8_python2_xml.workaround.patch
>
>
> We have an opennebula with French accents in networks names.
> Listing networks leads to a crash because of these accents in the XML 
> response of the cloud.



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to