Running script in module initialization

K

Kom2

Hello,
I'm trying to convert my project from python 2.5 to python 3.0 and I
have the following problem. My project is PYD library written in C++.
So I have this PyInit_ModuleName function containing PyModule_Create
call and this function also call some script with declarations:


PyObject* m;

m = PyModule_Create(&PyVRDAModule);
if (m == NULL) {
return NULL;
}
PyObject *d, *v;
d = PyModule_GetDict(m);
v = PyRun_StringFlags(txtPyPredefinedConstants), Py_file_input, d,
d, NULL);
......



txtPyPredefinedConstants is string with this content:

class CursorMoveType:
First = 0
Last = 1
Next = 2
Previous = 3
Bookmark = 4
NewRecord = 5


In Python 2.5 everything works fine, now in python3.0
PyRun_StringFlags returns NULL and I get error "__build_class__ not
found".

Can anybody tell mi please, what is wrong?

thanks Kom2
 
S

Steve Holden

Kom2 said:
Hello,
I'm trying to convert my project from python 2.5 to python 3.0 and I
have the following problem. My project is PYD library written in C++.
So I have this PyInit_ModuleName function containing PyModule_Create
call and this function also call some script with declarations:


PyObject* m;

m = PyModule_Create(&PyVRDAModule);
if (m == NULL) {
return NULL;
}
PyObject *d, *v;
d = PyModule_GetDict(m);
v = PyRun_StringFlags(txtPyPredefinedConstants), Py_file_input, d,
d, NULL);
......



txtPyPredefinedConstants is string with this content:

class CursorMoveType:
First = 0
Last = 1
Next = 2
Previous = 3
Bookmark = 4
NewRecord = 5


In Python 2.5 everything works fine, now in python3.0
PyRun_StringFlags returns NULL and I get error "__build_class__ not
found".

Can anybody tell mi please, what is wrong?
Presumably you haven't upgraded your extension module to use the new API?

http://wiki.python.org/moin/PortingExtensionModulesToPy3k

regards
Steve
 
G

Gabriel Genellina

__build_class__ is a (hidden?) function in the builtins module.
Your module isn't completely created yet, it lacks the __builtins__
attribute, so you can't execute any Python code that calls a builtin
function. In 2.5 the class statement was a lot simpler, but in 3.0 it
requires an auxiliary function (implemented as builtins.__build_class__)
You may try setting __builtins__ to the current builtins
(PyEval_GetBuiltins) before executing Python code.
Or, if those 7 lines are all you need, you may just write the equivalent C
code.
 
K

Kom2

En Mon, 23 Feb 2009 05:51:27 -0200, Kom2 <[email protected]> escribió:




__build_class__ is a (hidden?) function in the builtins module.
Your module isn't completely created yet, it lacks the __builtins__  
attribute, so you can't execute any Python code that calls a builtin  
function. In 2.5 the class statement was a lot simpler, but in 3.0 it  
requires an auxiliary function (implemented as builtins.__build_class__)
You may try setting __builtins__ to the current builtins  
(PyEval_GetBuiltins) before executing Python code.
Or, if those 7 lines are all you need, you may just write the equivalent C  
code.

Thanks for answer, calling PyEval_GetBuiltins could also help.
I solved this issue by getting dictionary of __main__ module as global
dictionary -

d = PyModule_GetDict(m);
PyObject* glMod = PyImport_AddModule("__main__");
if (glMod == NULL) {
return NULL;
}
PyObject* glDict = PyModule_GetDict(glMod);
v = PyRun_StringFlags(txtPyPredefinedConstants, Py_file_input,
glDict, d, NULL);

- and it works.
 

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
474,431
Messages
2,571,678
Members
48,796
Latest member
Greg L.

Latest Threads

Top