The devel-docs tell me about METH_CLASS: The method will be passed the type object as the first parameter rather than an instance of the type. This is used to create class methods, similar to what is created when using the classmethod() built-in function. New in version 2.3. 1. This seems wrong. The first parameter the function receives is NULL, the type object goes together with the remaining arguments in the second parameter (which is a tuple). Is the documentation wrong, or is the code wrong (I didn't find any use of METH_CLASS in the CVS sources)? [I must say that I'm not really sure how this relates to the calling convention flags. Have tried METH_O and METH_VARARGS so far.] 2. Since this is marked new in 2.3: Is it planned to backport this stuff into the 2.2.x series? Requiring Python 2.3 for my stuff seems a but harsh currently... 3. While we're at it, although I could try to find out myself: How would one create a classmethod in Python 2.2? Thanks, Thomas
The devel-docs tell me about METH_CLASS:
The method will be passed the type object as the first parameter rather than an instance of the type. This is used to create class methods, similar to what is created when using the classmethod() built-in function. New in version 2.3.
1. This seems wrong. The first parameter the function receives is NULL, the type object goes together with the remaining arguments in the second parameter (which is a tuple).
Is the documentation wrong, or is the code wrong (I didn't find any use of METH_CLASS in the CVS sources)?
I'll leave this for Fred to answer, since he implemented this.
[I must say that I'm not really sure how this relates to the calling convention flags. Have tried METH_O and METH_VARARGS so far.]
2. Since this is marked new in 2.3: Is it planned to backport this stuff into the 2.2.x series? Requiring Python 2.3 for my stuff seems a but harsh currently...
I don't plan to backport this to 2.2 unless there's an overwhelming demand.
3. While we're at it, although I could try to find out myself: How would one create a classmethod in Python 2.2?
You have to call classmethod() from C; it's available as &PyClassMethod_Type, you can call it with e.g. PyObject_Call. The argument should be a function object that you created by calling PyCFunction_New. Then stick it into your type's tp_dict (after calling PyType_Ready()). I know that's complicated; that's why we added METH_CLASS. :-) I'd recommend against trying this unless you absolutely need it. --Guido van Rossum (home page: http://www.python.org/~guido/)
From: "Guido van Rossum" <guido@python.org>
The devel-docs tell me about METH_CLASS: [...] 2. Since this is marked new in 2.3: Is it planned to backport this stuff into the 2.2.x series? Requiring Python 2.3 for my stuff seems a but harsh currently...
I don't plan to backport this to 2.2 unless there's an overwhelming demand.
What do you accept as overwhelming? The only person I could think of currently beside me is David Abrahams, but maybe I'm wrong ;-)
You have to call classmethod() from C; it's available as &PyClassMethod_Type, you can call it with e.g. PyObject_Call. The argument should be a function object that you created by calling PyCFunction_New. Then stick it into your type's tp_dict (after calling PyType_Ready()).
I know that's complicated; that's why we added METH_CLASS. :-) I'd recommend against trying this unless you absolutely need it.
I need it, so I asked :-) But I'm fine with your recipe (in these cases where I don't have a custom metaclass). Thanks, Thomas
The devel-docs tell me about METH_CLASS:
The method will be passed the type object as the first parameter rather than an instance of the type. This is used to create class methods, similar to what is created when using the classmethod() built-in function. New in version 2.3.
1. This seems wrong. The first parameter the function receives is NULL, the type object goes together with the remaining arguments in the second parameter (which is a tuple).
Is the documentation wrong, or is the code wrong (I didn't find any use of METH_CLASS in the CVS sources)?
I'll leave this for Fred to answer, since he implemented this.
If there is still a choice, I would prefer that the docs are correct, and the code should be fixed. This would allow to use, for example, METH_O in the usual way: a function which receives one parameter (in addition to the type object passed implicitely). Thomas
Thomas Heller writes:
Is the documentation wrong, or is the code wrong (I didn't find any use of METH_CLASS in the CVS sources)?
There's an example in sandbox/datetime/datetime.c; search for "datetime_now".
[I must say that I'm not really sure how this relates to the calling convention flags. Have tried METH_O and METH_VARARGS so far.]
Guido van Rossum writes:
You have to call classmethod() from C; it's available as &PyClassMethod_Type, you can call it with e.g. PyObject_Call. The argument should be a function object that you created by calling PyCFunction_New. Then stick it into your type's tp_dict (after calling PyType_Ready()).
I'll note that while this is a little tedious, it's easily packaged into a helper function. -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation
From: "Fred L. Drake, Jr." <fdrake@acm.org>
Thomas Heller writes:
Is the documentation wrong, or is the code wrong (I didn't find any use of METH_CLASS in the CVS sources)?
There's an example in sandbox/datetime/datetime.c; search for "datetime_now".
Yes, it is also used (demonstrated) in Modules/xxsubtype.c. But this doesn't answer my question. I still think that the docs and the code disagree. (and also I _hope_ that the docs are correct and the code is wrong ;-) Thomas
But this doesn't answer my question. I still think that the docs and the code disagree. (and also I _hope_ that the docs are correct and the code is wrong ;-)
Fred & I had a little whiteboard session. The docs indeed disagree with the code, and we agree that the code should be fixed. This requires some work: we'll have to introduce a new kind of descriptor that wraps a PyMethodDef pointer and creates a PyCFunction in response to the tp_descr_get call with the type as self. Fred will do this. --Guido van Rossum (home page: http://www.python.org/~guido/)
Thomas Heller writes:
Yes, it is also used (demonstrated) in Modules/xxsubtype.c.
I'd describe that as an incredibly degenerate case. ;-)
But this doesn't answer my question. I still think that the docs and the code disagree. (and also I _hope_ that the docs are correct and the code is wrong ;-)
Guido & I just had a little pow-wow about this, and decided that this is worth changing. I'll make the changes to the code so that the documentation is right. Can you please file a bug report on this so I don't lose track of it? It should be assigned to me, priority 6. (So that I actually need to review your distutils patch first. ;) Thanks for reading the development docs and bringing this up! -Fred -- Fred L. Drake, Jr. <fdrake at acm.org> PythonLabs at Zope Corporation
From: "Guido van Rossum" <guido@python.org>
But this doesn't answer my question. I still think that the docs and the code disagree. (and also I _hope_ that the docs are correct and the code is wrong ;-)
Fred & I had a little whiteboard session. The docs indeed disagree with the code, and we agree that the code should be fixed. This requires some work: we'll have to introduce a new kind of descriptor that wraps a PyMethodDef pointer and creates a PyCFunction in response to the tp_descr_get call with the type as self. Fred will do this.
Great. I filed a bug report http://www.python.org/sf/548651. From: "Fred L. Drake, Jr." <fdrake@acm.org>
Thanks for reading the development docs and bringing this up!
I do this for pure egoistic reasons ;-) Thomas
participants (3)
-
Fred L. Drake, Jr.
-
Guido van Rossum
-
Thomas Heller