473,394 Members | 1,774 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

_PyObject_New / PyObject_Init / PyInstance_New / etc?

(My apologies if this appears twice. It did not post the first time.)

I'm so confuzzled! How do I instantiate my new C Python object from C?

After flailing helplessly with my own code, and searching tirelessly with
Google, I stepped back to the classic noddy2.c example in the Python help
files and tried to modify it to not just define a Noddy object, but to
instantiate one as well. I'm not having any luck.

In noddy2.c, I added some printf statements just after the beginning of the
Noddy_dealloc, Noddy_new, and Noddy_init. I compiled noddy2.c and when
called from Python, it does exactly what I would expect:
>>import noddy2
n=noddy2.Noddy()
Noddy_new
Noddy_init
>>^Z
Noddy_dealloc

Perfect! Now I wanted to create a Noddy() object from C. The simplest way
to do this was to stick it in the Noddy_name function, since that was easily
called. I tried inserting some code with _PyObject_New and PyObject_Init:

static PyObject *
Noddy_name(Noddy* self)
{
static PyObject *format = NULL;
PyObject *args, *result;
printf("Noddy_name\n");
args=_PyObject_New(&NoddyType);
printf("%p\n", args);
PyObject_Init(args, &NoddyType);
printf("init done\n");
Py_DECREF(args);
printf("dec done\n");
.... (I left all the original Noddy_name code here) ...

Then I compiled, went into the Python interpreter and tried it. I would
have expected Noddy_name to create and destroy a Noddy object just like
noddy2.Noddy() does from the interpreter, but it doesn't:
>>import noddy2
n=noddy2.Noddy()
Noddy_new
Noddy_init
>>n.name()
Noddy_name
00B1A1B8
init done
Noddy_dealloc

As you can see, calling the name function did my printf of Noddy_name, and
then _PyObject_New returned a pointer to a Python object, but I don't think
it really is a Noddy object (not entirely at least). After all, Noddy_new
and Noddy_init are never called! The funny thing is that the Py_DECREF
destroys a Noddy object (you can see that Noddy_dealloc got called), but now
something is out of sync. since the construction didn't happen as expected
and Python crashes hard.

I've tried this as above, and with PyInstance_New, with PyObject_New (no
underscore), and PyObject_Call, but none of them work as I would expect. So
what is the *CORRECT* way to do this? Obviously I'm neglecting something
important.

My modified noddy2.c in case anyone wants to try it:
http://pastie.textmate.org/62901

Many thanks to anyone who can point me in the correct direction!

Gre7g
May 19 '07 #1
2 4777
En Sat, 19 May 2007 10:54:32 -0300, Gre7g Luterman <ha******@yahoo.com>
escribió:
I'm so confuzzled! How do I instantiate my new C Python object from C?
I tried inserting some code with _PyObject_New and PyObject_Init:

Then I compiled, went into the Python interpreter and tried it. I would
have expected Noddy_name to create and destroy a Noddy object just like
noddy2.Noddy() does from the interpreter, but it doesn't:
From Python, you create a Noddy object by *calling* its type. Do the same
in C:

return PyObject_CallObject((PyObject *) &NoddyType, NULL);

Or any other suitable variant of PyObject_CallXXX. (I've answered this
same question yesterday, when I was not sure about this; then I've tried
it and it appears to be working. But I've not read any docs telling this
is *the* right way to create an object).
I've tried this as above, and with PyInstance_New, with PyObject_New (no
underscore), and PyObject_Call, but none of them work as I would expect.
So
what is the *CORRECT* way to do this? Obviously I'm neglecting something
important.
PyInstance_New is for old style classes. PyObject_New only initializes the
object structure, and this is enough for most builtin types that usually
don't define __init__ (or tp_init). You were right with PyObject_Call, but
maybe you didn't invoke it correctly.

--
Gabriel Genellina

May 19 '07 #2
From Python, you create a Noddy object by *calling* its type. Do the same
in C:

return PyObject_CallObject((PyObject *) &NoddyType, NULL);

Or any other suitable variant of PyObject_CallXXX. (I've answered this
same question yesterday, when I was not sure about this; then I've tried
it and it appears to be working. But I've not read any docs telling this
is *the* right way to create an object).
Many thanks. That seems to work now.

It turns out that my previous problems stemmed from defining my type as a
static, so I was ending up with multiple copies of it and of course only one
type was getting fully initialized when the module was launched!
May 20 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike S. | last post by:
Hello, Has anyone had success compiling the informixdb-1.3 module under python 2.2? It seems the absense of makefile.pre.in in python 2.2 causes the break under py2.2. Is there an easy way...
2
by: mike | last post by:
Hi All, I have some "new-style" classes written in Python that I would like to embed inside a C++ application (somewhat grudgingly - if I had my way, the whole thing would be written in Python)....
0
by: Jeffrey Holle | last post by:
I am working on a Linux system using gcc v3.3 and python v2.3.2 I have a problem linking a cpp program to a shared library that uses the Python C API. With just referencing this shared library, I...
0
by: Ian Boisvert | last post by:
I'm trying to use PyInstance_New inside of a class factory to create instances of a class that is defined in Python. I first process some Python script that contains my class definition using the...
4
by: Bue Krogh Vedel-Larsen | last post by:
How do I delete a class instance created using PyInstance_New? I've tried calling Py_CLEAR on the instance, but __del__ isn't called. I've also tried calling PyObject_Del, but this gives an access...
1
by: J | last post by:
Hi everyone, Thanks for your awsome replies. I started replacing Javascript with python 2 days ago and I already have my first scripts running. Mainly due to the wealth of information in this...
3
by: J | last post by:
Hi, I am trying to add new data attributes to my extension classes from within a script. I am under the impression that python allows that implicity This is the definition of my class
2
by: Jeremy Moles | last post by:
Hey guys, sorry to ask another question of this nature, but I can't find the answer or a single example of it anywhere. I'm sure it's been asked before, but my google-fu isn't strong enough to find...
1
by: Jeremy Moles | last post by:
First of all, let me say I really do appreciate--and frequently use--the ample and easy to read Python documentation. However, there are a few things I'm still unclear on, even after asking...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.