Patchwork shelve: drop pickle usage

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 14, 2013, 5:11 p.m.
Message ID <1beaedd7faaa9cf2a645.1381770692@vulgaris>
Download mbox | patch
Permalink /patch/2771/
State Accepted
Commit 5b327880a660323087ac8ecc1f00db809e38a208
Headers show

Comments

Pierre-Yves David - Oct. 14, 2013, 5:11 p.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1381769952 -7200
#      Mon Oct 14 18:59:12 2013 +0200
# Node ID 1beaedd7faaa9cf2a645b4d9c7d19fd5b4e75523
# Parent  740bf14129972cc0c4406790785f392bec6aec52
shelve: drop pickle usage

Pickle was used to the `shelvedstate` file. However the content of the file is
very simple and we can handle serialisation ourself. Not using pickle is a net
win.

Note incrementing the format version as no releases have been done so far.
Matt Mackall - Oct. 14, 2013, 7:30 p.m.
On Mon, 2013-10-14 at 19:11 +0200, pierre-yves.david@ens-lyon.org wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
> # Date 1381769952 -7200
> #      Mon Oct 14 18:59:12 2013 +0200
> # Node ID 1beaedd7faaa9cf2a645b4d9c7d19fd5b4e75523
> # Parent  740bf14129972cc0c4406790785f392bec6aec52
> shelve: drop pickle usage

Queued for default, thanks.

Patch

diff --git a/hgext/shelve.py b/hgext/shelve.py
--- a/hgext/shelve.py
+++ b/hgext/shelve.py
@@ -19,17 +19,12 @@  conflicts if necessary.
 You can have more than one shelved change outstanding at a time; each
 shelved change has a distinct name. For details, see the help for "hg
 shelve".
 """
 
-try:
-    import cPickle as pickle
-    pickle.dump # import now
-except ImportError:
-    import pickle
 from mercurial.i18n import _
-from mercurial.node import nullid
+from mercurial.node import nullid, bin, hex
 from mercurial import changegroup, cmdutil, scmutil, phases
 from mercurial import error, hg, mdiff, merge, patch, repair, util
 from mercurial import templatefilters
 from mercurial import lock as lockmod
 import errno
@@ -90,15 +85,21 @@  class shelvedstate(object):
     _filename = 'shelvedstate'
 
     @classmethod
     def load(cls, repo):
         fp = repo.opener(cls._filename)
-        (version, name, parents, stripnodes) = pickle.load(fp)
+        try:
+            version = int(fp.readline().strip())
 
-        if version != cls._version:
-            raise util.Abort(_('this version of shelve is incompatible '
-                               'with the version used in this repo'))
+            if version != cls._version:
+                raise util.Abort(_('this version of shelve is incompatible '
+                                   'with the version used in this repo'))
+            name = fp.readline().strip()
+            parents = [bin(h) for h in fp.readline().split()]
+            stripnodes = [bin(h) for h in fp.readline().split()]
+        finally:
+            fp.close()
 
         obj = cls()
         obj.name = name
         obj.parents = parents
         obj.stripnodes = stripnodes
@@ -106,13 +107,14 @@  class shelvedstate(object):
         return obj
 
     @classmethod
     def save(cls, repo, name, stripnodes):
         fp = repo.opener(cls._filename, 'wb')
-        pickle.dump((cls._version, name,
-                     repo.dirstate.parents(),
-                     stripnodes), fp)
+        fp.write('%i\n' % cls._version)
+        fp.write('%s\n' % name)
+        fp.write('%s\n' % ' '.join([hex(p) for p in repo.dirstate.parents()]))
+        fp.write('%s\n' % ' '.join([hex(n) for n in stripnodes]))
         fp.close()
 
     @classmethod
     def clear(cls, repo):
         util.unlinkpath(repo.join(cls._filename), ignoremissing=True)