python-filewrap 0.2.7__tar.gz → 0.2.8__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: python-filewrap
3
- Version: 0.2.7
3
+ Version: 0.2.8
4
4
  Summary: Python file wrappers.
5
5
  Home-page: https://github.com/ChenyangGao/web-mount-packs/tree/main/python-module/python-filewrap
6
6
  License: MIT
@@ -2,7 +2,7 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 2, 7)
5
+ __version__ = (0, 2, 8)
6
6
  __all__ = [
7
7
  "SupportsRead", "SupportsReadinto", "SupportsWrite", "SupportsSeek",
8
8
  "AsyncBufferedReader", "AsyncTextIOWrapper", "buffer_length",
@@ -640,9 +640,9 @@ class AsyncTextIOWrapper(TextIOWrapper):
640
640
  if newline is None:
641
641
  text_new = CRE_NOT_UNIX_NEWLINES.sub("\n", text_new)
642
642
  self._text = ""
643
- return text_new
643
+ return text + text_new
644
644
 
645
- ls_parts = [text]
645
+ ls_parts: list[str] = []
646
646
  add_part = ls_parts.append
647
647
 
648
648
  def process_part(data, errors="strict", /) -> int:
@@ -719,14 +719,29 @@ class AsyncTextIOWrapper(TextIOWrapper):
719
719
  idx += 1
720
720
  return idx
721
721
  if size > 0:
722
- text = await self.read(size)
723
- stop = find_stop(text)
724
- if not stop or stop == len(text):
725
- return text
726
- self._text = text[stop:] + self._text
727
- return text[:stop]
722
+ if text := self._text:
723
+ if stop := find_stop(text):
724
+ stop = min(stop, size)
725
+ self._text = text[stop:]
726
+ return text[:stop]
727
+ elif size <= len(text):
728
+ self._text = text[size:]
729
+ return text[:size]
730
+ self._text = ""
731
+ size -= len(text)
732
+ text_new = await self.read(size)
733
+ stop = find_stop(text_new)
734
+ if not stop or stop == len(text_new):
735
+ return text + text_new
736
+ self._text = text_new[stop:] + self._text
737
+ return text + text_new[:stop]
728
738
  else:
729
- ls_part: list[str] = []
739
+ if text := self._text:
740
+ if stop := find_stop(text):
741
+ self._text = text[stop:]
742
+ return text[:stop]
743
+ self._text = ""
744
+ ls_part: list[str] = [text]
730
745
  add_part = ls_part.append
731
746
  while text := await self.read(READ_BUFSIZE):
732
747
  if stop := find_stop(text):
@@ -1260,28 +1275,28 @@ def bytes_iter_to_reader(
1260
1275
  if at_end or not (bufsize := buffer_length(buf)):
1261
1276
  return 0
1262
1277
  with lock:
1263
- n = buffer_length(unconsumed)
1264
- if bufsize <= n:
1265
- buf[:], unconsumed = unconsumed[:bufsize], unconsumed[bufsize:]
1266
- pos += bufsize
1267
- return bufsize
1268
- buf[:n] = unconsumed
1269
- del unconsumed[:]
1278
+ if n := buffer_length(unconsumed):
1279
+ if bufsize <= n:
1280
+ buf[:], unconsumed = unconsumed[:bufsize], unconsumed[bufsize:]
1281
+ pos += bufsize
1282
+ return bufsize
1283
+ buf[:n] = unconsumed
1284
+ pos += n
1285
+ del unconsumed[:]
1270
1286
  try:
1271
1287
  while True:
1272
- b = memoryview(getnext())
1273
- if not b:
1274
- continue
1275
- m = n + buffer_length(b)
1276
- if m >= bufsize:
1277
- buf[n:] = b[:bufsize-n]
1278
- unconsumed += b[bufsize-n:]
1279
- pos += bufsize
1280
- return bufsize
1281
- else:
1282
- buf[n:m] = b
1283
- pos += buffer_length(b)
1284
- n = m
1288
+ if b := memoryview(getnext()):
1289
+ m = n + len(b)
1290
+ if m >= bufsize:
1291
+ delta = bufsize - n
1292
+ buf[n:] = b[:delta]
1293
+ unconsumed += b[delta:]
1294
+ pos += delta
1295
+ return bufsize
1296
+ else:
1297
+ buf[n:m] = b
1298
+ pos += len(b)
1299
+ n = m
1285
1300
  except StopIteration:
1286
1301
  at_end = True
1287
1302
  return n
@@ -1386,15 +1401,7 @@ def bytes_iter_to_async_reader(
1386
1401
  except:
1387
1402
  pass
1388
1403
  def close():
1389
- nonlocal at_end
1390
- try:
1391
- method = getattr(it, "aclose")
1392
- except AttributeError:
1393
- method = getattr(it, "close")
1394
- ret = method()
1395
- if isawaitable(ret):
1396
- run_async(ret)
1397
- at_end = True
1404
+ run_async(aclose())
1398
1405
  async def aclose():
1399
1406
  nonlocal at_end
1400
1407
  try:
@@ -1437,28 +1444,28 @@ def bytes_iter_to_async_reader(
1437
1444
  if at_end or not (bufsize := buffer_length(buf)):
1438
1445
  return 0
1439
1446
  async with lock:
1440
- n = buffer_length(unconsumed)
1441
- if bufsize <= n:
1442
- buf[:], unconsumed = unconsumed[:bufsize], unconsumed[bufsize:]
1443
- pos += bufsize
1444
- return bufsize
1445
- buf[:n] = unconsumed
1446
- del unconsumed[:]
1447
+ if n := buffer_length(unconsumed):
1448
+ if bufsize <= n:
1449
+ buf[:], unconsumed = unconsumed[:bufsize], unconsumed[bufsize:]
1450
+ pos += bufsize
1451
+ return bufsize
1452
+ buf[:n] = unconsumed
1453
+ pos += n
1454
+ del unconsumed[:]
1447
1455
  try:
1448
1456
  while True:
1449
- b = memoryview(await getnext())
1450
- if not b:
1451
- continue
1452
- m = n + buffer_length(b)
1453
- if m >= bufsize:
1454
- buf[n:] = b[:bufsize-n]
1455
- unconsumed += b[bufsize-n:]
1456
- pos += bufsize
1457
- return bufsize
1458
- else:
1459
- buf[n:m] = b
1460
- pos += buffer_length(b)
1461
- n = m
1457
+ if b := memoryview(await getnext()):
1458
+ m = n + len(b)
1459
+ if m >= bufsize:
1460
+ delta = bufsize - n
1461
+ buf[n:] = b[:delta]
1462
+ unconsumed += b[delta:]
1463
+ pos += delta
1464
+ return bufsize
1465
+ else:
1466
+ buf[n:m] = b
1467
+ pos += len(b)
1468
+ n = m
1462
1469
  except (StopIteration, StopAsyncIteration):
1463
1470
  at_end = True
1464
1471
  return n
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-filewrap"
3
- version = "0.2.7"
3
+ version = "0.2.8"
4
4
  description = "Python file wrappers."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"
File without changes