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-71810: fix corner case (length==0) for int.to_bytes() by skirpichev · Pull Request #138739 · python/cpython

Conversation

@skirpichev

@skirpichev skirpichev commented Sep 10, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Member
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True)  # was b''
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (-1).to_bytes(0, 'big', signed=True)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
  • Issue: int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero #71810

Sorry, something went wrong.

Uh oh!

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

All reactions
@skirpichev
pythongh-71810: fix corner case (length==0) for int.to_bytes()
33f60b9
```pycon
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True)  # was b''
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (-1).to_bytes(0, 'big', signed=True)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
```
@bedevere-app bedevere-app Bot mentioned this pull request Sep 10, 2025
int.to_bytes() and int.from_bytes(): raise ValueError when bytes count is zero #71810
Closed
@skirpichev skirpichev added the skip news label Sep 10, 2025
@skirpichev
skirpichev requested a review from vstinner September 10, 2025 10:56
@skirpichev
skirpichev marked this pull request as ready for review September 10, 2025 11:22
@bedevere-app bedevere-app Bot added the awaiting review label Sep 10, 2025
vstinner
vstinner approved these changes Sep 10, 2025
View reviewed changes

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

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting review labels Sep 10, 2025
@vstinner

This comment was marked as resolved.

Sign in to view
@vstinner

vstinner commented Sep 10, 2025

Copy link
Copy Markdown
Member

@serhiy-storchaka: I would suggest to backport this change to Python 3.13 and 3.14. What do you think?

serhiy-storchaka reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@skirpichev skirpichev removed the skip news label Sep 10, 2025
@skirpichev
add news
ea3d52f
@skirpichev
skirpichev requested a review from vstinner September 10, 2025 11:55
serhiy-storchaka
serhiy-storchaka reviewed Sep 10, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka 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.

It seems there are no enough tests for signed=True. Please add also tests for (128).to_bytes(0, 'big', signed=True) and (-129).to_bytes(0, 'big', signed=True).

Sorry, something went wrong.

Uh oh!

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

All reactions
@serhiy-storchaka serhiy-storchaka added needs backport to 3.13 only security fixes needs backport to 3.14 bugs and security fixes labels Sep 10, 2025
@skirpichev

skirpichev commented Sep 10, 2025

Copy link
Copy Markdown
Member Author

Disclaimer:
This pr implements "the minimal change necessary to remove ambiguity" (c) @mdickinson

I was thinking about (0).to_bytes(0, 'big', signed=True) too, but that means rejecting also int.from_bytes(b"", ..., signed=True). That case looks for me as a special case, that aren't special enough to warrant compatibility break.

All reactions

Sorry, something went wrong.

Uh oh!

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

@serhiy-storchaka

serhiy-storchaka commented Sep 10, 2025

Copy link
Copy Markdown
Member

Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev
address review: test for 128/-129
f0090e7
@skirpichev

skirpichev commented Sep 10, 2025

Copy link
Copy Markdown
Member Author

It seems there are no enough tests for signed=True. Please add also tests for (128).to_bytes(0, 'big', signed=True) and (-129).to_bytes(0, 'big', signed=True).

Done.

There are such tests, but rather for "automatic" value for length parameter (determined by result):

cpython/Lib/test/test_long.py

Line 1262 in 1ce0553

def test_to_bytes(self):

Or better add several tests for extreme values and values just above the limit for all combination of signedness and n=0, 1, 2.

It seems to be a big refactoring of tests. Maybe it does make sense as a separate pr?

All reactions

Sorry, something went wrong.

Uh oh!

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

serhiy-storchaka
serhiy-storchaka reviewed Sep 10, 2025
View reviewed changes
Comment thread Lib/test/test_long.py Resolved

Uh oh!

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

Comment thread Lib/test/test_long.py Resolved

Uh oh!

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

@skirpichev
+1
db7182a
@skirpichev
skirpichev requested a review from serhiy-storchaka September 11, 2025 05:09
serhiy-storchaka
serhiy-storchaka approved these changes Sep 11, 2025
View reviewed changes

@serhiy-storchaka serhiy-storchaka 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 would add much more tests. Anyway, LGTM. 👍

Sorry, something went wrong.

