Skip to content

Commit 347e5cd

Browse files
committed
Fix issue #40: separate out fix_execfile and fix_cmp
1 parent c3731c8 commit 347e5cd

File tree

4 files changed

+40
-13
lines changed

4 files changed

+40
-13
lines changed

docs/whatsnew.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,9 @@ On Python 3, as usual, ``object`` simply points to ``builtins.object``.
135135
past.builtins
136136
-------------
137137
The ``past.builtins`` package has been extended to add Py3 support for
138-
additional Py2 constructs that are not adequately handled by ``lib2to3`` (see upstream bug #). This
139-
includes custom ``execfile()`` and ``cmp()`` functions. ``futurize`` now
140-
invokes imports of these functions from ``past.builtins``.
138+
additional Py2 constructs that are not adequately handled by ``lib2to3`` (see
139+
upstream bug #). This includes custom ``execfile()`` and ``cmp()`` functions.
140+
``futurize`` now invokes imports of these functions from ``past.builtins``.
141141

142142

143143
Relative imports from Cython modules

libfuturize/fixes/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575

7676
libfuturize_fix_names_stage2 = set([
7777
# 'libfuturize.fixes.fix_add__future__imports_except_unicode_literals', # just in case
78+
'libfuturize.fixes.fix_cmp',
7879
'libfuturize.fixes.fix_execfile',
7980
'libfuturize.fixes.fix_future_builtins',
8081
'libfuturize.fixes.fix_future_standard_library',

libfuturize/fixes/fix_cmp.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# coding: utf-8
2+
"""
3+
Fixer for the cmp() function on Py2, which was removed in Py3.
4+
5+
Adds this import line::
6+
7+
from past.builtins import cmp
8+
9+
if cmp() is called in the code.
10+
"""
11+
12+
from __future__ import unicode_literals
13+
from lib2to3 import fixer_base
14+
15+
from libfuturize.fixer_util import touch_import_top
16+
17+
18+
expression = "name='cmp'"
19+
20+
21+
class FixCmp(fixer_base.BaseFix):
22+
BM_compatible = True
23+
run_order = 9
24+
25+
PATTERN = """
26+
power<
27+
({0}) trailer< '(' args=[any] ')' >
28+
rest=any* >
29+
""".format(expression)
30+
31+
def transform(self, node, results):
32+
name = results["name"]
33+
touch_import_top(u'past.builtins', name.value, node)
34+

libfuturize/fixes/fix_execfile.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,18 @@
88
99
Adds this import line::
1010
11-
from past.builtins import cmp
1211
from past.builtins import execfile
1312
14-
for each of the functions cmp, execfile that were removed from Py3.
15-
16-
Adds these imports after any other imports (in an initial block of them).
13+
for the function execfile() that was removed from Py3.
1714
"""
1815

1916
from __future__ import unicode_literals
20-
2117
from lib2to3 import fixer_base
22-
from lib2to3.pygram import python_symbols as syms
23-
from lib2to3.fixer_util import Name, Call, in_special_context
2418

2519
from libfuturize.fixer_util import touch_import_top
2620

2721

28-
replaced_builtins = '''cmp execfile'''.split()
29-
30-
expression = '|'.join(["name='{0}'".format(name) for name in replaced_builtins])
22+
expression = "name='execfile'"
3123

3224

3325
class FixExecfile(fixer_base.BaseFix):

0 commit comments

Comments
 (0)