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-111495: improve test coverage of codecs C API by picnixz · Pull Request #126030 · python/cpython

Conversation

@picnixz

@picnixz picnixz commented Oct 27, 2024 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

I found out that not all handlers were tested and that there was a way to check which exceptions are currently crashing. Note that I must determine whether the exception will make the handler crash directly in the Python code because if I access the attributes, I'll be using a buggy version (see #123378).

I'd like to first update the tests and then fix the handlers one by one. As I said in #123378 (comment) and #123378 (comment), just fixing the getters is not sufficient. I haven't sufficiently in the handlers themselves, but one assertion makes the replace errors handler crash so it wouldn't help just fixing the getters.

We really need to decide how to handle the start and end values of unicode errors in general but let's discuss it on #123378.

  • Issue: Add more C API tests #111495

Sorry, something went wrong.

Uh oh!

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

All reactions
@picnixz
improve test coverage of codecs C API
8e3fdfe
@picnixz picnixz added the skip news label Oct 27, 2024
@bedevere-app bedevere-app Bot added tests Tests in the Lib/test dir awaiting review labels Oct 27, 2024
@bedevere-app bedevere-app Bot mentioned this pull request Oct 27, 2024
Add more C API tests #111495
Closed
10 tasks done
@picnixz
picnixz requested review from encukou and vstinner October 27, 2024 10:36
@encukou

encukou commented Oct 28, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member

Edit: please disregard this comment, I tested the wrong code!

Click to expand the original

This passes for me when I remove the may_crash machinery:

diff --git a/Lib/test/test_capi/test_codecs.py b/Lib/test/test_capi/test_codecs.py
index b764981cca6..d3af4d576f9 100644
--- a/Lib/test/test_capi/test_codecs.py
+++ b/Lib/test/test_capi/test_codecs.py
@@ -745,28 +745,6 @@ def test_codec_stream_writer(self):
                 codec_stream_writer(NULL, stream, 'strict')
 
 
-class UnsafeUnicodeEncodeError(UnicodeEncodeError):
-    def __init__(self, encoding, message, start, end, reason):
-        self.may_crash = (end - start) < 0 or (end - start) >= len(message)
-        super().__init__(encoding, message, start, end, reason)
-
-
-class UnsafeUnicodeDecodeError(UnicodeDecodeError):
-    def __init__(self, encoding, message, start, end, reason):
-        # the case end - start >= len(message) does not crash
-        self.may_crash = (end - start) < 0
-        super().__init__(encoding, message, start, end, reason)
-
-
-class UnsafeUnicodeTranslateError(UnicodeTranslateError):
-    def __init__(self, message, start, end, reason):
-        # <= 0 because PyCodec_ReplaceErrors tries to check the Unicode kind
-        # of a 0-length result (which is by convention PyUnicode_1BYTE_KIND
-        # and not PyUnicode_2BYTE_KIND as it currently expects)
-        self.may_crash = (end - start) <= 0 or (end - start) >= len(message)
-        super().__init__(message, start, end, reason)
-
-
 class CAPICodecErrors(unittest.TestCase):
     @classmethod
     def _generate_exceptions(cls, atomic_literal, factory, objlens):
@@ -780,19 +758,19 @@ def _generate_exceptions(cls, atomic_literal, factory, objlens):
     @classmethod
     def generate_encode_errors(cls, objlen, *objlens):
         def factory(obj, start, end):
-            return UnsafeUnicodeEncodeError('utf-8', obj, start, end, 'reason')
+            return UnicodeEncodeError('utf-8', obj, start, end, 'reason')
         return tuple(cls._generate_exceptions('0', factory, [objlen, *objlens]))
 
     @classmethod
     def generate_decode_errors(cls, objlen, *objlens):
         def factory(obj, start, end):
-            return UnsafeUnicodeDecodeError('utf-8', obj, start, end, 'reason')
+            return UnicodeDecodeError('utf-8', obj, start, end, 'reason')
         return tuple(cls._generate_exceptions(b'0', factory, [objlen, *objlens]))
 
     @classmethod
     def generate_translate_errors(cls, objlen, *objlens):
         def factory(obj, start, end):
-            return UnsafeUnicodeTranslateError(obj, start, end, 'reason')
+            return UnicodeTranslateError(obj, start, end, 'reason')
         return tuple(cls._generate_exceptions('0', factory, [objlen, *objlens]))
 
     @classmethod
@@ -889,20 +867,11 @@ def test_codec_namereplace_errors_handler(self):
         self.do_test_codec_errors_handler(handler, exceptions, bad_exceptions)
 
     def do_test_codec_errors_handler(self, handler, exceptions, bad_exceptions):
-        at_least_one = False
         for exc in exceptions:
