From ecc58f3f38e0834961984fa7da9dff179d91cd89 Mon Sep 17 00:00:00 2001 From: changeling Date: Thu, 16 Jul 2020 13:51:18 -0500 Subject: [PATCH 1/4] Update Python 2 `.next()` to Python 3 `next()`. `.next()` has been replaced with `next()` in Python 3. Fixed. --- java2python/compiler/template.py | 2 +- java2python/compiler/visitor.py | 12 ++++++------ java2python/mod/basic.py | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/java2python/compiler/template.py b/java2python/compiler/template.py index 5c06182..cbaf360 100644 --- a/java2python/compiler/template.py +++ b/java2python/compiler/template.py @@ -157,7 +157,7 @@ def altIdent(self, name): for klass in self.parents(lambda v:v.isClass): if name in klass.variables: try: - method = self.parents(lambda v:v.isMethod).next() + method = next(self.parents(lambda v:v.isMethod)) except (StopIteration, ): return name if name in [p['name'] for p in method.parameters]: diff --git a/java2python/compiler/visitor.py b/java2python/compiler/visitor.py index e11a5ce..eec2bfb 100644 --- a/java2python/compiler/visitor.py +++ b/java2python/compiler/visitor.py @@ -540,7 +540,7 @@ def acceptSwitch(self, node, memo): def acceptSynchronized(self, node, memo): """ Accept and process a synchronized statement (not a modifier). """ - module = self.parents(lambda x:x.isModule).next() + module = next(self.parents(lambda x:x.isModule)) module.needsSyncHelpers = True if node.parent.type == tokens.MODIFIER_LIST: # Skip any synchronized modifier @@ -700,7 +700,7 @@ def acceptPrePost(self, node, memo): name = node.firstChildOfType(tokens.IDENT).text handler = self.configHandler('VariableNaming') rename = handler(name) - block = self.parents(lambda x:x.isMethod).next() + block = next(self.parents(lambda x:x.isMethod)) if pre: left = name else: @@ -725,7 +725,7 @@ def acceptBitShiftRight(self, node, memo): self.fs = 'bsr(' + FS.l + ', ' + FS.r + ')' self.left, self.right = visitors = factory(parent=self), factory() self.zipWalk(node.children, visitors, memo) - module = self.parents(lambda x:x.isModule).next() + module = next(self.parents(lambda x:x.isModule)) module.needsBsrFunc = True def acceptBitShiftRightAssign(self, node, memo): @@ -734,7 +734,7 @@ def acceptBitShiftRightAssign(self, node, memo): self.fs = FS.l + ' = bsr(' + FS.l + ', ' + FS.r + ')' self.left, self.right = visitors = factory(parent=self), factory() self.zipWalk(node.children, visitors, memo) - module = self.parents(lambda x:x.isModule).next() + module = next(self.parents(lambda x:x.isModule)) module.needsBsrFunc = True def acceptClassConstructorCall(self, node, memo): @@ -810,12 +810,12 @@ def acceptStaticArrayCreator(self, node, memo): def acceptSuper(self, node, memo): """ Accept and process a super expression. """ - cls = self.parents(lambda c:c.isClass).next() + cls = next(self.parents(lambda c:c.isClass)) self.right = self.factory.expr(fs='super({name}, self)'.format(name=cls.name)) def acceptSuperConstructorCall(self, node, memo): """ Accept and process a super constructor call. """ - cls = self.parents(lambda c:c.isClass).next() + cls = next(self.parents(lambda c:c.isClass)) fs = 'super(' + FS.l + ', self).__init__(' + FS.r + ')' self.right = self.factory.expr(fs=fs, left=cls.name) return self.right diff --git a/java2python/mod/basic.py b/java2python/mod/basic.py index e986061..97c5765 100644 --- a/java2python/mod/basic.py +++ b/java2python/mod/basic.py @@ -129,13 +129,13 @@ def maybeAbstractMethod(method): def maybeSynchronizedMethod(method): if 'synchronized' in method.modifiers: - module = method.parents(lambda x:x.isModule).next() + module = next(method.parents(lambda x:x.isModule)) module.needsSyncHelpers = True yield '@synchronized' def globalNameCounter(original, counter=count()): - return '__{0}_{1}'.format(original, counter.next()) + return '__{0}_{1}'.format(original, next(counter)) def getBsrSrc(): From b9c944a48591cd24e1b51e6552c0bb59a13f892a Mon Sep 17 00:00:00 2001 From: changeling Date: Thu, 16 Jul 2020 13:53:26 -0500 Subject: [PATCH 2/4] Replaced Python 2 `isinstance(x, file)` with `isinstance(x, io.IOBase)`. Updated `except` to use `as` keywords. Replaced Python 2 `isinstance(x, file)` with Python 3 `isinstance(x, io.IOBase)`. Fixed. Python 3 no longer allows `file` in isinstance(), updated to use `io.IOBase`. Fixed. Python 3 requires `as` keyword in `except`, in place of comma separator. Fixed. `_levelNames` has been replaced with `_nameToLevel` in `logging`. Fixed. Changed `print 'IOError: %s.' % (msg, )` to `print(f'IOError: {msg}.')` --- bin/j2py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/bin/j2py b/bin/j2py index 6eb1a40..b7c6e0d 100755 --- a/bin/j2py +++ b/bin/j2py @@ -6,10 +6,11 @@ This is all very ordinary. We import the package bits, open and read a file, translate it, and write it out. """ +import io import sys from argparse import ArgumentParser, ArgumentTypeError from collections import defaultdict -from logging import _levelNames as logLevels, exception, warning, info, basicConfig +from logging import _nameToLevel as logLevels, exception, warning, info, basicConfig from os import path, makedirs from time import time @@ -60,8 +61,8 @@ def runOneOrMany(options): """ Runs our main transformer with each of the input files. """ infile, outfile = options.inputfile, options.outputfile - if infile and not isinstance(infile, file) and path.isdir(infile): - if outfile and not isinstance(outfile, file) and not path.isdir(outfile): + if infile and not isinstance(infile, io.IOBase) and path.isdir(infile): + if outfile and not isinstance(outfile, io.IOBase) and not path.isdir(outfile): warning('Must specify output directory or stdout when using input directory.') return 2 def walker(arg, dirname, files): @@ -69,7 +70,7 @@ def runOneOrMany(options): fullname = path.join(dirname, name) options.inputfile = fullname info('opening %s', fullname) - if outfile and outfile != '-' and not isinstance(outfile, file): + if outfile and outfile != '-' and not isinstance(outfile, io.IOBase): full = path.abspath(path.join(outfile, fullname)) head, tail = path.split(full) tail = path.splitext(tail)[0] + '.py' @@ -89,15 +90,15 @@ def runTransform(options): timed['overall'] filein = fileout = filedefault = '-' - if options.inputfile and not isinstance(options.inputfile, file): + if options.inputfile and not isinstance(options.inputfile, io.IOBase): filein = options.inputfile - if options.outputfile and not isinstance(options.outputfile, file): + if options.outputfile and not isinstance(options.outputfile, io.IOBase): fileout = options.outputfile elif fileout != filedefault: fileout = '%s.py' % (path.splitext(filein)[0]) configs = options.configs - if options.configdirs and not isinstance(filein, file): + if options.configdirs and not isinstance(filein, io.IOBase): for configdir in options.configdirs: dirname = configFromDir(filein, configdir) if path.exists(dirname): @@ -110,15 +111,15 @@ def runTransform(options): source = open(filein).read() else: source = sys.stdin.read() - except (IOError, ), exc: + except (IOError, ) as exc: code, msg = exc.args[0:2] - print 'IOError: %s.' % (msg, ) + print(f'IOError: {msg}.') return code timed['comp'] try: tree = buildAST(source) - except (Exception, ), exc: + except (Exception, ) as exc: exception('exception while parsing') return 1 timed['comp_finish'] @@ -164,7 +165,7 @@ def runTransform(options): if not options.skipcompile: try: compile(source, '', 'exec') - except (SyntaxError, ), ex: + except (SyntaxError, ) as ex: warning('Generated source has invalid syntax. %s', ex) else: info('Generated source has valid syntax.') From 148512e7b1657b0e03a00f3377447f1d266957d7 Mon Sep 17 00:00:00 2001 From: changeling Date: Thu, 16 Jul 2020 14:13:09 -0500 Subject: [PATCH 3/4] Update Python 2 `print` statements to Python 3 `print()`, including `>>` to `file=` syntax. Update Python 2 `print` statements to Python 3 `print()`, including `>>` to `file=` syntax. Python 2 `print >> fd, str` syntax is now `print(str, file=fd)` in Python 3. Implemented f-strings where relevant for these. --- bin/j2py | 10 +++++----- java2python/lang/base.py | 6 +++--- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/bin/j2py b/bin/j2py index b7c6e0d..d3ffad9 100755 --- a/bin/j2py +++ b/bin/j2py @@ -143,16 +143,16 @@ def runTransform(options): if options.lexertokens: for idx, tok in enumerate(tree.parser.input.tokens): - print >> sys.stderr, '{0} {1}'.format(idx, tok) - print >> sys.stderr + print(f'{idx} {tok}', file=sys.stderr) + print(file=sys.stderr) if options.javaast: tree.dump(sys.stderr) - print >> sys.stderr + print(file=sys.stderr) if options.pytree: module.dumpRepr(sys.stderr) - print >> sys.stderr + print(file=sys.stderr) if not options.skipsource: if fileout == filedefault: @@ -160,7 +160,7 @@ def runTransform(options): else: output = open(fileout, 'w') module.name = path.splitext(filein)[0] if filein != '-' else '' - print >> output, source + print(source, file=output) if not options.skipcompile: try: diff --git a/java2python/lang/base.py b/java2python/lang/base.py index 2717b92..15ee93f 100644 --- a/java2python/lang/base.py +++ b/java2python/lang/base.py @@ -180,13 +180,13 @@ def innerDump(root, offset): args[2] = ' ' + self.colorText(ttyp, token.text) for com in self.selectComments(start, seen): for line in self.colorComments(com): - print >> fd, '{0}{1}'.format(indent, line) - print >> fd, nform.format(*args) + print(f'{indent}{line}', file=fd) + print(nform.format(*args), file=fd) for child in root.getChildren(): innerDump(child, offset+1) for com in self.selectComments(root.tokenStopIndex, seen): for line in self.colorComments(com): - print >> fd, '{0}{1}'.format(indent, line) + print(f'{indent}{line}', file=fd) innerDump(self, level) def dumps(self, level=0): From 71076c00d948fa4debdbe60861289b539883dd22 Mon Sep 17 00:00:00 2001 From: changeling Date: Thu, 16 Jul 2020 15:18:16 -0500 Subject: [PATCH 4/4] Update `setup.py` to install from GEGlobalResearch github with Python 3 `antlr-python3-runtime-3.1.3`. `setup.py` has been changed to install from `https://github.com/GEGlobalResearch/java2python/archive/master.zip` and with the `antlr_python_runtime` from `https://github.com/altigee/antlr-python3-runtime-3.1.3/archive/master.zip`. --- setup.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/setup.py b/setup.py index 210d623..4d243b7 100644 --- a/setup.py +++ b/setup.py @@ -46,9 +46,8 @@ def doc_files(): author='Troy Melhase', author_email='troy@troy.io', - url='https://github.com/natural/java2python/', - download_url='https://github.com/downloads/natural/java2python/java2python-0.5.1.tar.gz', - + url='https://github.com/GEGlobalResearch/java2python/', + download_url='https://github.com/GEGlobalResearch/java2python/archive/master.zip', keywords=['java', 'java2python', 'compiler'], classifiers=filter(None, classifiers.split('\n')), @@ -81,6 +80,7 @@ def doc_files(): ('doc', doc_files()), ], - install_requires=['antlr_python_runtime==3.1.3'], - + install_requires=[ + 'antlr_python_runtime@https://github.com/altigee/antlr-python3-runtime-3.1.3/archive/master.zip', + ], )