2to3 issues with execfile on python 3.0 on files with encoding

Discussion in 'Python' started by Fabio Zadrozny, May 6, 2010.

  1. Right now, it seems that the default implementation of execfile in
    2to3 is something as:

    exec(compile(open(file).read()+"\n", file, 'exec'), globals, locals)

    But it seems it won't deal with encodings properly... and also, in
    CPython just making an open without a close is OK, because of the
    reference counting, but in Jython it shouldn't be relied on (so, an
    explicit close should be done).

    The default replacement should be really providing a new execfile that
    gets the encoding in the first 2 lines and opens it with the proper
    encoding set (and properly closes the stream).

    So, below is what I think would be a correct replacement... Can it be
    made easier? Is there any utility to get the encoding already? Maybe
    an execfile should be created again in Python 3? (even if not in the
    __builtin__, but at least somewhere, as that seems really tricky to
    get right)

    Note that some 'with' statements could be used, but I chose not to
    because I wanted it to be at least syntax-compatible with earlier
    versions of python.

    def execfile(file):
    stream = open(file)
    encoding = None
    #Get encoding!
    for i in range(2):
    line = stream.readline() #Should not raise an exception
    even if there are no more contents
    #Must be a comment line
    if line.strip().startswith('#'):
    #Don't import re if there's no chance that there's an
    encoding in the line
    if 'coding' in line:
    import re
    p = re.search(r"coding[:=]\s*([-\w.]+)", line)
    if p:
    encoding = p.group(1)

    if encoding is not None:
    stream = open(file, encoding=encoding)
    stream = open(file)
    contents = stream.read()

    exec(compile(contents+"\n", file, 'exec'), globals, locals)
    #execute the script


    Fabio Zadrozny, May 6, 2010
    1. Advertisements

  2. The default replacement should be really providing a new execfile that
    No. The default replacement should really open the file in binary mode.

    Martin v. Loewis, May 7, 2010
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.