amygdalama / nagini

replacing python statements and functions with harry potter spells
51 stars 2 forks source link

Use __builtin__ instead of __builtins__ #1

Closed merwok closed 10 years ago

merwok commented 10 years ago

The code written to turn import into accio relies on a detail of CPython instead of a defined part of the language: http://stackoverflow.com/questions/11181519/python-whats-the-difference-between-builtin-and-builtins

Excellent hack by the way :‑D

amygdalama commented 10 years ago

Thanks for the link! After reading the Python documentation, I still don't entirely understand the difference. I'll look into this today.

merwok commented 10 years ago

In short: the module that can be changed is named __builtin__; the other thing named __builtins__ is an implementation detail in CPython that you can mostly ignore, it’s not part of the Python language (i.e. your code is not guaranteed to work with other interpreters like PyPy if you use it).

amygdalama commented 10 years ago

Ah, I am beginning to understand. I'm not concerned with the code in this repository being dependent on CPython implementation details, because it is only intended to be used with my modified version of CPython. For example, you couldn't import the harrypotter.py script in this repo in another interpreter (even a muggle CPython interpreter) because the script uses accio instead of import. So this script will only work when interpreted by my version of CPython anyway.

However, I should totally use __builtin__ instead of __builtins__ in the harrypotter.py file for another reason! The script was initially created to be ran at PYTHONSTARTUP. However, if someone wanted to instead import, ahem, accio it, it wouldn't work, because in that case __builtins__ would be a dictionary and __builtins__.float, etc, would throw an AttributeError: 'dict' object has no attribute 'float'.

Thanks for bringing this up! It was fun to learn about.

merwok commented 10 years ago

It’s one of the unclean corners that the import system is full of :‑) Python 3.3 and 3.4 clean up some of these things.

Glad to help : )