Getting __builtins__ into my local dictionary

Chris Walsh chris.walsh at real-sense.com
Wed Sep 11 19:58:21 EDT 2002


chris.walsh at real-sense.com (Chris Walsh) wrote in message news:<1691b372.0209110828.31501d82 at posting.google.com>...
> 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.

PS: My Python dir(__builtins__) lists the following on startup:

================START=OF=PYTHON===============
Python 2.2.1 (#34, Apr  9 2002, 19:34:33) [MSC 32 bit (Intel)] on
win32
Type "copyright", "credits" or "license" for more information.
IDLE 0.8 -- press F1 for help
>>> dir(__builtins__)
['ArithmeticError', 'AssertionError', 'AttributeError',
'DeprecationWarning', 'EOFError', 'Ellipsis', 'EnvironmentError',
'Exception', 'False', 'FloatingPointError', 'IOError', 'ImportError',
'IndentationError', 'IndexError', 'KeyError', 'KeyboardInterrupt',
'LookupError', 'MemoryError', 'NameError', 'None', 'NotImplemented',
'NotImplementedError', 'OSError', 'OverflowError', 'OverflowWarning',
'ReferenceError', 'RuntimeError', 'RuntimeWarning', 'StandardError',
'StopIteration', 'SyntaxError', 'SyntaxWarning', 'SystemError',
'SystemExit', 'TabError', 'True', 'TypeError', 'UnboundLocalError',
'UnicodeError', 'UserWarning', 'ValueError', 'Warning',
'WindowsError', 'ZeroDivisionError', '__debug__', '__doc__',
'__import__', '__name__', 'abs', 'apply', 'bool', 'buffer',
'callable', 'chr', 'classmethod', 'cmp', 'coerce', 'compile',
'complex', 'copyright', 'credits', 'delattr', 'dict', 'dir', 'divmod',
'eval', 'execfile', 'exit', 'file', 'filter', 'float', 'getattr',
'globals', 'hasattr', 'hash', 'help', 'hex', 'id', 'input', 'int',
'intern', 'isinstance', 'issubclass', 'iter', 'len', 'license',
'list', 'locals', 'long', 'map', 'max', 'min', 'object', 'oct',
'open', 'ord', 'pow', 'property', 'quit', 'range', 'raw_input',
'reduce', 'reload', 'repr', 'round', 'setattr', 'slice',
'staticmethod', 'str', 'super', 'tuple', 'type', 'unichr', 'unicode',
'vars', 'xrange', 'zip']
=================END==OF=PYTHON===============



More information about the Python-list mailing list