Finding the module from PyTypeObject?
Is there any way to (reliably) find the module that defined the class represented by a given PyTypeObject in C? -- Nick
Nick Bastin <nbastin@opnet.com> writes:
Is there any way to (reliably) find the module that defined the class represented by a given PyTypeObject in C?
Not especially appropriate for python-dev... I think the answer depends on what you mean by "reliably". __module__ is a good first bet, but can be defeated with sufficient malice (or mere inattention, in the case of types defined by C). Cheers, mwh -- ... the U.S. Department of Transportation today disclosed that its agents have recently cleared airport security checkpoints with an M1 tank, a beluga whale, and a fully active South American volcano. -- http://www.satirewire.com/news/march02/screeners.shtml
On Sep 29, 2004, at 12:56 PM, Michael Hudson wrote:
Nick Bastin <nbastin@opnet.com> writes:
Is there any way to (reliably) find the module that defined the class represented by a given PyTypeObject in C?
Not especially appropriate for python-dev...
I think the answer depends on what you mean by "reliably". __module__ is a good first bet, but can be defeated with sufficient malice (or mere inattention, in the case of types defined by C).
Ok, maybe more appropriately, what do people think of adding a PyType_GetModule (PyTypeObject *) which basically functions like type_module(PyTypeObject *, void *) (in Objects/typeobject.c) to the public C API, rather than having to dig around in the object themselves? -- Nick
Hello Nick, On Wed, Sep 29, 2004 at 01:24:23PM -0400, Nick Bastin wrote:
Ok, maybe more appropriately, what do people think of adding a PyType_GetModule (PyTypeObject *) which basically functions like type_module(PyTypeObject *, void *) (in Objects/typeobject.c) to the public C API, rather than having to dig around in the object themselves?
It looks overkill, when you can do instead: PyObject* module_name = PyObject_GetAttrString(type, "__module__"); Armin
On Sep 29, 2004, at 4:17 PM, Armin Rigo wrote:
Hello Nick,
On Wed, Sep 29, 2004 at 01:24:23PM -0400, Nick Bastin wrote:
Ok, maybe more appropriately, what do people think of adding a PyType_GetModule (PyTypeObject *) which basically functions like type_module(PyTypeObject *, void *) (in Objects/typeobject.c) to the public C API, rather than having to dig around in the object themselves?
It looks overkill, when you can do instead:
PyObject* module_name = PyObject_GetAttrString(type, "__module__");
That only works most of the time, I think. To be honest, I didn't try that, but it doesn't seem that type_module would jump through the hoops it does if that worked all of the time, unless parsing tp_name is legacy code. -- Nick
Nick Bastin <nbastin@opnet.com> writes:
On Sep 29, 2004, at 4:17 PM, Armin Rigo wrote:
Hello Nick,
On Wed, Sep 29, 2004 at 01:24:23PM -0400, Nick Bastin wrote:
Ok, maybe more appropriately, what do people think of adding a PyType_GetModule (PyTypeObject *) which basically functions like type_module(PyTypeObject *, void *) (in Objects/typeobject.c) to the public C API, rather than having to dig around in the object themselves?
It looks overkill, when you can do instead:
PyObject* module_name = PyObject_GetAttrString(type, "__module__");
That only works most of the time, I think. To be honest, I didn't try that, but it doesn't seem that type_module would jump through the hoops it does if that worked all of the time, unless parsing tp_name is legacy code.
Huh? The code above *winds up* calling type_module! Cheers, mwh -- <dash> i am trying to get Asterisk to work <dash> it is stabbing me in the face <dreid> yes ... i seem to recall that feature in the documentation -- from Twisted.Quotes
On Sep 30, 2004, at 7:46 AM, Michael Hudson wrote:
Nick Bastin <nbastin@opnet.com> writes:
On Sep 29, 2004, at 4:17 PM, Armin Rigo wrote:
Hello Nick,
On Wed, Sep 29, 2004 at 01:24:23PM -0400, Nick Bastin wrote:
Ok, maybe more appropriately, what do people think of adding a PyType_GetModule (PyTypeObject *) which basically functions like type_module(PyTypeObject *, void *) (in Objects/typeobject.c) to the public C API, rather than having to dig around in the object themselves?
It looks overkill, when you can do instead:
PyObject* module_name = PyObject_GetAttrString(type, "__module__");
That only works most of the time, I think. To be honest, I didn't try that, but it doesn't seem that type_module would jump through the hoops it does if that worked all of the time, unless parsing tp_name is legacy code.
Huh? The code above *winds up* calling type_module!
Doh, nevermind...I missed the getter def. -- Nick (::slinks off back under his rock now::)
participants (3)
-
Armin Rigo -
Michael Hudson -
Nick Bastin