diff options
author | Dmitry Shachnev <mitya57@gmail.com> | 2014-01-19 14:17:10 +0400 |
---|---|---|
committer | Dmitry Shachnev <mitya57@gmail.com> | 2014-01-19 14:17:10 +0400 |
commit | 344417db950d6e816ab2efb21737c2bdf9d1ad53 (patch) | |
tree | 97b00b55be0e28a73399acc0f80e21e6a4e24b28 /utils | |
parent | 59b355edaa4f17aff910bf2371526d5ce46fb648 (diff) | |
download | sphinx-344417db950d6e816ab2efb21737c2bdf9d1ad53.tar.gz |
Modernize the code now that Python 2.5 is no longer supported
- Use print function instead of print statement;
- Use new exception handling;
- Use in operator instead of has_key();
- Do not use tuple arguments in functions;
- Other miscellaneous improvements.
This is based on output of `futurize --stage1`, with some
manual corrections.
Diffstat (limited to 'utils')
-rwxr-xr-x | utils/check_sources.py | 25 | ||||
-rwxr-xr-x | utils/reindent.py | 29 |
2 files changed, 28 insertions, 26 deletions
diff --git a/utils/check_sources.py b/utils/check_sources.py index 5512420a..30f6329d 100755 --- a/utils/check_sources.py +++ b/utils/check_sources.py @@ -10,6 +10,7 @@ :copyright: Copyright 2007-2013 by the Sphinx team, see AUTHORS. :license: BSD, see LICENSE for details. """ +from __future__ import print_function import sys, os, re import cStringIO @@ -54,7 +55,7 @@ if sys.version_info < (3, 0): def check_syntax(fn, lines): try: compile(b('').join(lines), fn, "exec") - except SyntaxError, err: + except SyntaxError as err: yield 0, "not compilable: %s" % err @@ -77,9 +78,9 @@ def check_style_and_encoding(fn, lines): yield lno+1, 'using == None/True/False' try: line.decode(encoding) - except UnicodeDecodeError, err: + except UnicodeDecodeError as err: yield lno+1, "not decodable: %s\n Line: %r" % (err, line) - except LookupError, err: + except LookupError as err: yield 0, "unknown encoding: %s" % encoding encoding = 'latin1' @@ -183,7 +184,7 @@ def main(argv): elif len(args) == 1: path = args[0] else: - print args + print(args) parser.error('No more then one path supported') verbose = options.verbose @@ -214,7 +215,7 @@ def main(argv): continue if verbose: - print "Checking %s..." % fn + print("Checking %s..." % fn) try: f = open(fn, 'rb') @@ -222,8 +223,8 @@ def main(argv): lines = list(f) finally: f.close() - except (IOError, OSError), err: - print "%s: cannot open: %s" % (fn, err) + except (IOError, OSError) as err: + print("%s: cannot open: %s" % (fn, err)) num += 1 continue @@ -231,15 +232,15 @@ def main(argv): if not in_check_pkg and checker.only_pkg: continue for lno, msg in checker(fn, lines): - print >>out, "%s:%d: %s" % (fn, lno, msg) + print("%s:%d: %s" % (fn, lno, msg), file=out) num += 1 if verbose: - print + print() if num == 0: - print "No errors found." + print("No errors found.") else: - print out.getvalue().rstrip('\n') - print "%d error%s found." % (num, num > 1 and "s" or "") + print(out.getvalue().rstrip('\n')) + print("%d error%s found." % (num, num > 1 and "s" or "")) return int(num > 0) diff --git a/utils/reindent.py b/utils/reindent.py index 59828fd8..ef53fe8c 100755 --- a/utils/reindent.py +++ b/utils/reindent.py @@ -38,6 +38,7 @@ file is generated with shutil.copy(), but some corner cases regarding user/group and permissions could leave the backup file more readable that you'd prefer. You can always use the --nobackup option to prevent this. """ +from __future__ import print_function __version__ = "1" @@ -59,8 +60,8 @@ makebackup = True def usage(msg=None): if msg is not None: - print >> sys.stderr, msg - print >> sys.stderr, __doc__ + print(msg, file=sys.stderr) + print(__doc__, file=sys.stderr) def errprint(*args): sep = "" @@ -75,7 +76,7 @@ def main(): try: opts, args = getopt.getopt(sys.argv[1:], "drnvh", ["dryrun", "recurse", "nobackup", "verbose", "help"]) - except getopt.error, msg: + except getopt.error as msg: usage(msg) return for o, a in opts: @@ -101,7 +102,7 @@ def main(): def check(file): if os.path.isdir(file) and not os.path.islink(file): if verbose: - print "listing directory", file + print("listing directory", file) names = os.listdir(file) for name in names: fullname = os.path.join(file, name) @@ -113,10 +114,10 @@ def check(file): return if verbose: - print "checking", file, "...", + print("checking", file, "...", end=' ') try: f = open(file) - except IOError, msg: + except IOError as msg: errprint("%s: I/O Error: %s" % (file, str(msg))) return @@ -124,24 +125,24 @@ def check(file): f.close() if r.run(): if verbose: - print "changed." + print("changed.") if dryrun: - print "But this is a dry run, so leaving it alone." + print("But this is a dry run, so leaving it alone.") if not dryrun: bak = file + ".bak" if makebackup: shutil.copyfile(file, bak) if verbose: - print "backed up", file, "to", bak + print("backed up", file, "to", bak) f = open(file, "w") r.write(f) f.close() if verbose: - print "wrote new", file + print("wrote new", file) return True else: if verbose: - print "unchanged." + print("unchanged.") return False def _rstrip(line, JUNK='\n \t'): @@ -262,7 +263,7 @@ class Reindenter: return line # Line-eater for tokenize. - def tokeneater(self, type, token, (sline, scol), end, line, + def tokeneater(self, type, token, position, end, line, INDENT=tokenize.INDENT, DEDENT=tokenize.DEDENT, NEWLINE=tokenize.NEWLINE, @@ -285,7 +286,7 @@ class Reindenter: elif type == COMMENT: if self.find_stmt: - self.stats.append((sline, -1)) + self.stats.append((position[0], -1)) # but we're still looking for a new stmt, so leave # find_stmt alone @@ -298,7 +299,7 @@ class Reindenter: # ENDMARKER. self.find_stmt = 0 if line: # not endmarker - self.stats.append((sline, self.level)) + self.stats.append((position[0], self.level)) # Count number of leading blanks. def getlspace(line): |