py3: conditionalize cPickle import by adding in util
authorPulkit Goyal <7895pulkit@gmail.com>
Sat, 04 Jun 2016 14:38:00 +0530
changeset 29324 b501579147f1
parent 29321 de4a80a2b45c
child 29325 4f2f8baba2ff
py3: conditionalize cPickle import by adding in util The cPickle is renamed to _pickle in python3 and this C extension is available in pickle which was not included in earlier versions. So imports are conditionalized to import cPickle in py2 and pickle in py3. Moreover the use of pickle in py2 is switched to cPickle as the C extension is faster. The hack is added in util.py and the modules import util.pickle
hgext/convert/common.py
hgext/convert/cvsps.py
hgext/convert/subversion.py
hgext/histedit.py
mercurial/formatter.py
mercurial/pycompat.py
mercurial/util.py
tests/test-check-py3-compat.t
--- a/hgext/convert/common.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/hgext/convert/common.py	Sat Jun 04 14:38:00 2016 +0530
@@ -7,7 +7,6 @@
 from __future__ import absolute_import
 
 import base64
-import cPickle as pickle
 import datetime
 import errno
 import os
@@ -21,6 +20,7 @@
     util,
 )
 
+pickle = util.pickle
 propertycache = util.propertycache
 
 def encodeargs(args):
--- a/hgext/convert/cvsps.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/hgext/convert/cvsps.py	Sat Jun 04 14:38:00 2016 +0530
@@ -6,7 +6,6 @@
 # GNU General Public License version 2 or any later version.
 from __future__ import absolute_import
 
-import cPickle as pickle
 import os
 import re
 
@@ -16,6 +15,8 @@
     util,
 )
 
+pickle = util.pickle
+
 class logentry(object):
     '''Class logentry has the following attributes:
         .author    - author name as CVS knows it
--- a/hgext/convert/subversion.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/hgext/convert/subversion.py	Sat Jun 04 14:38:00 2016 +0530
@@ -3,7 +3,6 @@
 # Copyright(C) 2007 Daniel Holth et al
 from __future__ import absolute_import
 
-import cPickle as pickle
 import os
 import re
 import sys
@@ -21,6 +20,7 @@
 
 from . import common
 
+pickle = util.pickle
 stringio = util.stringio
 propertycache = util.propertycache
 urlerr = util.urlerr
--- a/hgext/histedit.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/hgext/histedit.py	Sat Jun 04 14:38:00 2016 +0530
@@ -173,7 +173,6 @@
 
 import errno
 import os
-import pickle
 import sys
 
 from mercurial.i18n import _
@@ -197,6 +196,7 @@
     util,
 )
 
+pickle = util.pickle
 release = lock.release
 cmdtable = {}
 command = cmdutil.command(cmdtable)
--- a/mercurial/formatter.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/mercurial/formatter.py	Sat Jun 04 14:38:00 2016 +0530
@@ -7,7 +7,6 @@
 
 from __future__ import absolute_import
 
-import cPickle
 import os
 
 from .i18n import _
@@ -20,8 +19,11 @@
     encoding,
     error,
     templater,
+    util,
 )
 
+pickle = util.pickle
+
 class baseformatter(object):
     def __init__(self, ui, topic, opts):
         self._ui = ui
@@ -107,7 +109,7 @@
         self._data.append(self._item)
     def end(self):
         baseformatter.end(self)
-        self._ui.write(cPickle.dumps(self._data))
+        self._ui.write(pickle.dumps(self._data))
 
 def _jsonifyobj(v):
     if isinstance(v, tuple):
--- a/mercurial/pycompat.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/mercurial/pycompat.py	Sat Jun 04 14:38:00 2016 +0530
@@ -11,6 +11,12 @@
 from __future__ import absolute_import
 
 try:
+    import cPickle as pickle
+    pickle.dumps
+except ImportError:
+    import pickle
+
+try:
     import cStringIO as io
     stringio = io.StringIO
 except ImportError:
--- a/mercurial/util.py	Fri Jun 03 21:49:26 2016 +0900
+++ b/mercurial/util.py	Sat Jun 04 14:38:00 2016 +0530
@@ -47,6 +47,7 @@
 
 for attr in (
     'empty',
+    'pickle',
     'queue',
     'urlerr',
     # we do import urlreq, but we do it outside the loop
--- a/tests/test-check-py3-compat.t	Fri Jun 03 21:49:26 2016 +0900
+++ b/tests/test-check-py3-compat.t	Sat Jun 04 14:38:00 2016 +0530
@@ -34,10 +34,9 @@
   hgext/clonebundles.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   hgext/color.py: invalid syntax: invalid syntax (<unknown>, line *) (glob)
   hgext/convert/bzr.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
-  hgext/convert/common.py: error importing module: <ImportError> No module named 'cPickle' (line *) (glob)
   hgext/convert/convcmd.py: error importing: <SyntaxError> invalid syntax (bundle*.py, line *) (error at bundlerepo.py:*) (glob)
   hgext/convert/cvs.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
-  hgext/convert/cvsps.py: error importing module: <ImportError> No module named 'cPickle' (line *) (glob)
+  hgext/convert/cvsps.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   hgext/convert/darcs.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
   hgext/convert/filemap.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
   hgext/convert/git.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
@@ -45,7 +44,7 @@
   hgext/convert/hg.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   hgext/convert/monotone.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
   hgext/convert/p*.py: error importing module: <SystemError> Parent module 'hgext.convert' not loaded, cannot perform relative import (line *) (glob)
-  hgext/convert/subversion.py: error importing module: <ImportError> No module named 'cPickle' (line *) (glob)
+  hgext/convert/subversion.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   hgext/convert/transport.py: error importing module: <ImportError> No module named 'svn.client' (line *) (glob)
   hgext/eol.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   hgext/extdiff.py: error importing module: <SyntaxError> invalid syntax (archival.py, line *) (line *) (glob)
@@ -98,9 +97,9 @@
   mercurial/exchange.py: error importing module: <SyntaxError> invalid syntax (bundle*.py, line *) (line *) (glob)
   mercurial/extensions.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/filelog.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
-  mercurial/filemerge.py: error importing: <ImportError> No module named 'cPickle' (error at formatter.py:*) (glob)
+  mercurial/filemerge.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/fileset.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
-  mercurial/formatter.py: error importing module: <ImportError> No module named 'cPickle' (line *) (glob)
+  mercurial/formatter.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/graphmod.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/help.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/hg.py: error importing: <SyntaxError> invalid syntax (bundle*.py, line *) (error at bundlerepo.py:*) (glob)
@@ -140,7 +139,7 @@
   mercurial/templatefilters.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/templatekw.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/templater.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
-  mercurial/ui.py: error importing: <ImportError> No module named 'cPickle' (error at formatter.py:*) (glob)
+  mercurial/ui.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/unionrepo.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)
   mercurial/url.py: error importing module: <ImportError> No module named 'httplib' (line *) (glob)
   mercurial/verify.py: error importing: <AttributeError> 'dict' object has no attribute 'iteritems' (error at revset.py:*) (glob)