Message 187627 - Python tracker (original) (raw)
Attached a new patch that improves the following things:
- added some tests;
- the code in the previous message is now handled correctly;
- the patch now raises a proper SyntaxWarning;
There are still several problems though:
- it doesn't work for the global namespace. Is there a way to know if we are inside a function or not?
- the patch leaks, and I couldn't figure out where the leak is;
- I tried to include the name of the variable in the warning, but:
- PyErr_WarnExplicit wants a const char* as msg and doesn't support formatting;
- It doesn't seem possible to use other PyErr_Warn* functions here;
- Using PyUnicode_FromFormat() works, but then I don't know how to convert the result to const char* (I used PyObject_REPR(), but that is probably wrong; PyUnicode_AS_DATA() might work but is deprecated; the PyUnicode_nBYTE_DATA() functions return a Py_UCSn*);
- more testcases are needed
While compiling I got this warning: ./setup.py:330: SyntaxWarning: "name 'why' is already defined but implicitly deleted after end of except clause" except ImportError as why:
This comes from a code like: try: foo() except Exception as why: pass try: bar() except Exception as why: pass
and in this case there shouldn't be any warning. A possible way to fix this is to keep a "whitelist" of locals that are first defined as an except target, but then it will still break if there's a why = ... between the two try/except and it's starting to get too complicated...