is there a tp_getattr equivalent for module?
is there a tp_getattr equivalent for module? i want to dynamically check if an object exists in a module space dynamically and return it if it does.
currently i have mymodule.getattr("foo") where getattr is my lookup function plugged thru PyMethodDef/Py_ModuleInit
i want to be able to access foo directly, like so: mymodule.foo
would this work?
PyObject *mod_sys;
mod_sys = PyImport_ImportModule( "sys" ); /* new ref */ dict = PyModule_GetDict( mod_sys ); /* borrowed ref */
PyObject *key, *value; int pos = 0;
if (dict) { while (PyDict_Next(dict, &pos, &key, &value)) { /* do stuff here */ } }
On Wed, 2007-12-05 at 17:56 -0600, Anton Tropashko wrote:
is there a tp_getattr equivalent for module? i want to dynamically check if an object exists in a module space dynamically and return it if it does.
currently i have mymodule.getattr("foo") where getattr is my lookup function plugged thru PyMethodDef/Py_ModuleInit
i want to be able to access foo directly, like so: mymodule.foo
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
On 07/12/2007, Campbell Barton cbarton@metavr.com wrote:
would this work?
PyObject *mod_sys;
mod_sys = PyImport_ImportModule( "sys" ); /* new ref */ dict = PyModule_GetDict( mod_sys ); /* borrowed ref */
Don't do this, it breaks the module as object transparency. In some solutions, python modules can be replaced by proxy objects that deman load the real object, and they hook into getattr, so this solution won't work for them.
Modules are normal python objects. You can use PyObject_GetAttrString as usual...
PyObject *key, *value;
int pos = 0;
if (dict) { while (PyDict_Next(dict, &pos, &key, &value)) { /* do stuff here */ } }
is there a tp_getattr equivalent for module? i want to dynamically check if an object exists in a module space dynamically and return it if it does.
currently i have mymodule.getattr("foo") where getattr is my lookup function plugged
On Wed, 2007-12-05 at 17:56 -0600, Anton Tropashko wrote: thru PyMethodDef/Py_ModuleInit
i want to be able to access foo directly, like so: mymodule.foo
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
capi-sig mailing list capi-sig@python.org http://mail.python.org/mailman/listinfo/capi-sig
-- Gustavo J. A. M. Carneiro INESC Porto, Telecommunications and Multimedia Unit "The universe is always one step beyond logic." -- Frank Herbert
"Anton Tropashko" sndive@gmail.com writes:
is there a tp_getattr equivalent for module? i want to dynamically check if an object exists in a module space dynamically and return it if it does.
You don't need to call tp_getattr yourself, PyObject_GetAttr will do it for you. If you need to check whether an object exists in a module without accessing it using mymodule.name, simply use getattr on the module object like you'd do with any other object. For example:
import mymodule name = 'foo' if hasattr(mymodule, name): print 'mymodule has', name, 'and its value is', getattr(mymodule, name) else: print 'no', name, 'in mymodule'
Equivalent C code would use PyObject_HasAttr/PyObject_GetAttr or its *String variants.
participants (4)
-
Anton Tropashko
-
Campbell Barton
-
Gustavo Carneiro
-
Hrvoje Niksic