Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   How do I create a vanilla object in C? (http://www.velocityreviews.com/forums/t706098-how-do-i-create-a-vanilla-object-in-c.html)

rnichols@mightyheave.com 11-21-2009 08:49 PM

How do I create a vanilla object in C?
 
I need to create a vanilla object in C, something I can do
PyObject_SetAttr on. Obviously, I do something somewhat like this every
time I create a minimal python class. I need to know how to do this in C
though.

Carl Banks 11-22-2009 03:05 AM

Re: How do I create a vanilla object in C?
 
On Nov 21, 12:49*pm, rnich...@mightyheave.com wrote:
> I need to create a vanilla object in C, something I can do
> PyObject_SetAttr on. *Obviously, I do something somewhat like this every
> time I create a minimal python class. *I need to know how to do this in C
> though.



First of all, if you don't need to share this object with any Python
code, then I'd suggest you just use a dict (create with PyDict_New).

If you do need to share it with Python, one thing to consider is
whether it isn't easier to create the object in Python and pass it to
the C code.

If these suggestions aren't suitable, then you'll have to create a new
vanilla type, then create an instance of that type. Unfortunately
it's not too convenient from C. It's easy enough to define types and
C (see the C-API documentation), but in order to use PyObject_SetAttr
on it you'll have to make sure the type has a dictionary, so you'd
have to set the tp_dictoffset member to the offset of your dict in the
Python structure.

Another thing you can do is to call type directly (as you can do in
Python) with the name, bases, dict, as in the following example (error
checking and reference counting omitted):

name = PyString_FromString("vanilla");
bases = PyTuple_New(0);
dict = PyDict_New();
vanilla_type = PyObject_CallObject(
&PyType_Type,name,bases,dict,0);

Then call the vanilla type (however you create it) to get a vanilla
object:

vanilla_object = PyObject_CallObject(vanilla_type,0);


Definitely not the most straightforward thing to do from C.


Carl Banks

sturlamolden 11-22-2009 09:57 AM

Re: How do I create a vanilla object in C?
 
On 22 Nov, 04:05, Carl Banks <pavlovevide...@gmail.com> wrote:

> name = PyString_FromString("vanilla");
> bases = PyTuple_New(0);
> dict = PyDict_New();
> vanilla_type = PyObject_CallObject(
> * * * * &PyType_Type,name,bases,dict,0);
>
> Then call the vanilla type (however you create it) to get a vanilla
> object:
>
> vanilla_object = PyObject_CallObject(vanilla_type,0);
>
> Definitely not the most straightforward thing to do from C.



It is much easier to do this from Cython.

cdef class foo:
# fields stored in C struct
pass

class foo:
# fields stored in Python dict
pass

The let the Cython compiler generate the C code you need.

http://docs.cython.org/src/tutorial/cdef_classes.html




















All times are GMT. The time now is 01:49 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.