Web Images Videos Maps News Shopping Gmail more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Documentation bugs in 3.1 - C-API - TypeObjects
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  6 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
DreiJane  
View profile  
 More options Nov 14, 11:04 am
Newsgroups: comp.lang.python
Followup-To: comp.lang.python
From: DreiJane <jo...@h-labahn.de>
Date: Sat, 14 Nov 2009 08:04:32 -0800 (PST)
Local: Sat, Nov 14 2009 11:04 am
Subject: Documentation bugs in 3.1 - C-API - TypeObjects
Hello,

this page http://docs.python.org/3.1/c-api/typeobj.html has a bad
error:

"
PyTypeObject* PyObject.ob_type

This is the type’s type, in other words its metatype. It is
initialized by the argument to the PyObject_HEAD_INIT macro, and its
value should normally be &PyType_Type. However, for dynamically
loadable extension modules that must be usable on Windows (at least),
the compiler complains that this is not a valid initializer.
Therefore, the convention is to pass NULL to the PyObject_HEAD_INIT
macro and to initialize this field explicitly at the start of the
module’s initialization function, before doing anything else. This is
typically done like this:

Foo_Type.ob_type = &PyType_Type;
"

This cannot work, because Foo_Type is no PyObject but a PyVarObject
(independent
of the use of PyVarObject_HEAD_INIT or PyObject_HEAD_INIT). The code
line would
work so:

((PyObject *)&Foo_Type)->ob_type = &PyType_Type

But in the Tutorial for Extensions and Embedding we are advised as
follows:
"
This is so important that we’re going to pick the top of it apart
still further:
          PyVarObject_HEAD_INIT(NULL, 0)
This line is a bit of a wart; what we’d like to write is:
          PyVarObject_HEAD_INIT(&PyType_Type, 0)
as the type of a type object is “type”, but this isn’t strictly
conforming C and some compilers complain. Fortunately, this member
will be filled in for us by PyType_Ready().
"

What now ?

Another problem, which might be a documentation bug, is the last
sentence here:
"
destructor PyTypeObject.tp_dealloc

A pointer to the instance destructor function. This function must be
defined unless the type guarantees that its instances will never be
deallocated (as is the case for the singletons None and Ellipsis).

