← Back to team overview

openerp-community team mailing list archive

Re: OCA guidelines for code compatibility with Python 2.6

 

An initial trial of converting openobject-server to compatibility with python3, shows that it isn't too complicated. This is a look about 20 minutes. I stalled at a python3-psycopg2 error.

Nothing huge, a few imports are different, print statements need to be changed to functions, exceptions need the `as` keyword, etc

=== modified file 'openerp/osv/fields.py'
--- openerp/osv/fields.py	2014-02-13 10:08:36 +0000
+++ openerp/osv/fields.py	2014-02-26 15:29:38 +0000
@@ -40,7 +40,11 @@
 import logging
 import pytz
 import re
-import xmlrpclib
+import sys
+if sys.version_info.major == 2:
+    import xmlrpclib
+elif sys.version_info.major == 3:
+    import xmlrpc.client as xmlrpclib
 from psycopg2 import Binary

 import openerp

=== modified file 'openerp/sql_db.py'
--- openerp/sql_db.py	2013-03-01 12:07:44 +0000
+++ openerp/sql_db.py	2014-02-26 16:02:08 +0000
@@ -224,7 +224,7 @@
         try:
             params = params or None
             res = self._obj.execute(query, params)
-        except psycopg2.ProgrammingError, pe:
+        except psycopg2.ProgrammingError as pe:
if self._default_log_exceptions if log_exceptions is None else log_exceptions: _logger.error("Programming error: %s, in query %s", pe, query)
             raise

=== modified file 'openerp/tools/cache.py'
--- openerp/tools/cache.py	2012-12-08 18:11:51 +0000
+++ openerp/tools/cache.py	2014-02-26 15:58:42 +0000
@@ -1,3 +1,4 @@
+from __future__ import print_function
 import lru

 class ormcache(object):
@@ -114,12 +115,12 @@
     class A():
         @ormcache()
         def m(self,a,b):
-            print  "A::m(", self,a,b
+            print("A::m(", self, a, b)
             return 1

         @ormcache_multi(multi=3)
         def n(self,cr,uid,ids):
-            print  "m", self,cr,uid,ids
+            print("m", self, cr, uid, ids)
             return dict([(i,i) for i in ids])

     a=A()
@@ -127,12 +128,12 @@
     r=a.m(1,2)
     r=a.n("cr",1,[1,2,3,4])
     r=a.n("cr",1,[1,2])
-    print r
+    print(r)
     for i in a._ormcache:
-        print a._ormcache[i].d
+        print(a._ormcache[i].d)
     a.n.clear_cache(a,1,1)
     r=a.n("cr",1,[1,2])
-    print r
+    print(r)
     r=a.n("cr",1,[1,2])

 # For backward compatibility

=== modified file 'openerp/tools/config.py'
--- openerp/tools/config.py	2013-01-15 12:40:58 +0000
+++ openerp/tools/config.py	2014-02-26 15:46:22 +0000
@@ -20,10 +20,14 @@
 #

##############################################################################

-import ConfigParser
 import optparse
 import os
 import sys
+if sys.version_info.major == 2:
+    import ConfigParser
+elif sys.version_info.major == 3:
+    import configparser as ConfigParser
+    basestring = str
 import openerp
 import openerp.conf
 import openerp.loglevels as loglevels
@@ -604,7 +608,7 @@
             try:
                 p.write(file(self.rcfile, 'w'))
                 if not rc_exists:
-                    os.chmod(self.rcfile, 0600)
+                    os.chmod(self.rcfile, 0o600)
             except IOError:
sys.stderr.write("ERROR: couldn't write the config file\n")


=== modified file 'openerp/tools/convert.py'
--- openerp/tools/convert.py	2013-12-03 09:24:33 +0000
+++ openerp/tools/convert.py	2014-02-26 16:01:50 +0000
@@ -19,7 +19,11 @@
 #

##############################################################################

-import cStringIO
+import sys
+if sys.version_info.major == 2:
+    from cStringIO import StringIO
+elif sys.version_info.major ==3:
+    from io import StringIO
 import csv
 import logging
 import os.path
@@ -894,7 +898,7 @@

     pool = pooler.get_pool(cr.dbname)

