I had a problem implementing python 3 for an application that should run its own scripts in python. Since the scripts can be completely different, and sometimes provided by users, I try to isolate each execution, and there is no need to save any data between the execution of different scripts.
So my solution is to wrap each execution between Py_Initialize and Py_Finalize . It looks something like this:
void ExecuteScript(const char* script) { Py_Initialize(); PyRun_SimpleString( script ); Py_Finalize(); }
However, this is not executed for a specific python script the second time the 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, for the first time around the script, "done!" was printed. But the second time it throws an exception inside the copy function.
It seems that the python mechanism has remained in some strange state after the initialization is completed the first time. Please note that this is python 3.
It is also very interesting to note that Python 2.7 did not have this problem.
I suppose there may be other examples that could better show what is happening, but I have not had time to find.
Full sources of the test project can be found here: https://docs.google.com/file/d/0B86-G0mwwxZvNGpoM1Jia3E2Wmc/edit?usp=sharing
Please note: the file is 8 MB in size as it includes the python distribution.
Any ideas on how to solve this problem are appreciated.
EDIT: I also put a copy of the project containing the flag to switch between Python 3 and Python 2.7 (file - 31 MB): https://docs.google.com/file/d/0B86-G0mwwxZvbWRldTd5b2NNMWM/edit?usp=sharing
EDIT: Well, I tested Python3.2 and it worked fine. This seems like an error only in Python3.3. Adding as a problem: http://bugs.python.org/issue17408#