Skip to content

Add check if str(handle) correctly converted the object, and throw py::error_already_set if not - #2392

Merged
rwgk merged 2 commits into
pybind:str-bytes-cleanupfrom
YannickJadoul:fix-python2-str-malformed_utf8
Aug 17, 2020
Merged

Add check if str(handle) correctly converted the object, and throw py::error_already_set if not#2392
rwgk merged 2 commits into
pybind:str-bytes-cleanupfrom
YannickJadoul:fix-python2-str-malformed_utf8

Conversation

@YannickJadoul

Copy link
Copy Markdown
Collaborator

Only problematic on Python 2, I think. Discovered here: #2380 (comment)

@YannickJadoul

Copy link
Copy Markdown
CollaboratorAuthor

Also note that by checking always checking the result after calling raw_ptr, we can more the if (!str_value) throw error_already_set(); inside the Python 2-only preprocessor block, and get rid of one full pointer comparison (!) on Python 3, when calling the constructor from py::object! ;-)

@YannickJadoul

Copy link
Copy Markdown
CollaboratorAuthor

Of course. I tried this locally based on #2380, but detail::PyUnicode_Check_Permissive first still needs to die, such that py::str cannot contain a bytes-object anymore!

@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from 235168a to d58307dCompareAugust 14, 2020 00:39
@YannickJadoul

Copy link
Copy Markdown
CollaboratorAuthor

Rebased onto #2380, but waiting for #2380 to get merged before this can get rebased and merged.

Comment threadtests/test_pytypes.py Outdated
@rwgk
rwgkforce-pushed the str-bytes-cleanup branch 2 times, most recently from f38fafc to 6cd4552CompareAugust 14, 2020 22:03
@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch 3 times, most recently from 7eee06e to 366f713CompareAugust 15, 2020 21:51
@YannickJadoulYannickJadoul self-assigned this Aug 15, 2020

@bstaleticbstaletic left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks good, apart from that sys.version.major thing.

@rwgk
rwgkforce-pushed the str-bytes-cleanup branch from 1d553e4 to 0b49467CompareAugust 16, 2020 17:24
@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from 366f713 to 6452317CompareAugust 16, 2020 17:41
@henryiii

henryiii commented Aug 16, 2020

Copy link
Copy Markdown
Collaborator

@YannickJadoul if you used pre-commit install this couldn't happen. 😜

@henryiii

henryiii commented Aug 16, 2020

Copy link
Copy Markdown
Collaborator

This might benefit from being rebased after #2397

@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from 6452317 to b25eaceCompareAugust 16, 2020 17:47
Comment threadtests/test_pytypes.py Outdated
Comment threadinclude/pybind11/cast.h
@YannickJadoul

Copy link
Copy Markdown
CollaboratorAuthor

@YannickJadoul if you used pre-commit install this couldn't happen. stuck_out_tongue_winking_eye

OK, you win this one ;-)

@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from b25eace to f7d376cCompareAugust 16, 2020 21:13
@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from f7d376c to 8e53b83CompareAugust 16, 2020 21:29
@YannickJadoul
YannickJadoulforce-pushed the fix-python2-str-malformed_utf8 branch from 8e53b83 to 3dbafa6CompareAugust 16, 2020 21:30
@YannickJadoul

Copy link
Copy Markdown
CollaboratorAuthor

Fixed this. It's quite a minor PR, so a few more quick checks (@bstaletic & @henryiii, the things you mentioned should be fixed).
@rwgk, maybe you should give this a quick final check (since it involves py::str), and merge into str-bytes-cleanup at will if you're happy with it?

@wjakob

Copy link
Copy Markdown
Member

LGTM.

@rwgkrwgk left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Looks great, thanks Yannick!
I'll hold off merging only because I'm still unclear why the current str-bytes-cleanup branch fails to pass some tests in the Google environment (I didn't get a chance to work on it at all yesterday).
(For completeness: the failing tests are NOT pybind11 unit tests.)

@rwgk

rwgk commented Aug 17, 2020

Copy link
Copy Markdown
Collaborator

Looks great, thanks Yannick!
I'll hold off merging only because I'm still unclear why the current str-bytes-cleanup branch fails to pass some tests in the Google environment (I didn't get a chance to work on it at all yesterday).
(For completeness: the failing tests are NOT pybind11 unit tests.)

For future reference: the test failure was due to accidental str vs bytes mixup, masked by the double-nature of pybind11::str.

@rwgk
rwgk merged commit 7ae6bb2 into pybind:str-bytes-cleanupAug 17, 2020
@YannickJadoul
YannickJadoul deleted the fix-python2-str-malformed_utf8 branch August 17, 2020 22:08
rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 8, 2020
… py::error_already_set if not.
Similar to pybind#2392, but does not depend on pybind#2409.
Splitting out this PR from pybind#2409 to make that PR as simple as possible.
Net effects of this PR:
* Adds missing test coverage.
* Changes TypeError to UnicodeDecodeError for Python 2.
This PR has two commits. Please do not squash, to make the behavior change obvious in the commit history.
rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 10, 2020
YannickJadoul added a commit to YannickJadoul/pybind11 that referenced this pull request Sep 10, 2020
YannickJadoul added a commit to YannickJadoul/pybind11 that referenced this pull request Sep 11, 2020
YannickJadoul added a commit that referenced this pull request Sep 11, 2020
…py::error_already_set if not (bis) (#2477)
* Add check if `str(handle)` correctly converted the object, and throw py::error_already_set if not
* Fix tests on Python 3
* Apply @rwgk's fixes to cherry-picked commits from #2392
rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 16, 2020
rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 16, 2020
rwgk added a commit to rwgk/pybind11 that referenced this pull request Sep 27, 2020
rwgk added a commit to rwgk/pybind11 that referenced this pull request Oct 13, 2020
rwgk added a commit to rwgk/pybind11 that referenced this pull request Oct 18, 2020
rwgk added a commit to rwgk/pybind11 that referenced this pull request Oct 29, 2020
@rwgkrwgk mentioned this pull request Feb 10, 2023
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants

@YannickJadoul@henryiii@wjakob@rwgk@bstaletic