Svenskithesource/pySpy

[BUG] Add const feature doesn't work as expect

Closed this issue · 0 comments

There is currently two bugs with the current state of the code, the first one is present at:

pySpy/pyspy/pyspy.py

Lines 120 to 122 in 31bc912

current_file.co_names = list(current_file.co_consts)
current_file.co_consts.append(value)
current_file.co_names = tuple(current_file.co_consts)

It's applying the co_consts value in the co_names property incorrectly.

The second bug is here:

pySpy/pyspy/pyspy.py

Lines 326 to 330 in 31bc912

if not isinstance(value, types.CodeType) and not isinstance(value, editor.Code):
with dpg.table_row():
dpg.add_text(index)
dpg.add_input_text(default_value=repr(value), tag=f"const_{index}", width=400,
user_data="co_consts_apply", callback=apply_changes, on_enter=True)

Whenever there is a code object within the co_consts attribute, this object gets skipped, but the index does not remain the same, since it is coming from an enumerate function.

This results in a bug in the function that applies the changes that happened to the consts, since the index of the code object that was skipped, will return None.

pySpy/pyspy/pyspy.py

Lines 65 to 71 in 31bc912

while True:
value = dpg.get_value(f"const_{i}")
if value is None:
break
new_consts.append(ast.literal_eval(value))
i += 1

The "ideal" solution would be having a custom counter, but this would result on yet another bug, if we look closely at the code mentioned above, we can see that it updates the co_consts based on the list that is saved at the Dear PyGui internals. But if we skip the code while adding and displaying it, the code will never be there, which would cause every code object to be lost from the co_consts list.

We need to find a way to display the code objects but do not allow the user to modify it on the const view, and then get this code objects while saving the new co_const list, since simply calling ast.literal_eval wouldn't work in this scenario.