GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-96268: Fix loading invalid UTF-8 by mdboom · Pull Request #96270 · python/cpython

Conversation

@mdboom

@mdboom mdboom commented Aug 25, 2022 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This makes tokenizer.c:valid_utf8 match stringlib/codecs.h:decode_utf8.

This also fixes the related test so it will always detect the expected failure
and error message.

  • Issue: Assert and incorrect error message when loading source file containing invalid UTF-8 #96268

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
mdboom added 2 commits August 25, 2022 10:13
@mdboom
pythongh-96268: Fix loading invalid UTF-8
e4aaa14
This makes tokenizer.c:valid_utf8 match stringlib/codecs.h:decode_utf8.

This also fixes the related test so it will always detect the expected failure
and error message.
@mdboom
Add blurb
407eef7
@mdboom
mdboom requested review from lysnikolaou and pablogsal as code owners August 25, 2022 14:20
@bedevere-bot bedevere-bot added the awaiting review label Aug 25, 2022
@mdboom mdboom added 🔨 test-with-buildbots Test PR w/ buildbots; report in status section topic-unicode needs backport to 3.11 only security fixes and removed awaiting review labels Aug 25, 2022
@bedevere-bot

bedevere-bot commented Aug 25, 2022

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @mdboom for commit 407eef7 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 25, 2022
@mdboom mdboom added the awaiting review label Aug 25, 2022
mdboom added 2 commits August 25, 2022 10:22
@mdboom
Merge remote-tracking branch 'upstream/main' into fix-valid-utf8
e453819
@mdboom
Fix blurb
3d60ff7
ezio-melotti
ezio-melotti reviewed Aug 25, 2022
View reviewed changes
Comment thread Lib/test/test_source_encoding.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/test/test_source_encoding.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Parser/tokenizer.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

mdboom
mdboom commented Aug 25, 2022
View reviewed changes
Comment thread Lib/test/test_source_encoding.py Outdated
def test_pep263(self):
self.assertEqual(
"�����".encode("utf-8"),
"ðÉÔÏÎ".encode("utf-8"),

@mdboom mdboom Aug 25, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Sorry, these were changed unintentionally by my editor. Going to revert...

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mdboom mdboom Aug 25, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, actually these were added by Github's generated commit 6d43cc which accepted @ezio-melotti's suggestion. Seems like a bug in Github, which isn't surprising, given this file is not valid UTF-8. I'll clean this up by hand.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ezio-melotti reacted with eyes emoji
All reactions
  • 👀 1 reaction
@mdboom
Use assertIn instead
5cc57ad
@mdboom
mdboom force-pushed the fix-valid-utf8 branch from 6d43cc4 to 5cc57ad Compare August 25, 2022 16:25
@mdboom
mdboom requested a review from ezio-melotti August 25, 2022 16:25
@gvanrossum
gvanrossum self-requested a review August 29, 2022 16:52
gvanrossum
gvanrossum reviewed Aug 30, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Got me nerd-sniped. :-)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/test/test_source_encoding.py Outdated
# not via a signal.
self.assertGreaterEqual(rc, 1)
self.assertIn(b"Non-UTF-8 code starting with", stderr)
self.assertIn(b"on line 5", stderr)

@gvanrossum gvanrossum Aug 30, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Am I miscounting here? The string in the template appears to me to be on the 4th line.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mdboom mdboom Aug 31, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Good catch. Indeed you are correct.

The generation of the error message adds 1 to tok->lineno. I don't know if that's correct or not, but it seems like other error messages that report tok->lineno don't do that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gvanrossum gvanrossum Aug 31, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hm. There's a comment in tokenizer.c right above the PyErr_Format() call explaining why 1 has to be added. But I wonder if your change disturbed this logic? I don't understand how, though. It could also be that the comment was wrong. Maybe @pablogsal understands this logic?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 31, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

IIRC this is because the parser (or at least some parts of it) emits line numbers that start with 0 but the rest of the VM needs line numbers starting at 1 to display exceptions. But there has been some time since I had to deal with this so some details could be missing.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@gvanrossum gvanrossum Aug 31, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

The mystery is that in the updated test, an error in a string on line 4 is reported at line 5. Unless I misread the test.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 31, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Hummmm, that may be pointing to something breaking. I bet that this is pointing past the file. Without looking in detail I don't know exactly what could be going on with this specific test. Unfortunately it may be that there was some implicit contract on the reporting that these changes are breaking.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 31, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Ah, I think there is some kind of bug here. These are the errors in different versions:

❯ python3.8 lel.py
  File "lel.py", line 4
SyntaxError: Non-UTF-8 code starting with '\xc0' in file lel.py on line 4, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

❯ python3.9 lel.py
SyntaxError: Non-UTF-8 code starting with '\xc0' in file /Users/pgalindo3/lel.py on line 4, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

❯ python3.10 lel.py
SyntaxError: Non-UTF-8 code starting with '\xc0' in file /Users/pgalindo3/lel.py on line 5, but no encoding declared; see https://python.org/dev/peps/pep-0263/ for details

So something changed in 3.10 around this, it seems.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 31, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I think that line is just wrong because the line generated is already good for the exception. I made this change:

