simple? embedding question

S

sndive

suppose i have imported two modules foo and bar with
foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar")
respectively.

Now suppose I have an artitrary python expression to evaluate.
Do I need to parse that thring and check for foo. and bar. before
jumping the usual
PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on
the PyObject for
the prefix or there is a better way?

btw PyRun_SimpleString("foo.baz()"); does not work:
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name 'foo' is not defined

and i potentially need a PyObject* back with the result of the
expression anyway.
 
F

Farshid Lashkari

suppose i have imported two modules foo and bar with
foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar")
respectively.

Now suppose I have an artitrary python expression to evaluate.
Do I need to parse that thring and check for foo. and bar. before
jumping the usual
PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on
the PyObject for
the prefix or there is a better way?

btw PyRun_SimpleString("foo.baz()"); does not work:
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name 'foo' is not defined

and i potentially need a PyObject* back with the result of the
expression anyway.

I believe the problem is that you are not importing the "foo" and "bar"
modules into the __main__ scope. Try using PyImport_ImportModuleEx,
which will allow you to specify the global scope to import the module
into. For example, to import the modules into the __main__ scope you
could do the following:

PyObject* mainmod = PyImport_AddModule("__main__");
PyObject* maindict = PyModule_GetDict(mainmod);

foo = PyImport_ImportModuleEx("foo", maindict , maindict , NULL);
bar = PyImport_ImportModuleEx("bar", maindict , maindict , NULL);

Once the modules are imported into the __main__ scope, you should be
able to use PyRun_SimpleString() to evaluate expressions.

-Farshid
 
K

kyosohma

suppose i have imported two modules foo and bar with
foo=PyImport_ImportModule("foo") and bar=PyImport_ImportModule("bar")
respectively.

Now suppose I have an artitrary python expression to evaluate.
Do I need to parse that thring and check for foo. and bar. before
jumping the usual
PyModule_GetDict,PyDict_GetItemString,PyObject_CallObject hoop hoop on
the PyObject for
the prefix or there is a better way?

btw PyRun_SimpleString("foo.baz()"); does not work:
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name 'foo' is not defined

and i potentially need a PyObject* back with the result of the
expression anyway.

I'm confused. What is the benefit of importing this way in the first
place? Everything I've read has always been either of the following
(or a variant thereof):

import foo

or

from foo import bar

Mike

P.S. Yes, I did see the docs mention this method here:
http://docs.python.org/api/importing.html but i have always followed
http://effbot.org/zone/import-confusion.htm or http://docs.python.org/ref/import.html
 
S

sndive

I believe the problem is that you are not importing the "foo" and "bar"
modules into the __main__ scope. Try using PyImport_ImportModuleEx,
which will allow you to specify the global scope to import the module
into. For example, to import the modules into the __main__ scope you
could do the following:

PyObject* mainmod = PyImport_AddModule("__main__");
PyObject* maindict = PyModule_GetDict(mainmod);

foo = PyImport_ImportModuleEx("foo", maindict , maindict , NULL);
bar = PyImport_ImportModuleEx("bar", maindict , maindict , NULL);

Once the modules are imported into the __main__ scope, you should be
able to use PyRun_SimpleString() to evaluate expressions.

-Farshid

i switched to PyImport_ImportModuleEx per your suggestion but i still
get

Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name '__main__' is not defined

i tried PyRun_SimpleString("__main__.foo.baz()"); :
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name '__main__' is not defined

i do not have if __name__ == '__main__':
statement anywhere in the module i'm importing,
but nevertheless i checked that PyImport_AddModule("__main__");
and subsequent getdict return non null pointers.
does non null dictionary indicate that PyImport_AddModule
succeeded as opposed to creating an empty module object?
 
F

Farshid Lashkari

i switched to PyImport_ImportModuleEx per your suggestion but i still
get

Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name '__main__' is not defined

i tried PyRun_SimpleString("__main__.foo.baz()"); :
Traceback (most recent call last):
File "<string>", line 1, in ?
NameError: name '__main__' is not defined

i do not have if __name__ == '__main__':
statement anywhere in the module i'm importing,
but nevertheless i checked that PyImport_AddModule("__main__");
and subsequent getdict return non null pointers.
does non null dictionary indicate that PyImport_AddModule
succeeded as opposed to creating an empty module object?

Sorry, I misunderstood the behavior of the PyImport_ImportModuleEx()
function. You can use the PyImport_AddModule() function as before, but
you must explicitly add the module to the __main__ module. Here is some
code:

PyObject *mainmod = PyImport_AddModule("__main__");
PyObject *foo = PyImport_ImportModule("foo");
Py_INCREF(foo); //Increment foo module since PyModule_AddObject() steals
reference
PyModule_AddObject(mainmod, "foo", foo);
PyRun_SimpleString("foo.baz()");

-Farshid
 
S

sndive

Sorry, I misunderstood the behavior of the PyImport_ImportModuleEx()
function. You can use the PyImport_AddModule() function as before, but
you must explicitly add the module to the __main__ module. Here is some
code:

PyObject *mainmod = PyImport_AddModule("__main__");
PyObject *foo = PyImport_ImportModule("foo");
Py_INCREF(foo); //Increment foo module since PyModule_AddObject() steals
reference
PyModule_AddObject(mainmod, "foo", foo);
PyRun_SimpleString("foo.baz()");

-Farshid

it works!
could i cache maindict pointer in a global var or am i condemned to
call
PyObject* mainmod = PyImport_AddModule("__main__");
assert(mainmod);
PyObject* maindict = PyModule_GetDict(mainmod);
assert(maindict);
every time i want to PyRun_String?
i call Py_Finalize only atexit.

i also found boost library docs of some help:
http://service-spi.web.cern.ch/serv...n/doc/tutorial/doc/using_the_interpreter.html

btw what kind of object is returned in case i use Py_file_input
in PyRun_String?
 
G

Gabriel Genellina

I'm confused. What is the benefit of importing this way in the first
place? Everything I've read has always been either of the following
(or a variant thereof):

import foo

or

from foo import bar

They're different things. The PyImport_XXX stuff are C functions used in C
code - usually inside an application *embedding* Python. The latter is
plain old Python code.
 
F

Farshid Lashkari

it works!
could i cache maindict pointer in a global var or am i condemned to
call
PyObject* mainmod = PyImport_AddModule("__main__");
assert(mainmod);
PyObject* maindict = PyModule_GetDict(mainmod);
assert(maindict);
every time i want to PyRun_String?
i call Py_Finalize only atexit.

i also found boost library docs of some help:
http://service-spi.web.cern.ch/serv...n/doc/tutorial/doc/using_the_interpreter.html

btw what kind of object is returned in case i use Py_file_input
in PyRun_String?

Yes, you can keep a reference to maindict if you wish, but make sure you
increment the reference count since PyModule_GetDict() returns a
borrowed reference.

-Farshid
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top