-    input = cStringIO.StringIO(csvcontent) #FIXME
+    input = StringIO(csvcontent) #FIXME
     reader = csv.reader(input, quotechar='"', delimiter=',')
     fields = reader.next()
     fname_partial = ""

=== modified file 'openerp/tools/misc.py'
--- openerp/tools/misc.py	2013-11-25 08:42:15 +0000
+++ openerp/tools/misc.py	2014-02-26 15:54:58 +0000
@@ -38,7 +38,11 @@
 import zipfile
 from collections import defaultdict
 from datetime import datetime
-from itertools import islice, izip, groupby
+from itertools import islice, groupby
+if sys.version_info == 2:
+    from itertools import izip
+elif sys.version_info == 3:
+    izip = zip
 from lxml import etree
 from which import which
 from threading import local

Le 2014-02-26 09:58, Sandy Carter a écrit :
+1
This seems like the most reponsible approach.

Le 2014-02-26 05:14, Yannick Vaucher a écrit :
To sum up,

OpenERP 7.0 is compatible with python 2.6
And we would like to prepare further for newest python version.

So we keep the compatibility on OpenERP 7.0 with python 2.6.

And we could do the work during module portage to 8.0 to prepare for
python 3 with __futur__ import?
Plus allow to break compatibility with 2.6 in OpenERP 8.0 OCA?

Cheers,


On Tue, Feb 25, 2014 at 11:29 AM, Leonardo Pistone
<leonardo.pistone@xxxxxxxxxxxxxx
<mailto:leonardo.pistone@xxxxxxxxxxxxxx>> wrote:

    Eric, Stéphane,

    maybe we are talking about different things: I was referring to
string
    literals (and making the suggestion of unicode_literals if we drop
    py2.6 to avoid forgetting all the u'')

    The actual encoding of the python code doesn't worry me too much, as
    is should be relevant mostly only for contributor names in the
headers
    (and my name doesn't have any accents, even though many add an é
to it
    :)

    On Tue, Feb 25, 2014 at 10:36 AM, Stéphane Wirtel <stw@xxxxxxxxxxx
    <mailto:stw@xxxxxxxxxxx>> wrote:
     > Why in Unicode?
     >
     >
     >
     > On 25 Feb 2014, at 10:34, Eric Caudal wrote:
     >
     >> All openerp code should be Unicode compatible by default!
     >> (and this is still not the case... eg: filenames)
     >>
     >> Eric CAUDAL
     >>
     >> Eric Caudal
     >> /CEO/
     >> --
     >> *Elico Corporation, Shanghai branch
     >> /OpenERP Premium Certified Training Partner/  *
     >>
     >> Cell: + 86 186 2136 1670 <tel:%2B%2086%20186%202136%201670>
     >> Office: + 86 21 6211 8017/27/37
     >> Skype: elico.corp
     >> eric.caudal@xxxxxxxxxxxxxx <mailto:eric.caudal@xxxxxxxxxxxxxx>
      <mailto:eric.caudal@xxxxxxxxxxxxxx
    <mailto:eric.caudal@xxxxxxxxxxxxxx>>
     >> http://www.elico-corp.com
     >>
     >>
     >> Elico Corp
     >> On 02/25/2014 05:22 PM, Leonardo Pistone wrote:
     >>>
     >>> A sidenote on bytestrings/unicode (typically the biggest
    headache in
     >>> py2 -> py3 ports).
     >>>
     >>> Python 3 or not, I once lost a few hours of sleep because we all
     >>> define field descriptions with plain quotes (thus, in py2,
     >>> bytestrings), but the translation system actually returns
unicode.
     >>> That was maybe 6.1, but I guess it is still like that.
     >>>
     >>> In theory, in a functional addon (inside the framework things
    get more
     >>> complicated) we should probably handle unicode only, letting the
     >>> framework do the right encoding/decoding up the stack.
     >>>
     >>> I find this a bad practice, and IMHO something like from
__future__
     >>> import unicode_literals could force us to do that.
     >>>
     >>> On Tue, Feb 25, 2014 at 8:04 AM, Alexandre Fayolle
     >>> <alexandre.fayolle@xxxxxxxxxxxxxx
    <mailto:alexandre.fayolle@xxxxxxxxxxxxxx>> wrote:
     >>>>
     >>>> On 24/02/2014 17:00, Daniel Reis wrote:
     >>>>
     >>>> Hi all,
     >>>>
     >>>> In a merge proposal for 7.0. came up the question of what
