[pypy-svn] r18650 - in pypy/dist/pypy/module/_socket: . test
afa at codespeak.net
afa at codespeak.net
Sat Oct 15 19:01:53 CEST 2005
Author: afa
Date: Sat Oct 15 19:01:51 2005
New Revision: 18650
Modified:
pypy/dist/pypy/module/_socket/interp_socket.py
pypy/dist/pypy/module/_socket/test/test_socket2.py
Log:
valentino, afa: correct implementation of socket.htonl
Modified: pypy/dist/pypy/module/_socket/interp_socket.py
==============================================================================
--- pypy/dist/pypy/module/_socket/interp_socket.py (original)
+++ pypy/dist/pypy/module/_socket/interp_socket.py Sat Oct 15 19:01:51 2005
@@ -232,44 +232,48 @@
Convert a 16-bit integer from network to host byte order.
"""
- try:
- return space.wrap(socket.ntohs(x))
- except socket.error, e:
- raise wrap_socketerror(space, e)
+ return space.wrap(socket.ntohs(x))
ntohs.unwrap_spec = [ObjSpace, int]
-def ntohl(space, x):
+def ntohl(space, w_x):
"""ntohl(integer) -> integer
Convert a 32-bit integer from network to host byte order.
"""
- try:
- return space.wrap(socket.ntohl(x))
- except socket.error, e:
- raise wrap_socketerror(space, e)
-ntohl.unwrap_spec = [ObjSpace, int]
+ if space.is_true(space.isinstance(w_x, space.w_int)):
+ x = space.int_w(w_x)
+ elif space.is_true(space.isinstance(w_x, space.w_long)):
+ x = space.uint_w(w_x)
+ else:
+ raise OperationError(space.w_TypeError,
+ space.wrap("expected int/long"))
+
+ return space.wrap(socket.ntohl(x))
+ntohl.unwrap_spec = [ObjSpace, W_Root]
def htons(space, x):
"""htons(integer) -> integer
Convert a 16-bit integer from host to network byte order.
"""
- try:
- return space.wrap(socket.htons(x))
- except socket.error, e:
- raise wrap_socketerror(space, e)
+ return space.wrap(socket.htons(x))
htons.unwrap_spec = [ObjSpace, int]
-def htonl(space, x):
+def htonl(space, w_x):
"""htonl(integer) -> integer
Convert a 32-bit integer from host to network byte order.
"""
- try:
- return space.wrap(socket.htonl(x))
- except socket.error, e:
- raise wrap_socketerror(space, e)
-htonl.unwrap_spec = [ObjSpace, int]
+ if space.is_true(space.isinstance(w_x, space.w_int)):
+ x = space.int_w(w_x)
+ elif space.is_true(space.isinstance(w_x, space.w_long)):
+ x = space.uint_w(w_x)
+ else:
+ raise OperationError(space.w_TypeError,
+ space.wrap("expected int/long"))
+
+ return space.wrap(socket.htonl(x))
+htonl.unwrap_spec = [ObjSpace, W_Root]
def inet_aton(space, ip):
"""inet_aton(string) -> packed 32-bit IP representation
Modified: pypy/dist/pypy/module/_socket/test/test_socket2.py
==============================================================================
--- pypy/dist/pypy/module/_socket/test/test_socket2.py (original)
+++ pypy/dist/pypy/module/_socket/test/test_socket2.py Sat Oct 15 19:01:51 2005
@@ -178,6 +178,27 @@
def setup_class(cls):
cls.space = space
+ def test_NtoH(self):
+ import _socket as socket
+ # This just checks that htons etc. are their own inverse,
+ # when looking at the lower 16 or 32 bits.
+ sizes = {socket.htonl: 32, socket.ntohl: 32,
+ socket.htons: 16, socket.ntohs: 16}
+ for func, size in sizes.items():
+ mask = (1L<<size) - 1
+ for i in (0, 1, 0xffff, ~0xffff, 2, 0x01234567, 0x76543210):
+ assert i & mask == func(func(i&mask)) & mask
+
+ swapped = func(mask)
+ assert swapped & mask == mask
+ try:
+ func(1L<<34)
+ except OverflowError:
+ pass
+ else:
+ assert False
+
+
def test_newsocket(self):
import socket
s = socket.socket()
More information about the Pypy-commit
mailing list