Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ python trace.py <path to python file>

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

### Supported value types

The tracer currently understands a subset of Python's built‑in types. It can
record integers, booleans, strings, lists, floats, tuples, byte strings,
complex numbers and simple objects (via ``__dict__`` introspection). Values that
don't fall in these categories are stored as raw strings.

Dictionary and set types are not yet handled.

## Future directions

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).
Expand Down
35 changes: 33 additions & 2 deletions src/trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ def _register_builtin_types(self) -> None:
self._register_type(12, "Bool")
self._register_type(9, "Symbol")
self._register_type(24, "No type")
self._register_type(8, "Float")
self._register_type(27, "Tuple")
self._register_type(16, "Bytes")
self._register_type(6, "Complex")

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

def _value(self, val: Any) -> Dict[str, Any]:
if isinstance(val, int):
return {"kind": "Int", "type_id": self.types["Integer"], "i": val}
if isinstance(val, bool):
return {"kind": "Bool", "type_id": self.types["Bool"], "b": val}
if isinstance(val, int):
return {"kind": "Int", "type_id": self.types["Integer"], "i": val}
if isinstance(val, float):
type_id = self._ensure_type(8, "Float")
return {"kind": "Float", "type_id": type_id, "f": val}
if isinstance(val, str):
return {"kind": "String", "type_id": self.types["String"], "text": val}
if isinstance(val, (bytes, bytearray)):
type_id = self._ensure_type(16, "Bytes")
return {"kind": "Raw", "type_id": type_id, "r": str(val)}
if isinstance(val, list):
type_id = self._ensure_type(0, "Array")
return {
Expand All @@ -133,8 +143,29 @@ def _value(self, val: Any) -> Dict[str, Any]:
"elements": [self._value(v) for v in val],
"is_slice": False,
}
if isinstance(val, tuple):
type_id = self._ensure_type(27, "Tuple")
return {
"kind": "Tuple",
"type_id": type_id,
"elements": [self._value(v) for v in val],
}
if isinstance(val, complex):
type_id = self._ensure_type(6, "Complex")
return {
"kind": "Struct",
"type_id": type_id,
"field_values": [self._value(val.real), self._value(val.imag)],
}
if val is None:
return {"kind": "None", "type_id": self.types["No type"]}
if hasattr(val, "__dict__"):
type_id = self._ensure_type(6, val.__class__.__name__)
return {
"kind": "Struct",
"type_id": type_id,
"field_values": [self._value(v) for v in val.__dict__.values()],
}
type_id = self._ensure_type(16, "Object")
return {"kind": "Raw", "type_id": type_id, "r": str(val)}

Expand Down
62 changes: 49 additions & 13 deletions tests/fixtures/array_sum.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@
}
}
},
{
"Type": {
"kind": 8,
"lang_type": "Float",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 27,
"lang_type": "Tuple",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 16,
"lang_type": "Bytes",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 6,
"lang_type": "Complex",
"specific_info": {
"kind": "None"
}
}
},
{
"Path": ""
},
Expand Down Expand Up @@ -111,7 +147,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -148,7 +184,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -183,7 +219,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -216,7 +252,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -262,7 +298,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -318,7 +354,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -371,7 +407,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -424,7 +460,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -477,7 +513,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -530,7 +566,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -583,7 +619,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -636,7 +672,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down Expand Up @@ -718,7 +754,7 @@
"variable_id": 0,
"value": {
"kind": "Sequence",
"type_id": 5,
"type_id": 9,
"elements": [
{
"kind": "Int",
Expand Down
36 changes: 36 additions & 0 deletions tests/fixtures/calc.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@
}
}
},
{
"Type": {
"kind": 8,
"lang_type": "Float",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 27,
"lang_type": "Tuple",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 16,
"lang_type": "Bytes",
"specific_info": {
"kind": "None"
}
}
},
{
"Type": {
"kind": 6,
"lang_type": "Complex",
"specific_info": {
"kind": "None"
}
}
},
{
"Path": ""
},
Expand Down
Loading
Loading