Skip to content
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

Error processing SyntaxError exception from a Python Script #2235

Merged
merged 2 commits into from
Apr 1, 2024

Conversation

nbbeatty
Copy link
Contributor

When executing a Python script with a syntax error via Windows Scripting Host, the exception always comes back as "Unknown" with Python 3.10, 3.11 and 3.12

Expected behavior:
From Windows OnScriptError( IActiveScriptError * pSE )
EXCEPTINFO eiError;
pSE->GetExceptionInfo( &eiError );

eiError->bstrDescription should contain "invalid syntax" and info for line number and offset should be available.

Actual behavior:
eiError->bstrDescription contains "Unknown" and the line number and offset are 0.

The problem is in class AXScriptException:
The function _BuildFromSyntaxError that parses the SyntaxError exception has a tuple assignment that is failing because two new fields were added in 3.10 (end_lineno and end_offset).

The failing line in _BuildFromSyntaxError is:
(filename, lineno, offset, line) = value[1]
because value[1] starting with Python 3.10 now has six elements.

To be backwardly compatible with Python 3.7, 3.8 and 3.9 and not get an exception in 3.10, 3.11 and 3.12 I have coded:
filename = value[1][0]
lineno = value[1][1]
offset = value[1][2]
line = value[1][3]

filename = value[1][0]
lineno = value[1][1]
offset = value[1][2]
line = value[1][3]
Copy link
Collaborator

@Avasam Avasam Mar 30, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pretty sure this entire function can be simplified to:

    def _BuildFromSyntaxError(self, exc: SyntaxError):
        # Some of these may be None, which upsets us!
        msg = exc.msg or "Unknown Error"
        offset = exc.offset or 0
        line = exc.text or ""

        self.description = FormatForAX(msg)
        self.lineno = exc.lineno
        self.colno = offset - 1
        self.linetext = ExpandTabs(line.rstrip())

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. This is nicely simplified and elegant. I have tested with Python 3.10.2 and 3.12.2 and I am getting the expected behavior from my C++ code when a Python script executed through Windows Scripting Host has a syntax error.

Copy link
Owner

@mhammond mhammond left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mhammond mhammond merged commit f2b667c into mhammond:main Apr 1, 2024
27 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants