473,385 Members | 1,782 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

creating simple Python scripting interfaces via C++

I have Python embedded in a C++ application (yes, yes, I know, I'd
prefer it the other way around too) and essentially need to expose some
read-only values and functions to Python so it can be used to script
the host application.

When scripting a similar app in TCL, it's possible to associate each
command with some client data, so that the command can be written in
the script as a free function but it actually executes in some sort of
context, accessed via the client data pointer in C++. In Python, there
doesn't appear to be this mechanism, so I think I'd have to inject the
context in another way, either as some sort of module-level global, or
as an object, implementing the previously free functions as methods.

Is this correct? If so, what is the simplest way of implementing the
former method - inserting the pointer to the required context as a long
(via PyDict_SetItemString(globals, "context", PyInt_FromLong(pointer))
or similar) and then converting it back in the bound function? And for
the latter method, is it possible to make an arbitrary object and then
attach methods and the context data? Or will I have to create a whole
Python class for this (as in
http://aspn.activestate.com/ASPN/Coo...Recipe/54352)?

I'm not interested in wrapping whole C++ objects at this stage, and
libraries like Boost::Python aren't currently an option. I just need a
few pointers on doing it the low-level way for now.

--
Ben Sizer

Jan 8 '07 #1
2 1342
Ok, my first attempt at this creates proxy objects in Python, and
stores a pointer to the C++ instance in the Python object. I cast that
pointer to an int and pass it as a single parameter to the object's
__init__ function.

static PyObject* Actor_init(PyObject *self, PyObject *args)
{
PyObject* selfParam;
PyObject* ptrValue;
if (!PyArg_ParseTuple(args, "OO", &selfParam, &ptrValue))
return NULL;

PyObject_SetAttrString(selfParam, "_cpp_ptr", ptrValue);

Py_INCREF(Py_None);
return Py_None;
}

I have no idea why self is always NULL, when I'm calling the functions
as methods of an object. Any ideas why this is the case? For what it's
worth I attach each method via the PyMethodDef -PyCFunction_New ->
PyMethod_New -PyDict_SetItemString(classDict) route.

To get data back from the C++ object to Python, I extract that value
and cast it back to the appropriate pointer type.

static PyObject* Actor_showName(PyObject *self, PyObject *args)
{
PyObject* selfParam;
if (!PyArg_ParseTuple(args, "O", &selfParam))
return NULL;

PyObject* cppPtr = PyObject_GetAttrString(selfParam, "_cpp_ptr");
long cppPtrVal = PyInt_AsLong(cppPtr);
Actor* pActor = reinterpret_cast<Actor*>(cppPtrVal);

// Delegate to the C++ object
pActor->ShowName();

Py_INCREF(Py_None);
return Py_None;
}

I've omitted some error checking, but this is the way I'm going for
now. Are there any glaring errors I've made (apart from perhaps
assuming sizeof(pointer) <= sizeof(long), that is)? And is there
anywhere else more appropriate that I should be asking this question,
given the lack of responses to this and my other embedding topic so
far?

--
Ben Sizer

Jan 11 '07 #2
Ben Sizer wrote: And is there anywhere else more appropriate that I
should be asking this question, given the lack of responses to this
and my other embedding topic so far? You could try asking on the C++
SIG mailing list at python.org:
http://mail.python.org/mailman/listinfo/c++-sig David

Jan 11 '07 #3

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

Similar topics

8
by: Martin Maney | last post by:
Apologies if this isn't news here - I've been too busy this last week or so for even skimming the traffic here, in part because I've been messing around with Ubuntu's preview release on a spare...
36
by: Andrea Griffini | last post by:
I did it. I proposed python as the main language for our next CAD/CAM software because I think that it has all the potential needed for it. I'm not sure yet if the decision will get through, but...
11
by: Adonis | last post by:
What I do not understand, or not clear to me I should say, is how can some people regard Python as a scripting language? In particular the JAVA crowd. Unless my understanding is off, and from what...
20
by: xeys_00 | last post by:
I posted a article earlier pertaining programming for my boss. Now I am gonna ask a question about programming for myself. I just finished my first C++ Class. Next semester is a class on...
16
by: Robert Wierschke | last post by:
hi I'm learning python since 3 days. I' ve some programming experience in BASIC, Pascal, C, C++ and Java. Actually I want to add a scripting language to this repertoire (I have virtually no...
5
by: James Owens | last post by:
I anticipate working with XML and XSL a lot over the next few years (I'm in technical documentation). I also happen to be looking for a new scripting language for some CGI interfaces I'll be...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
10
by: brooksr | last post by:
I know VB5/VBA very well but have not used VB to create web pages but need to do so. Can someone explain the purposes and differences between VBScript and VB.NET to create web pages? Also...
28
by: jmDesktop | last post by:
Studying OOP and noticed that Python does not have Interfaces. Is that correct? Is my schooling for nought on these OOP concepts if I use Python. Am I losing something if I don't use the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.