[pypy-svn] pypy fast-forward: Reduce code duplication

amauryfa commits-noreply at bitbucket.org
Tue Jan 11 22:41:25 CET 2011


Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: fast-forward
Changeset: r40598:e3ab70d50f4a
Date: 2011-01-11 19:22 +0100
http://bitbucket.org/pypy/pypy/changeset/e3ab70d50f4a/

Log:	Reduce code duplication

diff --git a/pypy/rlib/rsocket.py b/pypy/rlib/rsocket.py
--- a/pypy/rlib/rsocket.py
+++ b/pypy/rlib/rsocket.py
@@ -128,6 +128,13 @@
         raise RSocketError("unknown address family")
     from_object = staticmethod(from_object)
 
+    @staticmethod
+    def _check_port(space, port):
+        from pypy.interpreter.error import OperationError
+        if port < 0 or port > 0xffff:
+            raise OperationError(space.w_ValueError, space.wrap(
+                "port must be 0-65535."))
+
     def fill_from_object(self, space, w_address):
         """ Purely abstract
         """
@@ -293,7 +300,6 @@
                                space.wrap(self.get_port())])
 
     def from_object(space, w_address):
-        from pypy.interpreter.error import OperationError
         # Parse an app-level object representing an AF_INET address
         try:
             w_host, w_port = space.unpackiterable(w_address, 2)
@@ -301,11 +307,7 @@
             raise TypeError("AF_INET address must be a tuple of length 2")
         host = space.str_w(w_host)
         port = space.int_w(w_port)
-
-        if port < 0 or port > 0xffff:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "AF_INET port must be 0-65535."))
-
+        Address._check_port(space, port)
         return INETAddress(host, port)
     from_object = staticmethod(from_object)
 
@@ -314,11 +316,7 @@
         from pypy.interpreter.error import OperationError
         _, w_port = space.unpackiterable(w_address, 2)
         port = space.int_w(w_port)
-
-        if port < 0 or port > 0xffff:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "AF_INET port must be 0-65535."))
-
+        self._check_port(space, port)
         a = self.lock(_c.sockaddr_in)
         rffi.setintfield(a, 'c_sin_port', htons(port))
         self.unlock()
@@ -403,11 +401,7 @@
                                "to 4, not %d" % len(pieces_w))
         host = space.str_w(pieces_w[0])
         port = space.int_w(pieces_w[1])
-
-        if port < 0 or port > 0xffff:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "AF_INET6 port must be 0-65535."))
-
+        Address._check_port(space, port)
         if len(pieces_w) > 2: flowinfo = space.uint_w(pieces_w[2])
         else:                 flowinfo = 0
         if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])
@@ -423,11 +417,7 @@
             raise RSocketError("AF_INET6 address must be a tuple of length 2 "
                                "to 4, not %d" % len(pieces_w))
         port = space.int_w(pieces_w[1])
-
-        if port < 0 or port > 0xffff:
-            raise OperationError(space.w_ValueError, space.wrap(
-                "AF_INET6 port must be 0-65535."))
-
+        self._check_port(space, port)
         if len(pieces_w) > 2: flowinfo = space.uint_w(pieces_w[2])
         else:                 flowinfo = 0
         if len(pieces_w) > 3: scope_id = space.uint_w(pieces_w[3])


More information about the Pypy-commit mailing list