-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
traceback.extract_stack raises exception if source filename is pyc and exists on disk #92336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
From the py_compile.compile docs: "If dfile is specified, it is used as the name of the source file in error messages instead of file." dfile= in your example code is pointing to a |
The pycompile docs do not say this. From the sentence you quoted (and the rest of the docs) there are no requirements on what the format of dfile must be. It just says "source file". The docs also clearly state "used for error messages". In no way shape or form would I then expect the error handling path to open the file, I expected the From my PoV, there are two ways to address this:
|
A pyc is not a source file, it's a compiled bytecode file.
The source file is used to put source code lines in the traceback. Obviously a pyc won't help with that.
It allows the UnicodeDecodeError to propagate to the caller. Seem pretty clear to me. I don't think there is anything to fix here. |
Throw an exception if source file has a |
It did throw an exception.
The docs are clear that it should be a source file. |
Sure, but if you'd like to spend an entire day in the bowels of django's template loader figuring out why on earth traceback is exploding in a ball of flames for unbeknownst reasons, then be my guest. All because of a single mention of "source file" in the docs that did not highlight how paramount it was not point also have this point at a pyc. So, I think you can understand why I'm pretty insistent that something should be done rather than "Meh, don't do that. Docs are perfect and the exception message is really helpful". |
I understand that you are upset because you had a hard time debugging this. I'd suggest you think about how unit testing would have made it easier to find this problem (it's not an intermittent issue - a single test covering this function call would have caught it). Are you saying that you read the docs, misunderstood them, and then wrote your code with this bug in it based on that misunderstanding? |
I'm not upset, just frustrated because I know how easy it was to cause something that took a long time to diagnose. I don't want someone else to do the same and I believe that there is something that can be done to prevent it from happening again. Unittests would not have helped me, because all 12k of them passed. The compilation runs as part of our build system, so it was only when the database migrations were being run in the appliance chroot that django then exploded whilst loading the template engine. In the chroot/appliance we only have the binary files, however their location on disk is the same as the dfile when they were compiled (important for later). I couldn't reproduce it in my dev environment because of the difference in file system layout. My major gripe is that in the docs the only information about dfile is
name of the source file was the key bit of information I gleaned from reading that sentence a few times. |
Fair enough, we can state that the source file is read. |
Actually you are right about linecache as well: the doc for getline says "This function will never raise an exception — it will return '' on errors". So it should swallow the UnicodeDecodeError from fp.readlines(). |
…ding errors (pythonGH-94410) (cherry picked from commit 21cbdae) Co-authored-by: Irit Katriel <[email protected]>
…ding errors (pythonGH-94410) (cherry picked from commit 21cbdae) Co-authored-by: Irit Katriel <[email protected]>
…rrors (GH-94410) (cherry picked from commit 21cbdae) Co-authored-by: Irit Katriel <[email protected]>
…rrors (GH-94410) (cherry picked from commit 21cbdae) Co-authored-by: Irit Katriel <[email protected]>
Thank you @huwcbjones . The documentation change and the linecache bugfix have been pushed to all versions from 3.10. |
Bug report
If you compile a python file with the cfile as a pyc and it exists on disk, then if you extract the stack when this module is in the stack,
linecache
will try to load the file as utf-8 and explode.Simple proof of concept below:
results in a traceback of
Your environment
macOS 11.6.1 (x64): 3.7.4, 3.8.1, 3.9.1, 3.10.0
Debian Buster (x64): 3.7.3, 3.9.2
Debian Bullseye (x64): 3.9.2
The text was updated successfully, but these errors were encountered: