Mercurial > hg
annotate contrib/hgfixes/fix_bytesmod.py @ 25329:101e84121c13
dispatch: disable demandimport for the --debugger option
Something in Python 2.7.9 or so broke the --debugger option with
ui.debugger = ipdb. I get the traceback below. There is some apparent
confusion with demandimport. This should be disabled anyway for the
--debugger option. The debugger must be imported right away, before
any other dispatch. There's no benefit in delaying the debugger
import.
This patch uses the demandimport.deactivated() context manager.
Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/mercurial/dispatch.py", line 121, in _runcatch
debugmod = __import__(debugger)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 115, in _demandimport
return _hgextimport(_import, name, globals, locals, fromlist, level)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/ipdb/__init__.py", line 16, in <module>
from ipdb.__main__ import set_trace, post_mortem, pm, run, runcall, runeval, launch_ipdb_on_exception
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 134, in _demandimport
mod = _hgextimport(_origimport, name, globals, locals)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/ipdb/__main__.py", line 29, in <module>
if IPython.__version__ > '0.10.2':
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 106, in __getattribute__
self._load()
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 78, in _load
mod = _hgextimport(_import, head, globals, locals, None, level)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/IPython/__init__.py", line 45, in <module>
from .config.loader import Config
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 132, in _demandimport
return _origimport(name, globals, locals, fromlist, level)
File "/usr/lib/python2.7/dist-packages/IPython/config/__init__.py", line 16, in <module>
from .application import *
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 115, in _demandimport
return _hgextimport(_import, name, globals, locals, fromlist, level)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/IPython/config/application.py", line 30, in <module>
from IPython.external.decorator import decorator
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 134, in _demandimport
mod = _hgextimport(_origimport, name, globals, locals)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/IPython/external/decorator/__init__.py", line 2, in <module>
from decorator import *
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 115, in _demandimport
return _hgextimport(_import, name, globals, locals, fromlist, level)
File "/usr/lib/python2.7/dist-packages/mercurial/demandimport.py", line 47, in _hgextimport
return importfunc(name, globals, *args)
File "/usr/lib/python2.7/dist-packages/decorator.py", line 240, in <module>
'ContextManager', (_GeneratorContextManager,), dict(__call__=__call__))
author | Jordi Gutiérrez Hermoso <jordigh@octave.org> |
---|---|
date | Thu, 28 May 2015 16:42:21 -0400 |
parents | d20817ac628a |
children |
rev | line source |
---|---|
11749
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
1 """Fixer that changes bytes % whatever to a function that actually formats |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
2 it.""" |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
3 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
4 from lib2to3 import fixer_base |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
5 from lib2to3.fixer_util import is_tuple, Call, Comma, Name, touch_import |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
6 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
7 # XXX: Implementing a blacklist in 2to3 turned out to be more troublesome than |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
8 # blacklisting some modules inside the fixers. So, this is what I came with. |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
9 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
10 blacklist = ['mercurial/demandimport.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
11 'mercurial/py3kcompat.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
12 'mercurial/i18n.py', |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
13 ] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
14 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
15 def isnumberremainder(formatstr, data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
16 try: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
17 if data.value.isdigit(): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
18 return True |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
19 except AttributeError: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
20 return False |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
21 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
22 class FixBytesmod(fixer_base.BaseFix): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
23 # XXX: There's one case (I suppose) I can't handle: when a remainder |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
24 # operation like foo % bar is performed, I can't really know what the |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
25 # contents of foo and bar are. I believe the best approach is to "correct" |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
26 # the to-be-converted code and let bytesformatter handle that case in |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
27 # runtime. |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
28 PATTERN = ''' |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
29 term< formatstr=STRING '%' data=STRING > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
30 term< formatstr=STRING '%' data=atom > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
31 term< formatstr=NAME '%' data=any > | |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
32 term< formatstr=any '%' data=any > |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
33 ''' |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
34 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
35 def transform(self, node, results): |
20399
74daabdf5ab5
fix_bytesmod: fix defects in fix_bytesmod so it produces working code
Augie Fackler <raf@durin42.com>
parents:
19872
diff
changeset
|
36 for bfn in blacklist: |
74daabdf5ab5
fix_bytesmod: fix defects in fix_bytesmod so it produces working code
Augie Fackler <raf@durin42.com>
parents:
19872
diff
changeset
|
37 if self.filename.endswith(bfn): |
74daabdf5ab5
fix_bytesmod: fix defects in fix_bytesmod so it produces working code
Augie Fackler <raf@durin42.com>
parents:
19872
diff
changeset
|
38 return |
74daabdf5ab5
fix_bytesmod: fix defects in fix_bytesmod so it produces working code
Augie Fackler <raf@durin42.com>
parents:
19872
diff
changeset
|
39 if not self.filename.endswith('mercurial/py3kcompat.py'): |
20701
d20817ac628a
fix_bytesmod: use the "from mercurial" form of the import to avoid breaking httpclient
Augie Fackler <raf@durin42.com>
parents:
20399
diff
changeset
|
40 touch_import('mercurial', 'py3kcompat', node=node) |
11749
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
41 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
42 formatstr = results['formatstr'].clone() |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
43 data = results['data'].clone() |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
44 formatstr.prefix = '' # remove spaces from start |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
45 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
46 if isnumberremainder(formatstr, data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
47 return |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
48 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
49 # We have two possibilities: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
50 # 1- An identifier or name is passed, it is going to be a leaf, thus, we |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
51 # just need to copy its value as an argument to the formatter; |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
52 # 2- A tuple is explicitly passed. In this case, we're gonna explode it |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
53 # to pass to the formatter |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
54 # TODO: Check for normal strings. They don't need to be translated |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
55 |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
56 if is_tuple(data): |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
57 args = [formatstr, Comma().clone()] + \ |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
58 [c.clone() for c in data.children[:]] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
59 else: |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
60 args = [formatstr, Comma().clone(), data] |
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
61 |
19872
681f7b9213a4
check-code: check for spaces around = for named parameters
Mads Kiilerich <madski@unity3d.com>
parents:
11749
diff
changeset
|
62 call = Call(Name('bytesformatter', prefix=' '), args) |
11749
e627fef94604
hgfixes: added a fixer that makes bytes to be formatted correctly
Renato Cunha <renatoc@gmail.com>
parents:
diff
changeset
|
63 return call |