Getting __builtins__ into my local dictionary

Chris Walsh chris.walsh at
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:

	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);

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

	List of 3 items: ['__builtins__', '__doc__', '__name__']

	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']

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

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,


More information about the Python-list mailing list