Mercurial > hg
annotate mercurial/py3kcompat.py @ 27764:dd0c5f4d1b53
util: adjust 'datapath' to be correct in a frozen OS X package
Apparently unlike py2exe, py2app copies the Mercurial source tree as-is to a
Contents/Resources subdirectory of an app bundle, and places its binary stub in
Contents/MacOS. (The Windows install has the 'hgext' and 'mercurial' modules in
'lib/library.zip', while the help and templates subdirectories have been moved
out of the mercurial directory to the root of the installation. I assume that
the python code living in a zip file is why "py2exe doesn't support __file__".)
Therefore, prior to this change, Mercurial in a frozen app bundle on OS X would
go looking for help *.txt, templates and locale info in Contents/MacOS, where
they don't exist.
There are only a handful of places that test for frozen, and not all of them are
wrong for OS X, so it seems wiser to handle them on a case by case basis, rather
that try to change mainfrozen(). The remaining cases are:
1) util.hgexecutable() wrongly points to the bundled python executable, and
affects $HG in util.system() launched processes (e.g. external hooks)
2) util.hgcmd() wrongly points to the bundled python executable, but it seems
to only affect 'hg serve -d'
3) hook._pythonhook() may be OK, since I didn't see anything outrageous when
printing sys.path from an internal hook. I'm not sure if this special
case is needed on OS X though.
4) sslutil._plainapplepython() is OK, because sys.executable is not
/usr/bin/python, nor is it in /System/Library/Frameworks
author | Matt Harbison <matt_harbison@yahoo.com> |
---|---|
date | Sun, 10 Jan 2016 17:49:01 -0500 |
parents | 5bfd01a3c2a9 |
children |
rev | line source |
---|---|
11748
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
1 # py3kcompat.py - compatibility definitions for running hg in py3k |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
2 # |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
3 # Copyright 2010 Renato Cunha <renatoc@gmail.com> |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
4 # |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
5 # This software may be used and distributed according to the terms of the |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
6 # GNU General Public License version 2 or any later version. |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
7 |
27486
5bfd01a3c2a9
py3compat: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21292
diff
changeset
|
8 from __future__ import absolute_import |
11748
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
9 |
27486
5bfd01a3c2a9
py3compat: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21292
diff
changeset
|
10 import builtins |
5bfd01a3c2a9
py3compat: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21292
diff
changeset
|
11 import numbers |
5bfd01a3c2a9
py3compat: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21292
diff
changeset
|
12 |
5bfd01a3c2a9
py3compat: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
21292
diff
changeset
|
13 Number = numbers.Number |
11748
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
14 |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
15 def bytesformatter(format, args): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
16 '''Custom implementation of a formatter for bytestrings. |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
17 |
17424
e7cfe3587ea4
fix trivial spelling errors
Mads Kiilerich <mads@kiilerich.com>
parents:
11878
diff
changeset
|
18 This function currently relies on the string formatter to do the |
11748
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
19 formatting and always returns bytes objects. |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
20 |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
21 >>> bytesformatter(20, 10) |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
22 0 |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
23 >>> bytesformatter('unicode %s, %s!', ('string', 'foo')) |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
24 b'unicode string, foo!' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
25 >>> bytesformatter(b'test %s', 'me') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
26 b'test me' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
27 >>> bytesformatter('test %s', 'me') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
28 b'test me' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
29 >>> bytesformatter(b'test %s', b'me') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
30 b'test me' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
31 >>> bytesformatter('test %s', b'me') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
32 b'test me' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
33 >>> bytesformatter('test %d: %s', (1, b'result')) |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
34 b'test 1: result' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
35 ''' |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
36 # The current implementation just converts from bytes to unicode, do |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
37 # what's needed and then convert the results back to bytes. |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
38 # Another alternative is to use the Python C API implementation. |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
39 if isinstance(format, Number): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
40 # If the fixer erroneously passes a number remainder operation to |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
41 # bytesformatter, we just return the correct operation |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
42 return format % args |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
43 if isinstance(format, bytes): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
44 format = format.decode('utf-8', 'surrogateescape') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
45 if isinstance(args, bytes): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
46 args = args.decode('utf-8', 'surrogateescape') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
47 if isinstance(args, tuple): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
48 newargs = [] |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
49 for arg in args: |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
50 if isinstance(arg, bytes): |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
51 arg = arg.decode('utf-8', 'surrogateescape') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
52 newargs.append(arg) |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
53 args = tuple(newargs) |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
54 ret = format % args |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
55 return ret.encode('utf-8', 'surrogateescape') |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
56 builtins.bytesformatter = bytesformatter |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
57 |
11878
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
58 origord = builtins.ord |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
59 def fakeord(char): |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
60 if isinstance(char, int): |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
61 return char |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
62 return origord(char) |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
63 builtins.ord = fakeord |
8bb1481cf08f
py3kcompat: added fake ord implementation for py3k
Renato Cunha <renatoc@gmail.com>
parents:
11748
diff
changeset
|
64 |
11748
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
65 if __name__ == '__main__': |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
66 import doctest |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
67 doctest.testmod() |
37a70a784397
py3kcompat: added a "compatibility layer" for py3k
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
68 |