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 functions that should return unsigned int return signed int
Type: behavior Stage:
Components: Library (Lib) Versions:
process
Status: closed Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: christian.heimes, giampaolo.rodola, gvanrossum, jafo, mthibaut
Priority: low Keywords:

Created on 2007-12-30 21:30 by mthibaut, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (10)
msg59047 - (view) Author: Maarten Thibaut (mthibaut) Date: 2007-12-30 21:30
Socket library functions such as ntohs() return uint16_t, but inside
Python these return values show up as negative numbers. One possible fix
is to convert these return values using pack:

struct.unpack('H', struct.pack('h', ntohs(number)))[0] & 0xffff
msg59049 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-30 21:45
Which number shows up as a negative number on your system?
msg59050 - (view) Author: Maarten Thibaut (mthibaut) Date: 2007-12-30 21:51
Numbers returned from ntohs() turn up as negative. But ntohs() is typed as
uint16_t, so they shouldn't be. This is on solaris.
msg59051 - (view) Author: Christian Heimes (christian.heimes) * (Python committer) Date: 2007-12-30 21:53
Please provide an example number
msg59052 - (view) Author: Maarten Thibaut (mthibaut) Date: 2007-12-30 22:01
Try this on Linux and Solaris:
-----------------------
from socket import ntohs, htons

print ntohs(htons(55000))
-----------------------

On Linux:
55000

On Solaris:
-10536
msg59074 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-01 19:06
This should be fixed. Maarten, can you create a patch that works for
you? (Since few of us have access to the platform where it breaks.)
msg59075 - (view) Author: Maarten Thibaut (mthibaut) Date: 2008-01-01 21:40
Guido, the problem is that my python foo is severely lacking - but I'll
have a stab at it.

Python believes that the number coming back from the C library is
negative. We can fool it by packing the "signed short" into a
system-native format, and then unpacking it explicitly into an unsigned
short. So I propose to do precisely that on big endian systems using
sys.byteorder: wrap these functions with a converter. Would that be
acceptible?
msg59076 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-01-01 21:53
Maarten, can you build Python 2.6 from svn?  (See
http://www.python.org/dev/faq/#subversion-svn for help.)

It looks like this has been fixed there, per r53434.  Perhaps the same
changes can be backported.
msg63845 - (view) Author: Sean Reifschneider (jafo) * (Python committer) Date: 2008-03-18 02:16
Maarten: Do you have time to try doing a test build as suggested by
Guido, to report if this issue is resolved?
msg63848 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2008-03-18 02:22
This is likely fixed; setting the status to pending and priority to low.
 I believe this will eventually close it.
History
Date User Action Args
2022-04-11 14:56:29adminsetgithub: 46052
2008-07-20 11:16:54georg.brandlsetstatus: pending -> closed
2008-03-18 02:22:15gvanrossumsetstatus: open -> pending
priority: normal -> low
messages: + msg63848
2008-03-18 02:16:18jafosetpriority: normal
nosy: + jafo
type: behavior
messages: + msg63845
2008-01-01 21:53:53gvanrossumsetmessages: + msg59076
2008-01-01 21:40:08mthibautsetmessages: + msg59075
2008-01-01 19:06:51gvanrossumsetnosy: + gvanrossum
messages: + msg59074
2007-12-30 23:11:27giampaolo.rodolasetnosy: + giampaolo.rodola
2007-12-30 22:01:11mthibautsetmessages: + msg59052
2007-12-30 21:53:33christian.heimessetmessages: + msg59051
2007-12-30 21:51:45mthibautsetmessages: + msg59050
2007-12-30 21:45:53christian.heimessetnosy: + christian.heimes
messages: + msg59049
2007-12-30 21:30:45mthibautcreate