[pypy-svn] r72559 - in pypy/branch/fix-64/pypy/module/_socket: . test

arigo at codespeak.net arigo at codespeak.net
Mon Mar 22 15:19:01 CET 2010


Author: arigo
Date: Mon Mar 22 15:18:59 2010
New Revision: 72559

Modified:
   pypy/branch/fix-64/pypy/module/_socket/interp_func.py
   pypy/branch/fix-64/pypy/module/_socket/test/test_sock_app.py
Log:
Fix test_NtoH, and htons etc.


Modified: pypy/branch/fix-64/pypy/module/_socket/interp_func.py
==============================================================================
--- pypy/branch/fix-64/pypy/module/_socket/interp_func.py	(original)
+++ pypy/branch/fix-64/pypy/module/_socket/interp_func.py	Mon Mar 22 15:18:59 2010
@@ -2,6 +2,7 @@
 from pypy.module._socket.interp_socket import converted_error, W_RSocket
 from pypy.rlib import rsocket
 from pypy.rlib.rsocket import SocketError
+from pypy.rlib.rarithmetic import r_uint
 from pypy.interpreter.error import OperationError, operationerrfmt
 
 def gethostname(space):
@@ -152,30 +153,24 @@
     return space.newtuple([space.wrap(sock1), space.wrap(sock2)])
 socketpair.unwrap_spec = [ObjSpace, int, int, int]
 
+# The following 4 functions refuse all negative numbers, like CPython 2.6.
+# They could also check that the argument is not too large, but CPython 2.6
+# is not doing that consistently.
 def ntohs(space, x):
     """ntohs(integer) -> integer
 
     Convert a 16-bit integer from network to host byte order.
     """
     return space.wrap(rsocket.ntohs(x))
-ntohs.unwrap_spec = [ObjSpace, int]
+ntohs.unwrap_spec = [ObjSpace, r_uint]
 
-def ntohl(space, w_x):
+def ntohl(space, x):
     """ntohl(integer) -> integer
 
     Convert a 32-bit integer from network to host byte order.
     """
-    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 operationerrfmt(space.w_TypeError,
-                              "expected int/long, %s found",
-                              space.type(w_x).getname(space, "?"))
-
     return space.wrap(rsocket.ntohl(x))
-ntohl.unwrap_spec = [ObjSpace, W_Root]
+ntohl.unwrap_spec = [ObjSpace, r_uint]
 
 def htons(space, x):
     """htons(integer) -> integer
@@ -183,24 +178,15 @@
     Convert a 16-bit integer from host to network byte order.
     """
     return space.wrap(rsocket.htons(x))
-htons.unwrap_spec = [ObjSpace, int]
+htons.unwrap_spec = [ObjSpace, r_uint]
 
-def htonl(space, w_x):
+def htonl(space, x):
     """htonl(integer) -> integer
 
     Convert a 32-bit integer from host to network byte order.
     """
-    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 operationerrfmt(space.w_TypeError,
-                              "expected int/long, %s found",
-                              space.type(w_x).getname(space, "?"))
-
     return space.wrap(rsocket.htonl(x))
-htonl.unwrap_spec = [ObjSpace, W_Root]
+htonl.unwrap_spec = [ObjSpace, r_uint]
 
 def inet_aton(space, ip):
     """inet_aton(string) -> packed 32-bit IP representation

Modified: pypy/branch/fix-64/pypy/module/_socket/test/test_sock_app.py
==============================================================================
--- pypy/branch/fix-64/pypy/module/_socket/test/test_sock_app.py	(original)
+++ pypy/branch/fix-64/pypy/module/_socket/test/test_sock_app.py	Mon Mar 22 15:18:59 2010
@@ -335,6 +335,7 @@
         s.close()
 
     def test_NtoH(self):
+        import sys
         import _socket as socket
         # This just checks that htons etc. are their own inverse,
         # when looking at the lower 16 or 32 bits.
@@ -348,7 +349,28 @@
             swapped = func(mask)
             assert swapped & mask == mask
             try:
-                func(1L<<34)
+                func(-1)
+            except (OverflowError, ValueError):
+                pass
+            else:
+                assert False
+            try:
+                func(sys.maxint*2+2)
+            except OverflowError:
+                pass
+            else:
+                assert False
+
+    def test_NtoH_overflow(self):
+        skip("we are not checking for overflowing values yet")
+        import _socket as socket
+        # Checks that we cannot give too large values to htons etc.
+        # Skipped for now; CPython 2.6 is also not consistent.
+        sizes = {socket.htonl: 32, socket.ntohl: 32,
+                 socket.htons: 16, socket.ntohs: 16}
+        for func, size in sizes.items():
+            try:
+                func(1L << size)
             except OverflowError:
                 pass
             else:



More information about the Pypy-commit mailing list