[C++-sig] Function handling with Boost

2009-06-21 Thread Christopher Schramm
Hi,

I'm new to Python's C API and developing my very first project using
Boost::Python (bpy). My goal is executing user defined scripts within an
embedded python environment using custom stdout and stderr and providing
a special built in module.

Beside a bunch of classes (which can be defined using bpy quite
comfortable) the module shall contain a couple of functions
and since bpy is apparently not primarily targeted at embedding and
doesn't provide a function type this is the point where all my problems
start.

My current solution is to get the global scope:

global = import("__main__").attr("__dict__");

And add my module using Py_InitModule3:

global["my_mod"] = object(handle<>(borrowed(Py_InitModule3("my_mod",
functions, "docstr";

Adding classes and there methods is a nice, clear and easy task:

global["my_mod"].attr("__dict__")["myclass"] =
class_("myclass", bpy::init<>())
.def("f1", &CPPClass::f1)
.def("f2", &CPPClass::f2);

But the functions need to be in the functions parameter of
Py_InitModule3, defined like:

PyMethodDef functions[] = {
{"f1", &f1, METH_VARARGS, "my_doc"},
{"f2", &f2, METH_VARARGS, "my_doc"},
{0, 0}
};

And now the implementations have to be PyCFunction type, i. e. have the
signature PyObject* (PyObject*, PyObject*), not a (more or less)
arbitrary one they could have when used in bpy's def(). This means the
number of arguments given to the function within the python code could
be wrong, I need to convert from PyObject to bpy types to C++ types and
all the way back and there are traps like returning temporary pointers
or memory leaks.

An implementation could look like:

PyObject* f1(PyObject* self, PyObject* args)
{
bpy::handle<> arg1, arg2;
PyArg_UnpackTuple(args, "func", 1, 2, &arg1, &arg2);

if (arg2.get()) {
...
}
...
...bpy::extract(bpy::object(arg1))...

return Py_BuildValue("s", my_cstr_result);
}

Beside the problems already mentioned this means missing bpy features
for the functions like automatic docstrings, return value policies etc.

So I would really like to know whether there's a better way to do what
I'm doing. Especially a more boost like way ;-)

Thanks for any reply!
Christopher Schramm

___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig


[C++-sig] Differences between 32-bit and 64-bit python25.lib

2009-06-21 Thread Jesse Lehrman
I'm trying to compile SIP/PyQT 4.5 for Windows Python 64-bit. I'm running into 
an error when attempting to link the SIP/PyQT object files against 
python25.lib. The errors are "can't find reference..." to symbols that should 
be in python25.lib. I tried using the 32-bit version of python25.lib and it 
worked. This led me to look at python25.lib using dumpbin.exe. I'm using the 
install binaries from www.python.org.

It seems that most of the exports in the 32-bit version have a preceding 
underscore while in the 64-bit version they don't. For example:

python25.lib 32-bit:
  _PyFloat_AsDouble
  _PyFloat_AsReprString
  _PyFloat_AsString
  _PyFloat_Fini
  _PyFloat_FromDouble
  _PyFloat_FromString
  _PyFloat_Type

python25.lib 64-bit:
  PyFloat_AsDouble
  PyFloat_AsReprString
  PyFloat_AsString
  PyFloat_Fini
  PyFloat_FromDouble
  PyFloat_FromString
  PyFloat_Type

Does anybody have an idea of why this is done and how I can get around it?

Thank you,

Jesse



Please consider the environment before printing this email.
___
Cplusplus-sig mailing list
[email protected]
http://mail.python.org/mailman/listinfo/cplusplus-sig