From 0e81247efb5212c87dd6f2914e093f5a126744fb Mon Sep 17 00:00:00 2001 From: Suleyman Poyraz Date: Wed, 30 Aug 2017 20:29:18 +0000 Subject: [PATCH 1/3] add ignore file --- .gitignore | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c8f31cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,100 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +env/ +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +*.egg-info/ +.installed.cfg +*.egg + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*,cover +.hypothesis/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +target/ + +# IPython Notebook +.ipynb_checkpoints + +# pyenv +.python-version + +# celery beat schedule file +celerybeat-schedule + +# dotenv +.env + +# virtualenv +venv/ +ENV/ + +# Spyder project settings +.spyderproject + +# Rope project settings +.ropeproject + +#2to3 files +*.bak + +#patch outputs +*.orig +*.rej + +#vi & vim swaps +*.swp +*.swo From ccda0ae6fe6f94b4b65568a612ca52aac69a6434 Mon Sep 17 00:00:00 2001 From: Suleyman Poyraz Date: Wed, 30 Aug 2017 20:29:31 +0000 Subject: [PATCH 2/3] Added python3 support --- Makefile.py | 6 +++--- lib/preprocess.py | 22 +++++++++++----------- setup.py | 4 ++-- test/inputs/define_types.py | 16 ++++++++-------- test/inputs/define_undef.py | 18 +++++++++--------- test/inputs/defined.py | 6 +++--- test/inputs/elif.py | 12 ++++++------ test/inputs/else.py | 6 +++--- test/inputs/else_expr.py | 6 +++--- test/inputs/empty.py | 2 +- test/inputs/error.py | 4 ++-- test/inputs/exc1.py | 2 +- test/inputs/exc2.py | 4 ++-- test/inputs/exc3.py | 2 +- test/inputs/exc4.py | 6 +++--- test/inputs/exc5.py | 2 +- test/inputs/file_and_line.py | 10 +++++----- test/inputs/if.py | 4 ++-- test/inputs/ifdef.py | 14 +++++++------- test/inputs/ifndef.py | 14 +++++++------- test/inputs/ignore_error.py | 4 ++-- test/inputs/keep_lines_bugs.py | 6 +++--- test/inputs/nested.py | 10 +++++----- test/inputs/nested_bug1.py | 10 +++++----- test/inputs/subst_bug2.py | 2 +- test/inputs/substitution.py | 2 +- test/inputs/undefined.py | 4 ++-- test/outputs/define_types.py | 16 ++++++++-------- test/outputs/define_undef.py | 6 +++--- test/outputs/defined.py | 4 ++-- test/outputs/elif.py | 4 ++-- test/outputs/else.py | 4 ++-- test/outputs/else_expr.py | 6 +++--- test/outputs/empty.py | 2 +- test/outputs/file_and_line.py | 10 +++++----- test/outputs/if.py | 4 ++-- test/outputs/ifdef.py | 8 ++++---- test/outputs/ifndef.py | 8 ++++---- test/outputs/ignore_error.py | 4 ++-- test/outputs/keep_lines_bugs.py | 6 +++--- test/outputs/nested.py | 8 ++++---- test/outputs/nested_bug1.py | 4 ++-- test/outputs/subst_bug2.py | 2 +- test/outputs/substitution.py | 2 +- test/test_preprocess.py | 2 +- test/test_preprocess_inputs.py | 24 ++++++++++++------------ test/testlib.py | 26 +++++++++++++------------- test/testsupport.py | 4 ++-- 48 files changed, 176 insertions(+), 176 deletions(-) diff --git a/Makefile.py b/Makefile.py index 3d1e27f..a53ff18 100644 --- a/Makefile.py +++ b/Makefile.py @@ -60,7 +60,7 @@ def make(self): # Don't (yet) support Python 3. continue ver_str = "%s.%s" % ver - print "-- test with Python %s (%s)" % (ver_str, python) + print("-- test with Python %s (%s)" % (ver_str, python)) assert ' ' not in python sh.run_in_dir("%s test.py" % python, join(self.dir, "test")) @@ -220,7 +220,7 @@ def make(self): path = join(self.dir, "TO""DO.txt") todos = re.compile("^- ", re.M).findall(open(path, 'r').read()) - print "(plus %d TODOs from TO""DO.txt)" % len(todos) + print("(plus %d TODOs from TO""DO.txt)" % len(todos)) def _dump_pattern_in_path(self, pattern, path): os.system("grep -nH '%s' '%s'" % (pattern, path)) @@ -372,7 +372,7 @@ def _paths_from_path_patterns(path_patterns, files=True, dirs="never", from os.path import basename, exists, isdir, join from glob import glob - assert not isinstance(path_patterns, basestring), \ + assert not isinstance(path_patterns, str), \ "'path_patterns' must be a sequence, not a string: %r" % path_patterns GLOB_CHARS = '*?[' diff --git a/lib/preprocess.py b/lib/preprocess.py index 59286ea..0f3d736 100755 --- a/lib/preprocess.py +++ b/lib/preprocess.py @@ -193,14 +193,14 @@ def __str__(self): #---- internal logging facility class _Logger: - DEBUG, INFO, WARN, ERROR, CRITICAL = range(5) + DEBUG, INFO, WARN, ERROR, CRITICAL = list(range(5)) def __init__(self, name, level=None, streamOrFileName=sys.stderr): self._name = name if level is None: self.level = self.WARN else: self.level = level - if type(streamOrFileName) == types.StringType: + if type(streamOrFileName) == bytes: self.stream = open(streamOrFileName, 'w') self._opennedStream = 1 else: @@ -261,7 +261,7 @@ def _evaluate(expr, defines): #interpolated = _interpolate(s, defines) try: rv = eval(expr, {'defined':lambda v: v in defines}, defines) - except Exception, ex: + except Exception as ex: msg = str(ex) if msg.startswith("name '") and msg.endswith("' is not defined"): # A common error (at least this is presumed:) is to have @@ -382,16 +382,16 @@ def preprocess(infile, outfile=sys.stdout, defines={}, fin = open(infile, 'r') lines = fin.readlines() fin.close() - if type(outfile) in types.StringTypes: + if type(outfile) in str: if force and os.path.exists(outfile): - os.chmod(outfile, 0777) + os.chmod(outfile, 0o777) os.remove(outfile) fout = open(outfile, 'w') else: fout = outfile defines['__FILE__'] = infile - SKIP, EMIT = range(2) # states + SKIP, EMIT = list(range(2)) # states states = [(EMIT, # a state is (, 0, # , 0)] # ) @@ -702,7 +702,7 @@ def getContentType(self, path): basename = os.path.basename(path) contentType = None # Try to determine from the path. - if not contentType and self.filenameMap.has_key(basename): + if not contentType and basename in self.filenameMap: contentType = self.filenameMap[basename] log.debug("Content type of '%s' is '%s' (determined from full "\ "path).", path, contentType) @@ -712,13 +712,13 @@ def getContentType(self, path): if sys.platform.startswith("win"): # Suffix patterns are case-insensitive on Windows. suffix = suffix.lower() - if self.suffixMap.has_key(suffix): + if suffix in self.suffixMap: contentType = self.suffixMap[suffix] log.debug("Content type of '%s' is '%s' (determined from "\ "suffix '%s').", path, contentType, suffix) # Try to determine from the registered set of regex patterns. if not contentType: - for regex, ctype in self.regexMap.items(): + for regex, ctype in list(self.regexMap.items()): if regex.search(basename): contentType = ctype log.debug("Content type of '%s' is '%s' (matches regex '%s')", @@ -771,7 +771,7 @@ def main(argv): optlist, args = getopt.getopt(argv[1:], 'hVvo:D:fkI:sc:', ['help', 'version', 'verbose', 'force', 'keep-lines', 'substitute', 'content-types-path=']) - except getopt.GetoptError, msg: + except getopt.GetoptError as msg: sys.stderr.write("preprocess: error: %s. Your invocation was: %s\n"\ % (msg, argv)) sys.stderr.write("See 'preprocess --help'.\n") @@ -826,7 +826,7 @@ def main(argv): contentTypesRegistry = ContentTypesRegistry(contentTypesPaths) preprocess(infile, outfile, defines, force, keepLines, includePath, substitute, contentTypesRegistry=contentTypesRegistry) - except PreprocessError, ex: + except PreprocessError as ex: if log.isDebugEnabled(): import traceback traceback.print_exc(file=sys.stderr) diff --git a/setup.py b/setup.py index ee66233..e2d66f9 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ # Distutils before Python 2.3 doesn't accept classifiers. _setup = setup def setup(**kwargs): - if kwargs.has_key("classifiers"): + if "classifiers" in kwargs: del kwargs["classifiers"] _setup(**kwargs) @@ -62,7 +62,7 @@ def setup(**kwargs): package_dir={"": "lib"}, scripts=[script], description=doclines[0], - classifiers=filter(None, classifiers.split("\n")), + classifiers=[_f for _f in classifiers.split("\n") if _f], long_description="\n".join(doclines[2:]), ) diff --git a/test/inputs/define_types.py b/test/inputs/define_types.py index 258af51..0c899da 100644 --- a/test/inputs/define_types.py +++ b/test/inputs/define_types.py @@ -10,26 +10,26 @@ # #define FOO_DICT {'a':1, 'b':2} # #if FOO_NONE is None -print "FOO_NONE" +print("FOO_NONE") # #endif # #if FOO_INT == 42 -print "FOO_INT" +print("FOO_INT") # #endif # #if FOO_FLOAT == 3.14 -print "FOO_FLOAT" +print("FOO_FLOAT") # #endif # #if FOO_STRING1 == "foo" -print "FOO_STRING1" +print("FOO_STRING1") # #endif # #if FOO_STRING2 == "foo" -print "FOO_STRING2" +print("FOO_STRING2") # #endif # #if FOO_LIST == [1, 'a', 2.5] -print "FOO_LIST" +print("FOO_LIST") # #endif # #if FOO_TUPLE == (1, 'a', 2.5) -print "FOO_TUPLE" +print("FOO_TUPLE") # #endif # #if FOO_DICT == {'a':1, 'b':2} -print "FOO_DICT" +print("FOO_DICT") # #endif diff --git a/test/inputs/define_undef.py b/test/inputs/define_undef.py index e68def6..85af883 100644 --- a/test/inputs/define_undef.py +++ b/test/inputs/define_undef.py @@ -6,34 +6,34 @@ if __name__ == '__main__': # #if FOO_A - print "a" + print("a") # #endif # #if FOO_B - print "b" + print("b") # #endif # #if defined("FOO_B") - print "b defined" + print("b defined") # #endif # #if FOO_C - print "c" + print("c") # #endif # #if defined("FOO_D") - print "d defined" + print("d defined") # #endif # #undef FOO_B # #undef FOO_D # #if FOO_A - print "a" + print("a") # #endif # #if defined("FOO_B") - print "b defined" + print("b defined") # #endif # #if FOO_C - print "c" + print("c") # #endif # #if defined("FOO_D") - print "d defined" + print("d defined") # #endif diff --git a/test/inputs/defined.py b/test/inputs/defined.py index 5b7f505..f791344 100644 --- a/test/inputs/defined.py +++ b/test/inputs/defined.py @@ -2,9 +2,9 @@ if __name__ == '__main__': # #if defined('ASDF') - print "ASDF defined" + print("ASDF defined") # #else - print "ASDF not defined" + print("ASDF not defined") # #endif - print "bye" + print("bye") diff --git a/test/inputs/elif.py b/test/inputs/elif.py index 1da4aaf..b4e0f85 100644 --- a/test/inputs/elif.py +++ b/test/inputs/elif.py @@ -5,15 +5,15 @@ # #define UN 1 if __name__ == '__main__': # #if ZERO - print "a" + print("a") # #elif ZERO - print "b" + print("b") # #elif ONE - print "c" + print("c") # #elif ONE - print "d" + print("d") # #else - print "e" + print("e") # #endif - print "bye" + print("bye") diff --git a/test/inputs/else.py b/test/inputs/else.py index b87fcfd..fd8e8f3 100644 --- a/test/inputs/else.py +++ b/test/inputs/else.py @@ -3,9 +3,9 @@ # #define ZERO 0 if __name__ == '__main__': # #if ZERO - print "zero" + print("zero") # #else - print "not zero" + print("not zero") # #endif - print "bye" + print("bye") diff --git a/test/inputs/else_expr.py b/test/inputs/else_expr.py index 1da2f64..37a7c8e 100644 --- a/test/inputs/else_expr.py +++ b/test/inputs/else_expr.py @@ -5,9 +5,9 @@ # #define UN 1 if __name__ == '__main__': # #if ONE - print "hi" + print("hi") # #else ZERO - print "hello" + print("hello") # #endif - print "bye" + print("bye") diff --git a/test/inputs/empty.py b/test/inputs/empty.py index 897e2b6..0d18d48 100644 --- a/test/inputs/empty.py +++ b/test/inputs/empty.py @@ -3,5 +3,5 @@ """This is a Python file with NO preprocessor stmts in it.""" if __name__ == '__main__': - print "hi" + print("hi") diff --git a/test/inputs/error.py b/test/inputs/error.py index 66848f7..e7a92f8 100644 --- a/test/inputs/error.py +++ b/test/inputs/error.py @@ -5,8 +5,8 @@ # #define UN 1 if __name__ == '__main__': # #if ONE - print "hi" + print("hi") # #error This is an error string. # #endif - print "bye" + print("bye") diff --git a/test/inputs/exc1.py b/test/inputs/exc1.py index ef62ab3..f0c57b4 100644 --- a/test/inputs/exc1.py +++ b/test/inputs/exc1.py @@ -1,2 +1,2 @@ # #if 1 -print "1" +print("1") diff --git a/test/inputs/exc2.py b/test/inputs/exc2.py index b24c6ed..f6d47f0 100644 --- a/test/inputs/exc2.py +++ b/test/inputs/exc2.py @@ -1,6 +1,6 @@ # #if 0 -print "0" +print("0") # #endif # #endif -print "bye" +print("bye") diff --git a/test/inputs/exc3.py b/test/inputs/exc3.py index 92d4b39..a32f67b 100644 --- a/test/inputs/exc3.py +++ b/test/inputs/exc3.py @@ -1,4 +1,4 @@ # #if 0 -print "0" +print("0") # #endif # #endif diff --git a/test/inputs/exc4.py b/test/inputs/exc4.py index ff2ddf1..d5e77b0 100644 --- a/test/inputs/exc4.py +++ b/test/inputs/exc4.py @@ -1,8 +1,8 @@ # #if 1 -print "1" +print("1") # #else -print "not 1" +print("not 1") # #elif 0 -print "0" +print("0") # #endif diff --git a/test/inputs/exc5.py b/test/inputs/exc5.py index 3c61656..1bef649 100644 --- a/test/inputs/exc5.py +++ b/test/inputs/exc5.py @@ -1,4 +1,4 @@ # #if defined(FOO) -print "foo" +print("foo") # #endif diff --git a/test/inputs/file_and_line.py b/test/inputs/file_and_line.py index 2234e02..3808869 100644 --- a/test/inputs/file_and_line.py +++ b/test/inputs/file_and_line.py @@ -1,20 +1,20 @@ # #if defined('__FILE__') -print "__FILE__ is defined" +print("__FILE__ is defined") # #endif # #if __FILE__.endswith("file_and_line.py") -print "__FILE__.endswith('file_and_line.py')" +print("__FILE__.endswith('file_and_line.py')") # #endif # #if defined('__LINE__') -print "__LINE__ is defined" +print("__LINE__ is defined") # #endif # #if __LINE__ == 14 -print "__LINE__ is 14" +print("__LINE__ is 14") # #endif # #if __LINE__ == 18 -print "__LINE__ is 18" +print("__LINE__ is 18") # #endif diff --git a/test/inputs/if.py b/test/inputs/if.py index 54add0e..ebe9c47 100644 --- a/test/inputs/if.py +++ b/test/inputs/if.py @@ -5,7 +5,7 @@ # #define UN 1 if __name__ == '__main__': # #if ONE - print "hi" + print("hi") # #endif - print "bye" + print("bye") diff --git a/test/inputs/ifdef.py b/test/inputs/ifdef.py index 90ff5b7..05fe415 100644 --- a/test/inputs/ifdef.py +++ b/test/inputs/ifdef.py @@ -5,19 +5,19 @@ # #define UN 1 if __name__ == '__main__': # #ifdef ONE - print "ONE defined" + print("ONE defined") # #else - print "ONE not defined" + print("ONE not defined") # #endif # #ifdef ZERO - print "ZERO defined" + print("ZERO defined") # #else - print "ZERO not defined" + print("ZERO not defined") # #endif # #ifdef FOO - print "FOO defined" + print("FOO defined") # #else - print "FOO not defined" + print("FOO not defined") # #endif - print "bye" + print("bye") diff --git a/test/inputs/ifndef.py b/test/inputs/ifndef.py index c267ab5..c5fe270 100644 --- a/test/inputs/ifndef.py +++ b/test/inputs/ifndef.py @@ -5,19 +5,19 @@ # #define UN 1 if __name__ == '__main__': # #ifndef ONE - print "ONE not defined" + print("ONE not defined") # #else - print "ONE defined" + print("ONE defined") # #endif # #ifndef ZERO - print "ZERO not defined" + print("ZERO not defined") # #else - print "ZERO defined" + print("ZERO defined") # #endif # #ifndef FOO - print "FOO not defined" + print("FOO not defined") # #else - print "FOO defined" + print("FOO defined") # #endif - print "bye" + print("bye") diff --git a/test/inputs/ignore_error.py b/test/inputs/ignore_error.py index 31ab4e4..3ac2645 100644 --- a/test/inputs/ignore_error.py +++ b/test/inputs/ignore_error.py @@ -1,6 +1,6 @@ #!python -print "hi" +print("hi") # #if 0 # The following should not cause an error, but did up until @@ -8,5 +8,5 @@ # #error womba womba womba # #endif -print "bye" +print("bye") diff --git a/test/inputs/keep_lines_bugs.py b/test/inputs/keep_lines_bugs.py index c573a39..7a7bfe4 100644 --- a/test/inputs/keep_lines_bugs.py +++ b/test/inputs/keep_lines_bugs.py @@ -1,7 +1,7 @@ # Test a bug where --keep-lines wasn't obeyed for directives inside # skipped #if-blocks. -print "this is line 4" +print("this is line 4") # #ifdef UNDEFINED # #error "UNDEFINED is defined!" @@ -9,8 +9,8 @@ # #endif -print "this is line 12" +print("this is line 12") # #define FOO 2 -print "this is line 15" +print("this is line 15") diff --git a/test/inputs/nested.py b/test/inputs/nested.py index ae0ada2..d1d97a1 100644 --- a/test/inputs/nested.py +++ b/test/inputs/nested.py @@ -5,14 +5,14 @@ # #define UN 1 if __name__ == '__main__': # #if ONE - print "one" + print("one") # #if UN - print "un" + print("un") # #endif - print "still, just one" + print("still, just one") # #if ZERO - print "zero" + print("zero") # #endif # #endif - print "bye" + print("bye") diff --git a/test/inputs/nested_bug1.py b/test/inputs/nested_bug1.py index eac0150..a21e1b3 100644 --- a/test/inputs/nested_bug1.py +++ b/test/inputs/nested_bug1.py @@ -1,13 +1,13 @@ #!python if __name__ == '__main__': - print "hi" + print("hi") # #if 0 - print "should not be emitted 1" + print("should not be emitted 1") # #if 1 - print "should not be emitted 2" + print("should not be emitted 2") # #endif - print "should not be emitted 3" + print("should not be emitted 3") # #endif - print "bye" + print("bye") diff --git a/test/inputs/subst_bug2.py b/test/inputs/subst_bug2.py index 8adc339..b5be3c6 100644 --- a/test/inputs/subst_bug2.py +++ b/test/inputs/subst_bug2.py @@ -9,5 +9,5 @@ if __name__ == '__main__': foo = 1 # The V-A-R should _not_ be substituted in the string. - print "VAR is", VAR + print("VAR is", VAR) diff --git a/test/inputs/substitution.py b/test/inputs/substitution.py index acc0b44..8ffcad3 100644 --- a/test/inputs/substitution.py +++ b/test/inputs/substitution.py @@ -4,5 +4,5 @@ if __name__ == '__main__': foo = 1 - print VAR + print(VAR) diff --git a/test/inputs/undefined.py b/test/inputs/undefined.py index bec6e49..1979745 100644 --- a/test/inputs/undefined.py +++ b/test/inputs/undefined.py @@ -2,7 +2,7 @@ if __name__ == '__main__': # #if FOO - print "foo is not defined" + print("foo is not defined") # #endif - print "bye" + print("bye") diff --git a/test/outputs/define_types.py b/test/outputs/define_types.py index a84d4c4..e60f900 100644 --- a/test/outputs/define_types.py +++ b/test/outputs/define_types.py @@ -1,11 +1,11 @@ #!python -print "FOO_NONE" -print "FOO_INT" -print "FOO_FLOAT" -print "FOO_STRING1" -print "FOO_STRING2" -print "FOO_LIST" -print "FOO_TUPLE" -print "FOO_DICT" +print("FOO_NONE") +print("FOO_INT") +print("FOO_FLOAT") +print("FOO_STRING1") +print("FOO_STRING2") +print("FOO_LIST") +print("FOO_TUPLE") +print("FOO_DICT") diff --git a/test/outputs/define_undef.py b/test/outputs/define_undef.py index 7a69042..da15da3 100644 --- a/test/outputs/define_undef.py +++ b/test/outputs/define_undef.py @@ -2,9 +2,9 @@ if __name__ == '__main__': - print "b defined" - print "c" + print("b defined") + print("c") - print "c" + print("c") diff --git a/test/outputs/defined.py b/test/outputs/defined.py index 88adfb0..1dddfae 100644 --- a/test/outputs/defined.py +++ b/test/outputs/defined.py @@ -1,6 +1,6 @@ #!python if __name__ == '__main__': - print "ASDF not defined" - print "bye" + print("ASDF not defined") + print("bye") diff --git a/test/outputs/elif.py b/test/outputs/elif.py index 5e18fe9..8126f12 100644 --- a/test/outputs/elif.py +++ b/test/outputs/elif.py @@ -1,6 +1,6 @@ #!python if __name__ == '__main__': - print "c" - print "bye" + print("c") + print("bye") diff --git a/test/outputs/else.py b/test/outputs/else.py index 5a2cef8..7bb6f5d 100644 --- a/test/outputs/else.py +++ b/test/outputs/else.py @@ -1,6 +1,6 @@ #!python if __name__ == '__main__': - print "not zero" - print "bye" + print("not zero") + print("bye") diff --git a/test/outputs/else_expr.py b/test/outputs/else_expr.py index 72c8a6e..cdc8487 100644 --- a/test/outputs/else_expr.py +++ b/test/outputs/else_expr.py @@ -1,8 +1,8 @@ #!python if __name__ == '__main__': - print "hi" + print("hi") # #else ZERO - print "hello" - print "bye" + print("hello") + print("bye") diff --git a/test/outputs/empty.py b/test/outputs/empty.py index 897e2b6..0d18d48 100644 --- a/test/outputs/empty.py +++ b/test/outputs/empty.py @@ -3,5 +3,5 @@ """This is a Python file with NO preprocessor stmts in it.""" if __name__ == '__main__': - print "hi" + print("hi") diff --git a/test/outputs/file_and_line.py b/test/outputs/file_and_line.py index 071c18a..1880d22 100644 --- a/test/outputs/file_and_line.py +++ b/test/outputs/file_and_line.py @@ -1,10 +1,10 @@ -print "__FILE__ is defined" +print("__FILE__ is defined") -print "__FILE__.endswith('file_and_line.py')" +print("__FILE__.endswith('file_and_line.py')") -print "__LINE__ is defined" +print("__LINE__ is defined") -print "__LINE__ is 14" +print("__LINE__ is 14") -print "__LINE__ is 18" +print("__LINE__ is 18") diff --git a/test/outputs/if.py b/test/outputs/if.py index facb86a..d679037 100644 --- a/test/outputs/if.py +++ b/test/outputs/if.py @@ -1,6 +1,6 @@ #!python if __name__ == '__main__': - print "hi" - print "bye" + print("hi") + print("bye") diff --git a/test/outputs/ifdef.py b/test/outputs/ifdef.py index 3452adb..46239e8 100644 --- a/test/outputs/ifdef.py +++ b/test/outputs/ifdef.py @@ -1,8 +1,8 @@ #!python if __name__ == '__main__': - print "ONE defined" - print "ZERO defined" - print "FOO not defined" - print "bye" + print("ONE defined") + print("ZERO defined") + print("FOO not defined") + print("bye") diff --git a/test/outputs/ifndef.py b/test/outputs/ifndef.py index 3452adb..46239e8 100644 --- a/test/outputs/ifndef.py +++ b/test/outputs/ifndef.py @@ -1,8 +1,8 @@ #!python if __name__ == '__main__': - print "ONE defined" - print "ZERO defined" - print "FOO not defined" - print "bye" + print("ONE defined") + print("ZERO defined") + print("FOO not defined") + print("bye") diff --git a/test/outputs/ignore_error.py b/test/outputs/ignore_error.py index 54f87f2..1fb1160 100644 --- a/test/outputs/ignore_error.py +++ b/test/outputs/ignore_error.py @@ -1,5 +1,5 @@ #!python -print "hi" -print "bye" +print("hi") +print("bye") diff --git a/test/outputs/keep_lines_bugs.py b/test/outputs/keep_lines_bugs.py index 898173b..491b5a1 100644 --- a/test/outputs/keep_lines_bugs.py +++ b/test/outputs/keep_lines_bugs.py @@ -1,7 +1,7 @@ # Test a bug where --keep-lines wasn't obeyed for directives inside # skipped #if-blocks. -print "this is line 4" +print("this is line 4") @@ -9,8 +9,8 @@ -print "this is line 12" +print("this is line 12") -print "this is line 15" +print("this is line 15") diff --git a/test/outputs/nested.py b/test/outputs/nested.py index 828787e..4d44741 100644 --- a/test/outputs/nested.py +++ b/test/outputs/nested.py @@ -1,8 +1,8 @@ #!python if __name__ == '__main__': - print "one" - print "un" - print "still, just one" - print "bye" + print("one") + print("un") + print("still, just one") + print("bye") diff --git a/test/outputs/nested_bug1.py b/test/outputs/nested_bug1.py index facb86a..d679037 100644 --- a/test/outputs/nested_bug1.py +++ b/test/outputs/nested_bug1.py @@ -1,6 +1,6 @@ #!python if __name__ == '__main__': - print "hi" - print "bye" + print("hi") + print("bye") diff --git a/test/outputs/subst_bug2.py b/test/outputs/subst_bug2.py index d6381d0..9060500 100644 --- a/test/outputs/subst_bug2.py +++ b/test/outputs/subst_bug2.py @@ -8,5 +8,5 @@ if __name__ == '__main__': foo = 1 # The V-A-R should _not_ be substituted in the string. - print "VAR is", foo + print("VAR is", foo) diff --git a/test/outputs/substitution.py b/test/outputs/substitution.py index 8d554b5..dee58c1 100644 --- a/test/outputs/substitution.py +++ b/test/outputs/substitution.py @@ -3,5 +3,5 @@ if __name__ == '__main__': foo = 1 - print foo + print(foo) diff --git a/test/test_preprocess.py b/test/test_preprocess.py index 64f4ea9..5de1cba 100644 --- a/test/test_preprocess.py +++ b/test/test_preprocess.py @@ -56,7 +56,7 @@ def test_false_recursive_includes(self): # If this second one works then the bug is fixed. try: preprocess.preprocess(inFile, outFile) - except preprocess.PreprocessError, ex: + except preprocess.PreprocessError as ex: self.fail("Second independant preprocess() call incorrectly "\ "re-used list of already preprocessed files froma "\ "previous call.") diff --git a/test/test_preprocess_inputs.py b/test/test_preprocess_inputs.py index 4641066..866a87a 100644 --- a/test/test_preprocess_inputs.py +++ b/test/test_preprocess_inputs.py @@ -47,20 +47,20 @@ def _testOneInputFile(self, fname): dummy, err, retval = testsupport.run(argv) try: out = open(outfile, 'r').read() - except IOError, ex: + except IOError as ex: self.fail("unexpected error running '%s': '%s' was not generated:\n" "\t%s" % (' '.join(argv), outfile, err)) if DEBUG: - print - print "*"*50, "cmd" - print ' '.join(argv) - print "*"*50, "stdout" - print out - print "*"*50, "stderr" - print err - print "*"*50, "retval" - print str(retval) - print "*" * 50 + print() + print("*"*50, "cmd") + print(' '.join(argv)) + print("*"*50, "stdout") + print(out) + print("*"*50, "stderr") + print(err) + print("*"*50, "retval") + print(str(retval)) + print("*" * 50) # Compare results with the expected. expoutfile = os.path.join('outputs', fname) # expected stdout output @@ -80,7 +80,7 @@ def _testOneInputFile(self, fname): massaged_experr = experr.replace("inputs/", "inputs"+os.sep) diff = list(difflib.ndiff(massaged_experr.strip().splitlines(1), err.strip().splitlines(1))) - self.failUnlessEqual(massaged_experr.strip(), err.strip(), + self.assertEqual(massaged_experr.strip(), err.strip(), " != :\n%s"\ % pprint.pformat(diff)) elif err: diff --git a/test/testlib.py b/test/testlib.py index cc6b0cd..233be76 100644 --- a/test/testlib.py +++ b/test/testlib.py @@ -248,9 +248,9 @@ def testmods_from_testdir(testdir): finally: os.chdir(old_dir) sys.path.remove(testabsdir) - except TestSkipped, ex: + except TestSkipped as ex: log.warn("'%s' module skipped: %s", testmod_name, ex) - except Exception, ex: + except Exception as ex: log.warn("could not import test module '%s': %s (skipping, " "run with '-d' for full traceback)", testmod_path, ex) @@ -289,7 +289,7 @@ class TestListLoader(unittest.TestLoader): continue for testcase in loader.loadTestsFromTestCase(testcase_class): yield testcase - except Exception, ex: + except Exception as ex: testmod_path = testmod.__file__ if testmod_path.endswith(".pyc"): testmod_path = testmod_path[:-1] @@ -326,7 +326,7 @@ def tests_from_manifest(testdir_from_ns): be used to group all test cases from that module into an instance of that TestSuite subclass. This allows for overriding of test running behaviour. """ - for ns, testdir in testdir_from_ns.items(): + for ns, testdir in list(testdir_from_ns.items()): for testmod in testmods_from_testdir(testdir): if hasattr(testmod, "test_suite_class"): testsuite_class = testmod.test_suite_class @@ -442,23 +442,23 @@ def list_tests(testdir_from_ns, tags): if log.isEnabledFor(logging.INFO): # long-form for i, t in enumerate(tests): if i: - print + print() testfile = t.testmod.__file__ if testfile.endswith(".pyc"): testfile = testfile[:-1] - print "%s:" % t.shortname() - print " from: %s#%s.%s" \ - % (testfile, t.testcase.__class__.__name__, t.testfn_name) + print("%s:" % t.shortname()) + print(" from: %s#%s.%s" \ + % (testfile, t.testcase.__class__.__name__, t.testfn_name)) wrapped = textwrap.fill(' '.join(t.tags()), WIDTH-10) - print " tags: %s" % _indent(wrapped, 8, True) + print(" tags: %s" % _indent(wrapped, 8, True)) if t.doc(): - print _indent(t.doc(), width=2) + print(_indent(t.doc(), width=2)) else: for t in tests: line = t.shortname() + ' ' if t.explicit_tags(): line += '[%s]' % ' '.join(t.explicit_tags()) - print line + print(line) #---- text test runner that can handle TestSkipped reasonably @@ -696,13 +696,13 @@ def harness(testdir_from_ns={None: os.curdir}, argv=sys.argv, logging.basicConfig() try: log_level, action, tags = _parse_opts(argv[1:], default_tags or []) - except getopt.error, ex: + except getopt.error as ex: log.error(str(ex) + " (did you need a '--' before a '-TAG' argument?)") return 1 log.setLevel(log_level) if action == "help": - print __doc__ + print(__doc__) return 0 if action == "list": return list_tests(testdir_from_ns, tags) diff --git a/test/testsupport.py b/test/testsupport.py index 23a1a9d..4ed09f5 100644 --- a/test/testsupport.py +++ b/test/testsupport.py @@ -42,7 +42,7 @@ def run(argv): results. Returns (, , ). Note: 'argv' may also just be the command string. """ - if type(argv) in (types.ListType, types.TupleType): + if type(argv) in (list, tuple): cmd = _joinArgv(argv) else: cmd = argv @@ -78,7 +78,7 @@ def run(argv): def _rmtreeOnError(rmFunction, filePath, excInfo): if excInfo[0] == OSError: # presuming because file is read-only - os.chmod(filePath, 0777) + os.chmod(filePath, 0o777) rmFunction(filePath) def rmtree(dirname): From 7f9fbe29cf27f1974ea8eacb46265c3215ca6b03 Mon Sep 17 00:00:00 2001 From: Suleyman Poyraz Date: Tue, 29 Sep 2020 17:12:09 +0300 Subject: [PATCH 3/3] Refactored old garbage code. --- lib/preprocess.py | 245 ++++++++++++++++++++++++++-------------------- 1 file changed, 139 insertions(+), 106 deletions(-) mode change 100755 => 100644 lib/preprocess.py diff --git a/lib/preprocess.py b/lib/preprocess.py old mode 100755 new mode 100644 index 0f3d736..c49c4b8 --- a/lib/preprocess.py +++ b/lib/preprocess.py @@ -1,6 +1,7 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 # Copyright (c) 2002-2008 ActiveState Software Inc. # License: MIT License (http://www.opensource.org/licenses/mit-license.php) +# Adapted for inary with python3 by Zaryob """ Preprocess a file. @@ -47,7 +48,7 @@ of the form: where the are the native comment delimiters for - that file type. + that file type. Examples @@ -58,7 +59,7 @@ ... - + Python (*.py), Perl (*.pl), Tcl (*.tcl), Ruby (*.rb), Bash (*.sh), or make ([Mm]akefile*) files: @@ -108,7 +109,7 @@ error to refer to a variable that has not been defined by a -D option or by an in-content #define. - Special built-in methods for expressions: - defined(varName) Return true if given variable is defined. + defined(varName) Return true if given variable is defined. Tips @@ -125,16 +126,13 @@ __version_info__ = (1, 1, 0) __version__ = '.'.join(map(str, __version_info__)) -import os -import sys import getopt -import types +import os import re -import pprint - +import sys -#---- exceptions +# ---- exceptions class PreprocessError(Exception): def __init__(self, errmsg, file=None, lineno=None, line=None): @@ -143,6 +141,7 @@ def __init__(self, errmsg, file=None, lineno=None, line=None): self.lineno = lineno self.line = line Exception.__init__(self, errmsg, file, lineno, line) + def __str__(self): s = "" if self.file is not None: @@ -152,13 +151,12 @@ def __str__(self): if self.file is not None or self.lineno is not None: s += " " s += self.errmsg - #if self.line is not None: + # if self.line is not None: # s += ": " + self.line return s - -#---- global data +# ---- global data # Comment delimiter info. # A mapping of content type to a list of 2-tuples defining the line @@ -166,53 +164,57 @@ def __str__(self): # be a string (in which case it is transformed into a pattern allowing # whitespace on either side) or a compiled regex. _commentGroups = { - "Python": [ ('#', '') ], - "Perl": [ ('#', '') ], - "PHP": [ ('/*', '*/'), ('//', ''), ('#', '') ], - "Ruby": [ ('#', '') ], - "Tcl": [ ('#', '') ], - "Shell": [ ('#', '') ], + "Python": [('#', '')], + "Perl": [('#', '')], + "PHP": [('/*', '*/'), ('//', ''), ('#', '')], + "Ruby": [('#', '')], + "Tcl": [('#', '')], + "Shell": [('#', '')], # Allowing for CSS and JavaScript comments in XML/HTML. - "XML": [ (''), ('/*', '*/'), ('//', '') ], - "HTML": [ (''), ('/*', '*/'), ('//', '') ], - "Makefile": [ ('#', '') ], - "JavaScript": [ ('/*', '*/'), ('//', '') ], - "CSS": [ ('/*', '*/') ], - "C": [ ('/*', '*/') ], - "C++": [ ('/*', '*/'), ('//', '') ], - "Java": [ ('/*', '*/'), ('//', '') ], - "C#": [ ('/*', '*/'), ('//', '') ], - "IDL": [ ('/*', '*/'), ('//', '') ], - "Text": [ ('#', '') ], - "Fortran": [ (re.compile(r'^[a-zA-Z*$]\s*'), ''), ('!', '') ], - "TeX": [ ('%', '') ], + "XML": [(''), ('/*', '*/'), ('//', '')], + "HTML": [(''), ('/*', '*/'), ('//', '')], + "Makefile": [('#', '')], + "JavaScript": [('/*', '*/'), ('//', '')], + "CSS": [('/*', '*/')], + "C": [('/*', '*/')], + "C++": [('/*', '*/'), ('//', '')], + "Java": [('/*', '*/'), ('//', '')], + "C#": [('/*', '*/'), ('//', '')], + "IDL": [('/*', '*/'), ('//', '')], + "Text": [('#', '')], + "Fortran": [(re.compile(r'^[a-zA-Z*$]\s*'), ''), ('!', '')], + "TeX": [('%', '')], } - -#---- internal logging facility +# ---- internal logging facility class _Logger: DEBUG, INFO, WARN, ERROR, CRITICAL = list(range(5)) + def __init__(self, name, level=None, streamOrFileName=sys.stderr): self._name = name if level is None: self.level = self.WARN else: self.level = level - if type(streamOrFileName) == bytes: + if isinstance(streamOrFileName, bytes): self.stream = open(streamOrFileName, 'w') self._opennedStream = 1 else: self.stream = streamOrFileName self._opennedStream = 0 + def __del__(self): if self._opennedStream: self.stream.close() + def getLevel(self): return self.level + def setLevel(self, level): self.level = level + def _getLevelName(self, level): levelNameMap = { self.DEBUG: "DEBUG", @@ -222,13 +224,25 @@ def _getLevelName(self, level): self.CRITICAL: "CRITICAL", } return levelNameMap[level] + def isEnabled(self, level): return level >= self.level - def isDebugEnabled(self): return self.isEnabled(self.DEBUG) - def isInfoEnabled(self): return self.isEnabled(self.INFO) - def isWarnEnabled(self): return self.isEnabled(self.WARN) - def isErrorEnabled(self): return self.isEnabled(self.ERROR) - def isFatalEnabled(self): return self.isEnabled(self.FATAL) + + def isDebugEnabled(self): + return self.isEnabled(self.DEBUG) + + def isInfoEnabled(self): + return self.isEnabled(self.INFO) + + def isWarnEnabled(self): + return self.isEnabled(self.WARN) + + def isErrorEnabled(self): + return self.isEnabled(self.ERROR) + + def isFatalEnabled(self): + return self.isEnabled(self.FATAL) + def log(self, level, msg, *args): if level < self.level: return @@ -236,31 +250,36 @@ def log(self, level, msg, *args): message = message + (msg % args) + "\n" self.stream.write(message) self.stream.flush() + def debug(self, msg, *args): self.log(self.DEBUG, msg, *args) + def info(self, msg, *args): self.log(self.INFO, msg, *args) + def warn(self, msg, *args): self.log(self.WARN, msg, *args) + def error(self, msg, *args): self.log(self.ERROR, msg, *args) + def fatal(self, msg, *args): self.log(self.CRITICAL, msg, *args) -log = _Logger("preprocess", _Logger.WARN) +log = _Logger("preprocess", _Logger.WARN) -#---- internal support stuff +# ---- internal support stuff def _evaluate(expr, defines): """Evaluate the given expression string with the given context. WARNING: This runs eval() on a user string. This is unsafe. """ - #interpolated = _interpolate(s, defines) + # interpolated = _interpolate(s, defines) try: - rv = eval(expr, {'defined':lambda v: v in defines}, defines) + rv = eval(expr, {'defined': lambda v: v in defines}, defines) except Exception as ex: msg = str(ex) if msg.startswith("name '") and msg.endswith("' is not defined"): @@ -271,7 +290,7 @@ def _evaluate(expr, defines): varName = msg[len("name '"):-len("' is not defined")] if expr.find("defined(%s)" % varName) != -1: # "defined(FOO)" in expr instead of "defined('FOO')" - msg += " (perhaps you want \"defined('%s')\" instead of "\ + msg += " (perhaps you want \"defined('%s')\" instead of " \ "\"defined(%s)\")" % (varName, varName) elif msg.startswith("invalid syntax"): msg = "invalid syntax: '%s'" % expr @@ -280,10 +299,10 @@ def _evaluate(expr, defines): return rv -#---- module API +# ---- module API -def preprocess(infile, outfile=sys.stdout, defines={}, - force=0, keepLines=0, includePath=[], substitute=0, +def preprocess(infile, outfile=sys.stdout, defines=None, + force=0, keepLines=0, includePath=None, substitute=0, contentType=None, contentTypesRegistry=None, __preprocessedFiles=None): """Preprocess the given file. @@ -312,15 +331,22 @@ def preprocess(infile, outfile=sys.stdout, defines={}, Returns the modified dictionary of defines or raises PreprocessError if there was some problem. """ - if __preprocessedFiles is None: + if includePath is None: + includePath = [] + + if defines is None: + defines = {} + + if __preprocessedFiles is None: __preprocessedFiles = [] - log.info("preprocess(infile=%r, outfile=%r, defines=%r, force=%r, "\ - "keepLines=%r, includePath=%r, contentType=%r, "\ + + log.info("preprocess(infile=%r, outfile=%r, defines=%r, force=%r, " + "keepLines=%r, includePath=%r, contentType=%r, " "__preprocessedFiles=%r)", infile, outfile, defines, force, keepLines, includePath, contentType, __preprocessedFiles) absInfile = os.path.normpath(os.path.abspath(infile)) if absInfile in __preprocessedFiles: - raise PreprocessError("detected recursive #include of '%s'"\ + raise PreprocessError("detected recursive #include of '%s'" % infile) __preprocessedFiles.append(os.path.abspath(infile)) @@ -335,8 +361,8 @@ def preprocess(infile, outfile=sys.stdout, defines={}, try: cgs = _commentGroups[contentType] except KeyError: - raise PreprocessError("don't know comment delimiters for content "\ - "type '%s' (file '%s')"\ + raise PreprocessError("don't know comment delimiters for content " + "type '%s' (file '%s')" % (contentType, infile)) # Generate statement parsing regexes. Basic format: @@ -352,13 +378,13 @@ def preprocess(infile, outfile=sys.stdout, defines={}, # ... # # #endif stmts = ['#\s*(?Pif|elif|ifdef|ifndef)\s+(?P.*?)', - '#\s*(?Pelse|endif)', - '#\s*(?Perror)\s+(?P.*?)', - '#\s*(?Pdefine)\s+(?P[^\s]*?)(\s+(?P.+?))?', - '#\s*(?Pundef)\s+(?P[^\s]*?)', - '#\s*(?Pinclude)\s+"(?P.*?)"', + r'#\s*(?Pelse|endif)', + r'#\s*(?Perror)\s+(?P.*?)', + r'#\s*(?Pdefine)\s+(?P[^\s]*?)(\s+(?P.+?))?', + r'#\s*(?Pundef)\s+(?P[^\s]*?)', + r'#\s*(?Pinclude)\s+"(?P.*?)"', r'#\s*(?Pinclude)\s+(?P[^\s]+?)', - ] + ] patterns = [] for stmt in stmts: # The comment group prefix and suffix can either be just a @@ -379,7 +405,7 @@ def preprocess(infile, outfile=sys.stdout, defines={}, # Process the input file. # (Would be helpful if I knew anything about lexing and parsing # simple grammars.) - fin = open(infile, 'r') + fin = open(infile) lines = fin.readlines() fin.close() if type(outfile) in str: @@ -391,10 +417,10 @@ def preprocess(infile, outfile=sys.stdout, defines={}, fout = outfile defines['__FILE__'] = infile - SKIP, EMIT = list(range(2)) # states - states = [(EMIT, # a state is (, - 0, # , - 0)] # ) + SKIP, EMIT = list(range(2)) # states + states = [(EMIT, # a state is (, + 0, # , + 0)] # ) lineNum = 0 for line in lines: lineNum += 1 @@ -402,7 +428,7 @@ def preprocess(infile, outfile=sys.stdout, defines={}, defines['__LINE__'] = lineNum # Is this line a preprocessor stmt line? - #XXX Could probably speed this up by optimizing common case of + # XXX Could probably speed this up by optimizing common case of # line NOT being a preprocessor stmt line. for stmtRe in stmtRes: match = stmtRe.match(line) @@ -422,7 +448,7 @@ def preprocess(infile, outfile=sys.stdout, defines={}, else: try: val = eval(val, {}, {}) - except: + except BaseException: pass defines[var] = val elif op == "undef": @@ -441,18 +467,18 @@ def preprocess(infile, outfile=sys.stdout, defines={}, else: # This is the first include form: #include "path" f = match.group("fname") - + for d in [os.path.dirname(infile)] + includePath: fname = os.path.normpath(os.path.join(d, f)) if os.path.exists(fname): break else: - raise PreprocessError("could not find #include'd file "\ - "\"%s\" on include path: %r"\ + raise PreprocessError("could not find #include'd file " + "\"%s\" on include path: %r" % (f, includePath)) defines = preprocess(fname, fout, defines, force, keepLines, includePath, substitute, - contentTypesRegistry=contentTypesRegistry, + contentTypesRegistry=contentTypesRegistry, __preprocessedFiles=__preprocessedFiles) elif op in ("if", "ifdef", "ifndef"): if op == "if": @@ -470,17 +496,17 @@ def preprocess(infile, outfile=sys.stdout, defines={}, else: states.append((SKIP, 0, 0)) except KeyError: - raise PreprocessError("use of undefined variable in "\ + raise PreprocessError("use of undefined variable in " "#%s stmt" % op, defines['__FILE__'], defines['__LINE__'], line) elif op == "elif": expr = match.group("expr") try: - if states[-1][2]: # already had #else in this if-block - raise PreprocessError("illegal #elif after #else in "\ - "same #if block", defines['__FILE__'], - defines['__LINE__'], line) - elif states[-1][1]: # if have emitted in this if-block + if states[-1][2]: # already had #else in this if-block + raise PreprocessError("illegal #elif after #else in " + "same #if block", defines['__FILE__'], + defines['__LINE__'], line) + elif states[-1][1]: # if have emitted in this if-block states[-1] = (SKIP, 1, 0) elif states[:-1] and states[-2][0] == SKIP: # Were are nested in a SKIP-portion of an if-block. @@ -490,16 +516,16 @@ def preprocess(infile, outfile=sys.stdout, defines={}, else: states[-1] = (SKIP, 0, 0) except IndexError: - raise PreprocessError("#elif stmt without leading #if "\ + raise PreprocessError("#elif stmt without leading #if " "stmt", defines['__FILE__'], defines['__LINE__'], line) elif op == "else": try: - if states[-1][2]: # already had #else in this if-block - raise PreprocessError("illegal #else after #else in "\ - "same #if block", defines['__FILE__'], - defines['__LINE__'], line) - elif states[-1][1]: # if have emitted in this if-block + if states[-1][2]: # already had #else in this if-block + raise PreprocessError("illegal #else after #else in " + "same #if block", defines['__FILE__'], + defines['__LINE__'], line) + elif states[-1][1]: # if have emitted in this if-block states[-1] = (SKIP, 1, 1) elif states[:-1] and states[-2][0] == SKIP: # Were are nested in a SKIP-portion of an if-block. @@ -507,20 +533,20 @@ def preprocess(infile, outfile=sys.stdout, defines={}, else: states[-1] = (EMIT, 1, 1) except IndexError: - raise PreprocessError("#else stmt without leading #if "\ + raise PreprocessError("#else stmt without leading #if " "stmt", defines['__FILE__'], defines['__LINE__'], line) elif op == "endif": try: states.pop() except IndexError: - raise PreprocessError("#endif stmt without leading #if"\ + raise PreprocessError("#endif stmt without leading #if" "stmt", defines['__FILE__'], defines['__LINE__'], line) elif op == "error": if not (states and states[-1][0] == SKIP): error = match.group("error") - raise PreprocessError("#error: "+error, defines['__FILE__'], + raise PreprocessError("#error: " + error, defines['__FILE__'], defines['__LINE__'], line) log.debug("states: %r", states) if keepLines: @@ -560,7 +586,7 @@ def preprocess(infile, outfile=sys.stdout, defines={}, return defines -#---- content-type handling +# ---- content-type handling _gDefaultContentTypes = """ # Default file types understood by "preprocess.py". @@ -631,6 +657,7 @@ def preprocess(infile, outfile=sys.stdout, defines={}, Text .kkf # Keybinding schemes files """ + class ContentTypesRegistry: """A class that handles determining the filetype of a given path. @@ -655,14 +682,14 @@ def _load(self): localContentTypesPath = join(dirname(__file__), "content.types") if exists(localContentTypesPath): log.debug("load content types file: `%r'" % localContentTypesPath) - self._loadContentType(open(localContentTypesPath, 'r').read()) + self._loadContentType(open(localContentTypesPath).read()) for path in (self.contentTypesPaths or []): log.debug("load content types file: `%r'" % path) - self._loadContentType(open(path, 'r').read()) + self._loadContentType(open(path).read()) def _loadContentType(self, content, path=None): """Return the registry for the given content.types file. - + The registry is three mappings: -> -> @@ -674,11 +701,13 @@ def _loadContentType(self, content, path=None): if words[i][0] == '#': del words[i:] break - if not words: continue + if not words: + continue contentType, patterns = words[0], words[1:] if not patterns: - if line[-1] == '\n': line = line[:-1] - raise PreprocessError("bogus content.types line, there must "\ + if line[-1] == '\n': + line = line[:-1] + raise PreprocessError("bogus content.types line, there must " "be one or more patterns: '%s'" % line) for pattern in patterns: if pattern.startswith('.'): @@ -704,7 +733,7 @@ def getContentType(self, path): # Try to determine from the path. if not contentType and basename in self.filenameMap: contentType = self.filenameMap[basename] - log.debug("Content type of '%s' is '%s' (determined from full "\ + log.debug("Content type of '%s' is '%s' (determined from full " "path).", path, contentType) # Try to determine from the suffix. if not contentType and '.' in basename: @@ -714,7 +743,7 @@ def getContentType(self, path): suffix = suffix.lower() if suffix in self.suffixMap: contentType = self.suffixMap[suffix] - log.debug("Content type of '%s' is '%s' (determined from "\ + log.debug("Content type of '%s' is '%s' (determined from " "suffix '%s').", path, contentType, suffix) # Try to determine from the registered set of regex patterns. if not contentType: @@ -730,7 +759,10 @@ def getContentType(self, path): contentType = "XML" return contentType + _gDefaultContentTypesRegistry = None + + def getDefaultContentTypesRegistry(): global _gDefaultContentTypesRegistry if _gDefaultContentTypesRegistry is None: @@ -738,8 +770,8 @@ def getDefaultContentTypesRegistry(): return _gDefaultContentTypesRegistry -#---- internal support stuff -#TODO: move other internal stuff down to this section +# ---- internal support stuff +# TODO: move other internal stuff down to this section try: reversed @@ -756,23 +788,24 @@ def reversed(seq): # 'sorted' added in Python 2.4. Note that I'm only implementing enough # of sorted as is used in this module. def sorted(seq, key=None): - identity = lambda x: x + def identity(x): return x key_func = (key or identity) sseq = list(seq) - sseq.sort(lambda self, other: cmp(key_func(self), key_func(other))) + sseq.sort(lambda self, other: (key_func(self) > key_func( + other)) - (key_func(self) < key_func(other))) for item in sseq: yield item -#---- mainline +# ---- mainline def main(argv): try: optlist, args = getopt.getopt(argv[1:], 'hVvo:D:fkI:sc:', - ['help', 'version', 'verbose', 'force', 'keep-lines', - 'substitute', 'content-types-path=']) + ['help', 'version', 'verbose', 'force', 'keep-lines', + 'substitute', 'content-types-path=']) except getopt.GetoptError as msg: - sys.stderr.write("preprocess: error: %s. Your invocation was: %s\n"\ + sys.stderr.write("preprocess: error: %s. Your invocation was: %s\n" % (msg, argv)) sys.stderr.write("See 'preprocess --help'.\n") return 1 @@ -816,7 +849,7 @@ def main(argv): contentTypesPaths.append(optarg) if len(args) != 1: - sys.stderr.write("preprocess: error: incorrect number of "\ + sys.stderr.write("preprocess: error: incorrect number of " "arguments: argv=%r\n" % argv) return 1 else: @@ -834,7 +867,7 @@ def main(argv): sys.stderr.write("preprocess: error: %s\n" % str(ex)) return 1 + if __name__ == "__main__": __file__ = sys.argv[0] - sys.exit( main(sys.argv) ) - + sys.exit(main(sys.argv))