C API HowTo ? accessing objects described by dotted string from a (PyObject * )

M

Mike Hoegeman

I'm in a situation where

-- i have a PyObject * that is a instance of an object
-- i need to access a subobject of that (PyObject *) that is
described to me as a dotted string, e.g.:


"subobject1.listsubobject2[-1].subobject3"

In other words, I need to get

self.subobject1.listsubobject2[0].subobject3

where 'self' is the (PyObject *) I have in my C Program

do i have to parse the string and do the C API getattr calls myself
or is there some easier way to do it?

Thanks for any suggestions you all may have..

-mike
 
M

Martin v. =?iso-8859-15?q?L=F6wis?=

-- i have a PyObject * that is a instance of an object
-- i need to access a subobject of that (PyObject *) that is
described to me as a dotted string, e.g.:


"subobject1.listsubobject2[-1].subobject3"

In other words, I need to get

self.subobject1.listsubobject2[0].subobject3

where 'self' is the (PyObject *) I have in my C Program

do i have to parse the string and do the C API getattr calls myself
or is there some easier way to do it?

You could evaluate the string. As this is relative to an object, you
would first rephrase this as

self.subobject1.listsubobject2[-1].subobject3

Then you build a dictionary where 'self' refers to that object
(PyDict_New, PyDict_SetItemString), then you evaluate the
expression in the context of the dictionary. This, in turn,
is done by invoking Py_CompileString and PyEval_EvalCode.

Notice that executing arbitrary untrusted code is a security risk.

HTH,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top