-            # See https://github.com/python/cpython/issues/123378 and related
-            # discussion and issues for details.
-            if exc.may_crash:
-                continue
-
-            at_least_one = True
             with self.subTest(handler=handler, exc=exc):
                 # test that the handler does not crash
                 self.assertIsInstance(handler(exc), tuple)
 
-        self.assertTrue(at_least_one, "all exceptions are crashing")
-
         for bad_exc in bad_exceptions:
             with self.subTest('bad type', handler=handler, exc=bad_exc):
                 self.assertRaises(TypeError, handler, bad_exc)

What am I missing?

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Oct 28, 2024 •
edited
Loading

Uh oh!

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

Copy link
Copy Markdown
Member Author

Huh... I don't really know now. Are the PoCs:

./python -c "import codecs; codecs.xmlcharrefreplace_errors(UnicodeEncodeError('bad', '', 0, 1, 'reason'))"
./python -c "import codecs; codecs.replace_errors(UnicodeTranslateError('000', 1, -7, 'reason'))"

crashing for you? I am no more on my dev env so I'll have a look at it tomorrow.

EDIT 1: Ah! I actually did not test the translate errors in replace_error.
EDIT 2: I'll also cover a larger range of errors (because not all PoCs are generated).
EDIT 3:

./python -c "import codecs; codecs.backslashreplace_errors(UnicodeDecodeError('utf-8', b'00000', 9, 2, 'reason'))"

does not crash, it simply raises a SystemError due to negative size.

All reactions

Sorry, something went wrong.

Uh oh!

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

@picnixz

picnixz commented Oct 29, 2024

Copy link
Copy Markdown
Member Author

@encukou I think I know: did you configure Python using --with-pydebug? the crashes are assertion-only so that's why you didn't catch them I think. You only have SystemError due to negative sizes though in release mode.

All reactions

Sorry, something went wrong.

Uh oh!

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

@encukou

encukou commented Oct 29, 2024

Copy link
Copy Markdown
Member

Ah, misconfiguratoin on my side. Sorry for the noise!

All reactions

Sorry, something went wrong.

Uh oh!

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

encukou
encukou reviewed Oct 29, 2024
View reviewed changes

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

These look good, but I think some of the code could be made easier to read.
It's all subjective, of course. Do you find your version easier (or have particular reason to write them that way)?

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

@picnixz

picnixz commented Oct 29, 2024

Copy link
Copy Markdown
Member Author

It's all subjective, of course. Do you find your version easier (or have particular reason to write them that way)?

Not really, that was the first approach I had in mind :D but I'll include some of your suggestions, thanks!

All reactions

Sorry, something went wrong.

Uh oh!

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

picnixz added 3 commits October 29, 2024 14:46
@picnixz
address Petr's review
03959c4
@picnixz
Simplify tests
b993a2e
@picnixz
Simplify tests
69d0643
encukou
encukou approved these changes Oct 31, 2024
View reviewed changes

@encukou encukou 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!

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Oct 31, 2024
picnixz
picnixz commented Oct 31, 2024
View reviewed changes
Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

@picnixz
Update Lib/test/test_capi/test_codecs.py
05f75b6
@picnixz

picnixz commented Oct 31, 2024

Copy link
Copy Markdown
Member Author

Thanks Petr! (you just reviewed when I left my dev session...)

All reactions

Sorry, something went wrong.

Uh oh!

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

encukou
encukou reviewed Nov 1, 2024
View reviewed changes
Comment thread Lib/test/test_capi/test_codecs.py Resolved

Uh oh!

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

@picnixz @encukou
Update Lib/test/test_capi/test_codecs.py
c36232a
Co-authored-by: Petr Viktorin <[email protected]>
@encukou
encukou enabled auto-merge (squash) November 1, 2024 13:14
@encukou
encukou merged commit 32e07fd into python:main Nov 1, 2024
@bedevere-app bedevere-app Bot removed the awaiting merge label Nov 1, 2024
@picnixz
picnixz deleted the fix/c-api-codecs-test-111495 branch November 1, 2024 13:32
picnixz added a commit to picnixz/cpython that referenced this pull request Dec 8, 2024
@picnixz
pythongh-111495: improve test coverage of codecs C API (pythonGH-126030)
629634a
For now, skip some crashers (tracked in pythongh-123378).
ebonnal pushed a commit to ebonnal/cpython that referenced this pull request Jan 12, 2025
@picnixz @ebonnal
pythongh-111495: improve test coverage of codecs C API (pythonGH-126030)
2ea6baa
For now, skip some crashers (tracked in pythongh-123378).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@encukou encukou encukou approved these changes
@vstinner vstinner Awaiting requested review from vstinner

Uh oh!

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

Assignees

No one assigned

Labels

skip news tests Tests in the Lib/test dir

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.

2 participants

@picnixz @encukou
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.