From df8988b6cbcd03c011890978a68891da8e2a4d54 Mon Sep 17 00:00:00 2001 From: sunil Date: Thu, 23 Nov 2023 16:40:57 +0530 Subject: [PATCH 1/3] Avoid linecache unique file generation in case of licecache disabled --- src/cattrs/gen/__init__.py | 22 +++++++++++----------- src/cattrs/gen/typeddicts.py | 20 ++++++++++---------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/cattrs/gen/__init__.py b/src/cattrs/gen/__init__.py index ec7d01b1..eda992a0 100644 --- a/src/cattrs/gen/__init__.py +++ b/src/cattrs/gen/__init__.py @@ -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] @@ -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] diff --git a/src/cattrs/gen/typeddicts.py b/src/cattrs/gen/typeddicts.py index ed02249d..5f412f12 100644 --- a/src/cattrs/gen/typeddicts.py +++ b/src/cattrs/gen/typeddicts.py @@ -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( @@ -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] From 5831157ef7a214ff8bcd2860dc24a6d87615e10b Mon Sep 17 00:00:00 2001 From: sunil Date: Fri, 24 Nov 2023 10:29:22 +0530 Subject: [PATCH 2/3] Fix syntax error for comma --- src/cattrs/gen/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/cattrs/gen/__init__.py b/src/cattrs/gen/__init__.py index eda992a0..cf6dbe39 100644 --- a/src/cattrs/gen/__init__.py +++ b/src/cattrs/gen/__init__.py @@ -219,7 +219,7 @@ def make_dict_unstructure_fn( ) linecache.cache[fname] = len(script), None, total_lines, fname - eval(compile(script, fname "exec"), globs) + eval(compile(script, fname, "exec"), globs) finally: working_set.remove(cl) if not working_set: @@ -632,7 +632,7 @@ def make_dict_structure_fn( 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) + eval(compile(script, fname, "exec"), globs) return globs[fn_name] From bfd2ea8d87f0e266361b64159915507b079151b6 Mon Sep 17 00:00:00 2001 From: Sunil Sahu Date: Mon, 27 Nov 2023 19:32:46 +0530 Subject: [PATCH 3/3] Avoid unique file generation in case of linecache disabled --- HISTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/HISTORY.md b/HISTORY.md index 9c20081c..1e2a36a0 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -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)