[pypy-commit] pypy py3k: Fixes for the select module.
amauryfa
noreply at buildbot.pypy.org
Wed Oct 19 23:11:07 CEST 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch: py3k
Changeset: r48234:e2fc88ce5b9c
Date: 2011-10-19 22:31 +0200
http://bitbucket.org/pypy/pypy/changeset/e2fc88ce5b9c/
Log: Fixes for the select module. _socket._accept() must return the raw
fd.
diff --git a/pypy/module/_socket/interp_socket.py b/pypy/module/_socket/interp_socket.py
--- a/pypy/module/_socket/interp_socket.py
+++ b/pypy/module/_socket/interp_socket.py
@@ -22,14 +22,14 @@
def _accept_w(self, space):
"""_accept() -> (socket object, address info)
- Wait for an incoming connection. Return a new socket representing the
- connection, and the address of the client. For IP sockets, the address
- info is a pair (hostaddr, port).
+ Wait for an incoming connection. Return a new socket file descriptor
+ representing the connection, and the address of the client.
+ For IP sockets, the address info is a pair (hostaddr, port).
"""
try:
- sock, addr = self.accept(W_RSocket)
- return space.newtuple([space.wrap(sock),
- addr.as_object(sock.fd, space)])
+ fd, addr = self.accept()
+ return space.newtuple([space.wrap(fd),
+ addr.as_object(fd, space)])
except SocketError, e:
raise converted_error(space, e)
@@ -414,10 +414,14 @@
@unwrap_spec(family=int, type=int, proto=int)
def newsocket(space, w_subtype, family=AF_INET,
- type=SOCK_STREAM, proto=0):
+ type=SOCK_STREAM, proto=0, w_fileno=NoneNotWrapped):
sock = space.allocate_instance(W_RSocket, w_subtype)
try:
- W_RSocket.__init__(sock, family, type, proto)
+ if w_fileno:
+ W_RSocket.__init__(sock, family, type, proto,
+ fd=space.c_filedescriptor_w(w_fileno))
+ else:
+ W_RSocket.__init__(sock, family, type, proto)
except SocketError, e:
raise converted_error(space, e)
return space.wrap(sock)
@@ -493,7 +497,7 @@
Methods of socket objects (keyword arguments not allowed):
-_accept() -- accept a connection, returning new socket and client address
+_accept() -- accept a connection, returning new socket fd and client address
bind(addr) -- bind the socket to a local address
close() -- close the socket
connect(addr) -- connect the socket to a remote address
diff --git a/pypy/module/_warnings/test/test_warnings.py b/pypy/module/_warnings/test/test_warnings.py
--- a/pypy/module/_warnings/test/test_warnings.py
+++ b/pypy/module/_warnings/test/test_warnings.py
@@ -45,14 +45,14 @@
def test_show_source_line(self):
import warnings
- import sys, StringIO
+ import sys, io
from test.warning_tests import inner
# With showarning() missing, make sure that output is okay.
del warnings.showwarning
stderr = sys.stderr
try:
- sys.stderr = StringIO.StringIO()
+ sys.stderr = io.StringIO()
inner('test message')
result = sys.stderr.getvalue()
finally:
diff --git a/pypy/module/select/test/test_epoll.py b/pypy/module/select/test/test_epoll.py
--- a/pypy/module/select/test/test_epoll.py
+++ b/pypy/module/select/test/test_epoll.py
@@ -145,8 +145,8 @@
then = time.time()
assert not events
- client.send("Hello!")
- server.send("world!!!")
+ client.send(b"Hello!")
+ server.send(b"world!!!")
now = time.time()
events = ep.poll(1, 4)
diff --git a/pypy/module/select/test/test_select.py b/pypy/module/select/test/test_select.py
--- a/pypy/module/select/test/test_select.py
+++ b/pypy/module/select/test/test_select.py
@@ -39,7 +39,7 @@
try:
iwtd, owtd, ewtd = select.select([readend], [], [], 0)
assert iwtd == owtd == ewtd == []
- writeend.send('X')
+ writeend.send(b'X')
iwtd, owtd, ewtd = select.select([readend], [], [])
assert iwtd == [readend]
assert owtd == ewtd == []
@@ -80,7 +80,7 @@
if owtd == []:
break
assert owtd == [writeend]
- total_out += writeend.send('x' * 512)
+ total_out += writeend.send(b'x' * 512)
total_in = 0
while True:
iwtd, owtd, ewtd = select.select([readend], [], [], 0)
@@ -106,7 +106,7 @@
readend, writeend = self.getpair()
try:
try:
- total_out = writeend.send('x' * 512)
+ total_out = writeend.send(b'x' * 512)
finally:
# win32 sends the 'closed' event immediately, even when
# more data is available
@@ -167,7 +167,7 @@
for i in range(50):
n = (i*3) % 10
- writeends[n].send('X')
+ writeends[n].send(b'X')
iwtd, owtd, ewtd = select.select(readends, [], [])
assert iwtd == [readends[n]]
assert owtd == ewtd == []
@@ -239,7 +239,7 @@
def send(self, data):
return os.write(self.fd, data)
def recv(self, length):
- return os.read(self.fd, length)
+ return os.read(self.fd, length).decode()
def close(self):
return os.close(self.fd)
s1, s2 = os.pipe()
diff --git a/pypy/rlib/rsocket.py b/pypy/rlib/rsocket.py
--- a/pypy/rlib/rsocket.py
+++ b/pypy/rlib/rsocket.py
@@ -599,9 +599,11 @@
"""
_mixin_ = True # for interp_socket.py
fd = _c.INVALID_SOCKET
- def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0):
+ def __init__(self, family=AF_INET, type=SOCK_STREAM, proto=0,
+ fd=_c.INVALID_SOCKET):
"""Create a new socket."""
- fd = _c.socket(family, type, proto)
+ if _c.invalid_socket(fd):
+ fd = _c.socket(family, type, proto)
if _c.invalid_socket(fd):
raise self.error_handler()
# PLAT RISCOS
@@ -704,11 +706,9 @@
addrlen_p[0] = rffi.cast(_c.socklen_t, maxlen)
return addr, addr.addr_p, addrlen_p
- def accept(self, SocketClass=None):
+ def accept(self):
"""Wait for an incoming connection.
- Return (new socket object, client address)."""
- if SocketClass is None:
- SocketClass = RSocket
+ Return (new socket fd, client address)."""
if self._select(False) == 1:
raise SocketTimeout
address, addr_p, addrlen_p = self._addrbuf()
@@ -721,9 +721,7 @@
if _c.invalid_socket(newfd):
raise self.error_handler()
address.addrlen = rffi.cast(lltype.Signed, addrlen)
- sock = make_socket(newfd, self.family, self.type, self.proto,
- SocketClass)
- return (sock, address)
+ return (newfd, address)
def bind(self, address):
"""Bind the socket to a local address."""
diff --git a/pypy/rlib/test/test_rsocket.py b/pypy/rlib/test/test_rsocket.py
--- a/pypy/rlib/test/test_rsocket.py
+++ b/pypy/rlib/test/test_rsocket.py
@@ -166,7 +166,8 @@
lock.acquire()
thread.start_new_thread(connecting, ())
print 'waiting for connection'
- s1, addr2 = sock.accept()
+ fd1, addr2 = sock.accept()
+ s1 = RSocket(fd=fd1)
print 'connection accepted'
lock.acquire()
print 'connecting side knows that the connection was accepted too'
@@ -253,7 +254,8 @@
if errcodesok:
assert err.value.errno in (errno.EINPROGRESS, errno.EWOULDBLOCK)
- s1, addr2 = sock.accept()
+ fd1, addr2 = sock.accept()
+ s1 = RSocket(fd=fd1)
s1.setblocking(False)
assert addr.eq(s2.getpeername())
assert addr2.get_port() == s2.getsockname().get_port()
@@ -400,7 +402,8 @@
clientsock = RSocket(AF_UNIX)
clientsock.connect(a)
- s, addr = serversock.accept()
+ fd, addr = serversock.accept()
+ s = RSocket(AF_UNIX, fd=fd)
s.send('X')
data = clientsock.recv(100)
More information about the pypy-commit
mailing list