Hi,
As a beginner, I have a question about passing a class instance from Python script to my C code.
What I am doing is basically creating a class in the script:
class dbvalue:
def __init__(self, index, type):
self.id = index # unsigned integer
self.type = type # string
last_roi = dbvalue(id, type);
bmdl_batch.set_input_from_db(1, last_roi); <--------
I would like to call my C function with last_roi and able to recover id and type in the code. The C part is follows:
PyObject *set_input_from_db(PyObject* self, PyObject *args)
{
unsigned input;
PyObject *obj;
if (!PyArg_ParseArg(args, "iO:set_input_from_db", &input, &obj))
return NULL;
....
}
I am not sure if PyArg_ParseArg() is the right method to get the object. I used "O:.." to get it but after that, how do I acces index and type fields of the object. Do I create an equivalent class/struct in C code to? Again, all I want is pass the 2 values together in an object and able to recover then in C code.
Thanks,
Gamze
Hi all,
thanks for the numerous replies to my previous questions.
Everything in C works fine so far, but there is a very strange behaviour
in python after visiting the C-Function.
The return value of my C-Function is a list of floats. I can print that
list, I can do:
a=0
while a<len(list):
print list[a]
a=a+1
works fine, but if try to do
for element in list:
print element
It does that as well, but as soon as the end of the list is reached, I
do get an IndexError: list assignment index is out of range.
Surprisingly I do get that as well with any list, where I try to use the
for loop after using the C-function. So if I use an arbitrary list like
ll=[1,2,3]
for l in ll:
print l
I do get the same error, and I don't before calling the C-function...
Does that sound familiar to anybody? Is that caused by one of the
decref/incref things?
Kind regards
Philipp
when the program run as exe, everything goes right, but if i compile the program as a .so lib, use other program to load the .so lib and run, it goes wrong,in the lib i also use PyRun_SimpleString("import base64"); it goes wrong.....my python version is 2.3, i want to know what's the problem...... PyErr_Print() print the error info
below:
Traceback (most recent call last):
File "./myModule111.py", line 3, in ?
import base64
File "/usr/lib/python2.3/base64.py", line 7, in ?
import binascii
ImportError: /usr/lib/python2.3/lib-dynload/binascii.so: undefined symbol: PyExc_TypeError
i check the binascii.so, ldd -r binascii.so, this lib really has many undefined symbols include PyExc_TypeError, but the problem is when the program is executable, it can goes fine......
when i compile the program as .so lib, i add -Xlinker -export-dynamic, it doesn't solve the problem.........
******************************************************************************************
This email and its attachments contain confidential information from HUAWEI, which is intended only for the person or entity whose address is listed above. Any use of the information contained here in any way (including, but not limited to, total or partial disclosure, reproduction, or dissemination) by persons other than the intended recipient(s) is prohibited. If you receive this email in error, please notify the sender by phone or email
immediately and delete it!
*****************************************************************************************
Hi,
I would like to use a C program which manipulates table
as a python module acting on a list.
My C code is the following:
int sum(int *tab, int n)
{
int i, s = 0;
for (i=0; i<n; i++)
{
s += tab[i];
}
return s;
}
Now, I would like to use it as a python module
where tab would be a python list.
I read some examples in the documentation but
the size of the list is always assumed to be known
such that one can use PyArg_ParseTuple
with a format description for each argument.
I would like that my module behave like:
a=[1,2,3]
b=[2,8,20]
print my_module.sum(a) -> 6
print my_module.sum(b) -> 30
My question is:
which option should I use with PyArg_ParseTuple?
I'm totaly lost with all its option (O, O&, O!, etc)
Thx,
Marc.
Hi all,
I am trying to return an array from C to Python. Since it is of variable
size,
return Py_BuildValue("[f,f,f]", array[0], array[1], array[2]);
does not help. How do I do that?
Any help is appreciated,
kind regards
Philipp
Hi all,
Currently we have a python/c hybrid which works well and compiles against
2.3 and up nicely, and runs sensibly against all of these, however we still
need to compile directly against the correct python library before
distribution.
The question is, do any of you know a sensible way of compiling and
opening the python libraries such that, so long as the end user has at
least python2.3 libs (and compatible apis), that it will automagically just
run. This is primarily to avoid end users having multiple versions
of python on there systems, and to aid us with only having to build and
test one exectuable.
All suggestions greatly welcomed as we're struggling to find an elegant
solution to this.
cheers,
paddy
--
----------( I am an idiot )----------
paddy ----( )---- yddap
Htag.pl 0.0.23
recently I have been experimenting with a new api for blender3d, one
of the things it needs to be able to do is give python errors
(tracebacks) to our own error reporting api.
The problem is that Python/C api has no way to get the text that
PyErr_Print() prints, as a string.
This seems not such an unusual thing for an api to require, after all,
many applications dont run with a console to display text.
So far I think there are 3 ways to do this.
* Create a pipe, open it with fdopen(), then temporarily replace
stderr and stdout and call PyErr_Print().... then get the text from
that pipe. I havnt tried this yet and I'm not sure if its a good cross
platform solution.
* Copy and paste Pythons C traceback function into my own function
that builds a string rather then printing it. This really isnt an
attractive option.
* Use pythons traceback module from C (this isnt so bad, but Id prefer
not to have core parts of our api relying on *.py modules)
Is there some other way to do this? or is anyone currently doing this
in their application?
Here is the Python 3.x C api code for getting the traceback.
-------------------- snip
static void pyop_error_report(ReportList *reports)
{
/*
# in python this would be...
import traceback
string = ' '.join(traceback.format_exc())
*/
PyObject *type, *value, *traceback;
PyObject *mod;
PyObject *ret, *list, *string, *args;
/* Save the current exception */
PyErr_Fetch(&type, &value, &traceback);
mod = PyImport_ImportModule("traceback");
if (!mod) {
/* print some error */
return;
}
list= PyObject_CallMethod(mod, "format_exception", "OOO", type,
value, traceback);
string= PyUnicode_FromString("\n");
ret= PyUnicode_Join(string, list);
Py_DECREF(list);
Py_DECREF(string);
BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(ret));
Py_DECREF(ret);
PyErr_Clear();
}
--------------------
--
- Campbell