Skip to content

Commit

Permalink
bytecode resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mkorpela committed Jan 27, 2024
1 parent ccafd5a commit f4cf3ed
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 24 deletions.
36 changes: 12 additions & 24 deletions overrides/overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,42 +200,30 @@ def _get_base_classes(frame, namespace):

def _get_base_class_names(frame: FrameType) -> List[List[str]]:
"""Get baseclass names from the code object"""
extends: List[Tuple[str, str]] = []
add_last_step = True
current_item: List[str] = []
items: List[List[str]] = []

for instruction in dis.get_instructions(frame.f_code):
print(f"{instruction.offset} : {instruction.opname} {instruction.argval}")
if instruction.offset > frame.f_lasti:
break
if instruction.opcode not in dis.hasname:
continue
if not add_last_step:
extends = []
add_last_step = True

# Combine LOAD_NAME and LOAD_GLOBAL as they have similar functionality
if instruction.opname in ["LOAD_NAME", "LOAD_GLOBAL"]:
extends.append(("name", instruction.argval))
current_item = [instruction.argval]

elif instruction.opname == "LOAD_ATTR" and extends and extends[-1][0] == "name":
extends.append(("attr", instruction.argval))
elif instruction.opname == "LOAD_ATTR" and current_item:
current_item.append(instruction.argval)

# Reset on other instructions
else:
add_last_step = False

# Extracting class names
items: List[List[str]] = []
previous_item: List[str] = []
for t, s in extends:
if t == "name":
if previous_item:
items.append(previous_item)
previous_item = [s]
else:
previous_item += [s]

if previous_item:
items.append(previous_item)
elif current_item:
items.append(current_item)
current_item = []

if current_item:
items.append(current_item)
return items


Expand Down
2 changes: 2 additions & 0 deletions tests/test_overrides.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ def some_method(self):
"""Subber"""
return 1


class Nest:
class First:
class Second:
Expand Down Expand Up @@ -96,6 +97,7 @@ class SomeClass:
def check(self):
return 1


class ChildOfNested(Nest.First.Second):
@override
def foo(self):
Expand Down

0 comments on commit f4cf3ed

Please sign in to comment.