[Python-Dev] Python 3.0rc2: problem with exec()ing files (original) (raw)

Jukka Aho jukka.aho at iki.fi
Tue Nov 11 13:00:36 CET 2008


The current Py3k documentation [1] states that the built-in exec() function should be able to execute code from "open file objects":

--- 8< ---

exec(object[, globals[, locals]])

This function supports dynamic execution of Python code. object must be either a string, an open file object, or a code object. [...] If it is an open file, the file is parsed until EOF and executed.

--- 8< ---

However, at least on Python 3.0rc2 [2], this does not seem to work. Please consider the following example:

  1. First, let's prepare a source file which will contain a single line of Python code (it is assumed here that it is safe to save the file in the current working path):

f = open("exectest.py", mode="wt", encoding="utf-8") f.write("print('Hello world!')\n") 22 f.close()

  1. Then, try executing that newly created file with the exec() function. What I get is this:

f = open("exectest.py", mode="rb") exec(f) Traceback (most recent call last): File "<pyshell#30>", line 1, in exec(f) TypeError: exec() arg 1 must be a string, bytes or code object f <io.BufferedReader object at 0x013832D0>


So, am I seeing this behavior because...

a) I've misinterpreted the available documentation

b) The documentation does not agree with the current development goals

c) It is a bug or a yet-to-be-implemented feature of the built-in exec() function?


[1] <http://docs.python.org/dev/3.0/library/functions.html#exec>

[2] Python 3.0rc2 (r30rc2:67141, Nov 7 2008, 11:43:46) [MSC v.1500 32 bit (Intel)] on win32, to be exact.

-- znark



More information about the Python-Dev mailing list