thierry-martinez / pyml

OCaml bindings for Python
BSD 2-Clause "Simplified" License
187 stars 31 forks source link

Referencing __main__.__builtins__ causes AttributeError #102

Open kerrick-js opened 3 months ago

kerrick-js commented 3 months ago

py.ml references __main__.__builtins__ in a couple places. According to the docs, this attribute is an implementation detail and its behavior can't be relied upon:

CPython implementation detail: Users should not touch __builtins__; it is strictly an implementation detail.

This causes problems when running code under pdb. In pdb __main__.__builtins__ is a dict, not a module:

$ echo 'import __main__; print(type(__main__.__builtins__))' > foo.py
$ python3 foo.py
<class 'module'>
$ python3 -m pdb -c c -c q foo.py
<class 'dict'>
The program finished and will be restarted

This causes exceptions like

AttributeError: 'dict' object has no attribute 'SomeBuiltinThing'

when you try to reference attributes on this module.

Instead, py.ml should import the builtins module or reference sys.modules['builtins'].