From: John Keeping <john@keeping.me.uk>
To: Michael Haggerty <mhagger@alum.mit.edu>
Cc: Junio C Hamano <gitster@pobox.com>,
git@vger.kernel.org, Sverre Rabbelier <srabbelier@gmail.com>
Subject: [PATCH] git-remote-testpy: fix patch hashing on Python 3
Date: Sun, 27 Jan 2013 14:50:56 +0000 [thread overview]
Message-ID: <20130127145056.GP7498@serenity.lan> (raw)
In-Reply-To: <20130127141329.GN7498@serenity.lan>
When this change was originally made (0846b0c - git-remote-testpy: hash
bytes explicitly , I didn't realised that the "hex" encoding we chose is
a "bytes to bytes" encoding so it just fails with an error on Python 3
in the same way as the original code.
It is not possible to provide a single code path that works on Python 2
and Python 3 since Python 2.x will attempt to decode the string before
encoding it, which fails for strings that are not valid in the default
encoding. Python 3.1 introduced the "surrogateescape" error handler
which handles this correctly and permits a bytes -> unicode -> bytes
round-trip to be lossless.
At this point Python 3.0 is unsupported so we don't go out of our way to
try to support it.
Helped-by: Michael Haggerty <mhagger@alum.mit.edu>
Signed-off-by: John Keeping <john@keeping.me.uk>
---
On Sun, Jan 27, 2013 at 02:13:29PM +0000, John Keeping wrote:
> On Sun, Jan 27, 2013 at 05:44:37AM +0100, Michael Haggerty wrote:
> > So to handle all of the cases across Python versions as closely as
> > possible to the old 2.x code, it might be necessary to make the code
> > explicitly depend on the Python version number, like:
> >
> > hasher = _digest()
> > if sys.hexversion < 0x03000000:
> > pathbytes = repo.path
> > elif sys.hexversion < 0x03010000:
> > # If support for Python 3.0.x is desired (note: result can
> > # be different in this case than under 2.x or 3.1+):
> > pathbytes = repo.path.encode(sys.getfilesystemencoding(),
> > 'backslashreplace')
> > else
> > pathbytes = repo.path.encode(sys.getfilesystemencoding(),
> > 'surrogateescape')
> > hasher.update(pathbytes)
> > repo.hash = hasher.hexdigest()
How about this?
git-remote-testpy.py | 18 +++++++++++++++++-
1 file changed, 17 insertions(+), 1 deletion(-)
diff --git a/git-remote-testpy.py b/git-remote-testpy.py
index c7a04ec..16b0c52 100644
--- a/git-remote-testpy.py
+++ b/git-remote-testpy.py
@@ -36,6 +36,22 @@ if sys.hexversion < 0x02000000:
sys.stderr.write("git-remote-testgit: requires Python 2.0 or later.\n")
sys.exit(1)
+
+def _encode_filepath(path):
+ """Encodes a Unicode file path to a byte string.
+
+ On Python 2 this is a no-op; on Python 3 we encode the string as
+ suggested by [1] which allows an exact round-trip from the command line
+ to the filesystem.
+
+ [1] http://docs.python.org/3/c-api/unicode.html#file-system-encoding
+
+ """
+ if sys.hexversion < 0x03000000:
+ return path
+ return path.encode('utf-8', 'surrogateescape')
+
+
def get_repo(alias, url):
"""Returns a git repository object initialized for usage.
"""
@@ -45,7 +61,7 @@ def get_repo(alias, url):
repo.get_head()
hasher = _digest()
- hasher.update(repo.path.encode('hex'))
+ hasher.update(_encode_filepath(repo.path))
repo.hash = hasher.hexdigest()
repo.get_base_path = lambda base: os.path.join(
--
1.8.1.1
next prev parent reply other threads:[~2013-01-27 14:51 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-01-20 13:15 [PATCH v3 0/8] Python 3 support for git_remote_helpers John Keeping
2013-01-20 13:15 ` [PATCH v3 1/8] git_remote_helpers: Allow building with Python 3 John Keeping
2013-01-23 18:49 ` Sverre Rabbelier
2013-01-20 13:15 ` [PATCH v3 2/8] git_remote_helpers: fix input when running under " John Keeping
2013-01-23 19:20 ` Sverre Rabbelier
2013-01-23 19:47 ` John Keeping
2013-01-23 20:14 ` Sverre Rabbelier
2013-01-23 20:36 ` Junio C Hamano
2013-01-25 20:23 ` Brandon Casey
2013-02-05 16:07 ` Erik Faye-Lund
2013-01-20 13:15 ` [PATCH v3 3/8] git_remote_helpers: Force rebuild if python version changes John Keeping
2013-01-23 18:51 ` Sverre Rabbelier
2013-01-20 13:15 ` [PATCH v3 4/8] git_remote_helpers: Use 2to3 if building with Python 3 John Keeping
2013-01-20 13:15 ` [PATCH v3 5/8] svn-fe: allow svnrdump_sim.py to run " John Keeping
2013-01-20 13:15 ` [PATCH v3 6/8] git-remote-testpy: hash bytes explicitly John Keeping
2013-01-26 17:51 ` John Keeping
2013-01-26 21:44 ` Junio C Hamano
2013-01-26 23:32 ` [PATCH] git-remote-testpy: fix patch hashing on Python 3 John Keeping
2013-01-27 4:44 ` [PATCH v3 6/8] git-remote-testpy: hash bytes explicitly Michael Haggerty
2013-01-27 5:30 ` Junio C Hamano
2013-01-27 14:21 ` John Keeping
2013-01-27 5:30 ` Sverre Rabbelier
2013-01-27 8:41 ` Michael Haggerty
2013-01-27 14:13 ` John Keeping
2013-01-27 14:50 ` John Keeping [this message]
2013-01-27 19:49 ` [PATCH] git-remote-testpy: fix patch hashing on Python 3 Junio C Hamano
2013-01-27 20:04 ` John Keeping
2013-01-27 20:11 ` Junio C Hamano
2013-01-27 20:21 ` John Keeping
2013-01-27 20:38 ` Junio C Hamano
2013-01-27 20:47 ` Junio C Hamano
2013-01-27 22:42 ` John Keeping
2013-01-27 23:18 ` Junio C Hamano
2013-01-28 10:44 ` Michael Haggerty
2013-01-28 11:20 ` [PATCH] fixup! git-remote-testpy: fix path " John Keeping
2013-01-28 17:53 ` Junio C Hamano
2013-01-20 13:15 ` [PATCH v3 7/8] git-remote-testpy: don't do unbuffered text I/O John Keeping
2013-01-20 13:15 ` [PATCH v3 8/8] git-remote-testpy: call print as a function John Keeping
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20130127145056.GP7498@serenity.lan \
--to=john@keeping.me.uk \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=mhagger@alum.mit.edu \
--cc=srabbelier@gmail.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).