Skip to content

Commit b9c944a

Browse files
committed
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}.')`
1 parent ecc58f3 commit b9c944a

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

bin/j2py

Lines changed: 12 additions & 11 deletions
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 io
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,16 +61,16 @@ 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 isinstance(infile, io.IOBase) and path.isdir(infile):
65+
if outfile and not isinstance(outfile, io.IOBase) 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):
6869
for name in [name for name in files if name.endswith('.java')]:
6970
fullname = path.join(dirname, name)
7071
options.inputfile = fullname
7172
info('opening %s', fullname)
72-
if outfile and outfile != '-' and not isinstance(outfile, file):
73+
if outfile and outfile != '-' and not isinstance(outfile, io.IOBase):
7374
full = path.abspath(path.join(outfile, fullname))
7475
head, tail = path.split(full)
7576
tail = path.splitext(tail)[0] + '.py'
@@ -89,15 +90,15 @@ 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 isinstance(options.inputfile, io.IOBase):
9394
filein = options.inputfile
94-
if options.outputfile and not isinstance(options.outputfile, file):
95+
if options.outputfile and not isinstance(options.outputfile, io.IOBase):
9596
fileout = options.outputfile
9697
elif fileout != filedefault:
9798
fileout = '%s.py' % (path.splitext(filein)[0])
9899

99100
configs = options.configs
100-
if options.configdirs and not isinstance(filein, file):
101+
if options.configdirs and not isinstance(filein, io.IOBase):
101102
for configdir in options.configdirs:
102103
dirname = configFromDir(filein, configdir)
103104
if path.exists(dirname):
@@ -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(f'IOError: {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.')

0 commit comments

Comments
 (0)