The destructor function is called by the Py_DECREF() and Py_XDECREF()
macros when the new reference count is zero. At this point, the
instance is still in existence, but there are no references to it. The
destructor function should free all references which the instance
owns, free all memory buffers owned by the instance (using the freeing
function corresponding to the allocation function used to allocate the
buffer), and finally (as its last action) call the type’s tp_free
function. If the type is not subtypable (doesn’t have the
Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the
object deallocator directly instead of via tp_free.
"

What ? Where do we "call" these methods ? Primarily we write them down
to get members
of the Foo_Type struct and our question is where. Shall this sentence
mean, that we
write the function, we'd normally use for tp_dealloc und which behaves
like it, to the place of tp_free ?

I've run into terrible trouble with extension classes, which have
neither members nor init.
They work (and i am quite happy about that) but finding out, that
tp_dictoffset had to be
set (for what i wanted) took more than a day of searching - including
öecture of typeobject.c and object.c - and i cannot derive from them.

class(my_extension_class): ... doesn't crash, but results in objects,
which have the type
my_extension_class, what means, that they do not call their __init__.
In certain
circumstances a correct tp_free seems to be a premise for inheritance,
thus i'd very like
to understand the quoted passage.

Joost


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options Nov 14, 8:53 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Sun, 15 Nov 2009 02:53:29 +0100
Local: Sat, Nov 14 2009 8:53 pm
Subject: Re: Documentation bugs in 3.1 - C-API - TypeObjects

> This cannot work, because Foo_Type is no PyObject but a PyVarObject
> (independent
> of the use of PyVarObject_HEAD_INIT or PyObject_HEAD_INIT). The code
> line would
> work so:

> ((PyObject *)&Foo_Type)->ob_type = &PyType_Type

However, this is not what you should use. Instead, use

Py_Type(&Foo_Type) = &PyType_Type

> If the type is not subtypable (doesn’t have the
> Py_TPFLAGS_BASETYPE flag bit set), it is permissible to call the
> object deallocator directly instead of via tp_free.
> "

> What ? Where do we "call" these methods ?

You should typically call tp_free inside of tp_dealloc. For example,
string_dealloc ends with

       Py_TYPE(op)->tp_free(op);

In the specific case (Py_TYPE(op) is not subtypeable), you could
alternatively also call

       PyObject_Del(op);

If there are subclasses, they might have used a different allocator
(rather than the object allocator), hence you must call the deallocator
through tp_free.

HTH,
Martin


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DreiJane  
View profile  
 More options Nov 15, 1:25 am
Newsgroups: comp.lang.python
From: DreiJane <jo...@h-labahn.de>
Date: Sat, 14 Nov 2009 22:25:38 -0800 (PST)
Local: Sun, Nov 15 2009 1:25 am
Subject: Re: Documentation bugs in 3.1 - C-API - TypeObjects
Thanks !

Okay, i've already used the call of tp_free as the last
statement in tp_dealloc and do understand now, that a
call of tp_dealloc should be the last statement in the
code for tp_free in specific cases.

And yes, "Py_Type(&Foo_Type) = &PyType_Type" will be
more stable against changes of the object implementation.
Still there remains the difference to what is told with the
Noddy_Type in the tutorial.

Skimmimg through PyType_Ready in typeobject.c i find,
that

3760     if (Py_TYPE(type) == NULL && base != NULL)
3761              Py_TYPE(type) = Py_TYPE(base);

are the only lines referring to what is ob_type now. Thus
the quoted lines from the tutorial ending with "Fortunately,
this member will be filled in for us by PyType_Ready()."
are possibly wrong (respective outdated) too.

The two lines above are, what happens to my extension
classes, if i want to derive application classes from them.

class(my_extension_class): ...

will of course call more from Python than PyTypeReady,
but PyType(type) = Py_TYPE(base) is not getting corrected
by this "more" here and the class statement doesn't create
a new type. I will examine now, why this happens (without
a crash of the calling application !), but still welcome every
hint.  Seen from a strict perspective this is a bug in Python's
C-API. I'll save a freeze of the code for my extension for
anyone, who might interested to reproduce this.

Joost


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
DreiJane  
View profile  
 More options Nov 15, 1:21 pm
Newsgroups: comp.lang.python
From: DreiJane <jo...@h-labahn.de>
Date: Sun, 15 Nov 2009 10:21:30 -0800 (PST)
Local: Sun, Nov 15 2009 1:21 pm
Subject: Re: Documentation bugs in 3.1 - C-API - TypeObjects
Thanks a second time - the picture has
gotten clearer indeed. But for third-party
readers the complexities of this matter
require the correction, that

"Py_Type(&Foo_Type) = &PyType_Type"

must be:
"Py_TYPE(&Foo_Type) = &PyType_Type "

- or am i completely wrong ? Joost


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options Nov 15, 2:35 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Sun, 15 Nov 2009 20:35:23 +0100
Local: Sun, Nov 15 2009 2:35 pm
Subject: Re: Documentation bugs in 3.1 - C-API - TypeObjects

> Still there remains the difference to what is told with the
> Noddy_Type in the tutorial.

Please report that to bugs.python.org.

Regards,
Martin


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Martin v. Löwis  
View profile  
 More options Nov 15, 2:36 pm
Newsgroups: comp.lang.python
From: "Martin v. Löwis" <mar...@v.loewis.de>
Date: Sun, 15 Nov 2009 20:36:10 +0100
Local: Sun, Nov 15 2009 2:36 pm
Subject: Re: Documentation bugs in 3.1 - C-API - TypeObjects

DreiJane wrote:
> Thanks a second time - the picture has
> gotten clearer indeed. But for third-party
> readers the complexities of this matter
> require the correction, that

> "Py_Type(&Foo_Type) = &PyType_Type"

> must be:
> "Py_TYPE(&Foo_Type) = &PyType_Type "

> - or am i completely wrong ? Joost

No, you are right - I forgot that the spelling had been
changed to upper case.

Regards,
Martin


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google