From b377c95bd7ff23a1e26a5390dea63f745e1d993f Mon Sep 17 00:00:00 2001 From: Sachin Saharan Date: Tue, 10 Dec 2024 18:46:39 +0530 Subject: [PATCH] Fix test failures --- lib/python/pyflyby/_saveframe_reader.py | 32 +++++++++++++++---------- tests/test_saveframe_reader.py | 29 +++++++++++++--------- 2 files changed, 37 insertions(+), 24 deletions(-) diff --git a/lib/python/pyflyby/_saveframe_reader.py b/lib/python/pyflyby/_saveframe_reader.py index 92422b34..7bf4e2ff 100644 --- a/lib/python/pyflyby/_saveframe_reader.py +++ b/lib/python/pyflyby/_saveframe_reader.py @@ -245,11 +245,14 @@ def get_metadata(self, metadata, frame_idx=None): # Unpickle the 'function_object' metadata value. if metadata == "function_object": try: - metadata_value = pickle.loads(metadata_value) - except Exception: + if not isinstance(metadata_value, str): + metadata_value = pickle.loads(metadata_value) + except Exception as err: logging.warning("Can't unpickle the 'function_object' " - "value for frame: %a", frame_idx) - metadata_value = "Can't unpickle the 'function_object'" + "value for frame: %a. Error: %s", + key_item, err) + metadata_value = ( + f"Can't unpickle the 'function_object'. Error: {err}") frame_idx_to_metadata_value_map[key_item] = metadata_value return frame_idx_to_metadata_value_map @@ -262,10 +265,12 @@ def get_metadata(self, metadata, frame_idx=None): metadata_value = self._data[frame_idx][metadata] if metadata == "function_object": try: - metadata_value = pickle.loads(metadata_value) - except Exception: + if not isinstance(metadata_value, str): + metadata_value = pickle.loads(metadata_value) + except Exception as err: logging.warning("Can't unpickle the 'function_object' " - "value for frame: %a", frame_idx) + "value for frame: %a. Error: %s", + frame_idx, err) return metadata_value except KeyError: allowed_frame_idx = list( @@ -353,11 +358,12 @@ def get_variables(self, variables, frame_idx=None): continue try: variable_value = pickle.loads(variable_value) - except Exception: + except Exception as err: logging.warning( "Can't un-pickle the value of variable %a for frame " - "%a", variable, frame_idx) - variable_value = "Can't un-pickle the variable." + "%a. Error: %s", variable, key_item, err) + variable_value = ( + f"Can't un-pickle the variable. Error: {err}") if len(variables) == 1: # Single variable is queried. frame_idx_to_variables_map[key_item] = variable_value @@ -398,12 +404,12 @@ def get_variables(self, variables, frame_idx=None): continue try: variable_value = pickle.loads(variable_value) - except Exception: + except Exception as err: logging.warning( "Can't un-pickle the value of variable %a for frame " - "%a", variable, frame_idx) + "%a. Error: %s", variable, frame_idx, err) if len(variables) > 1: - variable_value = "Can't un-pickle the variable." + variable_value = f"Can't un-pickle the variable. Error: {err}" if len(variables) == 1: # Single variable is queried. Directly return the value. return variable_value diff --git a/tests/test_saveframe_reader.py b/tests/test_saveframe_reader.py index 78560d66..8ebac360 100644 --- a/tests/test_saveframe_reader.py +++ b/tests/test_saveframe_reader.py @@ -145,6 +145,10 @@ def call_saveframe(pkg_name, tmpdir, frames): return filename +def get_func2_qualname(): + return "func2" if VERSION_INFO < (3, 11) else "mod2_cls.func2" + + def test_saveframe_reader_repr_1(tmpdir): pkg_name = create_pkg(tmpdir) filename = call_saveframe(pkg_name, tmpdir, frames=1) @@ -174,7 +178,7 @@ def test_saveframe_reader_repr_2(tmpdir): f'\'{tmpdir}/{pkg_name}/pkg1/pkg2/mod3.py,6,func3\'\n Code: raise ' 'ValueError("Error is raised")\n Variables: [\'var1\', \'var2\', \'' f'func3_var3\']\n\nFrame 2:\n Filename: \'{tmpdir}/{pkg_name}/pkg1/' - 'mod2.py\'\n Line Number: 10\n Function: mod2_cls.func2\n Module: ' + f'mod2.py\'\n Line Number: 10\n Function: {get_func2_qualname()}\n Module: ' f'{pkg_name}.pkg1.mod2\n Frame ID: \'{tmpdir}/{pkg_name}/pkg1/mod2.py,' '10,func2\'\n Code: func3()\n Variables: [\'self\', \'var1\', \'var2' '\']\n\nException:\n Full String: ValueError: Error is ' @@ -229,12 +233,12 @@ def test_saveframe_reader_str_2(tmpdir): f'pkg2.mod3\n Frame ID: \'{tmpdir}/{pkg_name}/pkg1/pkg2/mod3.py,6,func3' '\'\n Code: raise ValueError("Error is raised")\n Variables: [\'var1' f'\', \'var2\', \'func3_var3\']\n\nFrame 2:\n Filename: \'{tmpdir}/' - f'{pkg_name}/pkg1/mod2.py\'\n Line Number: 10\n Function: mod2_cls.func2' - f'\n Module: {pkg_name}.pkg1.mod2\n Frame ID: \'{tmpdir}/{pkg_name}/' - 'pkg1/mod2.py,10,func2\'\n Code: func3()\n Variables: [\'self\', ' - '\'var1\', \'var2\']\n\nException:\n Full String: ValueError: Error is ' - 'raised\n String: Error is raised\n Class Name: ValueError\n ' - 'Qualified Name: ValueError\n') + f'{pkg_name}/pkg1/mod2.py\'\n Line Number: 10\n Function: ' + f'{get_func2_qualname()}\n Module: {pkg_name}.pkg1.mod2\n Frame ID: ' + f'\'{tmpdir}/{pkg_name}/pkg1/mod2.py,10,func2\'\n Code: func3()\n ' + f'Variables: [\'self\', \'var1\', \'var2\']\n\nException:\n Full ' + f'String: ValueError: Error is raised\n String: Error is raised\n ' + f'Class Name: ValueError\n Qualified Name: ValueError\n') assert str(reader) == expected @@ -357,11 +361,11 @@ def test_get_metadata_4(tmpdir): reader = SaveframeReader(filename) result = reader.get_metadata("function_qualname") - expected = {1: 'func3', 2: 'mod2_cls.func2', 3: 'func2', 4: 'func1'} + expected = {1: 'func3', 2: get_func2_qualname(), 3: 'func2', 4: 'func1'} assert result == expected result = reader.get_metadata("function_qualname", frame_idx=2) - expected = 'mod2_cls.func2' + expected = get_func2_qualname() assert result == expected @@ -372,7 +376,7 @@ def test_get_metadata_5(tmpdir): frame_idx_to_info = { 1: {'name': 'func3', 'qualname': 'func3'}, - 2: {'name': 'func2', 'qualname': 'mod2_cls.func2'}, + 2: {'name': 'func2', 'qualname': get_func2_qualname()}, 3: {'name': 'func2', 'qualname': 'func2'}, 4: {'name': 'func1', 'qualname': 'func1'}, 5: {'name': 'init_func1', 'qualname': 'init_func1'} @@ -382,13 +386,16 @@ def test_get_metadata_5(tmpdir): assert list(result.keys()) == [1, 2, 3, 4, 5] for key in result: func = result[key] + if isinstance(func, str): + continue name = func.__name__ qualname = func.__qualname__ assert name == frame_idx_to_info[key]['name'] assert qualname == frame_idx_to_info[key]['qualname'] result = reader.get_metadata("function_object", 2) - assert result.__qualname__ == 'mod2_cls.func2' + if not isinstance(result, str): + assert result.__qualname__ == get_func2_qualname() def test_get_metadata_6(tmpdir):