[openstack-dev] [nova] Port Nova to Python 3

Thomas Goirand thomas at goirand.fr
Thu May 7 22:14:19 UTC 2015


On 04/24/2015 10:20 PM, Kevin L. Mitchell wrote:
> On Fri, 2015-04-24 at 16:07 -0400, Sean Toner wrote:
>> What I meant by the worst of both worlds is that you don't get the nice
>> new features of python3, while simultaneously dealing with the headaches
>> of making code run under both python versions.  You'll have to do weird
>> things with imports (for example urllib) and deal with the
>> inconsistencies between some functions that return strings and some that
>> return unicode, and some that return bytes.
>>
>> It's not impossible, but you have to add that extra work while also
>> depriving yourself of the goodness of python3 only features :)
>
> This is why the 'six' library is such a godsend; this stuff is still not
> easy, but the hardest parts, like the imports problem, are already taken
> care of by six…and maintaining the bytes/strings/unicode distinction is
> actually just as useful in Python 2, it just doesn't have the machinery
> to really detect the mixing :)

Oh, this makes me think: how would one fix something like this?

     def unicode_convert(self, item):
         try:
 >           return unicode(item, "utf-8")
E           NameError: name 'unicode' is not defined

and something like this?

     def make(self, idp, sp, args):
         md5 = hashlib.md5()
         for arg in args:
             md5.update(arg.encode("utf-8"))
 >       md5.update(sp)
E       TypeError: Unicode-objects must be encoded before hashing

and one last one:

     def harvest_element_tree(self, tree):
         # Fill in the instance members from the contents of the
         # XML tree.
         for child in tree:
             self._convert_element_tree_to_member(child)
 >       for attribute, value in tree.attrib.iteritems():
             self._convert_element_attribute_to_member(attribute, value)
E           AttributeError: 'dict' object has no attribute 'iteritems'

I once found a document on the net about how to fix the iteritems 
thingy, but I can't find it again... :(

BTW, I did this:
-from Cookie import SimpleCookie
+try:
+    from Cookie import SimpleCookie
+except:
+    from http.cookies import SimpleCookie

Is there anything smarter to do with six? What's the rule with 
six.moves? Should I always just use the new location?

Also, is this a correct fix for the basestring issue in Py3?

+try:
+    basestring
+except NameError:
+    basestring = (str,bytes)

(FYI: I am trying to port pysaml2 to Python 3, and I already have a 
nearly 5k lines patch...)

Cheers,

Thomas Goirand (zigo)



More information about the OpenStack-dev mailing list