Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Where is PyMethod_GET_CLASS in Python 3?

25 views
Skip to first unread message

Infinity77

unread,
Dec 15, 2009, 11:08:01 AM12/15/09
to
Hi All,

When building C extensions In Python 2.X, there was a magical
PyMethod_GET_CLASS implemented like this:

#define PyMethod_GET_CLASS(meth) \
(((PyMethodObject *)meth) -> im_class)

It looks like Python 3 has wiped out the "im_class" attribute. Which
is the alternative was to handle this case in Python 3? How do I find
to which class this particular method belongs to? BTW, it's very,
very, *very* hard to find any possible reference to help migrating
existing C extensions from Python 2.X to Python 3.

Thank you for your suggestions.

Andrea.

Terry Reedy

unread,
Dec 15, 2009, 4:22:49 PM12/15/09
to pytho...@python.org
On 12/15/2009 11:08 AM, Infinity77 wrote:
> Hi All,
>
> When building C extensions In Python 2.X, there was a magical
> PyMethod_GET_CLASS implemented like this:
>
> #define PyMethod_GET_CLASS(meth) \
> (((PyMethodObject *)meth) -> im_class)
>
> It looks like Python 3 has wiped out the "im_class" attribute.

For bound methods, renamed to __class__ to be consistent with other
objects. Unbound methods were eliminated as extra cruft.

> Which
> is the alternative was to handle this case in Python 3? How do I find
> to which class this particular method belongs to?

A method (unbound) is simply a function accessed as an attribute of a
class. A function can be a method of 0 to n classes, and 'belongs' to
none of them.

Terry Jan Reedy

Infinity77

unread,
Dec 16, 2009, 3:37:41 AM12/16/09
to
Hi,

On Dec 15, 9:22 pm, Terry Reedy wrote:
> On 12/15/2009 11:08 AM, Infinity77 wrote:
>
> > Hi All,
>
> >      When building C extensions In Python 2.X, there was a magical
> > PyMethod_GET_CLASS implemented like this:
>
> > #define PyMethod_GET_CLASS(meth) \
> >    (((PyMethodObject *)meth) ->  im_class)
>
> > It looks like Python 3 has wiped out the "im_class" attribute.
>
> For bound methods, renamed to __class__ to be consistent with other
> objects. Unbound methods were eliminated as extra cruft.

First of all, thank you for your answer. However, being a complete
newbie in writing C extension, I couldn't seem to find a way to do
what I asked in the first place:

Try 1:

# define PyMethod_GET_CLASS(meth) \
(((PyMethodObject *)meth) -> __class__)

error C2039: '__class__' : is not a member of 'PyMethodObject'

Try 2:

PyObject * magicClass = method -> __class__

error C2039: '__class__' : is not a member of '_object'


I know I am doing something stupid, please be patient :-D . Any
suggestion is more than welcome.

Andrea.

Terry Reedy

unread,
Dec 16, 2009, 6:10:19 PM12/16/09
to pytho...@python.org
On 12/16/2009 3:37 AM, Infinity77 wrote:
> Hi,
>
> On Dec 15, 9:22 pm, Terry Reedy wrote:
>> On 12/15/2009 11:08 AM, Infinity77 wrote:
>>
>>> Hi All,
>>
>>> When building C extensions In Python 2.X, there was a magical
>>> PyMethod_GET_CLASS implemented like this:
>>
>>> #define PyMethod_GET_CLASS(meth) \
>>> (((PyMethodObject *)meth) -> im_class)
>>
>>> It looks like Python 3 has wiped out the "im_class" attribute.
>>
>> For bound methods, renamed to __class__ to be consistent with other
>> objects. Unbound methods were eliminated as extra cruft.

What I said is true at the Python level. At the C level, check the
appropriate .c or .h file for the structure definition. Sorry if they
are not the same.

Antoine Pitrou

unread,
Dec 17, 2009, 11:11:00 AM12/17/09
to pytho...@python.org
Le Tue, 15 Dec 2009 08:08:01 -0800, Infinity77 a écrit :
>
> When building C extensions In Python 2.X, there was a magical
> PyMethod_GET_CLASS implemented like this:
>
> #define PyMethod_GET_CLASS(meth) \
> (((PyMethodObject *)meth) -> im_class)
>
> It looks like Python 3 has wiped out the "im_class" attribute. Which is
> the alternative was to handle this case in Python 3?

First, is it a bound method? Unbound methods are just function objects in
py3k. Check that PyMethod_Check() returns true.
Second, have you tried Py_TYPE(PyMethod_GET_SELF(meth))?

> BTW, it's very, very,
> *very* hard to find any possible reference to help migrating existing C
> extensions from Python 2.X to Python 3.

There's http://docs.python.org/3.1/howto/cporting.html
You are encouraged to post any suggestions or corrections on the bug
tracker: http://bugs.python.org

Finally, there's also a dedicated mailing-list for porting to py3k:
http://mail.python.org/mailman/listinfo/python-porting
While it hasn't seen a lot of activity lately, I'm sure there are people
there willing to answer any questions you have!

Regards

Antoine.


0 new messages