Skip to content

Commit 99ba827

Browse files
committed
blob: gracefully handle KeyboardInterrupt during filtering
1 parent 39fdfb4 commit 99ba827

File tree

1 file changed

+34
-28
lines changed

1 file changed

+34
-28
lines changed

pygit2/blob.py

+34-28
Original file line numberDiff line numberDiff line change
@@ -55,38 +55,44 @@ def seekable(self):
5555
return False
5656

5757
def readinto(self, b, /):
58-
while self._chunk is None:
59-
if self._queue.empty():
60-
if self._thread.is_alive():
61-
time.sleep(0)
62-
continue
63-
else:
64-
# EOF
65-
return 0
66-
chunk = self._queue.get()
67-
if chunk:
68-
self._chunk = chunk
69-
70-
if len(self._chunk) <= len(b):
71-
bytes_written = len(self._chunk)
72-
b[:bytes_written] = self._chunk
73-
self._chunk = None
58+
try:
59+
while self._chunk is None:
60+
if self._queue.empty():
61+
if self._thread.is_alive():
62+
time.sleep(0)
63+
continue
64+
else:
65+
# EOF
66+
return 0
67+
chunk = self._queue.get()
68+
if chunk:
69+
self._chunk = chunk
70+
71+
if len(self._chunk) <= len(b):
72+
bytes_written = len(self._chunk)
73+
b[:bytes_written] = self._chunk
74+
self._chunk = None
75+
return bytes_written
76+
bytes_written = len(b)
77+
b[:] = self._chunk[:bytes_written]
78+
self._chunk = self._chunk[bytes_written:]
7479
return bytes_written
75-
bytes_written = len(b)
76-
b[:] = self._chunk[:bytes_written]
77-
self._chunk = self._chunk[bytes_written:]
78-
return bytes_written
80+
except KeyboardInterrupt:
81+
return 0
7982

8083
def close(self):
81-
while True:
82-
if self._queue.empty():
83-
if self._thread.is_alive():
84-
time.sleep(0)
84+
try:
85+
while True:
86+
if self._queue.empty():
87+
if self._thread.is_alive():
88+
time.sleep(0)
89+
else:
90+
break
8591
else:
86-
break
87-
else:
88-
self._queue.get()
89-
self._thread.join()
92+
self._queue.get()
93+
self._thread.join()
94+
except KeyboardInterrupt:
95+
pass
9096

9197

9298
class BlobIO(io.BufferedReader, AbstractContextManager):

0 commit comments

Comments
 (0)