Python
     >>>> versions
     >>>> should the code be compatible with.
     >>>>
     >>>> In particular, a dictionay comprehension is used the,
available in
     >>>> Python
     >>>> 2.7 but not compatible with Python 2.6.
     >>>> For reference, the MP is:
     >>>>
     >>>>

https://code.launchpad.net/~dreis-pt/project-service/7.0-issue_task-dr/+merge/195985

     >>>>
     >>>> So, the question here is:
     >>>> Regarding OCA reviewed modules, should the code targeting
    OpenERP >= 7.0
     >>>> be
     >>>> made to keep Python 2.6 compatibility?
     >>>>
     >>>> Regards
     >>>>
     >>>> Daniel Reis
     >>>>
     >>>>
     >>>>
     >>>> _______________________________________________
     >>>> Mailing list: https://launchpad.net/~openerp-community
     >>>> Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
     >>>> Unsubscribe : https://launchpad.net/~openerp-community
     >>>> More help   : https://help.launchpad.net/ListHelp
     >>>>
     >>>>
     >>>> I'm not sure OpenERP 7 can run on Python 2.6 (but I could well
    be deeply
     >>>> mistaken).
     >>>>
     >>>> I really wish we could mandate targetting Python 2.7 for OCA
    modules and
     >>>> take advantage of the additions brought by that release. It is
    nowadays
     >>>> fairly easy to get a python2.7 interpreter even if your distro
    does not
     >>>> provide one (virtualenv). We should also gently nag people
    about using
     >>>> the
     >>>> __future__ imports designed to ease porting to Python 3.x.
      The idea
     >>>> behind
     >>>> this is not to block useful modules but to start getting
    contributors in
     >>>> the
     >>>> mood for the change. This will save grief later.
     >>>>
     >>>> --
     >>>> Alexandre Fayolle
     >>>> Chef de Projet
     >>>> Tel : + 33 (0)4 79 26 57 94
     >>>>
     >>>> Camptocamp France SAS
     >>>> Savoie Technolac, BP 352
     >>>> 73377 Le Bourget du Lac Cedex
     >>>> http://www.camptocamp.com
     >>>>
     >>>>
     >>>> _______________________________________________
     >>>> Mailing list: https://launchpad.net/~openerp-community
     >>>> Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
     >>>> Unsubscribe : https://launchpad.net/~openerp-community
     >>>> More help   : https://help.launchpad.net/ListHelp
     >>>>
     >>> _______________________________________________
     >>> Mailing list: https://launchpad.net/~openerp-community
     >>> Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
     >>> Unsubscribe : https://launchpad.net/~openerp-community
     >>> More help   : https://help.launchpad.net/ListHelp
     >>
     >>
     >> _______________________________________________
     >> Mailing list: https://launchpad.net/~openerp-community
     >> Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
     >> Unsubscribe : https://launchpad.net/~openerp-community
     >> More help   : https://help.launchpad.net/ListHelp
     >
     >
     > _______________________________________________
     > Mailing list: https://launchpad.net/~openerp-community
     > Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
     > Unsubscribe : https://launchpad.net/~openerp-community
     > More help   : https://help.launchpad.net/ListHelp

    _______________________________________________
    Mailing list: https://launchpad.net/~openerp-community
    Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
    <mailto:openerp-community@xxxxxxxxxxxxxxxxxxx>
    Unsubscribe : https://launchpad.net/~openerp-community
    More help   : https://help.launchpad.net/ListHelp




--
Yannick Vaucher
Business Solutions Software Developer

Camptocamp SA
PSE A, CH-1015 Lausanne
Phone: +41 21 619 10 30
Office: +41 21 619 10 10
http://www.camptocamp.com/


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp


_______________________________________________
Mailing list: https://launchpad.net/~openerp-community
Post to     : openerp-community@xxxxxxxxxxxxxxxxxxx
Unsubscribe : https://launchpad.net/~openerp-community
More help   : https://help.launchpad.net/ListHelp


References