pylint-dev / pylint

It's not just a linter that annoys you!
https://pylint.readthedocs.io/en/latest/
GNU General Public License v2.0
5.26k stars 1.12k forks source link

[doc] exec-used, can't reproduce the nefarious example writing in an intermediary file #9913

Open Pierre-Sassoulas opened 3 weeks ago

Pierre-Sassoulas commented 3 weeks ago

Type of Changes

Type
:scroll: Docs

Description

I wanted to make the example's formatting better in details, ended up trying to reproduce it, but it seems the allow_globals parameters is smarter than we thought when originally documenting it. I get this error when uncommenting # allowed_globals:

Traceback (most recent call last):
  File "/home/pierre/pylint/a.py", line 4, in <module>
    exec(
  File "<string>", line 2, in <module>
SystemError: ../Objects/dictobject.c:1490: bad argument to internal function
import textwrap
allowed_globals = {"__builtins__": None}
exec(
    textwrap.dedent("""
    import textwrap

    with open("nefarious.py", "w") as f:
        f.write(textwrap.dedent('''
            def connive():
                print("Here's some code as nefarious as imaginable")
        '''))

    import nefarious
    nefarious.connive()
    """),
    allowed_globals,
)

What am I doing wrong @DanielNoord ?

DanielNoord commented 3 weeks ago

Where do you get this error? Locally this example also doesn't run? So perhaps it is just an invalid snippet?

Pierre-Sassoulas commented 3 weeks ago

I created it :) If you remove allowed_global from the exec call, it's going to work as expected (print the nefarious conniving).

DanielNoord commented 3 weeks ago

Sorry I still don't understand the issue 😅

If I run this code locally without the comment then I also get a crash. So isn't it excepted that you get some kind of (System)Error when you uncomment it?

Pierre-Sassoulas commented 3 weeks ago

Hmm not sure if it's due to mac vs Ubuntu or if there's some kind if misunderstanding. On my side it print when allowed_global is commented and crash when it's not. Maybe you can provide an exemple where print is being called inside an exec when allowed_globals should forbid it ?

DanielNoord commented 3 weeks ago

But in the old code there is no allowed_global right? So the new code without the comment is just invalid Python?