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 ctypes
import random
from happen.happen import keyword_patcher
class Maybe:
def __bool__(self):
return random.choice((True, False))
def __repr__(self):
return f"Maybe?"
def __str__(self):
return repr(self)
Maybe = Maybe()
keyword_patcher(('Maybe',), '__main__')
keyword_patcher(("Maybe",), "__main__")
original_isinstance = isinstance
def check(a, b):
if a is Maybe and b is bool:
return True
return isinstance(a, b)
builtins.isinstance = check
return original_isinstance(a, b)
builtins.isinstance = check
from maybe import Maybe
print(isinstance(Maybe, bool))
print(isinstance(int, bool))
import sys
import ast
import random
import sys
class KeywordListener(ast.NodeTransformer):
def __init__(self, keywords, *args, **kwargs):
super().__init__(*args, **kwargs)
self.keywords = keywords
def visit_Delete(self, node):
if any(filter(lambda target: target.id in self.keywords, node.targets)):
raise SyntaxError("can't delete keyword")
return node
def visit_Assign(self, node):
if any(filter(lambda target: target.id in self.keywords, node.targets)):
raise SyntaxError("can't assign to keyword")
return node
def keyword_patcher(keywords, 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
tree = ast.parse(
''.join(
open(
sys.modules[name].__file__.replace('pyc', 'py'),
'r'
).readlines()
)
"".join(open(sys.modules[name].__file__.replace("pyc", "py"), "r").readlines())
)
tree = KeywordListener(keywords).visit(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