Open dnesting opened 1 month ago
David Nesting wrote at 2024-10-19 15:30 -0700:
... Unfortunately it doesn't look like I can do that very easily, since the current
I think you are wrong (or at least overly pessimistic):
I think all you need is override RestrictedNodeTransformer.visit_Name
and remove the
if node.id == 'printed':
...
elif node.id == 'print':
...
BUG/PROBLEM REPORT / FEATURE REQUEST
RestrictingNodeTransformer
rewrites usage ofprint
, and expects its output to be retrieved withprinted
. This is too opinionated for my use case (plus I have existing code that I want to use this on that uses aprinted
variable for its own purpose). I'd like to simply replace theprint
builtin with my own implementation that I can do whatever I want with, such as collect output in a file or aStringIO
or similar where I can do something with it when execution completes.Unfortunately it doesn't look like I can do that very easily, since the current
print
handling seems to be pervasive in the transformer's implementation. Overriding the methods in question seems to mean copy-pasting a lot of code.I can't find any documentation that explains whether or not there's a good reason
__builtins__.print
just can't be overridden without all of the mangling that occurs inRestrictingNodeTransformer
. Is there? Can you recommend a path forward for me?What I expect to happen:
I'd love to be able to write a replacement
print
implementation that works something like:What actually happened:
I am forced instead down a
_print
andPrintCollector
journey that requires I modify the code I'm trying to execute by having it do something in the scope of the user code withprinted
. This seems like it requires a lot of code modification.What version of Python and Zope/Addons I am using:
RestrictedPython==7.4 Cpython 3.12 on Darwin