Skip to content

Commit

Permalink
Fix test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Sachin Saharan committed Dec 10, 2024
1 parent 734f14b commit b377c95
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 24 deletions.
32 changes: 19 additions & 13 deletions lib/python/pyflyby/_saveframe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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(
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
29 changes: 18 additions & 11 deletions tests/test_saveframe_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 '
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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


Expand All @@ -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'}
Expand All @@ -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):
Expand Down

0 comments on commit b377c95

Please sign in to comment.