AttributeError with embedded Python

P

PlayDough

I've embedded Python in an extension for a program we are using here
at work. And I'm a bit stumped as to why I am getting an
AttributeError only in the embedded Python.

First, a bit of what I am doing. We use a simulator for a
microprocessor we are using in our systems. Our simulator allows for
extensions that can be loaded as shared libraries. Rather than code
the entire extension in C/C++, I would like to make use of Python to
script the extension.

So, I first initialize Python (examples below leave out the error
checking, but it is there):

Py_Initialize();

And then I make sure the script directory is in the path with
Py_GetPath() and PySys_SetPath().

Finally, I import the script (say it is in a local file 'script.py'):

pName = PyString_FromString("script");
pModule = PyImport_Import(pName);

Once the module is imported, I get objects to the functions in the
script I want to call later, which I do with:

pName = PyString_FromString("foo");
pFunc = PyObject_GetAttr(pModule, pName);

Later, I come back and call the function:

pResult = PyObject_CallObject(pFunc, NULL);

And it always fails whenever I iterate of a list. Say for example my
Python function is:

def foo():
a = ['cat', 'window', 'defenstrate']
for x in a:
print x, len(x)

Now, the function runs, i.e. I see the output ("cat 3\nwindow
6\ndefenstrate 11\n"), but I always get the following error message:

Traceback (most recent call last):
File "./script.py", line 3, in foo
for x in a:
AttributeError: 'module' object has no attribute 'reset'

Now, if I run this exact same script using Python standalone, i.e.:

Python 2.3.4 (#1, Nov 20 2007, 15:18:15)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-9)] on linux2
Type "help", "copyright", "credits" or "license" for more information.cat 3
window 6
defenstrate 11
(I know, and old version of Python. But we are stuck with it because
our processes require us to validate a newer version if we change.)

This works great. What is the difference? Why does it work in one
context but not the other?

Thanks,
Pete
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top