rocky / python-uncompyle6

A cross-version Python bytecode decompiler
GNU General Public License v3.0
3.74k stars 408 forks source link

Fix parse37base.py local variable 'v' referenced before assignment #388

Closed djerryz closed 2 years ago

djerryz commented 2 years ago
  1. Fix Exception: line 472, in customize_grammar_rules "BUILD_SLICE value must be 2 or 3; is %s" % v UnboundLocalError: local variable 'v' referenced before assignment
  2. define v in a better place
rocky commented 2 years ago

Thanks for the bug report and fix. As I look at this, the problem with kind of solution is that sets a variable (and here something with a not very descriptive name v ) when it is meaningless.

Adding try except isn't the kind of programming construct that should be used in Python programs when it is necessary. Here, there are two alternatives.

First, one can know whether there is an attr by the opcode type: opcodes below value HAVE_ARGUMENT do now have an operand and do not have attr.

The second approach in 83ab853 is to just not use v here but use token.attr which I prefer because it is more specific.

In general, as a programming principle, I think it better programming practice to set variables only when they are needed rather than set a non-descriptive name like v that is used sometimes and sometimes not, especially in a function this long and complex.

I realize though that this is something of a matter of taste. (If v were named operand_value which is a clearer name, in my opinion that would have been a stronger reason to set it once. But here I would have only set it if it had been used.)

djerryz commented 2 years ago

Thanks. Good Job.