Skip to content

Commit 47023c2

Browse files
yoffCopilot
andcommitted
Python: test closure instance cross-talk
Add an inline type-tracking regression for two closures created from the same wrapper AST with different captured callables. Keep positive controls for the sensitive callable and ordinary captured data while marking the safe closure result as the current spurious flow. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
1 parent 1a8e317 commit 47023c2

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

  • python/ql/test/library-tests/dataflow/typetracking

python/ql/test/library-tests/dataflow/typetracking/test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,28 @@ def use_funcs_with_decorators():
107107
x = get_tracked2() # $ tracked
108108
y = unrelated_func() # $ SPURIOUS: tracked
109109

110+
# A decorator factory creates a distinct closure each time it is called. The captured
111+
# callable and data therefore belong to that closure instance, even though every
112+
# instance comes from the same wrapper AST.
113+
def make_capturing_wrapper(func, captured): # $ tracked
114+
def wrapper():
115+
print(captured) # $ tracked
116+
return func() # $ tracked
117+
return wrapper
118+
119+
def closure_sensitive_func():
120+
return tracked # $ tracked
121+
122+
def closure_safe_func():
123+
return "safe"
124+
125+
closure_sensitive = make_capturing_wrapper(closure_sensitive_func, tracked) # $ tracked
126+
closure_safe = make_capturing_wrapper(closure_safe_func, "safe")
127+
128+
def use_capturing_wrappers():
129+
sensitive = closure_sensitive() # $ tracked
130+
safe = closure_safe() # $ SPURIOUS: tracked
131+
110132
# ------------------------------------------------------------------------------
111133

112134
def expects_int(x): # $ int

0 commit comments

Comments
 (0)