Skip to content

Commit

Permalink
Avoid linecache unique file generation in case of linecache disabled (#…
Browse files Browse the repository at this point in the history
…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]>
  • Loading branch information
sahu-sunil and sunil authored Nov 27, 2023
1 parent bed932c commit b47f446
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
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

0 comments on commit b47f446

Please sign in to comment.