Skip to content

Commit b55041b

Browse files
author
Aidan Macdonald
committed
Moving to Python3
1 parent b803756 commit b55041b

File tree

13 files changed

+1181
-1180
lines changed

13 files changed

+1181
-1180
lines changed

java2python/compiler/block.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@
1616
from java2python.compiler import template, visitor
1717

1818

19-
def addTypeToModule((className, factoryName)):
19+
def addTypeToModule(xxx_todo_changeme):
2020
""" Constructs and adds a new type to this module. """
21+
(className, factoryName) = xxx_todo_changeme
2122
bases = (getattr(template, className), getattr(visitor, className))
2223
newType = type(className, bases, dict(factoryName=factoryName))
2324
setattr(modules[__name__], className, newType)
2425

2526

26-
map(addTypeToModule, (
27+
list(map(addTypeToModule, (
2728
('Annotation', 'at'),
2829
('Class', 'klass'),
2930
('Comment', 'comment'),
@@ -35,4 +36,4 @@ def addTypeToModule((className, factoryName)):
3536
('Module', 'module'),
3637
('Statement', 'statement'),
3738
)
38-
)
39+
))

java2python/compiler/template.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@
1313
# are usually a sign of a bad design and/or language limitations, and
1414
# this case is no exception.
1515

16-
from cStringIO import StringIO
16+
from io import StringIO
1717
from functools import partial
18-
from itertools import chain, ifilter, imap
18+
from itertools import chain
1919

2020
from java2python.lang import tokens
2121
from java2python.lib import FS, colors
22+
from functools import reduce
2223

2324

2425
class Factory(object):
@@ -74,7 +75,7 @@ def __init__(cls, name, bases, namespace):
7475
pass
7576

7677

77-
class Base(object):
78+
class Base(object, metaclass=FactoryTypeDetector):
7879
""" Base -> base class for formatting Python output.
7980
8081
This class defines a large set of attributes and methods for the
@@ -110,7 +111,6 @@ class Base(object):
110111
a the template as tree for debugging.
111112
112113
"""
113-
__metaclass__ = FactoryTypeDetector
114114
isAnnotation = isClass = isComment = isEnum = isExpression = \
115115
isInterface = isMethod = isModule = isStatement = False
116116

@@ -155,7 +155,7 @@ def altIdent(self, name):
155155
for klass in self.parents(lambda v:v.isClass):
156156
if name in klass.variables:
157157
try:
158-
method = self.parents(lambda v:v.isMethod).next()
158+
method = next(self.parents(lambda v:v.isMethod))
159159
except (StopIteration, ):
160160
return name
161161
if name in [p['name'] for p in method.parameters]:
@@ -173,20 +173,20 @@ def configHandler(self, part, suffix='Handler', default=None):
173173
def configHandlers(self, part, suffix='Handlers'):
174174
""" Returns config handlers for this type of template """
175175
name = '{0}{1}{2}'.format(self.typeName, part, suffix)
176-
return imap(self.toIter, chain(*self.config.every(name, [])))
176+
return map(self.toIter, chain(*self.config.every(name, [])))
177177

178178
def dump(self, fd, level=0):
179179
""" Writes the Python source code for this template to the given file. """
180180
indent, isNotNone = level * self.indent, lambda x:x is not None
181181
lineFormat = '{0}{1}\n'.format
182-
for line in ifilter(isNotNone, self.iterPrologue()):
182+
for line in filter(isNotNone, self.iterPrologue()):
183183
line = lineFormat(indent, line)
184184
fd.write(line if line.strip() else '\n')
185-
for item in ifilter(isNotNone, self.iterHead()):
185+
for item in filter(isNotNone, self.iterHead()):
186186
item.dump(fd, level+1)
187187
for item in self.iterBody():
188188
item.dump(fd, level+1)
189-
for line in ifilter(isNotNone, self.iterEpilogue()):
189+
for line in filter(isNotNone, self.iterEpilogue()):
190190
line = lineFormat(indent, line)
191191
fd.write(line if line.strip() else '\n')
192192

@@ -200,7 +200,7 @@ def dumpRepr(self, fd, level=0):
200200
""" Writes a debug string for this template to the given file. """
201201
indent, default = self.indent, lambda x, y:None
202202
fd.write('{0}{1!r}\n'.format(indent*level, self))
203-
for child in ifilter(None, self.children):
203+
for child in filter(None, self.children):
204204
getattr(child, 'dumpRepr', default)(fd, level+1)
205205

206206
@property
@@ -230,7 +230,7 @@ def iterPrologue(self):
230230
def iterHead(self):
231231
""" Yields the items in the head of this template. """
232232
items = chain(*(h(self) for h in self.configHandlers('Head')))
233-
return imap(self.toExpr, items)
233+
return map(self.toExpr, items)
234234

235235
def iterBody(self):
236236
""" Yields the items in the body of this template. """
@@ -304,10 +304,10 @@ def __init__(self, config, left='', right='', fs=FS.lr, parent=None, tail=''):
304304
def __repr__(self):
305305
""" Returns the debug string representation of this template. """
306306
parts, parent, showfs = [colors.blue(self.typeName)], self.parent, True
307-
if isinstance(self.left, (basestring, )) and self.left:
307+
if isinstance(self.left, str) and self.left:
308308
parts.append(colors.white('left:') + colors.yellow(self.left))
309309
showfs = False
310-
if isinstance(self.right, (basestring, )) and self.right:
310+
if isinstance(self.right, str) and self.right:
311311
parts.append(colors.white('right:') + colors.yellow(self.right))
312312
showfs = False
313313
if self.modifiers:

java2python/compiler/visitor.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
from functools import reduce, partial
16-
from itertools import ifilter, ifilterfalse, izip, tee
16+
from itertools import filterfalse, tee
1717
from logging import debug, warn
1818
from re import compile as recompile, sub as resub
1919

@@ -31,7 +31,7 @@ def __init__(self):
3131
class Base(object):
3232
""" Base -> Parent class for AST visitors. """
3333

34-
commentSubs = map(recompile, ['^\s*/(\*)+', '(\*)+/\s*$', '^\s*//'])
34+
commentSubs = list(map(recompile, ['^\s*/(\*)+', '(\*)+/\s*$', '^\s*//']))
3535

3636
def accept(self, node, memo):
3737
""" Accept a node, possibly creating a child visitor. """
@@ -48,7 +48,7 @@ def insertComments(self, tmpl, tree, index, memo):
4848
cache, parser, comTypes = memo.comments, tree.parser, tokens.commentTypes
4949
comNew = lambda t:t.type in comTypes and (t.index not in cache)
5050

51-
for tok in ifilter(comNew, parser.input.tokens[memo.last:index]):
51+
for tok in filter(comNew, parser.input.tokens[memo.last:index]):
5252
cache.add(tok.index)
5353

5454
# loop over parents until we find the top expression
@@ -70,7 +70,7 @@ def insertComments(self, tmpl, tree, index, memo):
7070
def stripComment(self, text):
7171
""" Regex substitutions for comments; removes comment characters. """
7272
subText = lambda value, regex:resub(regex, '', value)
73-
for text in ifilter(unicode.strip, text.split('\n')):
73+
for text in filter(str.strip, text.split('\n')):
7474
yield reduce(subText, self.commentSubs, text)
7575

7676
def walk(self, tree, memo=None):
@@ -95,7 +95,7 @@ def walk(self, tree, memo=None):
9595

9696
def zipWalk(self, nodes, visitors, memo):
9797
""" Walk the given nodes zipped with the given visitors. """
98-
for node, visitor in izip(nodes, visitors):
98+
for node, visitor in zip(nodes, visitors):
9999
visitor.walk(node, memo)
100100

101101
def nodeTypeToString(self, node):
@@ -132,7 +132,7 @@ def acceptType(self, node, memo):
132132
_acceptInterface = makeAcceptType('interface')
133133

134134
def acceptInterface(self, node, memo):
135-
module = self.parents(lambda x:x.isModule).next()
135+
module = next(self.parents(lambda x:x.isModule))
136136
module.needsAbstractHelpers = True
137137
return self._acceptInterface(node, memo)
138138

@@ -161,9 +161,9 @@ class ModifiersAcceptor(object):
161161
def acceptModifierList(self, node, memo):
162162
""" Accept and process class and method modifiers. """
163163
isAnno = lambda token:token.type==tokens.AT
164-
for ano in ifilter(isAnno, node.children):
164+
for ano in filter(isAnno, node.children):
165165
self.nodesToAnnos(ano, memo)
166-
for mod in ifilterfalse(isAnno, node.children):
166+
for mod in filterfalse(isAnno, node.children):
167167
self.nodesToModifiers(mod, node)
168168
return self
169169

@@ -407,7 +407,7 @@ def acceptCatch(self, node, memo):
407407

408408
def acceptContinue(self, node, memo):
409409
""" Accept and process a continue statement. """
410-
parent = node.parents(lambda x: x.type in {tokens.FOR, tokens.FOR_EACH, tokens.DO, tokens.WHILE}).next()
410+
parent = next(node.parents(lambda x: x.type in {tokens.FOR, tokens.FOR_EACH, tokens.DO, tokens.WHILE}))
411411
if parent.type == tokens.FOR:
412412
updateStat = self.factory.expr(parent=self)
413413
updateStat.walk(parent.firstChildOfType(tokens.FOR_UPDATE), memo)
@@ -561,7 +561,7 @@ def acceptSwitch(self, node, memo):
561561

562562
def acceptSynchronized(self, node, memo):
563563
""" Accept and process a synchronized statement (not a modifier). """
564-
module = self.parents(lambda x:x.isModule).next()
564+
module = next(self.parents(lambda x:x.isModule))
565565
module.needsSyncHelpers = True
566566
if node.parent.type == tokens.MODIFIER_LIST:
567567
# Skip any synchronized modifier
@@ -743,7 +743,7 @@ def acceptPrePost(self, node, memo):
743743
name = node.firstChildOfType(tokens.IDENT).text
744744
handler = self.configHandler('VariableNaming')
745745
rename = handler(name)
746-
block = self.parents(lambda x:x.isMethod).next()
746+
block = next(self.parents(lambda x:x.isMethod))
747747
if pre:
748748
left = name
749749
else:
@@ -768,7 +768,7 @@ def acceptBitShiftRight(self, node, memo):
768768
self.fs = 'bsr(' + FS.l + ', ' + FS.r + ')'
769769
self.left, self.right = visitors = factory(parent=self), factory()
770770
self.zipWalk(node.children, visitors, memo)
771-
module = self.parents(lambda x:x.isModule).next()
771+
module = next(self.parents(lambda x:x.isModule))
772772
module.needsBsrFunc = True
773773

774774
def acceptBitShiftRightAssign(self, node, memo):
@@ -777,7 +777,7 @@ def acceptBitShiftRightAssign(self, node, memo):
777777
self.fs = FS.l + ' = bsr(' + FS.l + ', ' + FS.r + ')'
778778
self.left, self.right = visitors = factory(parent=self), factory()
779779
self.zipWalk(node.children, visitors, memo)
780-
module = self.parents(lambda x:x.isModule).next()
780+
module = next(self.parents(lambda x:x.isModule))
781781
module.needsBsrFunc = True
782782

783783
def acceptClassConstructorCall(self, node, memo):
@@ -853,12 +853,12 @@ def acceptStaticArrayCreator(self, node, memo):
853853

854854
def acceptSuper(self, node, memo):
855855
""" Accept and process a super expression. """
856-
cls = self.parents(lambda c:c.isClass).next()
856+
cls = next(self.parents(lambda c:c.isClass))
857857
self.right = self.factory.expr(fs='super({name}, self)'.format(name=cls.name))
858858

859859
def acceptSuperConstructorCall(self, node, memo):
860860
""" Accept and process a super constructor call. """
861-
cls = self.parents(lambda c:c.isClass).next()
861+
cls = next(self.parents(lambda c:c.isClass))
862862
fs = 'super(' + FS.l + ', self).__init__(' + FS.r + ')'
863863
self.right = self.factory.expr(fs=fs, left=cls.name)
864864
return self.right

0 commit comments

Comments
 (0)