This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: socket module htonl/ntohl bug
Type: Stage:
Components: Library (Lib) Versions: Python 2.2
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: jhylton Nosy List: davidma, jhylton
Priority: normal Keywords:

Created on 2002-06-13 01:56 by davidma, last changed 2022-04-10 16:05 by admin. This issue is now closed.

Messages (3)
msg11188 - (view) Author: David Margrave (davidma) Date: 2002-06-13 01:56
Hello,

The following statement should work, but does not, on
python 1.5.2 and python 2.2:

>>> socket.htonl(pow(2L,32)-1)
Traceback (innermost last):
  File "<stdin>", line 1, in ?
OverflowError: long int too long to convert

2^32-1 is a valid 32-bit value, equal to 32 1s in
binary, and htonl
should be able to handle it.  

By way of comparison, the following simple C program
works properly:

#include <stdio.h>
#include <sys/socket.h>

int main(void)
{
        printf ("%u\n", htonl(4294967295));
}

When run, it prints out '4294967295' as expected.


Dave
msg11189 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-07-25 15:25
Logged In: YES 
user_id=31392

The problem here is that the functions are currently
implemented to only accept integers.  Note that you can pass
0xffffffff == -1 and get the result you expect.  If you're
using the struct module to unpack the raw bytes, you'll be
getting -1 anyway.  But it seems reasonable enough to extend
the functions to accept longs that are < 2**32.
msg11190 - (view) Author: Jeremy Hylton (jhylton) (Python triager) Date: 2002-07-25 16:08
Logged In: YES 
user_id=31392

Fixed in rev.  1.236 of socketmodule.c.
These functions will now accepts a long L such that 0 <= L <
2**32.
History
Date User Action Args
2022-04-10 16:05:25adminsetgithub: 36742
2002-06-13 01:56:51davidmacreate