This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Python 3.6.1 String Literal Error Not Going to sys.stderr
Type: behavior Stage: resolved
Components: Interpreter Core Versions: Python 3.6
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: George Gillan, steven.daprano
Priority: normal Keywords:

Created on 2017-07-10 21:07 by George Gillan, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
Python361_bug.txt George Gillan, 2017-07-10 21:07 Text file with same contents as "Comment" above.
Messages (2)
msg298083 - (view) Author: George Gillan (George Gillan) Date: 2017-07-10 21:07
Python 3.6.1 String Literal Error Not Going to sys.stderr

Using Windows 7 and Python 3.6.1. Attempting to redirect sys.stderr to a file. The application will be deployed via .pyw file instead of .py so the GUI application runs without a console window.

Is this fixable or 'as-is' due to the nature of the way the interpreter scans the code?

The following code worked as intended.

#!/usr/bin/env python3.6 
errfile = 'test_err.txt'

import sys
errorlog = open(errfile, 'w')
sys.stderr = errorlog

1/0

The code above produced a file called test_err.txt with the following contents:

Traceback (most recent call last):
  File "C:\Users\George\Coding\Python\test\mintest.py", line 8, in <module>
    1/0
ZeroDivisionError: division by zero


The following code did not work as intended.

#!/usr/bin/env python3.6 
errfile = 'test_err.txt'

import sys
errorlog = open(errfile, 'w')
sys.stderr = errorlog

print("test)

The code above did not create the file test_err.txt with the error going to the console window instead.


Copy-paste from the Windows console.

The successful test (first) showed nothing in the console window; the error was logged to the file as noted above. The unsuccessful test (second) did not create the file, instead the error was sent to the console.

C:\Users\George\Coding\Python\test>mintest

C:\Users\George\Coding\Python\test>mintest
  File "C:\Users\George\Coding\Python\test\mintest.py", line 8
    print("test)
               ^
SyntaxError: EOL while scanning string literal

C:\Users\George\Coding\Python\test>

PS: This is my first bug/issue submission here. Please coach me as needed if I messed it up.
msg298096 - (view) Author: Steven D'Aprano (steven.daprano) * (Python committer) Date: 2017-07-11 01:16
Before Python runs your code, it compiles it to byte-code. A SyntaxError means that the code cannot be compiled, and so it does not run.

So the SyntaxError is raised *before* any of the code runs, and standard error is not re-directed. This is expected behaviour, not a bug. You cannot catch compile-time errors at run-time.
History
Date User Action Args
2022-04-11 14:58:48adminsetgithub: 75077
2017-07-11 01:16:40steven.dapranosetstatus: open -> closed

nosy: + steven.daprano
messages: + msg298096

resolution: not a bug
stage: resolved
2017-07-10 21:07:26George Gillancreate