Skip to content

Commit b47f446

Browse files
sahu-sunilsunil
and
sunil
authored
Avoid linecache unique file generation in case of linecache disabled (#461)
* Avoid linecache unique file generation in case of licecache disabled * Fix syntax error for comma * Avoid unique file generation in case of linecache disabled --------- Co-authored-by: sunil <[email protected]>
1 parent bed932c commit b47f446

File tree

3 files changed

+22
-21
lines changed

3 files changed

+22
-21
lines changed

HISTORY.md

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
- Fix a regression when unstructuring `Any | None`.
66
([#453](https://github.com/python-attrs/cattrs/issues/453))
7+
- Generate unique files only in case of linecache enabled. ([#445](https://github.com/python-attrs/cattrs/issues/445) [#441](https://github.com/python-attrs/cattrs/pull/461))
78

89
## 23.2.1 (2023-11-18)
910

src/cattrs/gen/__init__.py

+11-11
Original file line numberDiff line numberDiff line change
@@ -212,22 +212,20 @@ def make_dict_unstructure_fn(
212212
+ [" return res"]
213213
)
214214
script = "\n".join(total_lines)
215-
216-
fname = generate_unique_filename(
217-
cl, "unstructure", reserve=_cattrs_use_linecache
218-
)
219-
220-
eval(compile(script, fname, "exec"), globs)
221-
222-
fn = globs[fn_name]
215+
fname = ""
223216
if _cattrs_use_linecache:
217+
fname = generate_unique_filename(
218+
cl, "unstructure", reserve=_cattrs_use_linecache
219+
)
224220
linecache.cache[fname] = len(script), None, total_lines, fname
221+
222+
eval(compile(script, fname, "exec"), globs)
225223
finally:
226224
working_set.remove(cl)
227225
if not working_set:
228226
del already_generating.working_set
229227

230-
return fn
228+
return globs[fn_name]
231229

232230

233231
DictStructureFn = Callable[[Mapping[str, Any], Any], T]
@@ -628,12 +626,14 @@ def make_dict_structure_fn(
628626
*pi_lines,
629627
]
630628

631-
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
632629
script = "\n".join(total_lines)
633-
eval(compile(script, fname, "exec"), globs)
630+
fname = ""
634631
if _cattrs_use_linecache:
632+
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
635633
linecache.cache[fname] = len(script), None, total_lines, fname
636634

635+
eval(compile(script, fname, "exec"), globs)
636+
637637
return globs[fn_name]
638638

639639

src/cattrs/gen/typeddicts.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,20 @@ def make_dict_unstructure_fn(
225225
]
226226
script = "\n".join(total_lines)
227227

228-
fname = generate_unique_filename(
229-
cl, "unstructure", reserve=_cattrs_use_linecache
230-
)
231-
232-
eval(compile(script, fname, "exec"), globs)
233-
234-
fn = globs[fn_name]
228+
fname = ""
235229
if _cattrs_use_linecache:
230+
fname = generate_unique_filename(
231+
cl, "unstructure", reserve=_cattrs_use_linecache
232+
)
236233
linecache.cache[fname] = len(script), None, total_lines, fname
234+
235+
eval(compile(script, fname, "exec"), globs)
237236
finally:
238237
working_set.remove(cl)
239238
if not working_set:
240239
del already_generating.working_set
241240

242-
return fn
241+
return globs[fn_name]
243242

244243

245244
def make_dict_structure_fn(
@@ -523,12 +522,13 @@ def make_dict_structure_fn(
523522
" return res",
524523
]
525524

526-
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
527525
script = "\n".join(total_lines)
528-
eval(compile(script, fname, "exec"), globs)
526+
fname = ""
529527
if _cattrs_use_linecache:
528+
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
530529
linecache.cache[fname] = len(script), None, total_lines, fname
531530

531+
eval(compile(script, fname, "exec"), globs)
532532
return globs[fn_name]
533533

534534

0 commit comments

Comments
 (0)