Visitar URL original
Docs: socket.htons() and friends raise ValueError for negative input since 3.14 · Issue #159012 · python/cpython · GitHub
Skip to content

Docs: socket.htons() and friends raise ValueError for negative input since 3.14 #159012

Description

@ArtVsMark

Documentation

Doc/library/socket.rst, htons() (and ntohs()):

.. versionchanged:: 3.10
Raises OverflowError if x does not fit in a 16-bit unsigned integer.

Since 3.14 a negative argument raises ValueError ("Cannot convert negative
int"); only too-large values still raise OverflowError. htonl()/ntohl() behave the same way:

import socket
for f in (socket.htons, socket.ntohs, socket.htonl, socket.ntohl):
    for v in (-1, 1 << 40):
        try:
            f(v)
        except Exception as e:
            print(f.__name__, v, type(e).__name__)
Version -1 1 << 40
3.13 OverflowError OverflowError
3.14 / 3.15 ValueError OverflowError

This probably came with gh-132987 (__index__() support in the socket module).
It may be intended, but it is not documented, and except OverflowError
code written against the 3.10 note no longer catches negative input.
Suggested fix: add a versionchanged:: 3.14 note to all four functions, or
mention it in What's New in 3.14 (Porting).


Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions