Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid linecache unique file generation in case of linecache disabled #461

Merged
merged 3 commits into from
Nov 27, 2023
Merged
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
1 change: 1 addition & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Fix a regression when unstructuring `Any | None`.
([#453](https://github.com/python-attrs/cattrs/issues/453))
- 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))

## 23.2.1 (2023-11-18)

Expand Down
22 changes: 11 additions & 11 deletions src/cattrs/gen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,22 +212,20 @@ def make_dict_unstructure_fn(
+ [" return res"]
)
script = "\n".join(total_lines)

fname = generate_unique_filename(
cl, "unstructure", reserve=_cattrs_use_linecache
)

eval(compile(script, fname, "exec"), globs)

fn = globs[fn_name]
fname = ""
if _cattrs_use_linecache:
fname = generate_unique_filename(
cl, "unstructure", reserve=_cattrs_use_linecache
)
linecache.cache[fname] = len(script), None, total_lines, fname

eval(compile(script, fname, "exec"), globs)
finally:
working_set.remove(cl)
if not working_set:
del already_generating.working_set

return fn
return globs[fn_name]


DictStructureFn = Callable[[Mapping[str, Any], Any], T]
Expand Down Expand Up @@ -628,12 +626,14 @@ def make_dict_structure_fn(
*pi_lines,
]

fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
script = "\n".join(total_lines)
eval(compile(script, fname, "exec"), globs)
fname = ""
if _cattrs_use_linecache:
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
linecache.cache[fname] = len(script), None, total_lines, fname

eval(compile(script, fname, "exec"), globs)

return globs[fn_name]


Expand Down
20 changes: 10 additions & 10 deletions src/cattrs/gen/typeddicts.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,21 +225,20 @@ def make_dict_unstructure_fn(
]
script = "\n".join(total_lines)

fname = generate_unique_filename(
cl, "unstructure", reserve=_cattrs_use_linecache
)

eval(compile(script, fname, "exec"), globs)

fn = globs[fn_name]
fname = ""
if _cattrs_use_linecache:
fname = generate_unique_filename(
cl, "unstructure", reserve=_cattrs_use_linecache
)
linecache.cache[fname] = len(script), None, total_lines, fname

eval(compile(script, fname, "exec"), globs)
finally:
working_set.remove(cl)
if not working_set:
del already_generating.working_set

return fn
return globs[fn_name]


def make_dict_structure_fn(
Expand Down Expand Up @@ -523,12 +522,13 @@ def make_dict_structure_fn(
" return res",
]

fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
script = "\n".join(total_lines)
eval(compile(script, fname, "exec"), globs)
fname = ""
if _cattrs_use_linecache:
fname = generate_unique_filename(cl, "structure", reserve=_cattrs_use_linecache)
linecache.cache[fname] = len(script), None, total_lines, fname

eval(compile(script, fname, "exec"), globs)
return globs[fn_name]


Expand Down