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).
Documentation
Doc/library/socket.rst,htons()(andntohs()):Since 3.14 a negative argument raises
ValueError("Cannot convert negativeint"); only too-large values still raise
OverflowError.htonl()/ntohl()behave the same way:-11 << 40This probably came with gh-132987 (
__index__()support in the socket module).It may be intended, but it is not documented, and
except OverflowErrorcode written against the 3.10 note no longer catches negative input.
Suggested fix: add a
versionchanged:: 3.14note to all four functions, ormention it in What's New in 3.14 (Porting).