Question

I'm having a problem embedding the python 3 engine for an app that need to run custom scripts in python. Since the scripts might be completely different, and sometimes user provided, I am trying to make each execution isolated and there is not need to preserve any data between execution of the different scripts.

So, my solution is to wrap each execution between Py_Initialize and Py_Finalize. It looks something like that:

void ExecuteScript(const char* script)
{
Py_Initialize();

PyRun_SimpleString( script );

Py_Finalize();
}

However, this fails for a particular python script the second time a script is executed with:

done!
Traceback (most recent call last):
  File "<string>", line 8, in <module>
  File "\Python33Test\Output\Debug\Python33\Lib\copy.py", line 89, in copy
    rv = reductor(2)
TypeError: attribute of type 'NoneType' is not callable

The python script looks like this:

class Data:
    value1 = 'hello'
    value2 = 0

import copy

d = Data()
dd = copy.copy( d )
print ( 'done!' )

As you can see, the first time around the script was executed the 'done!' was printed out. But the second time it rises an exception inside the copy function.

It looks like the python engine was left in some weird state after the first initialize-finalize. Note, this is python 3.

Also, it is very interesting to note that Python 2.7 did not have this problem.

I guess there might be other examples that could reveal better what's going, but i haven't had the time to find yet.

Full sources of the test project can be found here: https://docs.google.com/file/d/0B86-G0mwwxZvNGpoM1Jia3E2Wmc/edit?usp=sharing

Note, the file is 8MB because it includes the python distribution.

Any ideas of how to solve this are appreciated.

EDIT: I also put a copy of the project containing flag to switch between Python 3 and Python 2.7 (the file is 31 MB): https://docs.google.com/file/d/0B86-G0mwwxZvbWRldTd5b2NNMWM/edit?usp=sharing

EDIT: Well, I tested with Python3.2 and it worked fine. So it seems to be bug in Python3.3 only. Adding as an issue: http://bugs.python.org/issue17408#

Was it helpful?

Solution

Well, this was fast.

They actually found the issue being a problem in Python 3.3 code.

http://bugs.python.org/issue17408#

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top