Extending embedded Python: Adding single methods

  • Thread starter Torsten Bronger
  • Start date
T

Torsten Bronger

Hallöchen!

I'd like to script C++ funtions by an embedded Python interpreter.
So far, my C++ main() function contains:

Py_Initialize();
Py_InitModule("pp3", PythonMethods);
PyRun_SimpleString("from pp3 import *");
PyRun_AnyFile(stdin, NULL);
Py_Finalize();

"PythonMethods" is the vector of type PyMethodDef that contains the
function descriptors:

static PyMethodDef PythonMethods[] = {
{"toll", py_toll, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};

Then I say "toll()" in the input script which calls py_toll() in the
C++ source.


It works. However, is there a way to avoid this dummy "pp3" module
and add the C++ functions directy to the main namespace in the
Python script?

Tschö,
Torsten.
 
T

Thomas Heller

Torsten said:
Hallöchen!

I'd like to script C++ funtions by an embedded Python interpreter.
So far, my C++ main() function contains:

Py_Initialize();
Py_InitModule("pp3", PythonMethods);
PyRun_SimpleString("from pp3 import *");
PyRun_AnyFile(stdin, NULL);
Py_Finalize();

"PythonMethods" is the vector of type PyMethodDef that contains the
function descriptors:

static PyMethodDef PythonMethods[] = {
{"toll", py_toll, METH_VARARGS, ""},
{NULL, NULL, 0, NULL}
};

Then I say "toll()" in the input script which calls py_toll() in the
C++ source.


It works. However, is there a way to avoid this dummy "pp3" module
and add the C++ functions directy to the main namespace in the
Python script?

Yes. You can import __builtin__, and add methods to it.
This is a snippet from the bdist_wininst code, which embeds Python:

http://svn.python.org/view/python/trunk/PC/bdist_wininst/install.c?rev=38414&view=markup

PyMethodDef meth[] = {
{"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
{"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
{"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL},
{"file_created", FileCreated, METH_VARARGS, NULL},
{"directory_created", DirectoryCreated, METH_VARARGS, NULL},
{"message_box", PyMessageBox, METH_VARARGS, NULL},
};


....
mod = PyImport_ImportModule("__builtin__");
if (mod) {
int i;
for (i = 0; i < DIM(meth); ++i) {
PyObject_SetAttrString(mod, meth.ml_name,
PyCFunction_New(&meth, NULL));
}
}
....

Thomas
 
T

Torsten Bronger

Hallöchen!

Thomas Heller said:
Torsten said:
[...] However, is there a way to avoid this dummy "pp3" module
and add the C++ functions directy to the main namespace in the
Python script?

Yes. You can import __builtin__, and add methods to it. This is
a snippet from the bdist_wininst code, which embeds Python:

http://svn.python.org/view/python/trunk/PC/bdist_wininst/install.c?rev=38414&view=markup

PyMethodDef meth[] = {
{"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
{"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
{"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL},
{"file_created", FileCreated, METH_VARARGS, NULL},
{"directory_created", DirectoryCreated, METH_VARARGS, NULL},
{"message_box", PyMessageBox, METH_VARARGS, NULL},
};


...
mod = PyImport_ImportModule("__builtin__");
if (mod) {
int i;
for (i = 0; i < DIM(meth); ++i) {
PyObject_SetAttrString(mod, meth.ml_name,
PyCFunction_New(&meth, NULL));
}
}
...


Thank you, it works well. Just one more question: Why isn't "mod"
Py_DECREFed again?

Tschö,
Torsten.
 
T

Thomas Heller

Torsten said:
Hallöchen!

Thomas Heller said:
Torsten said:
[...] However, is there a way to avoid this dummy "pp3" module
and add the C++ functions directy to the main namespace in the
Python script?
Yes. You can import __builtin__, and add methods to it. This is
a snippet from the bdist_wininst code, which embeds Python:

http://svn.python.org/view/python/trunk/PC/bdist_wininst/install.c?rev=38414&view=markup

PyMethodDef meth[] = {
{"create_shortcut", CreateShortcut, METH_VARARGS, NULL},
{"get_special_folder_path", GetSpecialFolderPath, METH_VARARGS, NULL},
{"get_root_hkey", (PyCFunction)GetRootHKey, METH_NOARGS, NULL},
{"file_created", FileCreated, METH_VARARGS, NULL},
{"directory_created", DirectoryCreated, METH_VARARGS, NULL},
{"message_box", PyMessageBox, METH_VARARGS, NULL},
};


...
mod = PyImport_ImportModule("__builtin__");
if (mod) {
int i;
for (i = 0; i < DIM(meth); ++i) {
PyObject_SetAttrString(mod, meth.ml_name,
PyCFunction_New(&meth, NULL));
}
}
...


Thank you, it works well. Just one more question: Why isn't "mod"
Py_DECREFed again?


A mistake, as it seems.

Thomas
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top