473,408 Members | 2,839 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,408 software developers and data experts.

Creating a Python Type in C - tp_init and tp_new

I'm creating a type in a C function as follows:

static PyObject *Receive(PyObject *self, PyObject *args) {
pyMessageObject *msgobj = PyObject_New(pyMessageObject,
&pyMessageType);
return (PyObject *)msgobj;
}

I have (some lines omitted):

static PyTypeObject pyMessageType =
{
PyObject_HEAD_INIT(NULL)
...
pyMessage_Init, /*tp_init*/
0, /*tp_alloc*/
pyMessage_New, /*tp_new*/
};

I have noticed that pyMessage_New and pyMessage_Init are not called.
However if the type is created in Python then they are called. Why is
this and how can I solve it?

Thanks, Andy
(please reply to the newsgroup only - thanks)

May 17 '07 #1
1 2772
En Thu, 17 May 2007 16:10:54 -0300, <aj****@gmail.comescribió:
I'm creating a type in a C function as follows:

static PyObject *Receive(PyObject *self, PyObject *args) {
pyMessageObject *msgobj = PyObject_New(pyMessageObject,
&pyMessageType);
return (PyObject *)msgobj;
}

I have (some lines omitted):

static PyTypeObject pyMessageType =
{
PyObject_HEAD_INIT(NULL)
...
pyMessage_Init, /*tp_init*/
0, /*tp_alloc*/
pyMessage_New, /*tp_new*/
};

I have noticed that pyMessage_New and pyMessage_Init are not called.
However if the type is created in Python then they are called. Why is
this and how can I solve it?
I think tp_new and tp_init are used when you create an instance by calling
the type (in Python would be your_type())
At least it's in type's tp_call (type_call in typeobject.c) where __new__
and __init__ are checked and processed.
So I think you should create your object using
PyObject_CallObject(pyMessageType, NULL) but I'm not sure... just try and
post your results! Or perhaps there is another way, I don't know.

--
Gabriel Genellina

May 18 '07 #2

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

Similar topics

7
by: Boris Boutillier | last post by:
Hi all, Of what I heard Python 2.3 now allow creation of new types in C with slots. Is there somewhere some documentation on this, or at least an example ? Thanks a lot Boris
5
by: Dan Williams | last post by:
Hi people I understand that Python is strongly, but also dynamically typed. However, I need to create a variable which initially isn't actually set to anything, but still needs to be of a...
0
by: Heiko Wundram | last post by:
Hi all! Just a small question concerning type creation in a C object: If I create a class which is overridable, I'd like to have that class create objects of it's overridden type, e.g. I have a...
0
by: Neal D. Becker | last post by:
I'm using boost::python to transform various C++ classes and functions to python. It is all working quite well, but now I'm having trouble with documentation tools. I hope someone can tell me...
2
by: rdsteph | last post by:
Python411 is a series of podcasts about Python, aimed at hobbyists and others who are learning Python. Each episode focuses on one aspect of learning Python, or one kind of Python programming, and...
2
by: BranoZ | last post by:
I'm writing my own (list-like) type in C. It is implementing a Sequence Protocol. In 'sq_slice' method I would like to return a new instance of my class/type. How do I create (and initialize) an...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
2
by: Jeff Rush | last post by:
While I have a reasonable understanding of the differences in new-style versus old-style classes, tonight while working a C extension module I realized I don't know how to indicate which style my C...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 408 open ( -9) / 3585 closed (+20) / 3993 total (+11) Bugs : 968 open ( +8) / 6505 closed ( +7) / 7473 total (+15) RFE : 267 open...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.