Stack traces ought to flag when a module has been changed on disk

This thought is motivated by this bug report: https://bugs.python.org/issue35857 If you import a module, then edit the .py file that goes with it, and then an exception occurs, the stack trace can show the wrong line. It doesn't happen very often, but when it does happen, it can be very perplexing. Here's a proposal: When a stack trace is printed, before printing each line, the interpreter checks whether the file's modification time is later than the time recorded in the .pyc file. If the times are different, the stack trace can flag the line and print an addition line stating that the file may have changed and the stack trace may not be accurate. Something like this perhaps? Traceback (most recent call last): File "spam.py", line 99, in <spam> eggs.foo() File "eggs.py", line 123, in <eggs> ? for obj in sequence: File "cheese.py", line 456, in <cheese> n = len(x) *** one or more files may have changed *** lines starting with ? may be inaccurate TypeError: object of type 'NoneType' has no len() I don't think performance will matter. Generating stack traces are rarely performance critical, so I don't think that a few extra file system checks will make any meaningful difference. Thoughts? -- Steve

I think Steve's suggestion fails in this situation. Suppose wibble.py contains a function fn. Now do import wibble fn = wibble.fn # Modify and save wibble.py reload(wibble) fn() I've posted a message to this effect in the original bug https://bugs.python.org/msg334553 Please note that the original poster, after the cause has been explained, is happy for the bug to be closed. https://bugs.python.org/msg334551 Perhaps move discussion back to https://bugs.python.org/issue35857. -- Jonathan

On Wed, 30 Jan 2019 at 16:34, Alex Walters <tritium-list@sdamon.com> wrote:
Reload isn't the issue here. Even without the reload the call to `fun()` will no longer match the file on disk. reload was moved to the imp module for exactly that reason. Michael
-- http://www.michaelfoord.co.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html

I think using reload should raise warnings, since it doesn't work, and the reload case shouldn't be the killer of this really good idea.
In Python2, reload is in __builtin__ module (and so available without import at the Python console). Since Python 3.4 this functionality is in the importlib module. https://docs.python.org/3.7/library/importlib.html#importlib.reload reload is a 44 line pure Python function. No need to use it if you don't want to. https://github.com/python/cpython/blob/3.7/Lib/importlib/__init__.py#L133-L1... I was writing this as Michael Foord's similar (but concise) post came in. -- Jonathan

On Wed, Jan 30, 2019 at 3:43 AM Jonathan Fine <jfine2358@gmail.com> wrote:
Sure -- but this is just a warning that there *might* be an issue -- if a user doesn't get the warning in some cases (particularly cases in which they have used an "advanced" feature like reload) we aren't any worse off. +1 on the idea -- I don't see a downside. -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov

I think Steve's suggestion fails in this situation. Suppose wibble.py contains a function fn. Now do import wibble fn = wibble.fn # Modify and save wibble.py reload(wibble) fn() I've posted a message to this effect in the original bug https://bugs.python.org/msg334553 Please note that the original poster, after the cause has been explained, is happy for the bug to be closed. https://bugs.python.org/msg334551 Perhaps move discussion back to https://bugs.python.org/issue35857. -- Jonathan

On Wed, 30 Jan 2019 at 16:34, Alex Walters <tritium-list@sdamon.com> wrote:
Reload isn't the issue here. Even without the reload the call to `fun()` will no longer match the file on disk. reload was moved to the imp module for exactly that reason. Michael
-- http://www.michaelfoord.co.uk/ May you do good and not evil May you find forgiveness for yourself and forgive others May you share freely, never taking more than you give. -- the sqlite blessing http://www.sqlite.org/different.html

I think using reload should raise warnings, since it doesn't work, and the reload case shouldn't be the killer of this really good idea.
In Python2, reload is in __builtin__ module (and so available without import at the Python console). Since Python 3.4 this functionality is in the importlib module. https://docs.python.org/3.7/library/importlib.html#importlib.reload reload is a 44 line pure Python function. No need to use it if you don't want to. https://github.com/python/cpython/blob/3.7/Lib/importlib/__init__.py#L133-L1... I was writing this as Michael Foord's similar (but concise) post came in. -- Jonathan

On Wed, Jan 30, 2019 at 3:43 AM Jonathan Fine <jfine2358@gmail.com> wrote:
Sure -- but this is just a warning that there *might* be an issue -- if a user doesn't get the warning in some cases (particularly cases in which they have used an "advanced" feature like reload) we aren't any worse off. +1 on the idea -- I don't see a downside. -CHB -- Christopher Barker, Ph.D. Oceanographer Emergency Response Division NOAA/NOS/OR&R (206) 526-6959 voice 7600 Sand Point Way NE (206) 526-6329 fax Seattle, WA 98115 (206) 526-6317 main reception Chris.Barker@noaa.gov
participants (7)
-
Alex Walters
-
Anders Hovmöller
-
Chris Barker
-
Jonathan Fine
-
Michael Foord
-
Pradyun Gedam
-
Steven D'Aprano