Show More
@@ -0,0 +1,65 b'' | |||||
|
1 | # py3kcompat.py - compatibility definitions for running hg in py3k | |||
|
2 | # | |||
|
3 | # Copyright 2010 Renato Cunha <renatoc@gmail.com> | |||
|
4 | # | |||
|
5 | # This software may be used and distributed according to the terms of the | |||
|
6 | # GNU General Public License version 2 or any later version. | |||
|
7 | ||||
|
8 | import os, builtins | |||
|
9 | ||||
|
10 | from numbers import Number | |||
|
11 | ||||
|
12 | def bytesformatter(format, args): | |||
|
13 | '''Custom implementation of a formatter for bytestrings. | |||
|
14 | ||||
|
15 | This function currently relias on the string formatter to do the | |||
|
16 | formatting and always returns bytes objects. | |||
|
17 | ||||
|
18 | >>> bytesformatter(20, 10) | |||
|
19 | 0 | |||
|
20 | >>> bytesformatter('unicode %s, %s!', ('string', 'foo')) | |||
|
21 | b'unicode string, foo!' | |||
|
22 | >>> bytesformatter(b'test %s', 'me') | |||
|
23 | b'test me' | |||
|
24 | >>> bytesformatter('test %s', 'me') | |||
|
25 | b'test me' | |||
|
26 | >>> bytesformatter(b'test %s', b'me') | |||
|
27 | b'test me' | |||
|
28 | >>> bytesformatter('test %s', b'me') | |||
|
29 | b'test me' | |||
|
30 | >>> bytesformatter('test %d: %s', (1, b'result')) | |||
|
31 | b'test 1: result' | |||
|
32 | ''' | |||
|
33 | # The current implementation just converts from bytes to unicode, do | |||
|
34 | # what's needed and then convert the results back to bytes. | |||
|
35 | # Another alternative is to use the Python C API implementation. | |||
|
36 | if isinstance(format, Number): | |||
|
37 | # If the fixer erroneously passes a number remainder operation to | |||
|
38 | # bytesformatter, we just return the correct operation | |||
|
39 | return format % args | |||
|
40 | if isinstance(format, bytes): | |||
|
41 | format = format.decode('utf-8', 'surrogateescape') | |||
|
42 | if isinstance(args, bytes): | |||
|
43 | args = args.decode('utf-8', 'surrogateescape') | |||
|
44 | if isinstance(args, tuple): | |||
|
45 | newargs = [] | |||
|
46 | for arg in args: | |||
|
47 | if isinstance(arg, bytes): | |||
|
48 | arg = arg.decode('utf-8', 'surrogateescape') | |||
|
49 | newargs.append(arg) | |||
|
50 | args = tuple(newargs) | |||
|
51 | ret = format % args | |||
|
52 | return ret.encode('utf-8', 'surrogateescape') | |||
|
53 | builtins.bytesformatter = bytesformatter | |||
|
54 | ||||
|
55 | # Create bytes equivalents for os.environ values | |||
|
56 | for key in list(os.environ.keys()): | |||
|
57 | # UTF-8 is fine for us | |||
|
58 | bkey = key.encode('utf-8', 'surrogateescape') | |||
|
59 | bvalue = os.environ[key].encode('utf-8', 'surrogateescape') | |||
|
60 | os.environ[bkey] = bvalue | |||
|
61 | ||||
|
62 | if __name__ == '__main__': | |||
|
63 | import doctest | |||
|
64 | doctest.testmod() | |||
|
65 |
@@ -1,96 +1,97 b'' | |||||
1 | """Fixer that changes plain strings to bytes strings.""" |
|
1 | """Fixer that changes plain strings to bytes strings.""" | |
2 |
|
2 | |||
3 | import re |
|
3 | import re | |
4 |
|
4 | |||
5 | from lib2to3 import fixer_base |
|
5 | from lib2to3 import fixer_base | |
6 | from lib2to3.pgen2 import token |
|
6 | from lib2to3.pgen2 import token | |
7 | from lib2to3.fixer_util import Name |
|
7 | from lib2to3.fixer_util import Name | |
8 | from lib2to3.pygram import python_symbols as syms |
|
8 | from lib2to3.pygram import python_symbols as syms | |
9 |
|
9 | |||
10 | _re = re.compile(r'[rR]?[\'\"]') |
|
10 | _re = re.compile(r'[rR]?[\'\"]') | |
11 |
|
11 | |||
12 | # XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than |
|
12 | # XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than | |
13 | # blacklisting some modules inside the fixers. So, this is what I came with. |
|
13 | # blacklisting some modules inside the fixers. So, this is what I came with. | |
14 |
|
14 | |||
15 | blacklist = ['mercurial/demandimport.py', |
|
15 | blacklist = ['mercurial/demandimport.py', | |
|
16 | 'mercurial/py3kcompat.py', # valid python 3 already | |||
16 | 'mercurial/i18n.py', |
|
17 | 'mercurial/i18n.py', | |
17 | ] |
|
18 | ] | |
18 |
|
19 | |||
19 | def isdocstring(node): |
|
20 | def isdocstring(node): | |
20 | def isclassorfunction(ancestor): |
|
21 | def isclassorfunction(ancestor): | |
21 | symbols = (syms.funcdef, syms.classdef) |
|
22 | symbols = (syms.funcdef, syms.classdef) | |
22 | # if the current node is a child of a function definition, a class |
|
23 | # if the current node is a child of a function definition, a class | |
23 | # definition or a file, then it is a docstring |
|
24 | # definition or a file, then it is a docstring | |
24 | if ancestor.type == syms.simple_stmt: |
|
25 | if ancestor.type == syms.simple_stmt: | |
25 | try: |
|
26 | try: | |
26 | while True: |
|
27 | while True: | |
27 | if ancestor.type in symbols: |
|
28 | if ancestor.type in symbols: | |
28 | return True |
|
29 | return True | |
29 | ancestor = ancestor.parent |
|
30 | ancestor = ancestor.parent | |
30 | except AttributeError: |
|
31 | except AttributeError: | |
31 | return False |
|
32 | return False | |
32 | return False |
|
33 | return False | |
33 |
|
34 | |||
34 | def ismodule(ancestor): |
|
35 | def ismodule(ancestor): | |
35 | # Our child is a docstring if we are a simple statement, and our |
|
36 | # Our child is a docstring if we are a simple statement, and our | |
36 | # ancestor is file_input. In other words, our child is a lone string in |
|
37 | # ancestor is file_input. In other words, our child is a lone string in | |
37 | # the source file. |
|
38 | # the source file. | |
38 | try: |
|
39 | try: | |
39 | if (ancestor.type == syms.simple_stmt and |
|
40 | if (ancestor.type == syms.simple_stmt and | |
40 | ancestor.parent.type == syms.file_input): |
|
41 | ancestor.parent.type == syms.file_input): | |
41 | return True |
|
42 | return True | |
42 | except AttributeError: |
|
43 | except AttributeError: | |
43 | return False |
|
44 | return False | |
44 |
|
45 | |||
45 | def isdocassignment(ancestor): |
|
46 | def isdocassignment(ancestor): | |
46 | # Assigning to __doc__, definitely a string |
|
47 | # Assigning to __doc__, definitely a string | |
47 | try: |
|
48 | try: | |
48 | while True: |
|
49 | while True: | |
49 | if (ancestor.type == syms.expr_stmt and |
|
50 | if (ancestor.type == syms.expr_stmt and | |
50 | Name('__doc__') in ancestor.children): |
|
51 | Name('__doc__') in ancestor.children): | |
51 | return True |
|
52 | return True | |
52 | ancestor = ancestor.parent |
|
53 | ancestor = ancestor.parent | |
53 | except AttributeError: |
|
54 | except AttributeError: | |
54 | return False |
|
55 | return False | |
55 |
|
56 | |||
56 | if ismodule(node.parent) or \ |
|
57 | if ismodule(node.parent) or \ | |
57 | isdocassignment(node.parent) or \ |
|
58 | isdocassignment(node.parent) or \ | |
58 | isclassorfunction(node.parent): |
|
59 | isclassorfunction(node.parent): | |
59 | return True |
|
60 | return True | |
60 | return False |
|
61 | return False | |
61 |
|
62 | |||
62 | def shouldtransform(node): |
|
63 | def shouldtransform(node): | |
63 | specialnames = ['__main__'] |
|
64 | specialnames = ['__main__'] | |
64 |
|
65 | |||
65 | if node.value in specialnames: |
|
66 | if node.value in specialnames: | |
66 | return False |
|
67 | return False | |
67 |
|
68 | |||
68 | ggparent = node.parent.parent.parent |
|
69 | ggparent = node.parent.parent.parent | |
69 | sggparent = str(ggparent) |
|
70 | sggparent = str(ggparent) | |
70 |
|
71 | |||
71 | if 'getattr' in sggparent or \ |
|
72 | if 'getattr' in sggparent or \ | |
72 | 'hasattr' in sggparent or \ |
|
73 | 'hasattr' in sggparent or \ | |
73 | 'setattr' in sggparent or \ |
|
74 | 'setattr' in sggparent or \ | |
74 | 'encode' in sggparent or \ |
|
75 | 'encode' in sggparent or \ | |
75 | 'decode' in sggparent: |
|
76 | 'decode' in sggparent: | |
76 | return False |
|
77 | return False | |
77 |
|
78 | |||
78 | return True |
|
79 | return True | |
79 |
|
80 | |||
80 | class FixBytes(fixer_base.BaseFix): |
|
81 | class FixBytes(fixer_base.BaseFix): | |
81 |
|
82 | |||
82 | PATTERN = 'STRING' |
|
83 | PATTERN = 'STRING' | |
83 |
|
84 | |||
84 | def transform(self, node, results): |
|
85 | def transform(self, node, results): | |
85 | if self.filename in blacklist: |
|
86 | if self.filename in blacklist: | |
86 | return |
|
87 | return | |
87 | if node.type == token.STRING: |
|
88 | if node.type == token.STRING: | |
88 | if _re.match(node.value): |
|
89 | if _re.match(node.value): | |
89 | if isdocstring(node): |
|
90 | if isdocstring(node): | |
90 | return |
|
91 | return | |
91 | if not shouldtransform(node): |
|
92 | if not shouldtransform(node): | |
92 | return |
|
93 | return | |
93 | new = node.clone() |
|
94 | new = node.clone() | |
94 | new.value = 'b' + new.value |
|
95 | new.value = 'b' + new.value | |
95 | return new |
|
96 | return new | |
96 |
|
97 |
General Comments 0
You need to be logged in to leave comments.
Login now