Skip to content

How to know what needs to be added to the globals? #261

@seperman

Description

@seperman

BUG/PROBLEM REPORT / FEATURE REQUEST

Hello!

If I pass restricted_globals=dict(__builtins__=safe_builtins), my function does not compile. I don't see any errors that it was not compiled. How do I know what needs to be added to safe_builtins ? If I don't pass restricted_globals, then the function is compiled.

What I did:

from RestrictedPython import compile_restricted, safe_builtins
restricted_globals = dict(__builtins__=safe_builtins)

source_code = """
valid_sizes = ['6', '7', '8', '8.5', '9', '10', '10.5']

def check(row, valid_sizes=valid_sizes):
    return any('shoes' in category.lower() and all(size in valid_sizes for size in row['sizes']) for category in row['categories'])

"""

byte_code = compile_restricted(
    source_code,
    filename='<string>',
    mode='exec'
)

exec(byte_code, restricted_globals)
check({'categories': ['Shoes', "Women's Shoes", 'Clothing', "All Women's Shoes"], 'sizes': ['8.5', '7', '9.5', '7.5', '8']})

What I expect to happen:

check to be executed.

What actually happened:

NameError: name 'check' is not defined

What version of Python and Zope/Addons I am using:

Python 3.11.4
RestrictedPython==6.2
Ubuntu

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions