This repository was archived by the owner on Jan 13, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 2 files changed +72
-0
lines changed
Expand file tree Collapse file tree 2 files changed +72
-0
lines changed Original file line number Diff line number Diff line change @@ -76,6 +76,20 @@ def can_read(self):
7676
7777 return False
7878
79+ @property
80+ def buffer (self ):
81+ """
82+ Get access to the buffer itself.
83+ """
84+ return self ._buffer_view [self ._index :self ._buffer_end ]
85+
86+ def advance_buffer (self , count ):
87+ """
88+ Advances the buffer by the amount of data consumed outside the socket.
89+ """
90+ self ._index += count
91+ self ._bytes_in_buffer -= count
92+
7993 def new_buffer (self ):
8094 """
8195 This method moves all the data in the backing buffer to the start of
@@ -145,6 +159,23 @@ def recv(self, amt):
145159
146160 return data
147161
162+ def fill (self ):
163+ """
164+ Attempts to fill the buffer as much as possible. It will block for at
165+ most the time required to have *one* ``recv_into`` call return.
166+ """
167+ if not self ._remaining_capacity :
168+ self .new_buffer ()
169+
170+ count = self ._sck .recv_into (self ._buffer_view [self ._buffer_end :])
171+ if not count :
172+ raise ConnectionResetError ()
173+
174+ self ._bytes_in_buffer += count
175+
176+ return
177+
178+
148179 def readline (self ):
149180 """
150181 Read up to a newline from the network and returns it. The implicit
Original file line number Diff line number Diff line change @@ -207,6 +207,47 @@ def test_socket_readline_too_long(self, monkeypatch):
207207 with pytest .raises (LineTooLongError ):
208208 b .readline ()
209209
210+ def test_socket_fill_basic (self ):
211+ s = DummySocket ()
212+ b = BufferedSocket (s )
213+ s .inbound_packets = [b'Here' , b'begins' , b'the' ]
214+
215+ assert not len (b .buffer )
216+
217+ b .fill ()
218+ assert len (b .buffer ) == 4
219+
220+ b .fill ()
221+ assert len (b .buffer ) == 10
222+
223+ b .fill ()
224+ assert len (b .buffer ) == 13
225+
226+ def test_socket_fill_resizes_if_needed (self ):
227+ s = DummySocket ()
228+ b = BufferedSocket (s )
229+ s .inbound_packets = [b'Here' ]
230+ b ._index = 1000
231+
232+ assert not len (b .buffer )
233+
234+ b .fill ()
235+ assert len (b .buffer ) == 4
236+ assert b ._index == 0
237+
238+ def test_advancing_sockets (self ):
239+ s = DummySocket ()
240+ b = BufferedSocket (s )
241+ b ._buffer_view [0 :5 ] = b'abcde'
242+ b ._bytes_in_buffer += 5
243+
244+ assert len (b .buffer ) == 5
245+
246+ b .advance_buffer (3 )
247+ assert len (b .buffer ) == 2
248+
249+ assert b .buffer .tobytes () == b'de'
250+
210251
211252class DummySocket (object ):
212253 def __init__ (self ):
You can’t perform that action at this time.
0 commit comments