mhammond / pywin32

Python for Windows (pywin32) Extensions
5.08k stars 800 forks source link

Excessive exception traces with Pythonwin.exe v307 #2393

Closed SukramLeinad closed 1 month ago

SukramLeinad commented 1 month ago

Steps to reproduce the problem

Open the Pythonwin.exe and type

c := 3

Expected behavior (as per python.exe)

File "", line 1 c := 3 ^^ SyntaxError: invalid syntax

Actual behavior

Firing event 'ProcessEnter' failed. Traceback (most recent call last): File "C:\Program Files\Python313\Lib\code.py", line 65, in runsource code = self.compile(source, filename, symbol) File "C:\Program Files\Python313\Lib\codeop.py", line 152, in call return _maybe_compile(self.compiler, source, filename, symbol) File "C:\Program Files\Python313\Lib\codeop.py", line 74, in _maybe_compile return compiler(source, filename, symbol, incomplete_input=False) File "C:\Program Files\Python313\Lib\codeop.py", line 117, in call codeob = compile(source, filename, symbol, flags, True) File "", line 1 c := 3 ^^ SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "C:\Program Files\Python313\Lib\site-packages\pythonwin\pywin\scintilla\bindings.py", line 148, in fire rc = binding.handler(*args) File "C:\Program Files\Python313\Lib\site-packages\pythonwin\pywin\framework\interact.py", line 565, in ProcessEnterEvent if self.interp.runsource(


        source, "<interactive input>"
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    ):  # Need more input!
    ^
  File "C:\Program Files\Python313\Lib\code.py", line 68, in runsource
    self.showsyntaxerror(filename, source=source)
    ~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: PythonwinInteractiveInterpreter.showsyntaxerror() got an unexpected keyword argument 'source'

## System information

Python version and distribution:
PythonWin 3.13.0 (tags/v3.13.0:60403a5, Oct  7 2024, 09:38:07) [MSC v.1941 64 bit (AMD64)] on win32.

pywin32 version:
307, installed with pip

Windows Version:
Win 10 with local admin account

DLL locations:
C:\Program Files\Python313\Lib\site-packages\pywin32_system32\pywintypes313.dll
C:\Program Files\Python313\Lib\site-packages\pywin32_system32\pythoncom313.dll
mhammond commented 1 month ago

I think this line should change to def showsyntaxerror(self, filename=None, **kwargs): - are you able to check that fixes things?

SukramLeinad commented 1 month ago

Thank you very much! I have applied your fix locally and the behavior is now correct.

mhammond commented 1 month ago

Fixed by #2394

SukramLeinad commented 1 month ago

I noticed that the error statement is still somewhat mangled.

It reads:

Traceback (  File "<interactive input>", line 1

But i would expect this:

Traceback (most recent call last):
  File "<interactive input>", line 1, in `<module>`

As CPython has a very similar defect, I raised https://github.com/python/cpython/issues/125380

SukramLeinad commented 1 month ago

About my last comment here:

CPython's method traceback.print_exception omits “Traceback (most recent call last):” for syntax errors as there is no traceback object. And there is no traceback object as code with syntax errors won’t compile and therefor can’t be executed.

mhammond commented 1 month ago

Thanks! Feel free to open a new issue here for further improvements!