473,624 Members | 2,269 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Embedding/Extending Python in/with C++: non-static members?

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(PyO bject *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 PythonSpamModul eInstance
{
PythonSpamModul eInstance()
{
SpamMethods[0]={"system", spam_system, METH_VARARGS,"E xecute
a shell command."};
SpamMethods[1]={NULL, NULL, 0, NULL};
PyObject *spammodule=Py_ InitModule("spa m", SpamMethods); //
Needs an INCREF call?
}
~PythonSpamModu leInstance()
{
PyObject_Del("s pam", SpamMethods);
}
PyObject *spam_system(Py Object *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 PythonSpamModul eInstance 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?

Jul 16 '07 #1
3 2678
dmoore <da**********@g mail.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
Jul 17 '07 #2
On Jul 16, 9:45 am, dmoore <damienlmo...@g mail.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(PyO bject *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 PythonSpamModul eInstance
{
PythonSpamModul eInstance()
{
SpamMethods[0]={"system", spam_system, METH_VARARGS,"E xecute
a shell command."};
SpamMethods[1]={NULL, NULL, 0, NULL};
PyObject *spammodule=Py_ InitModule("spa m", SpamMethods); //
Needs an INCREF call?
}
~PythonSpamModu leInstance()
{
PyObject_Del("s pam", SpamMethods);
}
PyObject *spam_system(Py Object *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 PythonSpamModul eInstance 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.

Jul 19 '07 #3
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.

Jul 19 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
2775
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 working with Python for a year or more now, but only doing simple extending in C/C++. I'm now attempting some embedding and several questions have come to mind. BTW - I'm running Windows 2000 with Python23 and VisualC++ developers
2
2862
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 embedded scripting language (a very limited one). I would like to rip it out and replace it with Python. I am thinking that this would be relatively simple since the scripting language is a very small interface between the UI and the engine --...
3
1994
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 this works fine with the core python functionality, as soon as I run a script (on the embedded system) which tries to import modules which are not in the core system, like "xml" or "re", it fails and says it cannot find the related dll (for example...
1
1905
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 questions about python embedding: 1) Is there a good text book or other resource on embedding/extending? (I find it hard to learn only by the tutorial and C/Python API from the python.org site)
1
1831
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 Python. To be more specific: My app should: 1. Parse an input file. 2. Call a script in some scripting language, to generate an output file, for example in C++. For task 2 I need to call an embedded interpreter, and also provide call backs from...
3
2044
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 one-file version of this tutorial? 2) Is there anyone that can help me with this problem? The class is attached. Thanks all. Marco
1
1853
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 shown in "Extending..." there are some things I ma not familiar with so I turn to the index in the Reference Manual, but they are not listed. For example, PyEval_CallObject and PyDict_GetAttrString. My question is this: Is the documentation out of...
6
2996
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 interface is providing a convenient scripting toolkit for the application. One example is that a user can write a python script like: player = Player() game.loadPlayer(player) player.moveTo(location)
3
1668
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??? STEPS: initialize python regsiter foo() ########### don't know how to do this run python-script "c=foo(a,b)"
1
1341
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 and http://docs.python.org/ext/ext.html. Now I want to wrap classes and functions from the associated libraries so that I can write new applications completely in Python, but I'm not entirely sure how to start because I have some problems...
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8680
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8625
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2610
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.