Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Type allocation in extensions

Reply
Thread Tools

Type allocation in extensions

 
 
Nicholas Milkovits
Guest
Posts: n/a
 
      03-27-2007
Hi everyone,

I've been reading through the documentation on extending and embedding
python and the C API and I have a question about how allocation occurs
of one type from another type. For example lets so I make to C module
foo.c and bar.c and each has a python type. If I want to define a
method in foo.c that will return and new bar object how would I go
about doing that. Do I need to initialize tp_call and tp_alloc in
order to use PyObject_Call()? Also, If I do not supply an allocfunc
but instead initialize it to 0 when I declare my PyTypeObject for foo
does it automatically get set to a generic allocation function?

For example:

In python I want to be able to write:

f = Foo.new()
b = foo.bar()


bar.c

static PyTypeObject BarType = {
PyObject_HEAD_INIT(NULL)
0, // ob_size
"bar", // tp_name
sizeof(bar), // tp_basicsize
0, // tp_itemsize
(destructor) Bar_Free, // tp_dealloc
.........snip...........
0, //tp_call
.........snip...........
(initproc) Bar_Init, // tp_init
0, // tp_alloc
Bar_New, // tp_new
0, // tp_free


static PyObject *Bar_New(PyTypeObject *type, PyObject *args, PyObject
keywordArgs)
{
// How does this call work if I never set an allocfunc pointer when I
// declared the bar type
return= type->tp_alloc(type, 0);
}


foo.c

// Is PyObject_Call what I want to use and if so
// how does it work if tp_call was initialized to 0
// or not even specified in my BarType variable?
static PyObject *Foo_NewBar(foo *self, PyObject *args)
{
PyObject *createArgs, *bar_ref;

createArgs = PyTuple_New();
if (!createArgs)
return NULL;
Py_INCREF(self);
bar_ref = PyObject_Call( (PyObject*) &BarType, createArgs, NULL);
Py_DECREF(createArgs);
return bar_ref;
}

Thanks in advace for the help,
Nick
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
An idea for heap allocation at near stack allocation speed Bjarke Hammersholt Roune C++ 14 03-06-2011 08:07 AM
static memory allocation versus dynamic memory allocation Ken C Programming 24 11-30-2006 12:37 AM
Unable to access extensions menu or add extensions (I've tried past suggestions) nospam@spam.com Firefox 8 12-01-2005 01:33 AM
What is the difference between dynamic memory allocation,and stack allocation ? chris C++ 6 10-28-2005 05:27 AM
The new allocation scheme and extensions Shu-yu Guo Ruby 5 11-07-2003 03:31 AM



Advertisments