Skip to content

Commit 38f7b40

Browse files
Merge remote-tracking branch 'upstream/master'
Updating this branch with changes made in master
2 parents 0e5da56 + 5bcfb1b commit 38f7b40

File tree

5 files changed

+34
-7
lines changed

5 files changed

+34
-7
lines changed

docs/source/reference-io.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,10 @@ Socket objects
403403
left in an unknown state – possibly open, and possibly
404404
closed. The only reasonable thing to do is to close it.
405405

406+
.. method:: is_readable
407+
408+
Check whether the socket is readable or not.
409+
406410
.. method:: sendfile
407411

408412
`Not implemented yet! <https://github.com/python-trio/trio/issues/45>`__

newsfragments/760.feature.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Trio sockets have a new method `~trio.socket.SocketType.is_readable` that allows
2+
you to check whether a socket is readable. This is useful for HTTP/1.1 clients.

test-requirements.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ coverage==4.5.3 # via pytest-cov
1616
cryptography==2.7 # via pyopenssl, trustme
1717
decorator==4.4.0 # via ipython, traitlets
1818
entrypoints==0.3 # via flake8
19-
flake8==3.7.7
19+
flake8==3.7.8
2020
idna==2.8
2121
immutables==0.9
2222
importlib-metadata==0.18 # via pluggy, pytest
2323
ipython-genutils==0.2.0 # via traitlets
24-
ipython==7.6.0
24+
ipython==7.6.1
2525
isort==4.3.21 # via pylint
26-
jedi==0.13.3
26+
jedi==0.14.1
2727
lazy-object-proxy==1.4.1 # via astroid
2828
mccabe==0.6.1 # via flake8, pylint
2929
more-itertools==7.1.0 # via pytest
3030
outcome==1.0.0
3131
packaging==19.0 # via pytest
32-
parso==0.4.0 # via jedi
32+
parso==0.5.1 # via jedi
3333
pexpect==4.7.0 # via ipython
3434
pickleshare==0.7.5 # via ipython
3535
pluggy==0.12.0 # via pytest
@@ -44,7 +44,7 @@ pylint==2.3.1
4444
pyopenssl==19.0.0
4545
pyparsing==2.4.0 # via packaging
4646
pytest-cov==2.7.1
47-
pytest==5.0.0
47+
pytest==5.0.1
4848
six==1.12.0 # via astroid, cryptography, packaging, prompt-toolkit, pyopenssl, traitlets
4949
sniffio==1.1.0
5050
sortedcontainers==2.1.0
@@ -54,4 +54,4 @@ typed-ast==1.3.1 ; python_version < "3.8" and implementation_name == "cpython"
5454
wcwidth==0.1.7 # via prompt-toolkit, pytest
5555
wrapt==1.11.2 # via astroid
5656
yapf==0.27.0
57-
zipp==0.5.1 # via importlib-metadata
57+
zipp==0.5.2 # via importlib-metadata

trio/_socket.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import os as _os
22
import sys as _sys
3+
import select
34
import socket as _stdlib_socket
45
from functools import wraps as _wraps
56

@@ -289,7 +290,7 @@ def socket(
289290

290291
def _sniff_sockopts_for_fileno(family, type, proto, fileno):
291292
"""Correct SOCKOPTS for given fileno, falling back to provided values.
292-
293+
293294
"""
294295
# Wrap the raw fileno into a Python socket object
295296
# This object might have the wrong metadata, but it lets us easily call getsockopt
@@ -478,6 +479,15 @@ def shutdown(self, flag):
478479
if flag in [_stdlib_socket.SHUT_WR, _stdlib_socket.SHUT_RDWR]:
479480
self._did_shutdown_SHUT_WR = True
480481

482+
def is_readable(self):
483+
# use select.select on Windows, and select.poll everywhere else
484+
if _sys.platform == "win32":
485+
rready, _, _ = select.select([self._sock], [], [], 0)
486+
return bool(rready)
487+
p = select.poll()
488+
p.register(self._sock, select.POLLIN)
489+
return bool(p.poll(0))
490+
481491
async def wait_writable(self):
482492
await _core.wait_writable(self._sock)
483493

trio/tests/test_socket.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,17 @@ async def test_SocketType_simple_server(address, socket_type):
404404
assert await client.recv(1) == b"x"
405405

406406

407+
async def test_SocketType_is_readable():
408+
a, b = tsocket.socketpair()
409+
with a, b:
410+
assert not a.is_readable()
411+
await b.send(b"x")
412+
await _core.wait_readable(a)
413+
assert a.is_readable()
414+
assert await a.recv(1) == b"x"
415+
assert not a.is_readable()
416+
417+
407418
# On some macOS systems, getaddrinfo likes to return V4-mapped addresses even
408419
# when we *don't* pass AI_V4MAPPED.
409420
# https://github.com/python-trio/trio/issues/580

0 commit comments

Comments
 (0)