Issue #1761 - Enable use of Tauthon instead of Python 2.7 at build

Make essential Tauthon's builtins and modules available in sandboxes, without
disrupting building with Python 2.7.

1. Allow new builtins and modules. "__build_class__" is for
building... new-style classes (to handle the new metaclass syntax imported from
3.x). "_oserror" is necessary because of internal changes to raise errors
deriving from OSError (which are reported as IOError if not caught).
2. Look for modules and packages in the right places.

This was tested on FreeBSD, and should work out of the box on Linux.
This commit is contained in:
Olivier Certner 2021-04-07 15:20:32 +02:00 committed by roytam1
commit d094352ddf
4 changed files with 30 additions and 16 deletions

View file

@ -192,8 +192,9 @@ class ConfigureSandbox(dict):
b: __builtins__[b]
for b in ('None', 'False', 'True', 'int', 'bool', 'any', 'all', 'len',
'list', 'tuple', 'set', 'dict', 'isinstance', 'getattr',
'hasattr', 'enumerate', 'range', 'zip')
}, __import__=forbidden_import, str=unicode)
'hasattr', 'enumerate', 'range', 'zip', '__build_class__')
if b in __builtins__},
__import__=forbidden_import, str=unicode)
# Expose a limited set of functions from os.path
OS = ReadOnlyNamespace(path=ReadOnlyNamespace(**{

View file

@ -115,7 +115,10 @@ class Sandbox(dict):
def __init__(self, context, builtins=None, finder=default_finder):
"""Initialize a Sandbox ready for execution.
"""
self._builtins = builtins or self.BUILTINS
self._builtins = ReadOnlyDict(
(builtins or self.BUILTINS).viewitems() |
{b: __builtins__[b] for b in ('__build_class__',)
if b in __builtins__}.viewitems())
dict.__setitem__(self, '__builtins__', self._builtins)
assert isinstance(self._builtins, ReadOnlyDict)