473,395 Members | 1,639 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,395 software developers and data experts.

Newstyle Types Confounding!

I've been spending the last few night upgrading some 'classic' type
objects to the more modern 'newstyle' types. Unless I am not finding
some important page, this entire process is almost entirely
undocumented. I've found the "Noddy" example, which was a helping start.
But Noddy only gets you halfway there, and where it leaves off there is
no further assistance.

I've been using the builtin Python types source as example, but this
isn't answering most of my larger problems. Especially as the builtin
types seem inconsistent in how they do thing. I have a few questions and
am hoping to get a little guidance from those who understand this
process better.
First, I have found no comfortable way to create new type instances from
the C api. My types are wrappers for existing C structures. Originally
my code called PyObject_New and filled out the structure data. This
appears to be invalid with newstyle types, but I cannot discover the new
method. It appears there is no standard since all the builtin types do
this differently. I've ended up with something like this..

self = (PyMineObject*)PyMine_Type.tp_new(&PyMine_Type, NULL, NULL);

Can this be serious? This is the first time I've ever had to dig into
the big Type structure to access function pointers. I find this to be
disturbing, but I can learn to accept it if someone confirms this is the
way to allocate newstyle type instances.
My second problem has to do with weakrefs. Again I cannot locate actual
documentation on this, and the Noddy example ends before discussing
weakrefs. I've created the PyObject* weakreflist in my structure, set
the offset in tp_weaklistoffset, and initialized it to NULL. It took
many hours to figure out how this all works, mainly referring to
funcobject.c for examples. I believe I have this correct now, but I have
a lot of unexplained crashes that i suspect have to do with the weakref
list handling.
My third question comes to creating types that are inherited from other
types. I need the new subtype to add several of its own data fields to
its own structure. There are no examples of this in the Python source,
and there is no documentation referring to this. How can my new type
instances access structure data from its base. My only solution so far
is to duplicate all the structure members from the parent into the
child? Then duplicate all the code from the parent into the child. At
this point my child is actually a standalone type, except it refers to
the parent in tp_base.
Creating custom types in Python has become very difficult, if you intend
to support all the recent features of Python. Adding some documentation
and/or simple examples would go a long way towards a solution. I'm
wondering if some thought should be put into simplifying the interface.
Perhaps more macros and generic functions to help manage the new
features. It shocks me to find nearly every single "PyXXX_FromXXX"
implements object allocation in entirely independent ways, and none of
the methods seem very clean to me.

At this point I'm really shooting into the dark. I assume I'll have it
right when things stop crashing.

If I were to collect a narrowed, specific list of problem topics, would
the documentation team be interested in looking?
Jul 18 '05 #1
0 1154

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

Similar topics

2
by: Daniel Bickett | last post by:
I was reading the "Pickling and inheritance are making me hurt" thread, and the latest suggestion (as of this posting) was to do with the __setstate__ and __getstate__ methods. They caught my...
3
by: John Dibling | last post by:
Could somebody please direct me to a location in the standard where POD types are defined? That is, where in the standard is it defined what attributes a POD type has that a non-POD hasn't? Also,...
8
by: Shailesh | last post by:
One problem I've been wrestling with for a long time is how to use the C++ integral data types, vis-a-vis their size. The C++ rules guarantee that a char is at least 1 bytes, a short and int at...
8
by: Martin Stettner | last post by:
Hi, I would like to use covariant return types in mutual dependent classes like: class IB; class IA { virtual IB* getIB() = 0; }; class IB{ virtual IA* getIA() = 0;
188
by: infobahn | last post by:
printf("%p\n", (void *)0); /* UB, or not? Please explain your answer. */
5
by: Zach | last post by:
When it is being said that, "value types are created on the stack or inline as part of an object". If a value type is created in an object, and that object is being called, the value type in that...
58
by: jacob navia | last post by:
Hi people I have been working again in my tutorial, and I have finished the "types" chapter. If you feel like "jacob bashing" this is the occasion! I am asking for criticisms, or for things I may...
14
by: Lane Straatman | last post by:
I would like to write a 'struct'. I have a library that is all but completely inappropriate for this task. So I'm looking for C code that fills in the gaps between: #undef...
159
by: Bob Timpkinson | last post by:
Hi, I have a 32-bit machine... Is there anyway I can get gcc to use the following integer sizes? char: 8 bits short: 16 bits int: 32 bits long: 64 bits long long: 128 bits
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.