diff --git a/Parser/tokenizer.c b/Parser/tokenizer.c
index f2606f17d1..924c97ba8a 100644
--- a/Parser/tokenizer.c
+++ b/Parser/tokenizer.c
@@ -535,7 +535,7 @@ ensure_utf8(char *line, struct tok_state *tok)
                      "in file %U on line %i, "
                      "but no encoding declared; "
                      "see https://peps.python.org/pep-0263/ for details",
-                     badchar, tok->filename, tok->lineno + 1);
+                     badchar, tok->filename, tok->lineno);
         return 0;
     }
     return 1;

And the full (current) test suite passes without errors:

== Tests result: SUCCESS ==

407 tests OK.

29 tests skipped:
    test_curses test_dbm_gnu test_devpoll test_epoll test_gdb
    test_idle test_ioctl test_launcher test_msilib
    test_multiprocessing_fork test_ossaudiodev test_perf_profiler
    test_smtpnet test_socketserver test_spwd test_startfile test_tcl
    test_tix test_tkinter test_ttk test_ttk_textonly test_turtle
    test_urllib2net test_urllibnet test_winconsoleio test_winreg
    test_winsound test_xmlrpc_net test_zipfile64

Total duration: 6 min 1 see

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@pablogsal pablogsal Aug 31, 2022

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@mdboom do you want to include the fix in this PR?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@mdboom mdboom Aug 31, 2022

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

@pablogsal: Yes, it makes sense to just fix this in this PR.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Parser/tokenizer.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Parser/tokenizer.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Parser/tokenizer.c Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mdboom
mdboom requested a review from gvanrossum August 31, 2022 14:55
mdboom added 3 commits August 31, 2022 10:56
@mdboom
Fix reference to other decoding function
ad4de7a
@mdboom
Fix coding style
18927b1
@mdboom
Add comments about handled code ranges in each branch
f741a9d
@mdboom

mdboom commented Aug 31, 2022

Copy link
Copy Markdown
Contributor Author

@pablogsal: I leave it to you to decide whether this is backported to 3.11. If we don't backport, I'll file a separate PR for 3.11 to make the tests pass on buildbots with pydebug and saving coredump files (where they are currently failing).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mdboom
Fix line number in error message
f8e9e6e
gvanrossum
gvanrossum approved these changes Aug 31, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I'll let @pablogsal decide about the 3.11 and 3.10 backports. (It would be less risky to backport just the lineno fix perhaps?)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting review labels Aug 31, 2022
@gvanrossum gvanrossum added the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 31, 2022
@bedevere-bot

bedevere-bot commented Aug 31, 2022

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @gvanrossum for commit f8e9e6e 🤖

If you want to schedule another build, you need to add the ":hammer: test-with-buildbots" label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-buildbots Test PR w/ buildbots; report in status section label Aug 31, 2022
@mdboom mdboom mentioned this pull request Sep 6, 2022
Extended ASCII characters in multiline strings cause "SystemError: Negative size passed to PyUnicode_New" when the encoding is not specified #96611
Closed
mdboom added 2 commits September 6, 2022 15:39
@mdboom
Remove obsolete comment
ace4a8c
@mdboom
PEP7
df074a8
gvanrossum
gvanrossum approved these changes Sep 7, 2022
View reviewed changes

@gvanrossum gvanrossum left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Thanks. I think it's time to merge this.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@gvanrossum
gvanrossum merged commit 8bc356a into python:main Sep 7, 2022
@miss-islington

miss-islington commented Sep 7, 2022

Copy link
Copy Markdown
Contributor

Thanks @mdboom for the PR, and @gvanrossum for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒⛏🤖

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed awaiting merge needs backport to 3.11 only security fixes labels Sep 7, 2022
@bedevere-bot

bedevere-bot commented Sep 7, 2022

Copy link
Copy Markdown

GH-96668 is a backport of this pull request to the 3.11 branch.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 7, 2022
@mdboom @miss-islington
pythongh-96268: Fix loading invalid UTF-8 (pythonGH-96270)
4bca34e
This makes tokenizer.c:valid_utf8 match stringlib/codecs.h:decode_utf8.

It also fixes an off-by-one error introduced in 3.10 for the line number when the tokenizer reports bad UTF8.
(cherry picked from commit 8bc356a)

Co-authored-by: Michael Droettboom <[email protected]>
miss-islington added a commit that referenced this pull request Sep 7, 2022
@miss-islington @mdboom
gh-96268: Fix loading invalid UTF-8 (GH-96270)
ffafa9b
This makes tokenizer.c:valid_utf8 match stringlib/codecs.h:decode_utf8.

It also fixes an off-by-one error introduced in 3.10 for the line number when the tokenizer reports bad UTF8.
(cherry picked from commit 8bc356a)

Co-authored-by: Michael Droettboom <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@gvanrossum gvanrossum gvanrossum approved these changes
@pablogsal pablogsal Awaiting requested review from pablogsal pablogsal is a code owner
@lysnikolaou lysnikolaou Awaiting requested review from lysnikolaou lysnikolaou is a code owner
@ezio-melotti ezio-melotti Awaiting requested review from ezio-melotti

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

topic-unicode

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

6 participants

@mdboom @bedevere-bot @miss-islington @gvanrossum @pablogsal @ezio-melotti
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.