Skip to content

Commit aca703e

Browse files
committed
Python: CG trace: Add support for flask
1 parent bb80635 commit aca703e

File tree

2 files changed

+28
-6
lines changed

2 files changed

+28
-6
lines changed

python/tools/recorded-call-graph-metrics/projects.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,15 @@
1414
"setup": [
1515
"pip install nose"
1616
]
17+
},
18+
"flask": {
19+
"repo": "https://github.com/pallets/flask.git",
20+
"sha": "21c3df31de4bc2f838c945bd37d185210d9bab1a",
21+
"module_command": "pytest -c /dev/null tests examples",
22+
"setup": [
23+
"pip install -r requirements/tests.txt",
24+
"pip install -q -e examples/tutorial[test]",
25+
"pip install -q -e examples/javascript[test]"
26+
]
1727
}
1828
}

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import dataclasses
2+
import inspect
23
import logging
34
import os
45
import sys
@@ -104,6 +105,9 @@ class Callee:
104105
def _unkown_module_fixup(func):
105106
# TODO: Doesn't work for everything (for example: `OrderedDict.fromkeys`, `object.__new__`)
106107

108+
# TODO: Can make this logic easier by using `func.__self__`. For `f = dict().get`, `f.__self__.__class__ == dict`
109+
# and `dict.__new__.__self__ = dict`
110+
107111
module = func.__module__
108112
qualname = func.__qualname__
109113
cls_name, method_name = qualname.split(".")
@@ -114,12 +118,20 @@ def _unkown_module_fixup(func):
114118

115119
matching_classes = list()
116120
for klass in object.__subclasses__():
117-
# type(dict.get) == METHOD_DESCRIPTOR_TYPE
118-
# type(dict.__new__) == BUILTIN_FUNCTION_OR_METHOD
119-
if klass.__qualname__ == cls_name and type(
120-
getattr(klass, method_name, None)
121-
) in [BUILTIN_FUNCTION_OR_METHOD, METHOD_DESCRIPTOR_TYPE]:
122-
matching_classes.append(klass)
121+
122+
if inspect.isabstract(klass):
123+
continue
124+
125+
try:
126+
# type(dict.get) == METHOD_DESCRIPTOR_TYPE
127+
# type(dict.__new__) == BUILTIN_FUNCTION_OR_METHOD
128+
if klass.__qualname__ == cls_name and type(
129+
getattr(klass, method_name, None)
130+
) in [BUILTIN_FUNCTION_OR_METHOD, METHOD_DESCRIPTOR_TYPE]:
131+
matching_classes.append(klass)
132+
# For flask, observed to give `ValueError: Namespace class is abstract`, even with the isabstract above
133+
except ValueError:
134+
pass
123135

124136
if len(matching_classes) == 1:
125137
klass = matching_classes[0]

0 commit comments

Comments
 (0)