python-filewrap 0.0.8__tar.gz → 0.1.0.1__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.0.8
3
+ Version: 0.1.0.1
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,9 +2,10 @@
2
2
  # encoding: utf-8
3
3
 
4
4
  __author__ = "ChenyangGao <https://chenyanggao.github.io>"
5
- __version__ = (0, 0, 8)
5
+ __version__ = (0, 1)
6
6
  __all__ = [
7
- "SupportsRead", "SupportsWrite", "SupportsSeek",
7
+ "Buffer", "SupportsRead", "SupportsReadinto",
8
+ "SupportsWrite", "SupportsSeek",
8
9
  "bio_chunk_iter", "bio_chunk_async_iter",
9
10
  "bio_skip_iter", "bio_skip_async_iter",
10
11
  "bytes_iter_skip", "bytes_async_iter_skip",
@@ -25,7 +26,39 @@ from typing import runtime_checkable, Any, Protocol, TypeVar
25
26
  try:
26
27
  from collections.abc import Buffer # type: ignore
27
28
  except ImportError:
28
- Buffer = Any
29
+ from abc import ABC, abstractmethod
30
+ from array import array
31
+
32
+ def _check_methods(C, *methods):
33
+ mro = C.__mro__
34
+ for method in methods:
35
+ for B in mro:
36
+ if method in B.__dict__:
37
+ if B.__dict__[method] is None:
38
+ return NotImplemented
39
+ break
40
+ else:
41
+ return NotImplemented
42
+ return True
43
+
44
+ class Buffer(ABC): # type: ignore
45
+ __slots__ = ()
46
+
47
+ @abstractmethod
48
+ def __buffer__(self, flags: int, /) -> memoryview:
49
+ raise NotImplementedError
50
+
51
+ @classmethod
52
+ def __subclasshook__(cls, C):
53
+ if cls is Buffer:
54
+ return _check_methods(C, "__buffer__")
55
+ return NotImplemented
56
+
57
+ Buffer.register(bytes)
58
+ Buffer.register(bytearray)
59
+ Buffer.register(memoryview)
60
+ Buffer.register(array)
61
+
29
62
 
30
63
  from asynctools import async_chain, ensure_async, ensure_aiter
31
64
 
@@ -39,6 +72,11 @@ class SupportsRead(Protocol[_T_co]):
39
72
  def read(self, /, __length: int = ...) -> _T_co: ...
40
73
 
41
74
 
75
+ @runtime_checkable
76
+ class SupportsReadinto(Protocol):
77
+ def readinto(self, /, buf: Buffer = ...) -> int: ...
78
+
79
+
42
80
  @runtime_checkable
43
81
  class SupportsWrite(Protocol[_T_contra]):
44
82
  def write(self, /, __s: _T_contra) -> object: ...
@@ -316,7 +354,7 @@ def bytes_iter_skip(
316
354
  /,
317
355
  size: int = -1,
318
356
  callback: None | Callable[[int], Any] = None,
319
- ) -> Iterator[memoryview | Buffer]:
357
+ ) -> Iterator[Buffer]:
320
358
  it = iter(it)
321
359
  if size == 0:
322
360
  return it
@@ -340,7 +378,7 @@ async def bytes_async_iter_skip(
340
378
  /,
341
379
  size: int = -1,
342
380
  callback: None | Callable[[int], Any] = None,
343
- ) -> AsyncIterator[memoryview | Buffer]:
381
+ ) -> AsyncIterator[Buffer]:
344
382
  it = aiter(ensure_aiter(it))
345
383
  if size == 0:
346
384
  return it
@@ -566,7 +604,7 @@ def bytes_ensure_part_iter(
566
604
  it: Iterable[Buffer],
567
605
  /,
568
606
  partsize: int = COPY_BUFSIZE,
569
- ) -> Iterator[Buffer | memoryview]:
607
+ ) -> Iterator[Buffer]:
570
608
  n = partsize
571
609
  for b in it:
572
610
  m = memoryview(b)
@@ -594,7 +632,7 @@ async def bytes_ensure_part_async_iter(
594
632
  it: Iterable[Buffer] | AsyncIterable[Buffer],
595
633
  /,
596
634
  partsize: int = COPY_BUFSIZE,
597
- ) -> AsyncIterator[Buffer | memoryview]:
635
+ ) -> AsyncIterator[Buffer]:
598
636
  n = partsize
599
637
  async for b in ensure_aiter(it):
600
638
  m = memoryview(b)
@@ -1,6 +1,6 @@
1
1
  [tool.poetry]
2
2
  name = "python-filewrap"
3
- version = "0.0.8"
3
+ version = "0.1.0.1"
4
4
  description = "Python file wrappers."
5
5
  authors = ["ChenyangGao <wosiwujm@gmail.com>"]
6
6
  license = "MIT"