Skip to content

Commit 1842865

Browse files
committed
Upgrade to Python3 and antlr-python3-runtime >= 3.4 (tested with 3.5.3)
1 parent caf598b commit 1842865

File tree

10 files changed

+143
-149
lines changed

10 files changed

+143
-149
lines changed

Diff for: bin/j2py

+11-10
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ This is all very ordinary. We import the package bits, open and read
66
a file, translate it, and write it out.
77
88
"""
9+
import os.path
910
import sys
1011
from argparse import ArgumentParser, ArgumentTypeError
1112
from collections import defaultdict
12-
from logging import _levelNames as logLevels, exception, warning, info, basicConfig
13+
from logging import _nameToLevel as logLevels, exception, warning, info, basicConfig
1314
from os import path, makedirs
1415
from time import time
1516

@@ -60,8 +61,8 @@ def runOneOrMany(options):
6061
""" Runs our main transformer with each of the input files. """
6162
infile, outfile = options.inputfile, options.outputfile
6263

63-
if infile and not isinstance(infile, file) and path.isdir(infile):
64-
if outfile and not isinstance(outfile, file) and not path.isdir(outfile):
64+
if infile and not os.path.isfile(infile) and path.isdir(infile):
65+
if outfile and not os.path.isfile(outfile) and not path.isdir(outfile):
6566
warning('Must specify output directory or stdout when using input directory.')
6667
return 2
6768
def walker(arg, dirname, files):
@@ -89,9 +90,9 @@ def runTransform(options):
8990
timed['overall']
9091

9192
filein = fileout = filedefault = '-'
92-
if options.inputfile and not isinstance(options.inputfile, file):
93+
if options.inputfile and not os.path.isfile(options.inputfile):
9394
filein = options.inputfile
94-
if options.outputfile and not isinstance(options.outputfile, file):
95+
if options.outputfile and not os.path.isfile(options.outputfile):
9596
fileout = options.outputfile
9697
elif fileout != filedefault:
9798
fileout = '%s.py' % (path.splitext(filein)[0])
@@ -110,15 +111,15 @@ def runTransform(options):
110111
source = open(filein).read()
111112
else:
112113
source = sys.stdin.read()
113-
except (IOError, ), exc:
114+
except (IOError, ) as exc:
114115
code, msg = exc.args[0:2]
115-
print 'IOError: %s.' % (msg, )
116+
print ('IOError: %s.' % (msg, ))
116117
return code
117118

118119
timed['comp']
119120
try:
120121
tree = buildAST(source)
121-
except (Exception, ), exc:
122+
except (Exception, ) as exc:
122123
exception('exception while parsing')
123124
return 1
124125
timed['comp_finish']
@@ -164,7 +165,7 @@ def runTransform(options):
164165
if not options.skipcompile:
165166
try:
166167
compile(source, '<string>', 'exec')
167-
except (SyntaxError, ), ex:
168+
except (SyntaxError, ) as ex:
168169
warning('Generated source has invalid syntax. %s', ex)
169170
else:
170171
info('Generated source has valid syntax.')
@@ -256,4 +257,4 @@ def configScript(argv):
256257

257258

258259
if __name__ == '__main__':
259-
sys.exit(runMain(configScript(sys.argv[1:])))
260+
sys.exit(runMain(configScript(sys.argv[1:])))

Diff for: java2python/compiler/block.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from java2python.compiler import template, visitor
1717

1818

19-
def addTypeToModule((className, factoryName)):
19+
def addTypeToModule(className, factoryName):
2020
""" Constructs and adds a new type to this module. """
2121
bases = (getattr(template, className), getattr(visitor, className))
2222
newType = type(className, bases, dict(factoryName=factoryName))

Diff for: java2python/compiler/template.py

+8-8
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
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
@@ -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. """

Diff for: java2python/compiler/visitor.py

+6-6
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

@@ -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(unicode.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):
@@ -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

Diff for: java2python/lang/JavaLexer.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import sys
44
from antlr3 import *
5-
from antlr3.compat import set, frozenset
5+
6+
from java2python.lang.compat import version_str_to_tuple
67

78

89
# for convenience in actions

0 commit comments

Comments
 (0)