2to3: apply `dict` fixer. #3205

Merged
merged 2 commits into from Apr 7, 2013

Projects

None yet

2 participants

@charris
Member
charris commented Apr 7, 2013

In Python3 dict.items(), dict.keys(), and dict.values() are
iterators. This causes problems when a list is needed so the 2to3 fixer
explicitly constructs a list when is finds on of those functions.
However, that is usually not necessary, so a lot of the work here has
been cleaning up those places where the fix is not needed. The big
exception to that is the numpy/f2py/crackfortran.py file. The code
there makes extensive use of loops that modify the contents of the
dictionary being looped through, which raises an error. That together
with the obscurity of the code in that file made it safest to let the
dict fixer do its worst.

Closes #3050.

@charris charris 2to3: apply `dict` fixer.
In Python3 `dict.items()`, `dict.keys()`, and `dict.values()` are
iterators. This causes problems when a list is needed so the 2to3 fixer
explicitly constructs a list when is finds on of those functions.
However, that is usually not necessary, so a lot of the work here has
been cleaning up those places where the fix is not needed. The big
exception to that is the `numpy/f2py/crackfortran.py` file. The code
there makes extensive use of loops that modify the contents of the
dictionary being looped through, which raises an error. That together
with the obscurity of the code in that file made it safest to let the
`dict` fixer do its worst.

Closes #3050.
aab46a7
@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/core/_internal.py
@@ -332,7 +332,7 @@ def _index_fields(ary, fields):
'O': 'O',
'x': 'V', # padding
}
-_pep3118_native_typechars = ''.join(_pep3118_native_map.keys())
+_pep3118_native_typechars = ''.join(list(_pep3118_native_map.keys()))
@njsmith
njsmith Apr 7, 2013 Member

join works on iterators

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/core/_internal.py
@@ -356,7 +356,7 @@ def _index_fields(ary, fields):
'O': 'O',
'x': 'V', # padding
}
-_pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys())
+_pep3118_standard_typechars = ''.join(list(_pep3118_standard_map.keys()))
@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/core/_internal.py
@@ -505,7 +505,7 @@ def get_dummy_name():
offset += extra_offset
# Check if this was a simple 1-item type
- if len(fields.keys()) == 1 and not explicit_name and fields['f0'][1] == 0 \
+ if len(list(fields.keys())) == 1 and not explicit_name and fields['f0'][1] == 0 \
@njsmith
njsmith Apr 7, 2013 Member

len works on the return value from keys in both py2 and py3. (Though here i guess just len(fields) would also work...)

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/core/code_generators/genapi.py
@@ -448,7 +448,7 @@ def fullapi_hash(api_dicts):
def sorted_by_values(d):
"""Sort a dictionary by its values. Assume the dictionary items is of
the form func_name -> order"""
- return sorted(d.items(), key=lambda x_y: (x_y[1], x_y[0]))
+ return sorted(list(d.items()), key=lambda x_y: (x_y[1], x_y[0]))
@njsmith
njsmith Apr 7, 2013 Member

sorted works on arbitrary iterables

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/core/numerictypes.py
@@ -863,7 +863,7 @@ def sctype2char(sctype):
# Py3K
ScalarType = [int, float, complex, long, bool, bytes, str, memoryview]
-ScalarType.extend(_sctype2char_dict.keys())
+ScalarType.extend(list(_sctype2char_dict.keys()))
@njsmith
njsmith Apr 7, 2013 Member

list.extend works on arbitrary iterables.

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/distutils/fcompiler/__init__.py
@@ -542,7 +542,7 @@ def get_flags(tag, flags):
def dump_properties(self):
"""Print out the attributes of a compiler instance."""
props = []
- for key in self.executables.keys() + \
+ for key in iter(self.executables.keys()) + \
['version','libraries','library_dirs',
'object_switch','compile_switch']:
@njsmith
njsmith Apr 7, 2013 Member

Uh... this doesn't work, does it? the iter should be list?

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/distutils/fcompiler/__init__.py
@@ -848,7 +848,7 @@ def new_fcompiler(plat=None,
if compiler is not None:
msg = msg + " with '%s' compiler." % compiler
msg = msg + " Supported compilers are: %s)" \
- % (','.join(fcompiler_class.keys()))
+ % (','.join(list(fcompiler_class.keys())))
@njsmith
njsmith Apr 7, 2013 Member

join takes arbitrary iterables

@njsmith njsmith and 1 other commented on an outdated diff Apr 7, 2013
numpy/distutils/misc_util.py
@@ -2208,12 +2208,12 @@ def get_info(name):
return g.get(name, g.get(name + "_info", {}))
def show():
- for name,info_dict in globals().items():
+ for name,info_dict in globals().iteritems():
@njsmith
njsmith Apr 7, 2013 Member

I thought py3 had no iteritems? Won't this be an AttributeError?

@charris
charris Apr 7, 2013 Member

This came in because I started by replacing with explicit iterators and then let the fixer fix them. But it didn't fix string contents...

@njsmith njsmith commented on an outdated diff Apr 7, 2013
numpy/distutils/misc_util.py
if name[0] == "_" or type(info_dict) is not type({}): continue
print(name + ":")
if not info_dict:
print(" NOT AVAILABLE")
- for k,v in info_dict.items():
+ for k,v in info_dict.iteritems():
@njsmith
njsmith Apr 7, 2013 Member

Ditto.

@njsmith
Member
njsmith commented Apr 7, 2013

The rest look right to me.

@charris charris MAINT: Fixup more dictionary fixer fixes.
Most of these are places where an iterator will work as well as a
list, but two fix places where `iterkeys` was introduced which is
a bug.
6339930
@charris
Member
charris commented Apr 7, 2013

Thanks for the review, the PR needed it.

@charris
Member
charris commented Apr 7, 2013

In the light of a new day, it also looks good to me. I'm going to put it in.

@charris charris merged commit 1340fa6 into numpy:master Apr 7, 2013

1 check passed

default The Travis build passed
Details
@charris charris deleted the charris:2to3-apply-dict-fixer branch Apr 7, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment