简体   繁体   中英

Python 2.x to 3.x - Append Syntaxerror: how to make it compatibale with Python 3.x from 2.x?

I have a very simple question that I am having difficulty tracing down - which implies a very simple answer. I have a slew of scripts written by a slew of people over the last ~12 years or so in python 2x that I am having to port over to python 3x.

After searching and many, many, many fixes I have run into a change that is difficult to track down. I have a syntax error in e.append(lineno, line ) shown below. Any ideas on how to alter this to make it python 3x compatible would be super useful.

    self.__options = {}
    lineno = 0
    e = None                                  # None, or an exception
    while 1:
        line = fp.readline()
        if not line:
            break
        lineno = lineno + 1
        # skip blank lines
        if string.strip(line) == '':
            continue
        # skip lines starting with '['
        if line[0] == '[':
            continue
        # remove anything after '#'
        line = string.split(line, '#')[0]

        # key/value pairs can be seperated by whitespaces
        for opt in string.split(line):
            #if opt in string.whitespace:
            #    continue
            keyval = string.split(opt, '=')
            if len(keyval) == 2:
                self.__options[keyval[0]] = keyval[1]
            else:
                e = ParsingError(fpname)
                e.append(lineno, `line`)

    # if any parsing errors occurred, raise an exception
    if e:
        raise e

This is pretty neat historical syntax.

For backticks, I refer you to this answer: https://stackoverflow.com/a/1673087/2988730 . The gist is that backticks are shorthand for calling repr until they were removed in python 3. They are mentioned in the docs as the "conversion" operation:

repr ( object )

Return a string containing a printable representation of an object. This is the same value yielded by conversions (reverse quotes). It is sometimes useful to be able to access this operation as an ordinary function...

The append itself is a normal, though undocumented use of the ParsingError.append method:

def append(self, lineno, line):

Since version 3, ConfigParser had been renamed configparser , but still has the ParsingError.append method.

So you need a couple of changes:

  1. At the beginning of the file, change from ConfigParser import ParsingError to

    from configparser import ParsingError

  2. Change the line with the error to

    e.append(lineno, repr(line))

Note

The multiple arguments to append evoke, but aren't related to, a behavior of list.append that hasn't been supported since python 2.0. You can find a note here: https://docs.python.org/2/library/stdtypes.html#mutable-sequence-types . Specifically the footnote:

  1. The C implementation of Python has historically accepted multiple parameters and implicitly joined them into a tuple; this no longer works in Python 2.0. Use of this misfeature has been deprecated since Python 1.4.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM