Obtaining global and local dict's for current interpreter using C API

quadric at primenet.com quadric at primenet.com
Tue Nov 5 20:52:12 EST 2002


Hi,

I am new to Python but an experienced C++ programmer familiar with 
scripting etc..... and
very excited about Python.  I have successfully embedded/extended it and am 
attempting
to execute Python source from a variety of sources.  I have looked through 
all my Python books
for an answers to the following questions and can find none ( at least not 
yet ).

Can someone please help? I'm sure this is incredibly simple, I'm just a bit 
new at it.

I'm using the latest build (2.2.2) and programming using MS Visual C++ 6.0
( no flame from UNIX/Linux guys please ).


Question(s):

How , and using which C API functions, do you acquire the PyObject * to the 
dictionaries
( both local and global) of the current interpreter.  I AM NOT looking for 
the dictionary of an
imported module, but rather the dictionary of the current interpreter.  You 
know, the one
you get when executing  'dir()'  in the interpreter window.

The C API docs say to do the following:

PyObject * pdict = PyObject_Dir(NULL);

and , assuming an execution frame is active, should return the dictionary.

1>	What is an execution frame?
2>	How do I know if an execution frame is active?
3> 	I've tried this and only get NULL in return, indicating the absence of 
an active
	execution frame, I guess?

-----------------------------------------------------------------------------------

My code looks similar to this:

PyObject * pres = NULL , * pdict = NULL;
int  result = 0;

Py_Initialize();

pdict = PyObject_Dir(NULL);	// Returns a useless NULL pointer

if ( pdict != NULL )
{
	PyRun_SimpleString( "result = 4 + 5" );
	pres = PyObject_GetAttrString( pdict , "result" );	
	PyArg_Parse( pres , "i" , &result);
}


// decrement any references here

Py_Finalize();

--------------------------------------------------------------

The problem is that pdict is always NULL and I cannot retrieve values for 
variables
created within the namespace of the current interpreter by use of the 
PyRun_xxxx()
functions.



Any knowledgeable help would be greatly appreciated.

Thanks,

quadric at primenet.com















More information about the Python-list mailing list