[Piglit] [PATCH 11/20] framework: Port from execfile to compile & exec (required by python 3.x)

Jon Severinsson jon at severinsson.net
Wed Apr 17 08:49:45 PDT 2013

At Wednesday 17 April 2013 11:35:52 Kenneth Graunke wrote:
> On 04/12/2013 04:39 PM, Jon Severinsson wrote:
> >  [...]
> Two problems here:
> 1. This leaves the file open...you ought to close it after you're done
> reading from it.  The easiest way to do this is by doing:
> with open(filename, 'rb') as f:
>      exec(compile(f.read(), filename, 'exec'))

Oups, will do.

> 2. You missed a bunch of tests/*.tests files, which all use execfile()
> as well.

Oups, again.

> I'm not sure the best way to handle this...perhaps create a
> framework/util.py that defines execfile for Python 3?  Then we'd need
> imports everywhere, though...
> Thoughts?

Unfortunately, this is not an option, as that would make the files be evaluated 
in the context where we defined execfile, and not in the context where we called 
execfile. The only reason it works in all.test is that we only ever call the 
function in the same context as we declared it :(.

All in all, I believe the best thing to do is just use the following code 
snippet everywhere:
with open(«filename», 'rb') as f:
    exec(compile(f.read(), f.name, 'exec'))

A bit more verbose, but works in all python version (2.6+), and we don't have 
to wory about execution contexts...

/ Jon

More information about the Piglit mailing list