Uh oh!

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

All reactions
Comment thread Lib/test/test_long.py Resolved

Uh oh!

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

Comment thread Lib/test/test_long.py Resolved

Uh oh!

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

Comment thread Lib/test/test_long.py Resolved

Uh oh!

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

skirpichev and others added 2 commits September 11, 2025 10:04
@skirpichev @serhiy-storchaka
Apply suggestions from code review
aff76b8
Co-authored-by: Serhiy Storchaka <[email protected]>
@skirpichev
+ fix typo
60732b7
serhiy-storchaka
serhiy-storchaka approved these changes Sep 11, 2025
View reviewed changes
@vstinner
vstinner merged commit 011179a into python:main Sep 11, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Sep 11, 2025
@miss-islington-app

miss-islington-app Bot commented Sep 11, 2025

Copy link
Copy Markdown

Thanks @skirpichev for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

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 11, 2025
@skirpichev @serhiy-storchaka @miss-islington
pythongh-71810: Fix corner case (length==0) for int.to_bytes() (pytho…
08fc362
…nGH-138739)

```pycon
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True)  # was b''
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (-1).to_bytes(0, 'big', signed=True)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
```
(cherry picked from commit 011179a)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Sep 11, 2025
@skirpichev @serhiy-storchaka @miss-islington
pythongh-71810: Fix corner case (length==0) for int.to_bytes() (pytho…
8681aea
…nGH-138739)

```pycon
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True)  # was b''
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (-1).to_bytes(0, 'big', signed=True)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
```
(cherry picked from commit 011179a)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
@bedevere-app

bedevere-app Bot commented Sep 11, 2025

Copy link
Copy Markdown

GH-138782 is a backport of this pull request to the 3.14 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Sep 11, 2025
@bedevere-app

bedevere-app Bot commented Sep 11, 2025

Copy link
Copy Markdown

GH-138783 is a backport of this pull request to the 3.13 branch.

All reactions

Sorry, something went wrong.

Uh oh!

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

@bedevere-app bedevere-app Bot removed the needs backport to 3.13 only security fixes label Sep 11, 2025
@vstinner

vstinner commented Sep 11, 2025

Copy link
Copy Markdown
Member

Merged, thank you.

All reactions

Sorry, something went wrong.

Uh oh!

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

@skirpichev
skirpichev deleted the int-bytes/71810 branch September 11, 2025 10:46
vstinner pushed a commit that referenced this pull request Sep 11, 2025
@miss-islington @skirpichev @serhiy-storchaka
[3.13] gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-1…
7195d7f
…38739) (#138783)

gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-138739)

```pycon
>>> (0).to_bytes(0, 'big', signed=True)
b''
>>> (-1).to_bytes(0, 'big', signed=True)  # was b''
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    (-1).to_bytes(0, 'big', signed=True)
    ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
OverflowError: int too big to convert
```
(cherry picked from commit 011179a)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
hugovk pushed a commit that referenced this pull request Sep 12, 2025
@miss-islington @skirpichev @serhiy-storchaka
[3.14] gh-71810: Fix corner case (length==0) for int.to_bytes() (GH-1…
440b83f
…38739) (#138782)

Co-authored-by: Sergey B Kirpichev <[email protected]>
Co-authored-by: Serhiy Storchaka <[email protected]>
vstinner
vstinner reviewed Sep 13, 2025
View reviewed changes
Comment thread Objects/longobject.c
p += pincr;
}
else if (j == n && n > 0 && is_signed) {
else if (j == n && is_signed) {

@vstinner vstinner Sep 13, 2025

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.

This change introduced an undefined behavior: unsigned char msb = *(p - pincr); read is out of the bounds if n == 0. I wrote #138873 to fix it.

Sorry, something went wrong.

Uh oh!

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

All reactions

@skirpichev skirpichev Sep 14, 2025

Copy link
Copy Markdown
Member 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.

Oh, sorry. Thanks!

Sorry, something went wrong.

Uh oh!

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

vstinner reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@serhiy-storchaka serhiy-storchaka serhiy-storchaka approved these changes

Uh oh!

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

Assignees

No one assigned

Labels

None yet

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.

3 participants

@skirpichev @vstinner @serhiy-storchaka
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.