Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Getting module object by name in C extension module

Reply
Thread Tools

Getting module object by name in C extension module

 
 
Ilariu Raducan
Guest
Posts: n/a
 
      07-14-2004
Hi All,

Is it possible in a C extension module to get a reference to an already loaded module?
Something like:
PyObject* module_object = Py......("module_name");

Thank You,
Ilariu
 
Reply With Quote
 
 
 
 
Nick Smallbone
Guest
Posts: n/a
 
      07-14-2004
"Ilariu Raducan" <(E-Mail Removed)> wrote in message
newss7Jc.4541$(E-Mail Removed)...
> Hi All,
>
> Is it possible in a C extension module to get a reference to an already

loaded module?
> Something like:
> PyObject* module_object = Py......("module_name");
>
> Thank You,
> Ilariu


I haven't written any C extensions for a while, but looking at the
documentation this seems to be the way to go:

PyObject * modules = PyImport_GetModuleDict();
PyObject * module_object = PyDict_GetItemString(modules, "module_name");

The first line gets the value of sys.modules, and the second line looks up
the module name in it. Both of those are borrowed references, so you don't
need to Py_DECREF() them.

If the module might not have been loaded, you can do this:

PyObject * module_object = PyImport_Import("module_name");

but then you have to Py_DECREF() module_object once you're finished.

Nick


 
Reply With Quote
 
 
 
 
Ilariu Raducan
Guest
Posts: n/a
 
      07-14-2004
Thank You,
Ilariu
>
> I haven't written any C extensions for a while, but looking at the
> documentation this seems to be the way to go:
>
> PyObject * modules = PyImport_GetModuleDict();
> PyObject * module_object = PyDict_GetItemString(modules, "module_name");
>
> The first line gets the value of sys.modules, and the second line looks up
> the module name in it. Both of those are borrowed references, so you don't
> need to Py_DECREF() them.
>
> If the module might not have been loaded, you can do this:
>
> PyObject * module_object = PyImport_Import("module_name");
>
> but then you have to Py_DECREF() module_object once you're finished.
>
> Nick
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Not-quite-the-module-name qualified names in extension modules? What? Alf P. Steinbach /Usenet Python 0 07-10-2010 05:38 PM
How to create Python object in C/C++ extension by class name? Neil.Fang.CN Python 1 03-10-2008 09:47 AM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Getting name of object instance from within object method Martin Javascript 6 08-20-2004 12:24 PM
Re: Urgent! how to get object name, method name and attribute name based on the strings? ding feng C++ 2 06-25-2003 01:18 PM



Advertisments