Hi Folks:
I have a question about the use of static members in Python/C
extensions. Take the simple example from the "Extending and Embedding
the Python Interpreter" docs:
A simple module method:
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
...
}
listed in a method table:
static PyMethodDef SpamMethods[] = {
...
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
...
{NULL, NULL, 0, NULL} /* Sentinel */
};
that is registered with:
PyMODINIT_FUNC
initspam(void)
{
(void) Py_InitModule("spam", SpamMethods);
}
I have an application that embed multiple interpreters. I want to be
able to register methods for each interpreter that access C++ state
data relevant to that particular interpreter. In other words I want to
be able to register non-static class members in a class like the
following:
class PythonSpamModuleInstance
{
PythonSpamModuleInstance()
{
SpamMethods[0]={"system", spam_system, METH_VARARGS,"Execute
a shell command."};
SpamMethods[1]={NULL, NULL, 0, NULL};
PyObject *spammodule=Py_InitModule("spam", SpamMethods); //
Needs an INCREF call?
}
~PythonSpamModuleInstance()
{
PyObject_Del("spam", SpamMethods);
}
PyObject *spam_system(PyObject *self, PyObject *args)
{
// accesses non-static data in this class
}
PyMethodDef SpamMethods[2];
PyObject *spammodule;
// Other data specific to this instance of the module
};
The idea is that each time my app launches an embedded Interpreter it
will create an accompanying instance of PythonSpamModuleInstance that
allows the Interpreter to call the registered non-static C++ member
function spam_system. Calls to that member function will also affect
state information.
So the questions: will this work? In particular, will the use of non-
static member functions cause problems? Is there a better way of
creating extension modules for multiple embedded interpreters? If I am
forced to use static methods, how do i figure out which interpreter is
calling them? 3 2632
dmoore <da**********@gmail.comwrote:
I've obviously spent too much time with dynamic languages. The problem
with what I'm trying to do is obvious: In C++ you simply can't pass
pointers to call a particular instance of a C++ class method so there
is no way to initialize the PyMethodDef with class methods instances.
It is possible in theory. You can do what python does when it makes a
bound method and build a custom function at run time with the instance
and method built in.
How you build that function is tricky - you've basically got to poke
object code onto the heap which implements the method call to that
particular instance.
Looking at this page might give you some ideas http://gcc.gnu.org/onlinedocs/gccint/Trampolines.html
This probably isn't a good approach in reality though as it is very
architecture / compiler dependent!
--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
On Jul 16, 9:45 am, dmoore <damienlmo...@gmail.comwrote:
Hi Folks:
I have a question about the use of static members in Python/C
extensions. Take the simple example from the "Extending and Embedding
the Python Interpreter" docs:
A simple module method:
static PyObject *
spam_system(PyObject *self, PyObject *args)
{
...
}
listed in a method table:
static PyMethodDef SpamMethods[] = {
...
{"system", spam_system, METH_VARARGS,
"Execute a shell command."},
...
{NULL, NULL, 0, NULL} /* Sentinel */
};
that is registered with:
PyMODINIT_FUNC
initspam(void)
{
(void) Py_InitModule("spam", SpamMethods);
}
I have an application that embed multiple interpreters. I want to be
able to register methods for each interpreter that access C++ state
data relevant to that particular interpreter. In other words I want to
be able to register non-static class members in a class like the
following:
class PythonSpamModuleInstance
{
PythonSpamModuleInstance()
{
SpamMethods[0]={"system", spam_system, METH_VARARGS,"Execute
a shell command."};
SpamMethods[1]={NULL, NULL, 0, NULL};
PyObject *spammodule=Py_InitModule("spam", SpamMethods); //
Needs an INCREF call?
}
~PythonSpamModuleInstance()
{
PyObject_Del("spam", SpamMethods);
}
PyObject *spam_system(PyObject *self, PyObject *args)
{
// accesses non-static data in this class
}
PyMethodDef SpamMethods[2];
PyObject *spammodule;
// Other data specific to this instance of the module
};
The idea is that each time my app launches an embedded Interpreter it
will create an accompanying instance of PythonSpamModuleInstance that
allows the Interpreter to call the registered non-static C++ member
function spam_system. Calls to that member function will also affect
state information.
So the questions: will this work? In particular, will the use of non-
static member functions cause problems? Is there a better way of
creating extension modules for multiple embedded interpreters? If I am
forced to use static methods, how do i figure out which interpreter is
calling them?
test posting.
thanks for the responses Nick and "AnonMail"
I'm doing a similar thing, and I would imagine others are also.
1. In a python file, set up wrapper functions that the python user
actually uses (e.g FunctionA). Each function corresponds to a
particular C/C++ extension function that you are exposing (e.g.
CFunctionA).
....
4. Now when you enter you C/C++ function you can find your object by
looking
it up in some predefined table using the id. What we actually do is
not use
an integer but instead use a void pointer which is cast appropriately
once
inside C/C++. This avoids the lookup.
step 4 is smart :)
It's good to know that there is at least one data point of success
using the approach. I won't give up just yet. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Alicia Haumann |
last post by:
I accidentally sent this to webmaster@python.org, so this could be a
duplicate if "webmaster" forwards it to this list. :{
Hi, there.
Thanks for any help that can be offered. I've been...
|
by: Roose |
last post by:
With some googling I have found these resources:
http://docs.python.org/ext/win-dlls.html
http://www.python.org/doc/faq/windows.html
I have a large Win32/MFC/C/C++ application that has an...
|
by: stefan |
last post by:
Hi Folks,
I currenty extended some of my C++ functionality to python and also
embedded python to use python functionality in my C++ system (and use
as well these extended functions).
While...
|
by: amit |
last post by:
Hello,
I am currently studying how to embedd python. I am developing a
graphical C++ application. My goal is to embedd python script that will
control some kind of animation.
I have some...
|
by: Tommy Nordgren |
last post by:
I want to write an application that embeds and extends (at least) the
Python and Perl interpreters. Now i want to find as much as possible
about the Python tools used for extending and embedding...
|
by: Marco Meoni |
last post by:
Hi all! I've a problem with a C++ class that has to be included in a
python application. One way to do it is Extending and Embedding the
Python Interpreter
Now i have 2 questions
1) Is there a...
|
by: jeremito |
last post by:
I am trying to learn how to extend and/or embed Python. I have looked
at the document "Extending and Embedding the Python Interpreter" and
also "Python/C API Reference Manual. In the examples...
|
by: Qun Cao |
last post by:
Hi Everyone,
I am a beginner on cross language development. My problem at hand is to
build a python interface for a C++ application built on top of a 3D
game engine. The purpose of this python...
|
by: anonymisiert85 |
last post by:
At the moment i can run python-string-code from C (MinGW, WinXP)
But how can i register a C-function in python-RUNTIME and call this C
function from python - without wrapper dll's or libs???
...
|
by: Thomas Troeger |
last post by:
Dear all,
I've successfully embedded the Python interpreter into a set of C/C++
application programs that use a larger library project with information
from http://docs.python.org/api/api.html...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
|
by: GKJR |
last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
|
by: SueHopson |
last post by:
Hi All,
I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...
| |