Issue 30894: Python 3.6.1 String Literal Error Not Going to sys.stderr (original) (raw)

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 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.

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.