From ba52c4cae204c4fc7104b14ac446d4d5a65a26f8 Mon Sep 17 00:00:00 2001 From: Alexander Wauck Date: Tue, 31 Mar 2015 12:44:09 -0500 Subject: Make spice_codegen.py work on both Python 2 and 3 This is a new version of my previous patch that does not include six.py. It's still kind of big, but at least it's all spice-common changes now. There are also a few other fixes that Christophe brought to my attention. Note that six now needs to be installed on the system (python-six on Fedora and Debian, six on PyPI). This *should* be enough to make spice_codegen.py work on both Python 2 and Python 3. The major changes are as follows: * cStringIO.StringIO -> io.StringIO * str vs. unicode updates (io.StringIO doesn't like str) * integer division * foo.has_key(bar) -> bar in foo * import internal_thing -> from . import internal_thing * removed from __future__ import with_statement (might break Python 2.5?) * changed some lambdas to list comprehensions (done by 2to3) * cast some_dict.keys() to list where needed (e.g. for sorting) * use normal type names with isinstance instead of types.WhateverType Signed-off-by: Alexander Wauck --- python_modules/spice_parser.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'python_modules/spice_parser.py') diff --git a/python_modules/spice_parser.py b/python_modules/spice_parser.py index 44456ab..d60bb10 100644 --- a/python_modules/spice_parser.py +++ b/python_modules/spice_parser.py @@ -3,11 +3,12 @@ try: Forward, delimitedList, Group, Optional, Combine, alphas, nums, restOfLine, cStyleComment, \ alphanums, ParseException, ParseResults, Keyword, StringEnd, replaceWith except ImportError: - print "Module pyparsing not found." + six.print_("Module pyparsing not found.") exit(1) -import ptypes +from . import ptypes +import six import sys cvtInt = lambda toks: int(toks[0]) @@ -148,10 +149,10 @@ def parse(filename): try: bnf = SPICE_BNF() types = bnf.parseFile(filename) - except ParseException, err: - print >> sys.stderr, err.line - print >> sys.stderr, " "*(err.column-1) + "^" - print >> sys.stderr, err + except ParseException as err: + six.print_(err.line, file=sys.stderr) + six.print_(" "*(err.column-1) + "^", file=sys.stderr) + six.print_(err, file=sys.stderr) return None for t in types: -- cgit