How do I create a vanilla object in C?

Discussion in 'Python' started by rnichols@mightyheave.com, Nov 21, 2009.

  1. Guest

    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.
    , Nov 21, 2009
    #1
    1. Advertising

  2. Carl Banks Guest

    On Nov 21, 12:49 pm, 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
    Carl Banks, Nov 22, 2009
    #2
    1. Advertising

  3. sturlamolden Guest

    On 22 Nov, 04:05, Carl Banks <> 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
    sturlamolden, Nov 22, 2009
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. VisionSet

    Custom or vanilla Collection/Map?

    VisionSet, Sep 15, 2003, in forum: Java
    Replies:
    3
    Views:
    377
    Harald Hein
    Sep 15, 2003
  2. VisionSet
    Replies:
    1
    Views:
    321
    Roedy Green
    Jul 3, 2004
  3. Art
    Replies:
    3
    Views:
    459
  4. Neil Benn

    Vanilla python path

    Neil Benn, Jul 25, 2005, in forum: Python
    Replies:
    0
    Views:
    348
    Neil Benn
    Jul 25, 2005
  5. markritter150

    Vanilla Ice is back!

    markritter150, Jun 3, 2008, in forum: C Programming
    Replies:
    6
    Views:
    304
Loading...

Share This Page