Repository navigation
Add reversed() to safe_builtins - #336
divinity76 wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the addition of reversed() to the safe builtins.
No open issues 🎉
What changed in this PR
Adds reversed() to RestrictedPython’s safe builtins, addressing issue #335 so restricted code can request reverse iteration over sequences.
Changes:
- Includes
reversedin_safe_names. - Adds a test verifying reverse iteration through restricted evaluation.
- Documents the addition in the unreleased changelog.
| File | Description |
|---|---|
| tests/test_Guards.py | Tests that restricted evaluation supports reversed(). |
| src/RestrictedPython/Guards.py | Adds reversed to the safe builtin allowlist. |
| CHANGES.rst | Records the new builtin support. |
Review effort: Balanced
|
Thank you for your contribution. According to the contributing policies of the zopefoundation organization you need to sign a contributor agreement before any non-trivial change can be merged. For details please consult the Contributing guidelines for zopefoundation projects. |
|
I reviewed the PR and came to the following conclusion: I am going to reject this PR, and I am also going to remove
[x for x in secret] # RuntimeError: iteration refused by _getiter_
tuple(secret) # ('s', 'e', 'c')
sorted(secret) # ['c', 'e', 's']
reversed(secret) # works the same wayThe argument that reversed "exposes no attributes and mutates nothing" is true, but it misses the point: it exposes the elements of any iterable without the application getting a say. In Zope that is the difference between a validated item and an Unauthorized error.
|
Sorry if this is a silly question, but doesn't that mean |
Not a silly question at all, but a nice catch. # _getiter_ refuses `secret`
[x for x in secret] # RuntimeError: iteration of secret refused by _getiter_
[x for x in zip(secret)] # [('s',), ('e',), ('c',)]It went unnoticed for the same reason sorted and tuple did: Zope's AccessControl.ZopeGuards replaces it with guarded_zip, which runs every argument through guarded_iter first, so the Zope users who make up most of the audience never saw the raw builtin. Plain RestrictedPython users do. I will remove |
Fixes #335.
reversed()returns an iterator over an already-visible sequence; it exposes no attributes and mutates nothing, so it is safe to allow alongsidesorted,lenand friends.reversedto_safe_namesFull suite: 303 passed, 7 skipped.