[capi-sig] is there a tp_getattr equivalent for module?

Gustavo Carneiro gjcarneiro at gmail.com
Fri Dec 7 15:00:17 CET 2007


On 07/12/2007, Campbell Barton <cbarton at 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 */
>         }
> }
>
>
>
>
> 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 at python.org
> > http://mail.python.org/mailman/listinfo/capi-sig
>
> _______________________________________________
> capi-sig mailing list
> capi-sig at 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


More information about the capi-sig mailing list