Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit b86ca19

Browse files
committed
Python: CG trace: Apply better_compare_for_dataclass to all
1 parent 9bff615 commit b86ca19

3 files changed

Lines changed: 33 additions & 22 deletions

File tree

python/tools/recorded-call-graph-metrics/src/cg_trace/bytecode_reconstructor.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from types import FrameType
66
from typing import Any, List
77

8+
from cg_trace.utils import better_compare_for_dataclass
9+
810
LOGGER = logging.getLogger(__name__)
911

1012
# See https://docs.python.org/3/library/dis.html#python-bytecode-instructions for
@@ -18,6 +20,7 @@ class BytecodeExpr:
1820
"""
1921

2022

23+
@better_compare_for_dataclass
2124
@dataclasses.dataclass(frozen=True, eq=True, order=True)
2225
class BytecodeConst(BytecodeExpr):
2326
"""FOR LOAD_CONST"""
@@ -28,6 +31,7 @@ def __str__(self):
2831
return repr(self.value)
2932

3033

34+
@better_compare_for_dataclass
3135
@dataclasses.dataclass(frozen=True, eq=True, order=True)
3236
class BytecodeVariableName(BytecodeExpr):
3337
name: str
@@ -36,6 +40,7 @@ def __str__(self):
3640
return self.name
3741

3842

43+
@better_compare_for_dataclass
3944
@dataclasses.dataclass(frozen=True, eq=True, order=True)
4045
class BytecodeAttribute(BytecodeExpr):
4146
attr_name: str
@@ -45,6 +50,7 @@ def __str__(self):
4550
return f"{self.object}.{self.attr_name}"
4651

4752

53+
@better_compare_for_dataclass
4854
@dataclasses.dataclass(frozen=True, eq=True, order=True)
4955
class BytecodeSubscript(BytecodeExpr):
5056
key: BytecodeExpr
@@ -54,6 +60,7 @@ def __str__(self):
5460
return f"{self.object}[{self.key}]"
5561

5662

63+
@better_compare_for_dataclass
5764
@dataclasses.dataclass(frozen=True, eq=True, order=True)
5865
class BytecodeTuple(BytecodeExpr):
5966
elements: List[BytecodeExpr]
@@ -67,6 +74,7 @@ def __str__(self):
6774
return f"({elements_formatted})"
6875

6976

77+
@better_compare_for_dataclass
7078
@dataclasses.dataclass(frozen=True, eq=True, order=True)
7179
class BytecodeList(BytecodeExpr):
7280
elements: List[BytecodeExpr]
@@ -80,6 +88,7 @@ def __str__(self):
8088
return f"[{elements_formatted}]"
8189

8290

91+
@better_compare_for_dataclass
8392
@dataclasses.dataclass(frozen=True, eq=True, order=True)
8493
class BytecodeCall(BytecodeExpr):
8594
function: BytecodeExpr
@@ -88,6 +97,7 @@ def __str__(self):
8897
return f"{self.function}()"
8998

9099

100+
@better_compare_for_dataclass
91101
@dataclasses.dataclass(frozen=True, eq=True, order=True)
92102
class BytecodeUnknown(BytecodeExpr):
93103
opname: str
@@ -96,6 +106,7 @@ def __str__(self):
96106
return f"<{self.opname}>"
97107

98108

109+
@better_compare_for_dataclass
99110
@dataclasses.dataclass(frozen=True, eq=True, order=True)
100111
class BytecodeMakeFunction(BytecodeExpr):
101112
"""For MAKE_FUNCTION opcode"""
@@ -106,6 +117,7 @@ def __str__(self):
106117
return f"<MAKE_FUNCTION>(qualified_name={self.qualified_name})>"
107118

108119

120+
@better_compare_for_dataclass
109121
@dataclasses.dataclass(frozen=True, eq=True, order=True)
110122
class SomethingInvolvingScaryBytecodeJump(BytecodeExpr):
111123
opname: str

python/tools/recorded-call-graph-metrics/src/cg_trace/tracer.py

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Any, Optional, Tuple
77

88
from cg_trace.bytecode_reconstructor import BytecodeExpr, expr_from_frame
9+
from cg_trace.utils import better_compare_for_dataclass
910

1011
LOGGER = logging.getLogger(__name__)
1112

@@ -75,28 +76,6 @@ def hash_key(frame: FrameType) -> Tuple[str, int, int]:
7576
)
7677

7778

78-
def better_compare_for_dataclass(cls):
79-
"""When dataclass is used with `order=True`, the comparison methods is only implemented for
80-
objects of the same class. This decorator extends the functionality to compare class
81-
name if used against other objects.
82-
"""
83-
for op in [
84-
"__lt__",
85-
"__le__",
86-
"__gt__",
87-
"__ge__",
88-
]:
89-
old = getattr(cls, op)
90-
91-
def new(self, other):
92-
if type(self) == type(other):
93-
return old(self, other)
94-
return getattr(str, op)(self.__class__.__name__, other.__class__.__name__)
95-
96-
setattr(cls, op, new)
97-
return cls
98-
99-
10079
@dataclasses.dataclass(frozen=True, eq=True, order=True)
10180
class Callee:
10281
pass
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
def better_compare_for_dataclass(cls):
2+
"""When dataclass is used with `order=True`, the comparison methods is only implemented for
3+
objects of the same class. This decorator extends the functionality to compare class
4+
name if used against other objects.
5+
"""
6+
for op in [
7+
"__lt__",
8+
"__le__",
9+
"__gt__",
10+
"__ge__",
11+
]:
12+
old = getattr(cls, op)
13+
14+
def new(self, other):
15+
if type(self) == type(other):
16+
return old(self, other)
17+
return getattr(str, op)(self.__class__.__name__, other.__class__.__name__)
18+
19+
setattr(cls, op, new)
20+
return cls

0 commit comments

Comments
 (0)