Kaydet (Commit) b6ac4d3c authored tarafından Batuhan Taşkaya's avatar Batuhan Taşkaya

fix recursion problem on example, linting

üst 34306040
import builtins import builtins
import ctypes
import random import random
from happen.happen import keyword_patcher from happen.happen import keyword_patcher
class Maybe: class Maybe:
def __bool__(self): def __bool__(self):
return random.choice((True, False)) return random.choice((True, False))
def __repr__(self): def __repr__(self):
return f"Maybe?" return f"Maybe?"
def __str__(self): def __str__(self):
return repr(self) return repr(self)
Maybe = Maybe() Maybe = Maybe()
keyword_patcher(('Maybe',), '__main__') keyword_patcher(("Maybe",), "__main__")
original_isinstance = isinstance
def check(a, b): def check(a, b):
if a is Maybe and b is bool: if a is Maybe and b is bool:
return True return True
return isinstance(a, b) return original_isinstance(a, b)
builtins.isinstance = check
builtins.isinstance = check
from maybe import Maybe from maybe import Maybe
print(isinstance(Maybe, bool)) print(isinstance(Maybe, bool))
print(isinstance(int, bool))
import sys
import ast import ast
import random import random
import sys
class KeywordListener(ast.NodeTransformer): class KeywordListener(ast.NodeTransformer):
def __init__(self, keywords, *args, **kwargs): def __init__(self, keywords, *args, **kwargs):
...@@ -19,18 +20,14 @@ class KeywordListener(ast.NodeTransformer): ...@@ -19,18 +20,14 @@ class KeywordListener(ast.NodeTransformer):
return node return node
def keyword_patcher(keywords, name, *args, **kwargs): def keyword_patcher(keywords, name, *args, **kwargs):
module = __import__(name, *args, **kwargs) module = __import__(name, *args, **kwargs)
if module.__name__ != '__main__' and not hasattr(module, '__file__'): if module.__name__ != "__main__" and not hasattr(module, "__file__"):
return module return module
tree = ast.parse( tree = ast.parse(
''.join( "".join(open(sys.modules[name].__file__.replace("pyc", "py"), "r").readlines())
open(
sys.modules[name].__file__.replace('pyc', 'py'),
'r'
).readlines()
)
) )
tree = KeywordListener(keywords).visit(tree) tree = KeywordListener(keywords).visit(tree)
ast.fix_missing_locations(tree) ast.fix_missing_locations(tree)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment