-
-
Save wraithan/1279926 to your computer and use it in GitHub Desktop.
Evil Pure Python Evil
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def speak(): | |
| print('woof') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| def speak(): | |
| print('meow') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import inspect | |
| import example1 as example | |
| def replace(): | |
| # doesn't work because of scoping | |
| import example2 as example | |
| def inspect_replace(): | |
| current_frame = inspect.currentframe() | |
| # Essentially the same as 'u' in pdb | |
| outer_frame = current_frame.f_back | |
| import example2 | |
| outer_frame.f_globals['example'] = example2 | |
| # Have to delete any references to the frames or else they wont be | |
| # properly garbage collected | |
| del current_frame | |
| del outer_frame | |
| example.speak() | |
| replace() | |
| example.speak() | |
| inspect_replace() | |
| example.speak() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $ python2 replace.py | |
| woof | |
| woof | |
| meow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment