Skip to content

Commit 89683c9

Browse files
committed
feat(tracer): support more python types
1 parent 55a5c93 commit 89683c9

File tree

6 files changed

+1291
-15
lines changed

6 files changed

+1291
-15
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ python trace.py <path to python file>
1818

1919
however you probably want to use it in combination with CodeTracer, which would be released soon.
2020

21+
### Supported value types
22+
23+
The tracer currently understands a subset of Python's built‑in types. It can
24+
record integers, booleans, strings, lists, floats, tuples, byte strings,
25+
complex numbers and simple objects (via ``__dict__`` introspection). Values that
26+
don't fall in these categories are stored as raw strings.
27+
28+
Dictionary and set types are not yet handled.
29+
2130
## Future directions
2231

2332
The current Python support is an unfinished prototype. We can finish it. In the future, it may be expanded to function in a way to similar to the more complete implementations, e.g. [Noir](https://github.com/blocksense-network/noir/tree/blocksense/tooling/tracer).

src/trace.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,10 @@ def _register_builtin_types(self) -> None:
6161
self._register_type(12, "Bool")
6262
self._register_type(9, "Symbol")
6363
self._register_type(24, "No type")
64+
self._register_type(8, "Float")
65+
self._register_type(27, "Tuple")
66+
self._register_type(16, "Bytes")
67+
self._register_type(6, "Complex")
6468

6569
# -------------------------------------------------------------------- paths
6670
def _register_path(self, path: str) -> int:
@@ -119,12 +123,18 @@ def _ensure_type(self, kind: int, name: str) -> int:
119123
return self.types[name]
120124

121125
def _value(self, val: Any) -> Dict[str, Any]:
122-
if isinstance(val, int):
123-
return {"kind": "Int", "type_id": self.types["Integer"], "i": val}
124126
if isinstance(val, bool):
125127
return {"kind": "Bool", "type_id": self.types["Bool"], "b": val}
128+
if isinstance(val, int):
129+
return {"kind": "Int", "type_id": self.types["Integer"], "i": val}
130+
if isinstance(val, float):
131+
type_id = self._ensure_type(8, "Float")
132+
return {"kind": "Float", "type_id": type_id, "f": val}
126133
if isinstance(val, str):
127134
return {"kind": "String", "type_id": self.types["String"], "text": val}
135+
if isinstance(val, (bytes, bytearray)):
136+
type_id = self._ensure_type(16, "Bytes")
137+
return {"kind": "Raw", "type_id": type_id, "r": str(val)}
128138
if isinstance(val, list):
129139
type_id = self._ensure_type(0, "Array")
130140
return {
@@ -133,8 +143,29 @@ def _value(self, val: Any) -> Dict[str, Any]:
133143
"elements": [self._value(v) for v in val],
134144
"is_slice": False,
135145
}
146+
if isinstance(val, tuple):
147+
type_id = self._ensure_type(27, "Tuple")
148+
return {
149+
"kind": "Tuple",
150+
"type_id": type_id,
151+
"elements": [self._value(v) for v in val],
152+
}
153+
if isinstance(val, complex):
154+
type_id = self._ensure_type(6, "Complex")
155+
return {
156+
"kind": "Struct",
157+
"type_id": type_id,
158+
"field_values": [self._value(val.real), self._value(val.imag)],
159+
}
136160
if val is None:
137161
return {"kind": "None", "type_id": self.types["No type"]}
162+
if hasattr(val, "__dict__"):
163+
type_id = self._ensure_type(6, val.__class__.__name__)
164+
return {
165+
"kind": "Struct",
166+
"type_id": type_id,
167+
"field_values": [self._value(v) for v in val.__dict__.values()],
168+
}
138169
type_id = self._ensure_type(16, "Object")
139170
return {"kind": "Raw", "type_id": type_id, "r": str(val)}
140171

tests/fixtures/array_sum.json

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,42 @@
4444
}
4545
}
4646
},
47+
{
48+
"Type": {
49+
"kind": 8,
50+
"lang_type": "Float",
51+
"specific_info": {
52+
"kind": "None"
53+
}
54+
}
55+
},
56+
{
57+
"Type": {
58+
"kind": 27,
59+
"lang_type": "Tuple",
60+
"specific_info": {
61+
"kind": "None"
62+
}
63+
}
64+
},
65+
{
66+
"Type": {
67+
"kind": 16,
68+
"lang_type": "Bytes",
69+
"specific_info": {
70+
"kind": "None"
71+
}
72+
}
73+
},
74+
{
75+
"Type": {
76+
"kind": 6,
77+
"lang_type": "Complex",
78+
"specific_info": {
79+
"kind": "None"
80+
}
81+
}
82+
},
4783
{
4884
"Path": ""
4985
},
@@ -111,7 +147,7 @@
111147
"variable_id": 0,
112148
"value": {
113149
"kind": "Sequence",
114-
"type_id": 5,
150+
"type_id": 9,
115151
"elements": [
116152
{
117153
"kind": "Int",
@@ -148,7 +184,7 @@
148184
"variable_id": 0,
149185
"value": {
150186
"kind": "Sequence",
151-
"type_id": 5,
187+
"type_id": 9,
152188
"elements": [
153189
{
154190
"kind": "Int",
@@ -183,7 +219,7 @@
183219
"variable_id": 0,
184220
"value": {
185221
"kind": "Sequence",
186-
"type_id": 5,
222+
"type_id": 9,
187223
"elements": [
188224
{
189225
"kind": "Int",
@@ -216,7 +252,7 @@
216252
"variable_id": 0,
217253
"value": {
218254
"kind": "Sequence",
219-
"type_id": 5,
255+
"type_id": 9,
220256
"elements": [
221257
{
222258
"kind": "Int",
@@ -262,7 +298,7 @@
262298
"variable_id": 0,
263299
"value": {
264300
"kind": "Sequence",
265-
"type_id": 5,
301+
"type_id": 9,
266302
"elements": [
267303
{
268304
"kind": "Int",
@@ -318,7 +354,7 @@
318354
"variable_id": 0,
319355
"value": {
320356
"kind": "Sequence",
321-
"type_id": 5,
357+
"type_id": 9,
322358
"elements": [
323359
{
324360
"kind": "Int",
@@ -371,7 +407,7 @@
371407
"variable_id": 0,
372408
"value": {
373409
"kind": "Sequence",
374-
"type_id": 5,
410+
"type_id": 9,
375411
"elements": [
376412
{
377413
"kind": "Int",
@@ -424,7 +460,7 @@
424460
"variable_id": 0,
425461
"value": {
426462
"kind": "Sequence",
427-
"type_id": 5,
463+
"type_id": 9,
428464
"elements": [
429465
{
430466
"kind": "Int",
@@ -477,7 +513,7 @@
477513
"variable_id": 0,
478514
"value": {
479515
"kind": "Sequence",
480-
"type_id": 5,
516+
"type_id": 9,
481517
"elements": [
482518
{
483519
"kind": "Int",
@@ -530,7 +566,7 @@
530566
"variable_id": 0,
531567
"value": {
532568
"kind": "Sequence",
533-
"type_id": 5,
569+
"type_id": 9,
534570
"elements": [
535571
{
536572
"kind": "Int",
@@ -583,7 +619,7 @@
583619
"variable_id": 0,
584620
"value": {
585621
"kind": "Sequence",
586-
"type_id": 5,
622+
"type_id": 9,
587623
"elements": [
588624
{
589625
"kind": "Int",
@@ -636,7 +672,7 @@
636672
"variable_id": 0,
637673
"value": {
638674
"kind": "Sequence",
639-
"type_id": 5,
675+
"type_id": 9,
640676
"elements": [
641677
{
642678
"kind": "Int",
@@ -718,7 +754,7 @@
718754
"variable_id": 0,
719755
"value": {
720756
"kind": "Sequence",
721-
"type_id": 5,
757+
"type_id": 9,
722758
"elements": [
723759
{
724760
"kind": "Int",

tests/fixtures/calc.json

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,42 @@
4444
}
4545
}
4646
},
47+
{
48+
"Type": {
49+
"kind": 8,
50+
"lang_type": "Float",
51+
"specific_info": {
52+
"kind": "None"
53+
}
54+
}
55+
},
56+
{
57+
"Type": {
58+
"kind": 27,
59+
"lang_type": "Tuple",
60+
"specific_info": {
61+
"kind": "None"
62+
}
63+
}
64+
},
65+
{
66+
"Type": {
67+
"kind": 16,
68+
"lang_type": "Bytes",
69+
"specific_info": {
70+
"kind": "None"
71+
}
72+
}
73+
},
74+
{
75+
"Type": {
76+
"kind": 6,
77+
"lang_type": "Complex",
78+
"specific_info": {
79+
"kind": "None"
80+
}
81+
}
82+
},
4783
{
4884
"Path": ""
4985
},

0 commit comments

Comments
 (0)