Thanks to visit codestin.com
Credit goes to github.com

Skip to content

bpo-40585: Normalize errors messages in codeop when comparing them #20030

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

Merged
merged 4 commits into from
May 11, 2020

Conversation

pablogsal
Copy link
Member

@pablogsal pablogsal commented May 10, 2020

With the new parser, the error message contains always the trailing
newlines, causing the naive comparison of the repr of the error messages
in codeop to fail.

https://bugs.python.org/issue40585

@gvanrossum
Copy link
Member

Why not fix the new parser to produce an error message that doesn't end in a newline? This could affect other uses as well.

With the new parser, the error message contains always the trailing
newlines, causing the naive comparison of the repr of the error messages
in codeop to fail.
@pablogsal
Copy link
Member Author

Why not fix the new parser to produce an error message that doesn't end in a newline? This could affect other uses as well.

👍 I was not sure if we were relying on those newlines elsewhere.

I will try to do the normalization in pegen.c

@pablogsal
Copy link
Member Author

I was not sure if we were relying on those newlines elsewhere.

Indeed, there are tests that fail if the newline is not there. For instance:

/python -m test test_exceptions
0:00:00 load avg: 0.68 Run tests sequentially
0:00:00 load avg: 0.68 [1/1] test_exceptions
test test_exceptions failed -- Traceback (most recent call last):
  File "/home/pablogsal/github/python/master/Lib/test/test_exceptions.py", line 221, in testSyntaxErrorOffset
    check(b"""if 1:
  File "/home/pablogsal/github/python/master/Lib/test/test_exceptions.py", line 190, in check
    self.assertIn(line, cm.exception.text)
AssertionError: "                '''quux'''" not found in "                '''"

test_exceptions failed

== Tests result: FAILURE ==

1 test failed:
    test_exceptions

Total duration: 1.6 sec
Tests result: FAILURE

@pablogsal
Copy link
Member Author

Ok, I think I have something that works. I have reverted my changes to codeop.py and I have added trimming code in pegen.c

@gvanrossum
Copy link
Member

Indeed, there are tests that fail if the newline is not there.

But then you must not be doing the same thing as the old parser. I created a little test program:

for suffix in "", "\n", "\n\n":
    try:
        compile("raise=1" + suffix, "", "exec")
    except SyntaxError as err:
        print(repr(err))

Output with old parser always includes a newline:

(v38) ~/cpython$ ./python.exe  -X oldparser t.py
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1\n'))
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1\n'))
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1\n'))

But with new parser only when there's a newline in the source:

(v38) ~/cpython$ ./python.exe  t.py
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1'))
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1'))
SyntaxError('invalid syntax', ('', 1, 6, 'raise=1\n'))

@gvanrossum
Copy link
Member

gvanrossum commented May 10, 2020

FWIW there are also differences with eval and single modes. Try it.

  • New parser shows newline only for exec with two newlines appended
  • Old parser shows newline whenever present in the source, but always for exec

IIRC the old parser adds a newline in exec mode "just in case".

Not sure how to handle this. Maybe always append the newline???

@pablogsal
Copy link
Member Author

FWIW there are also differences with eval and single modes. Try it.

Ugh. The current version of the patch is always trimming all newlines. But seems that this is not what the old parser does. Seems that we need to maintain the last one when using exec, isn't it?

@gvanrossum
Copy link
Member

(See my edited comment.)

@pablogsal
Copy link
Member Author

Not sure how to handle this. Maybe always append the newline???

Apparently that makes some other tests in test_exceptions to fail because byte_offset_to_character_offset does not like the fiddling with the newlines. I am checking what's going on there

Copy link
Member

@gvanrossum gvanrossum left a comment

Choose a reason for hiding this comment

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

If this passes in CI, go for it.

@pablogsal
Copy link
Member Author

pablogsal commented May 11, 2020

Seems that with this version something fails in test_cmdline

Seems that it was something wrong with my .pythonrc

@pablogsal pablogsal merged commit 5b956ca into python:master May 11, 2020
@pablogsal pablogsal deleted the bpo-40585 branch May 19, 2021 19:00
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.

4 participants