author | Pierre-Yves David <pierre-yves.david@octobus.net> |
Tue, 11 Mar 2025 02:29:42 +0100 | |
branch | stable |
changeset 53042 | cdd7bf612c7b |
parent 49643 | e1c586b9a43c |
permissions | -rw-r--r-- |
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
1 |
# SPDX-License-Identifier: MIT |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
2 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
3 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
4 |
import inspect |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
5 |
import platform |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
6 |
import sys |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
7 |
import threading |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
8 |
import types |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
9 |
import warnings |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
10 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
11 |
from collections.abc import Mapping, Sequence # noqa |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
12 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
13 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
14 |
PYPY = platform.python_implementation() == "PyPy" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
15 |
PY36 = sys.version_info[:2] >= (3, 6) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
16 |
HAS_F_STRINGS = PY36 |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
17 |
PY310 = sys.version_info[:2] >= (3, 10) |
34397 | 18 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
19 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
20 |
if PYPY or PY36: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
21 |
ordered_dict = dict |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
22 |
else: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
23 |
from collections import OrderedDict |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
24 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
25 |
ordered_dict = OrderedDict |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
26 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
27 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
28 |
def just_warn(*args, **kw): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
29 |
warnings.warn( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
30 |
"Running interpreter doesn't sufficiently support code object " |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
31 |
"introspection. Some features like bare super() or accessing " |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
32 |
"__class__ will not work with slotted classes.", |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
33 |
RuntimeWarning, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
34 |
stacklevel=2, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
35 |
) |
34397 | 36 |
|
37 |
||
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
38 |
class _AnnotationExtractor: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
39 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
40 |
Extract type annotations from a callable, returning None whenever there |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
41 |
is none. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
42 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
43 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
44 |
__slots__ = ["sig"] |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
45 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
46 |
def __init__(self, callable): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
47 |
try: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
48 |
self.sig = inspect.signature(callable) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
49 |
except (ValueError, TypeError): # inspect failed |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
50 |
self.sig = None |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
51 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
52 |
def get_first_param_type(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
53 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
54 |
Return the type annotation of the first argument if it's not empty. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
55 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
56 |
if not self.sig: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
57 |
return None |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
58 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
59 |
params = list(self.sig.parameters.values()) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
60 |
if params and params[0].annotation is not inspect.Parameter.empty: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
61 |
return params[0].annotation |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
62 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
63 |
return None |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
64 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
65 |
def get_return_type(self): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
66 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
67 |
Return the return type if it's not empty. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
68 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
69 |
if ( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
70 |
self.sig |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
71 |
and self.sig.return_annotation is not inspect.Signature.empty |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
72 |
): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
73 |
return self.sig.return_annotation |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
74 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
75 |
return None |
34397 | 76 |
|
77 |
||
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
78 |
def make_set_closure_cell(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
79 |
"""Return a function of two arguments (cell, value) which sets |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
80 |
the value stored in the closure cell `cell` to `value`. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
81 |
""" |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
82 |
# pypy makes this easy. (It also supports the logic below, but |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
83 |
# why not do the easy/fast thing?) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
84 |
if PYPY: |
34397 | 85 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
86 |
def set_closure_cell(cell, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
87 |
cell.__setstate__((value,)) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
88 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
89 |
return set_closure_cell |
34397 | 90 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
91 |
# Otherwise gotta do it the hard way. |
34397 | 92 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
93 |
# Create a function that will set its first cellvar to `value`. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
94 |
def set_first_cellvar_to(value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
95 |
x = value |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
96 |
return |
34397 | 97 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
98 |
# This function will be eliminated as dead code, but |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
99 |
# not before its reference to `x` forces `x` to be |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
100 |
# represented as a closure cell rather than a local. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
101 |
def force_x_to_be_a_cell(): # pragma: no cover |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
102 |
return x |
34397 | 103 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
104 |
try: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
105 |
# Extract the code object and make sure our assumptions about |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
106 |
# the closure behavior are correct. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
107 |
co = set_first_cellvar_to.__code__ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
108 |
if co.co_cellvars != ("x",) or co.co_freevars != (): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
109 |
raise AssertionError # pragma: no cover |
34397 | 110 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
111 |
# Convert this code object to a code object that sets the |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
112 |
# function's first _freevar_ (not cellvar) to the argument. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
113 |
if sys.version_info >= (3, 8): |
34397 | 114 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
115 |
def set_closure_cell(cell, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
116 |
cell.cell_contents = value |
34397 | 117 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
118 |
else: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
119 |
args = [co.co_argcount] |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
120 |
args.append(co.co_kwonlyargcount) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
121 |
args.extend( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
122 |
[ |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
123 |
co.co_nlocals, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
124 |
co.co_stacksize, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
125 |
co.co_flags, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
126 |
co.co_code, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
127 |
co.co_consts, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
128 |
co.co_names, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
129 |
co.co_varnames, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
130 |
co.co_filename, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
131 |
co.co_name, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
132 |
co.co_firstlineno, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
133 |
co.co_lnotab, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
134 |
# These two arguments are reversed: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
135 |
co.co_cellvars, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
136 |
co.co_freevars, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
137 |
] |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
138 |
) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
139 |
set_first_freevar_code = types.CodeType(*args) |
34397 | 140 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
141 |
def set_closure_cell(cell, value): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
142 |
# Create a function using the set_first_freevar_code, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
143 |
# whose first closure cell is `cell`. Calling it will |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
144 |
# change the value of that cell. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
145 |
setter = types.FunctionType( |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
146 |
set_first_freevar_code, {}, "setter", (), (cell,) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
147 |
) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
148 |
# And call it to set the cell. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
149 |
setter(value) |
34397 | 150 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
151 |
# Make sure it works on this interpreter: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
152 |
def make_func_with_cell(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
153 |
x = None |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
154 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
155 |
def func(): |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
156 |
return x # pragma: no cover |
34397 | 157 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
158 |
return func |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
159 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
160 |
cell = make_func_with_cell().__closure__[0] |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
161 |
set_closure_cell(cell, 100) |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
162 |
if cell.cell_contents != 100: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
163 |
raise AssertionError # pragma: no cover |
34397 | 164 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
165 |
except Exception: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
166 |
return just_warn |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
167 |
else: |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
168 |
return set_closure_cell |
34397 | 169 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
170 |
|
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
171 |
set_closure_cell = make_set_closure_cell() |
34397 | 172 |
|
49643
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
173 |
# Thread-local global to track attrs instances which are already being repr'd. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
174 |
# This is needed because there is no other (thread-safe) way to pass info |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
175 |
# about the instances that are already being repr'd through the call stack |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
176 |
# in order to ensure we don't perform infinite recursion. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
177 |
# |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
178 |
# For instance, if an instance contains a dict which contains that instance, |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
179 |
# we need to know that we're already repr'ing the outside instance from within |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
180 |
# the dict's repr() call. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
181 |
# |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
182 |
# This lives here rather than in _make.py so that the functions in _make.py |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
183 |
# don't have a direct reference to the thread-local in their globals dict. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
184 |
# If they have such a reference, it breaks cloudpickle. |
e1c586b9a43c
attr: vendor 22.1.0
Matt Harbison <matt_harbison@yahoo.com>
parents:
34397
diff
changeset
|
185 |
repr_context = threading.local() |