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

F

Fabio Zadrozny

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)
try:
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)
break
finally:
stream.close()

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

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

Cheers,

Fabio
 
M

Martin v. Loewis

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).

No. The default replacement should really open the file in binary mode.

Regards,
Martin
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top