Add 2to3 fixer to process utils.gen.IterableMap correctly.

This commit is contained in:
Valentin Lorentz 2012-08-05 12:32:57 +02:00
parent 5a35c7fafd
commit 32a7da6f51
6 changed files with 91 additions and 7 deletions

22
2to3/fix_def_iteritems.py Normal file
View File

@ -0,0 +1,22 @@
"""Fixer for iteritems -> items methods."""
# Author: Valentin Lorentz
# Code modified from fix_nonzero by Collin Winter
from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, syms
class FixDefIteritems(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """
classdef< 'class' any+ ':'
suite< any*
funcdef< 'def' name='iteritems'
parameters< '(' NAME ')' > any+ >
any* > >
"""
def transform(self, node, results):
name = results["name"]
new = Name(u"items", prefix=name.prefix)
name.replace(new)

22
2to3/fix_def_iterkeys.py Normal file
View File

@ -0,0 +1,22 @@
"""Fixer for iterkeys -> keys methods."""
# Author: Valentin Lorentz
# Code modified from fix_nonzero by Collin Winter
from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, syms
class FixDefIterkeys(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """
classdef< 'class' any+ ':'
suite< any*
funcdef< 'def' name='iterkeys'
parameters< '(' NAME ')' > any+ >
any* > >
"""
def transform(self, node, results):
name = results["name"]
new = Name(u"keys", prefix=name.prefix)
name.replace(new)

View File

@ -0,0 +1,22 @@
"""Fixer for itervalues -> values methods."""
# Author: Valentin Lorentz
# Code modified from fix_nonzero by Collin Winter
from lib2to3 import fixer_base
from lib2to3.fixer_util import Name, syms
class FixDefItervalues(fixer_base.BaseFix):
BM_compatible = True
PATTERN = """
classdef< 'class' any+ ':'
suite< any*
funcdef< 'def' name='itervalues'
parameters< '(' NAME ')' > any+ >
any* > >
"""
def transform(self, node, results):
name = results["name"]
new = Name(u"values", prefix=name.prefix)
name.replace(new)

12
2to3/run.py Normal file
View File

@ -0,0 +1,12 @@
#! /usr/bin/python2.7
import sys
from lib2to3.main import main
import fix_def_iteritems, fix_def_itervalues, fix_def_iterkeys
# Hacks
sys.modules['lib2to3.fixes.fix_def_iteritems'] = fix_def_iteritems
sys.modules['lib2to3.fixes.fix_def_itervalues'] = fix_def_itervalues
sys.modules['lib2to3.fixes.fix_def_iterkeys'] = fix_def_iterkeys
sys.exit(main("lib2to3.fixes"))

1
sandbox/run_2to3.sh Executable file
View File

@ -0,0 +1 @@
python 2to3/run.py src/ plugins/ test/ scripts/* setup.py -wn -f all -f def_iteritems -f def_itervalues -f def_iterkeys "$@"

View File

@ -206,12 +206,14 @@ class IterableMap(object):
def iterkeys(self): def iterkeys(self):
for (key, _) in self.iteritems(): for (key, _) in self.iteritems():
yield key yield key
__iter__ = iterkeys
def itervalues(self): def itervalues(self):
for (_, value) in self.iteritems(): for (_, value) in self.iteritems():
yield value yield value
if sys.version_info[0] < 3:
# Our 2to3 fixers automatically rename iteritems/iterkeys/itervalues
# to items/keys/values
def items(self): def items(self):
return list(self.iteritems()) return list(self.iteritems())
@ -220,6 +222,9 @@ class IterableMap(object):
def values(self): def values(self):
return list(self.itervalues()) return list(self.itervalues())
__iter__ = iterkeys
else:
__iter__ = items
def __len__(self): def __len__(self):
ret = 0 ret = 0