atrace 0.1.1__tar.gz → 0.1.2__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: atrace
3
- Version: 0.1.1
3
+ Version: 0.1.2
4
4
  Summary: Generate trace tables for programs
5
5
  Project-URL: Repository, https://github.com/nwolff/atrace.git
6
6
  Author-email: Nicholas Wolff <nwolff@gmail.com>
@@ -9,14 +9,21 @@ Description-Content-Type: text/markdown
9
9
 
10
10
  # TODO:
11
11
 
12
- - entering a function, binding the local arguments, returning
13
12
  - ignore function definitions (look at the type of object)
14
13
 
15
14
  - display at end only
16
15
 
17
- # Usage and intent
16
+ - entering a function, binding the local arguments, returning
17
+
18
+ # Usage
18
19
 
19
- A package that automatically prints a trace table of a program, just by importing the module
20
+ Automatically prints a trace table of a program once the execution is finished.
21
+
22
+ Just import the module.
23
+
24
+ An animated example of a trace table: https://www.101computing.net/using-trace-tables/
25
+
26
+ # Requirements
20
27
 
21
28
  - Should not interfere with the running program (apart from capturing stdout)
22
29
  - Should display the trace at the end of execution (not while the program is interacting with the user)
@@ -28,7 +35,10 @@ A package that automatically prints a trace table of a program, just by importin
28
35
  - binding the local arguments
29
36
  - returning
30
37
 
31
- An animated example of a trace table: https://www.101computing.net/using-trace-tables/
38
+ # Supported environments
39
+
40
+ - Thonny, which adds a shitload of magic
41
+ - python 3.10+
32
42
 
33
43
  # Not in scope
34
44
 
@@ -1,13 +1,20 @@
1
1
  # TODO:
2
2
 
3
- - entering a function, binding the local arguments, returning
4
3
  - ignore function definitions (look at the type of object)
5
4
 
6
5
  - display at end only
7
6
 
8
- # Usage and intent
7
+ - entering a function, binding the local arguments, returning
8
+
9
+ # Usage
9
10
 
10
- A package that automatically prints a trace table of a program, just by importing the module
11
+ Automatically prints a trace table of a program once the execution is finished.
12
+
13
+ Just import the module.
14
+
15
+ An animated example of a trace table: https://www.101computing.net/using-trace-tables/
16
+
17
+ # Requirements
11
18
 
12
19
  - Should not interfere with the running program (apart from capturing stdout)
13
20
  - Should display the trace at the end of execution (not while the program is interacting with the user)
@@ -19,7 +26,10 @@ A package that automatically prints a trace table of a program, just by importin
19
26
  - binding the local arguments
20
27
  - returning
21
28
 
22
- An animated example of a trace table: https://www.101computing.net/using-trace-tables/
29
+ # Supported environments
30
+
31
+ - Thonny, which adds a shitload of magic
32
+ - python 3.10+
23
33
 
24
34
  # Not in scope
25
35
 
@@ -1,6 +1,6 @@
1
1
  [project]
2
2
  name = "atrace"
3
- version = "0.1.1"
3
+ version = "0.1.2"
4
4
  description = "Generate trace tables for programs"
5
5
  readme = "README.md"
6
6
  authors = [{ name = "Nicholas Wolff", email = "nwolff@gmail.com" }]
@@ -4,24 +4,20 @@ import sys
4
4
  from types import FrameType
5
5
  from typing import Any
6
6
 
7
- paused = True
7
+ module_of_interest = None
8
8
  last_locals = {}
9
9
 
10
- ignored_variables = set(
11
- [
12
- "tid",
13
- ]
14
- )
15
10
 
16
-
17
- def ignore_variable(var: str):
18
- return var in ignored_variables
11
+ def ignore_variable(name: str, value: Any):
12
+ # if name.startswith("_"):
13
+ # return True
14
+ return callable(value) # Functions, modules, tout ça
19
15
 
20
16
 
21
17
  def copy_carefully_using_ignored_variables(d: dict[str, Any]):
22
18
  res = {}
23
19
  for k, v in d.items():
24
- if not ignore_variable(k):
20
+ if not ignore_variable(k, v):
25
21
  res[k] = copy.deepcopy(v)
26
22
  return res
27
23
 
@@ -38,9 +34,8 @@ def copy_carefully(d: dict[str, Any]):
38
34
 
39
35
 
40
36
  def trace_vars(frame: FrameType, event: str, arg: Any):
41
- if paused:
37
+ if inspect.getmodule(frame) != module_of_interest:
42
38
  return
43
- # print(frame.f_lineno, frame.f_code.co_name, frame.f_locals, event)
44
39
  if event != "line":
45
40
  return trace_vars
46
41
  code = frame.f_code
@@ -58,15 +53,15 @@ def trace_vars(frame: FrameType, event: str, arg: Any):
58
53
  old_locals = last_locals[code.co_name]
59
54
 
60
55
  for var, new_val in locals_now.items():
61
- if not ignore_variable(var):
56
+ if not ignore_variable(var, new_val):
62
57
  if var not in old_locals:
63
58
  print(f"[{lineno}] NEW {var} = {new_val}")
64
59
  elif old_locals[var] != new_val:
65
60
  print(f"[{lineno}] MODIFIED {var}: {old_locals[var]} → {new_val}")
66
61
 
67
- for var in old_locals:
68
- if not ignore_variable(var):
69
- if var not in locals_now:
62
+ for var, old_val in old_locals.items():
63
+ if var not in locals_now:
64
+ if not ignore_variable(var, old_val):
70
65
  print(f"[{lineno}] DELETED {var}")
71
66
 
72
67
  last_locals[code.co_name] = locals_now
@@ -87,15 +82,16 @@ def get_importer_frame():
87
82
  return None
88
83
 
89
84
 
90
- # This will work next time we enter a function.
91
85
  # It also kicks off the tracing machinery (so that the lines below work)
92
- sys.settrace(trace_vars)
86
+ sys.settrace(just_kicking_off)
93
87
 
94
88
 
95
- # This reaches into the importing module's frame to setup tracing
89
+ # This reaches into the importing module's frame to setup tracing of code outside of functions
96
90
  importer_frame = get_importer_frame()
97
91
  if importer_frame:
98
92
  importer_frame.f_trace = trace_vars
99
- paused = False
93
+ module_of_interest = inspect.getmodule(importer_frame)
94
+ # This will work next time we enter a function.
95
+ sys.settrace(trace_vars)
100
96
  else:
101
97
  print("Cannot trace")
@@ -31,4 +31,7 @@ def recursive_count(x):
31
31
  return 1 + recursive_count(x - 1)
32
32
 
33
33
 
34
- print(recursive_count(20))
34
+ print(recursive_count(4))
35
+
36
+
37
+ # raise Exception("an exception")
File without changes
File without changes
File without changes
File without changes