Getting __builtins__ into my local dictionary

Chris Walsh chris.walsh at real-sense.com
Wed Sep 11 12:28:59 EDT 2002


Hi All,

I know this may sound like I am repeating a much discussed topic but I
want to create a multiple environments within the one Python
interpreter so that each envronment contains it's own local defs and
attributes but also understands functions such as str() (e.g. contains
__builtin__ module).

This is how I think I should do it:

================START=OF=CODE================
	PyObject *pMyMod  = PyModule_New("unique000");
	PyObject *pMyDict = PyModule_GetDict(pMyMod);

	PyDict_SetItemString(pMyDict, "__builtins__", PyEval_GetBuiltins());

	PyObject *pResultT;
	pResultT = PyRun_String("dir()\n",
		Py_eval_input, pMyDict, pMyDict);
	DumpAndRelease("dir()", pResultT);

	pResultT = PyRun_String("dir(__builtins__)\n",
		Py_eval_input, pMyDict, pMyDict);
	DumpAndRelease("dir(__builtins__)", pResultT);
=================END==OF=CODE================

The results I get from my Dump module (which just throws out the
contents of pResultT) is:

================START=OF=DUMP================
dir()
	List of 3 items: ['__builtins__', '__doc__', '__name__']

dir(__builtins__)
	List of 37 items: ['__class__', '__cmp__', '__contains__',
'__delattr__', '__delitem__', '__doc__', '__eq__', '__ge__',
'__getattribute__', '__getitem__', '__gt__', '__hash__', '__init__',
'__iter__', '__le__', '__len__', '__lt__', '__ne__', '__new__',
'__reduce__', '__repr__', '__setattr__', '__setitem__', '__str__',
'clear', 'copy', 'get', 'has_key', 'items', 'iteritems', 'iterkeys',
'itervalues', 'keys', 'popitem', 'setdefault', 'update', 'values']
=================END==OF=DUMP================

But... if I do a "dir(__builtins__)" from the Python interpreter there
are 118 items (not 37) including amongst other stuff, the str()
function.

How do I create my own module/dictionary which contains the
__builtin__ module which contains all the handy functions that
"dir(__builtins__)" shows up from the interpeter?

Kind Regards,

Chris.



More information about the Python-list mailing list