classification
Title: DeprecationWarning message applies to wrong context with exec()
Type: enhancement Stage:
Components: None Versions: Python 3.2
process
Status: open Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: ghazel, terry.reedy
Priority: normal Keywords:

Created on 2008-07-22 00:40 by ghazel, last changed 2010-07-09 05:09 by terry.reedy.

Messages (4)
msg70129 - (view) Author: Greg Hazel (ghazel) Date: 2008-07-22 00:40
exec()ing a line which causes a DeprecationWarning causes the warning 
to quote the file exec() occurs in instead of the string.

Demonstration of the issue:
http://codepad.org/aMTYQgN5
msg107441 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-06-09 23:42
In 3.1, I get
>>> exec("1/0")
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    exec("1/0")
  File "<string>", line 1, in <module>
ZeroDivisionError: int division or modulo by zero

In your example, would you really prefer that the warning message start with "<string>1" (or the 2.x equivalent) instead of "t.py1"? If I were running a multi-file app with warnings turned on, I would prefer the latter. The message "DeprecationWarning: raising a string exception is deprecated" is certainly enough to suggest that one search the file for 'raise'.

In any case, this would only be a bug if it disagreed with docs or if this were different from the usual behavior with deprecation warnings. It is too late for new features in 2.x and this does not apply to 3.x.
msg107444 - (view) Author: Greg Hazel (ghazel) Date: 2010-06-10 00:14
Searching the file for "raise" is sort of pointless, since exec() takes a string which might have come from anywhere, and there might be any number of exec() calls in the module. See: http://codepad.org/7EBMhb0O

There are at least two reasonable answers:

<string>:1: DeprecationWarning: raising a string exception is deprecated
  raise 'two'

or:

t.py:7: DeprecationWarning: raising a string exception is deprecated
  exec(x)

Either one would be fine, but randomly choosing the first line of the module where exec() is called is just nonsense.

If you want one that applies to Python 3.x, here you go: http://codepad.org/Mq1eZyoE
msg109683 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2010-07-09 05:09
New features only in 3.2
History
Date User Action Args
2010-07-09 05:09:47terry.reedysetmessages: + msg109683
versions: - Python 2.6, Python 2.5, Python 3.1
2010-06-10 00:14:22ghazelsetstatus: closed -> open

messages: + msg107444
versions: + Python 2.6, Python 3.1, Python 3.2
2010-06-09 23:42:37terry.reedysetstatus: open -> closed

nosy: + terry.reedy
messages: + msg107441

type: enhancement
resolution: rejected
2008-07-22 00:40:41ghazelcreate