Skip to content

Commit 059f5c6

Browse files
zedzhendataflake
andauthored
compile_restricted_mode fix (docs + code) (#324)
* update docs * update docs * add support bytes, bytearray, ast.Expression, ast.Interactive --------- Co-authored-by: Jens Vagelpohl <jens@plyp.com>
1 parent f1b33e4 commit 059f5c6

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎docs/usage/api.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ API overview
1515
:param flags: (optional). defaults to ``0``
1616
:param dont_inherit: (optional). defaults to ``False``
1717
:param policy: (optional). defaults to ``RestrictingNodeTransformer``
18-
:type source: str or ``ast.Module``
18+
:type source: str or bytes or bytearray or ``ast.Module`` or ``ast.Expression`` or ``ast.Interactive``
1919
:type filename: str or bytes or os.PathLike[typing.Any]
2020
:type mode: str
2121
:type flags: int

‎src/RestrictedPython/compile.py‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,19 @@ def _compile_restricted_mode(
3939
dont_inherit=dont_inherit)
4040
elif issubclass(policy, RestrictingNodeTransformer):
4141
c_ast = None
42-
allowed_source_types = [str, ast.Module]
42+
allowed_source_types = [
43+
str,
44+
bytes,
45+
bytearray,
46+
ast.Module,
47+
ast.Expression,
48+
ast.Interactive]
4349
if not issubclass(type(source), tuple(allowed_source_types)):
4450
raise TypeError('Not allowed source type: '
4551
'"{0.__class__.__name__}".'.format(source))
4652
c_ast = None
4753
# workaround for pypy issue https://bitbucket.org/pypy/pypy/issues/2552
48-
if isinstance(source, ast.Module):
54+
if isinstance(source, (ast.Module, ast.Expression, ast.Interactive)):
4955
c_ast = source
5056
else:
5157
try:

0 commit comments

Comments
 (0)