Skip to content

Instantly share code, notes, and snippets.

@mccutchen
Created January 22, 2012 16:28
Show Gist options
  • Select an option

  • Save mccutchen/1657592 to your computer and use it in GitHub Desktop.

Select an option

Save mccutchen/1657592 to your computer and use it in GitHub Desktop.
Evil Python monkeypatching test
import types
assert int(True) == 1 and int(False) == 0
# Who knew that __bulitins__ was a different object depending on whether you
# run a module directly or import it into another module?
if isinstance(__builtins__, types.ModuleType):
__builtins__.True, __builtins__.False = False, True
else:
__builtins__['True'], __builtins__['False'] = False, True
assert int(True) == 0 and int(False) == 1
assert int(True) == 1 and int(False) == 0
import evil
assert int(True) == 0 and int(False) == 1
print 'Importing evil successfully inverted True and False in main'
print 'int(True) => %d' % int(True)
print 'int(False) => %d' % int(False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment