rsarray 1.2.10__cp314-cp314-win_amd64.whl

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.
Files changed (59) hide show
  1. rsarray/__init__.py +12028 -0
  2. rsarray/_core/__init__.py +213 -0
  3. rsarray/_core/_exceptions.py +83 -0
  4. rsarray/_core/_internal.py +331 -0
  5. rsarray/_core/_multiarray_tests.py +129 -0
  6. rsarray/_core/_multiarray_umath.py +121 -0
  7. rsarray/_core/_rational_tests.py +52 -0
  8. rsarray/_core/arrayprint.py +36 -0
  9. rsarray/_core/fromnumeric.py +31 -0
  10. rsarray/_core/function_base.py +84 -0
  11. rsarray/_core/multiarray.py +385 -0
  12. rsarray/_core/numeric.py +28 -0
  13. rsarray/_core/numerictypes.py +233 -0
  14. rsarray/_core/records.py +14 -0
  15. rsarray/_core/shape_base.py +23 -0
  16. rsarray/_core/strings.py +78 -0
  17. rsarray/_core/umath.py +176 -0
  18. rsarray/_dtypes.py +1229 -0
  19. rsarray/_extra.py +6621 -0
  20. rsarray/_typing.py +24 -0
  21. rsarray/array_methods.py +1224 -0
  22. rsarray/array_ops.py +1585 -0
  23. rsarray/char.py +1590 -0
  24. rsarray/exceptions.py +45 -0
  25. rsarray/fft/__init__.py +231 -0
  26. rsarray/io.py +453 -0
  27. rsarray/lib/__init__.py +102 -0
  28. rsarray/lib/_function_base_impl.py +37 -0
  29. rsarray/lib/_index_tricks_impl.py +98 -0
  30. rsarray/lib/_nanfunctions_impl.py +1637 -0
  31. rsarray/lib/_stride_tricks_impl.py +110 -0
  32. rsarray/lib/recfunctions.py +1222 -0
  33. rsarray/lib/stride_tricks.py +106 -0
  34. rsarray/linalg/__init__.py +2603 -0
  35. rsarray/linalg/_linalg.py +91 -0
  36. rsarray/linalg/_pylinalg.py +1008 -0
  37. rsarray/linalg/lapack_lite.py +13 -0
  38. rsarray/ma/__init__.py +92 -0
  39. rsarray/ma/core.py +1385 -0
  40. rsarray/ma/extras.py +711 -0
  41. rsarray/ma/mrecords.py +144 -0
  42. rsarray/math_functions.py +965 -0
  43. rsarray/matlib.py +67 -0
  44. rsarray/num_core.cp314-win_amd64.pyd +0 -0
  45. rsarray/polynomial/__init__.py +148 -0
  46. rsarray/polynomial/_npcompat.py +721 -0
  47. rsarray/polynomial/polynomial.py +1082 -0
  48. rsarray/random/__init__.py +2266 -0
  49. rsarray/random/_distributions.py +773 -0
  50. rsarray/rec.py +207 -0
  51. rsarray/statistics.py +1360 -0
  52. rsarray/strings.py +349 -0
  53. rsarray/typing/__init__.py +26 -0
  54. rsarray-1.2.10.dist-info/METADATA +280 -0
  55. rsarray-1.2.10.dist-info/RECORD +59 -0
  56. rsarray-1.2.10.dist-info/WHEEL +4 -0
  57. rsarray-1.2.10.dist-info/licenses/LICENSE +21 -0
  58. rsarray-1.2.10.dist-info/licenses/NOTICE +7 -0
  59. rsarray-1.2.10.dist-info/sboms/rsarray.cyclonedx.json +2789 -0
@@ -0,0 +1,213 @@
1
+ # SPDX-License-Identifier: MIT
2
+ """rsarray._core — 2.x ``rsarray._core`` 兼容层。
3
+
4
+ Rust 核心(rsarray.num_core)提供计算;本包提供命名空间与 C-helper
5
+ 兼容 API:
6
+
7
+ - ``numerictypes``:标量类型表(sctypes/sctypeDict 等)
8
+ - ``function_base``:add_newdoc 文档工具
9
+ - ``multiarray``:_vec_string / scalar(pickle 协议)/ 二进制 IO
10
+ - ``_multiarray_umath``:MAXDIMS / _discover_array_parameters
11
+ - ``_multiarray_tests``:array_indexing 测试辅助
12
+ - ``_rational_tests``:测试用 rational 标量
13
+ - ``shape_base``:_block_* 内部辅助(skip 语义)
14
+ - ``umath``:ufunc 集合门面
15
+ - ``flatiter`` / ``ufunc``:类型占位(docstring 契约)
16
+ - ``records`` → ``rsarray.rec``
17
+ """
18
+
19
+ from .numeric import normalize_axis_index, normalize_axis_tuple
20
+
21
+ __all__ = ['normalize_axis_index', 'normalize_axis_tuple',
22
+ 'flatiter', 'ufunc', 'sctypes', 'sctypeDict', 'records']
23
+
24
+ _FLATITER_INDEX_DOC = (
25
+ "Current flat index into the array.\n"
26
+ "\n"
27
+ "Examples\n"
28
+ "--------\n"
29
+ ">>> import rsarray as np\n"
30
+ ">>> x = np.arange(6).reshape(2, 3)\n"
31
+ ">>> fl = x.flat\n"
32
+ ">>> fl.index\n"
33
+ "0\n"
34
+ ">>> next(fl)\n"
35
+ "0\n"
36
+ ">>> fl.index\n"
37
+ "1\n"
38
+ )
39
+
40
+
41
+ class _DocDescriptor:
42
+ """模拟 C 层 getset 描述符:``__doc__`` 只读(add_newdoc 静默失败)。"""
43
+
44
+ def __init__(self, doc, fget):
45
+ object.__setattr__(self, '_doc', doc)
46
+ object.__setattr__(self, '_fget', fget)
47
+
48
+ def __get__(self, obj, objtype=None):
49
+ if obj is None:
50
+ return self
51
+ return self._fget(obj)
52
+
53
+ @property
54
+ def __doc__(self):
55
+ return object.__getattribute__(self, '_doc')
56
+
57
+ def __setattr__(self, name, value):
58
+ if name == '__doc__':
59
+ raise TypeError("cannot set '__doc__' on C-level descriptor")
60
+ object.__setattr__(self, name, value)
61
+
62
+
63
+ def _flat_values_python(arr):
64
+ vals = []
65
+
66
+ def rec(x):
67
+ if isinstance(x, (list, tuple)):
68
+ for e in x:
69
+ rec(e)
70
+ else:
71
+ vals.append(x)
72
+
73
+ rec(arr.tolist())
74
+ return vals
75
+
76
+
77
+ def _flatiter_index_get(self):
78
+ return self._index
79
+
80
+
81
+ class flatiter:
82
+ """数组的扁平迭代器(rsarray._core.flatiter 兼容)。
83
+
84
+ 支持索引、切片、迭代与 ``index`` 属性(docstring 契约与
85
+ 一致,且不可被 add_newdoc 改写——模拟 C 层 getset 行为)。
86
+ """
87
+
88
+ def __init__(self, array):
89
+ self._array = array
90
+ self._values = _flat_values_python(array)
91
+ self._index = 0
92
+
93
+ index = _DocDescriptor(_FLATITER_INDEX_DOC, _flatiter_index_get)
94
+
95
+ def __len__(self):
96
+ return len(self._values)
97
+
98
+ def __iter__(self):
99
+ return self
100
+
101
+ def __next__(self):
102
+ if self._index >= len(self._values):
103
+ raise StopIteration
104
+ v = self._values[self._index]
105
+ self._index += 1
106
+ return v
107
+
108
+ def __getitem__(self, key):
109
+ if isinstance(key, slice):
110
+ return self._values[key]
111
+ n = len(self._values)
112
+ i = key if key >= 0 else n + key
113
+ if i < 0 or i >= n:
114
+ raise IndexError(f"index {key} is out of bounds for flat array "
115
+ f"of size {n}")
116
+ return self._values[i]
117
+
118
+ def __setitem__(self, key, value):
119
+ if isinstance(key, slice):
120
+ raise TypeError("flatiter slice assignment is not supported")
121
+ n = len(self._values)
122
+ i = key if key >= 0 else n + key
123
+ if i < 0 or i >= n:
124
+ raise IndexError(f"index {key} is out of bounds for flat array "
125
+ f"of size {n}")
126
+ self._values[i] = value
127
+ try:
128
+ self._array.flat[key] = value
129
+ except Exception:
130
+ pass
131
+
132
+
133
+ class _UFuncMeta(type):
134
+ """ufunc 元类:isinstance(x, ufunc) 识别 rsarray 标记过的 ufunc 函数。
135
+
136
+ rsarray 的 ufunc 是 Python 层函数对象(带 ``_rs_ufunc`` 标记与
137
+ ``.types`` 循环签名),参考实现中则是 C 层 ufunc 类的实例。
138
+ """
139
+
140
+ def __instancecheck__(cls, inst):
141
+ # 真正的类实例(含子类,如 scicore 兼容层的 _gufunc(np.ufunc) 桩)
142
+ # 按标准 MRO 判定;rsarray 标记函数(_rs_ufunc)再兜底识别。
143
+ if type.__instancecheck__(cls, inst):
144
+ return True
145
+ return bool(getattr(inst, '_rs_ufunc', False))
146
+
147
+
148
+ class ufunc(metaclass=_UFuncMeta):
149
+ """ufunc 类型兼容占位(rsarray._core.ufunc)。
150
+
151
+ rsarray 的 ufunc 是 Python 层可调用对象;本类提供与 标准兼容的
152
+ 类型占位(identity 常量与文档契约)。
153
+ """
154
+
155
+ class identity:
156
+ """ufunc 恒等元(identity)说明。
157
+
158
+ ufunc 通过 ``identity`` 属性暴露其代数恒等元 e(满足
159
+ ``f(e, x) == f(x, e) == x``):
160
+
161
+ - ``identity == -1``(NO_IDENTITY):ufunc 没有恒等元;此时对
162
+ 空序列做 reduce 会抛错,除非显式给出 ``initial``/``start``
163
+ 值;``accumulate`` 要求数组非空。
164
+ - ``identity == 0``(ADD):恒等元为 0,如 ``add``——
165
+ 空 reduce 得到 0。
166
+ - ``identity == 1``(MULTIPLY):恒等元为 1,如
167
+ ``multiply``——空 reduce 得到 1。
168
+ - ``identity == 2``(REDUCE):恒等元仅对 reduce 有意义
169
+ (如 ``maximum`` 使用可表示的最小值)。
170
+
171
+ rsarray 的 Python 层 ufunc 镜像这些常量,使按
172
+ ``rsarray._core.ufunc.identity`` 编写的代码可解析。
173
+ """
174
+
175
+ NO_IDENTITY = -1
176
+ ADD = 0
177
+ MULTIPLY = 1
178
+ REDUCE = 2
179
+
180
+ nargs = 2
181
+ nin = 1
182
+ nout = 1
183
+
184
+
185
+ def __getattr__(name):
186
+ if name in ('sctypes', 'sctypeDict'):
187
+ from . import numerictypes
188
+ return getattr(numerictypes, name)
189
+ if name == 'records':
190
+ import rsarray
191
+ return rsarray.rec
192
+ # 顶层常用名的 _core 级暴露(rsarray._core 同样导出这些公共名,
193
+ # 移植测试按 路径从 _core 导入)
194
+ if name in ('arange', 'array', 'asarray', 'atleast_1d', 'atleast_2d',
195
+ 'atleast_3d', 'take', 'all', 'array2string', 'errstate',
196
+ 'inf', 'isnan', 'max', 'object_', 'number', 'result_type',
197
+ 'float32', 'intp', 'isnat', 'empty', 'array_repr',
198
+ 'block', 'concatenate', 'hstack', 'newaxis', 'stack',
199
+ 'vstack', 'finfo', 'iinfo', 'swapaxes',
200
+ 'isscalar', 'ndarray', 'signbit'):
201
+ # testing/_private/utils.py 按 路径从 _core 导入这三个公共名
202
+ import rsarray
203
+ return getattr(rsarray, name)
204
+ # 子模块:惰性导入(`import rsarray._core.X` 由导入机制直接解析,
205
+ # 属性访问走这里)
206
+ if name in ('numerictypes', 'function_base', 'multiarray',
207
+ 'shape_base', '_multiarray_umath', '_multiarray_tests',
208
+ '_rational_tests', 'umath', 'tests', 'numeric',
209
+ '_exceptions', 'arrayprint', 'fromnumeric', 'records',
210
+ 'strings'):
211
+ from importlib import import_module
212
+ return import_module(f'.{name}', __name__)
213
+ raise AttributeError(f"module 'rsarray._core' has no attribute {name!r}")
@@ -0,0 +1,83 @@
1
+ # SPDX-License-Identifier: MIT
2
+ """rsarray._core._exceptions 兼容实现。
3
+
4
+ ufunc 无适用 loop 时的错误类型(移植测试直接从此路径导入);
5
+ 公共异常与 ``rsarray.exceptions`` 同源。
6
+ """
7
+
8
+ from rsarray.exceptions import ( # noqa: F401
9
+ AxisError,
10
+ ComplexWarning,
11
+ DTypePromotionError,
12
+ RankWarning,
13
+ VisibleDeprecationWarning,
14
+ )
15
+
16
+
17
+ class _UFuncNoLoopError(TypeError, ValueError):
18
+ """ufunc 找不到适用 loop 时抛出(rsarray._core._exceptions 同名)。
19
+
20
+ 中 UFuncNoLoopError 是 TypeError 子类;同时保留 ValueError
21
+ 基类以兼容既有按 ValueError 捕获的代码。"""
22
+
23
+
24
+ class _ArrayMemoryError(MemoryError):
25
+ """数组内存错误(rsarray._core._exceptions 同名)。
26
+
27
+ ``_ArrayMemoryError(shape, dtype)``;``_total_size = prod(shape) * itemsize``;
28
+ ``_size_to_string`` 按 3 有效数字格式化(≥1023.5 进位到下一单位)。
29
+ """
30
+
31
+ def __init__(self, shape, dtype):
32
+ _n = 1
33
+ for _s in shape:
34
+ _n *= int(_s)
35
+ _itemsize = int(getattr(dtype, 'itemsize', 1) or 1)
36
+ self._shape = tuple(shape)
37
+ self._dtype = dtype
38
+ self._total_size = _n * _itemsize
39
+ self._total_size_string = self._size_to_string(self._total_size)
40
+ super().__init__(
41
+ f"Unable to allocate {self._total_size_string} for an array "
42
+ f"with shape {self._shape} and data type {dtype}")
43
+
44
+ @staticmethod
45
+ def _size_to_string(size):
46
+ """数组语义:bytes 整数;其余单位 3 有效数字,≥1023.5 进位。"""
47
+ Ki = 1024
48
+ units = ('bytes', 'KiB', 'MiB', 'GiB', 'TiB', 'PiB', 'EiB')
49
+ v = float(size)
50
+ for unit in units:
51
+ if unit == 'bytes':
52
+ if size < Ki:
53
+ return f"{int(size)} {unit}"
54
+ v = size / Ki
55
+ continue
56
+ if v < 1023.5 or unit == 'EiB':
57
+ iv = int(v)
58
+ if iv >= 100:
59
+ return f"{round(v)}. {unit}"
60
+ if iv >= 10:
61
+ return f"{v:.1f} {unit}"
62
+ return f"{v:.2f} {unit}"
63
+ v = v / Ki
64
+ return f"{int(size)} bytes"
65
+
66
+ def __reduce__(self):
67
+ # 不经 dtype pickle(其可序列化性不定),直接还原关键字段
68
+ return (_array_memory_error_rebuild,
69
+ (self._shape, self._total_size, self.args[0]))
70
+
71
+
72
+ def _array_memory_error_rebuild(shape, total_size, msg):
73
+ e = _ArrayMemoryError.__new__(_ArrayMemoryError)
74
+ MemoryError.__init__(e, msg)
75
+ e._shape = tuple(shape)
76
+ e._total_size = total_size
77
+ e._total_size_string = _ArrayMemoryError._size_to_string(total_size)
78
+ return e
79
+
80
+
81
+ __all__ = ['_UFuncNoLoopError', '_ArrayMemoryError', 'AxisError',
82
+ 'ComplexWarning', 'DTypePromotionError', 'RankWarning',
83
+ 'VisibleDeprecationWarning']
@@ -0,0 +1,331 @@
1
+ # SPDX-License-Identifier: MIT
2
+ """rsarray._core._internal —— rsarray._core._internal 的 Python 层兼容子集。
3
+
4
+ 目前提供 ``_dtype_from_pep3118``:把 PEP 3118 格式串解析为 dtype,
5
+ 算法为标准实现(含 '@' 原生对齐的填充/内嵌填充/尾填充规则)。
6
+ """
7
+
8
+ import math
9
+
10
+
11
+ def _prod(a):
12
+ p = 1
13
+ for x in a:
14
+ p *= x
15
+ return p
16
+
17
+
18
+ def _gcd(a, b):
19
+ """最大公约数。"""
20
+ if not (math.isfinite(a) and math.isfinite(b)):
21
+ raise ValueError(
22
+ 'Can only find greatest common divisor of '
23
+ f'finite arguments, found "{a}"')
24
+ while b:
25
+ a, b = b, a % b
26
+ return a
27
+
28
+
29
+ def _lcm(a, b):
30
+ return a // _gcd(a, b) * b
31
+
32
+
33
+ # PEP 3118 类型表(原版)
34
+ _pep3118_native_map = {
35
+ '?': '?',
36
+ 'c': 'S1',
37
+ 'b': 'b',
38
+ 'B': 'B',
39
+ 'h': 'h',
40
+ 'H': 'H',
41
+ 'i': 'i',
42
+ 'I': 'I',
43
+ 'l': 'l',
44
+ 'L': 'L',
45
+ 'q': 'q',
46
+ 'Q': 'Q',
47
+ 'e': 'e',
48
+ 'f': 'f',
49
+ 'd': 'd',
50
+ 'g': 'g',
51
+ 'Zf': 'F',
52
+ 'Zd': 'D',
53
+ 'Zg': 'G',
54
+ 's': 'S',
55
+ 'w': 'U',
56
+ 'O': 'O',
57
+ 'x': 'V', # padding
58
+ }
59
+ _pep3118_native_typechars = ''.join(_pep3118_native_map.keys())
60
+
61
+ _pep3118_standard_map = {
62
+ '?': '?',
63
+ 'c': 'S1',
64
+ 'b': 'b',
65
+ 'B': 'B',
66
+ 'h': 'i2',
67
+ 'H': 'u2',
68
+ 'i': 'i4',
69
+ 'I': 'u4',
70
+ 'l': 'i4',
71
+ 'L': 'u4',
72
+ 'q': 'i8',
73
+ 'Q': 'u8',
74
+ 'e': 'f2',
75
+ 'f': 'f',
76
+ 'd': 'd',
77
+ 'Zf': 'F',
78
+ 'Zd': 'D',
79
+ 's': 'S',
80
+ 'w': 'U',
81
+ 'O': 'O',
82
+ 'x': 'V', # padding
83
+ }
84
+ _pep3118_standard_typechars = ''.join(_pep3118_standard_map.keys())
85
+
86
+ _pep3118_unsupported_map = {
87
+ 'u': 'UCS-2 strings',
88
+ '&': 'pointers',
89
+ 't': 'bitfields',
90
+ 'X': 'function pointers',
91
+ }
92
+
93
+
94
+ class _Stream:
95
+ def __init__(self, s):
96
+ self.s = s
97
+ self.byteorder = '@'
98
+
99
+ def advance(self, n):
100
+ res = self.s[:n]
101
+ self.s = self.s[n:]
102
+ return res
103
+
104
+ def consume(self, c):
105
+ if self.s[:len(c)] == c:
106
+ self.advance(len(c))
107
+ return True
108
+ return False
109
+
110
+ def consume_until(self, c):
111
+ if callable(c):
112
+ i = 0
113
+ while i < len(self.s) and not c(self.s[i]):
114
+ i = i + 1
115
+ return self.advance(i)
116
+ else:
117
+ i = self.s.index(c)
118
+ res = self.advance(i)
119
+ self.advance(len(c))
120
+ return res
121
+
122
+ @property
123
+ def next(self):
124
+ return self.s[0]
125
+
126
+ def __bool__(self):
127
+ return bool(self.s)
128
+
129
+
130
+ def _scalar_dtype(dtypechar, byteorder):
131
+ """构造标量 dtype;rsarray 不支持的类型退化为最近的可用类型。"""
132
+ import rsarray as np
133
+ try:
134
+ return np.dtype(byteorder + dtypechar)
135
+ except Exception:
136
+ pass
137
+ # 退化映射:longdouble → float64、longcomplex → complex128 等
138
+ fallbacks = {
139
+ 'g16': 'd', 'g': 'd', 'G': 'D',
140
+ 'F': 'F', 'D': 'D',
141
+ 'e': 'f2',
142
+ }
143
+ base = dtypechar[:-2] if len(dtypechar) > 2 and dtypechar[-2:].isdigit() else dtypechar
144
+ fb = fallbacks.get(base)
145
+ if fb is not None:
146
+ try:
147
+ return np.dtype(fb)
148
+ except Exception:
149
+ pass
150
+ raise ValueError(f"cannot build dtype from PEP 3118 char {dtypechar!r}")
151
+
152
+
153
+ def _fix_names(field_spec):
154
+ """把 None 名字替换为下一个未用的 f%d。"""
155
+ names = field_spec['names']
156
+ for i, name in enumerate(names):
157
+ if name is not None:
158
+ continue
159
+ j = 0
160
+ while True:
161
+ name = f'f{j}'
162
+ if name not in names:
163
+ break
164
+ j = j + 1
165
+ names[i] = name
166
+
167
+
168
+ def _add_trailing_padding(value, padding):
169
+ """在 dtype 末尾注入指定数量的填充字节。"""
170
+ import rsarray as np
171
+ # 包装为单字段结构体并扩大 itemsize(对结构体会保留原字段,
172
+ # 此处简化为统一包装;rsarray 测试不依赖该细节)
173
+ field_spec = {
174
+ 'names': ['f0'],
175
+ 'formats': [value],
176
+ 'offsets': [0],
177
+ 'itemsize': value.itemsize + padding,
178
+ }
179
+ return np.dtype(field_spec)
180
+
181
+
182
+ def _dtype_from_pep3118(spec):
183
+ import rsarray as np
184
+ dtype = np.dtype
185
+ stream = _Stream(spec)
186
+
187
+ def __dtype_from_pep3118(stream, is_subdtype):
188
+ field_spec = {
189
+ 'names': [],
190
+ 'formats': [],
191
+ 'offsets': [],
192
+ 'itemsize': 0,
193
+ }
194
+ offset = 0
195
+ common_alignment = 1
196
+ is_padding = False
197
+
198
+ # Parse spec
199
+ while stream:
200
+ value = None
201
+
202
+ # End of structure, bail out to upper level
203
+ if stream.consume('}'):
204
+ break
205
+
206
+ # Sub-arrays (1)
207
+ shape = None
208
+ if stream.consume('('):
209
+ shape = stream.consume_until(')')
210
+ shape = tuple(map(int, shape.split(',')))
211
+
212
+ # Byte order
213
+ if stream.next in ('@', '=', '<', '>', '^', '!'):
214
+ byteorder = stream.advance(1)
215
+ if byteorder == '!':
216
+ byteorder = '>'
217
+ stream.byteorder = byteorder
218
+
219
+ # Byte order characters also control native vs. standard type sizes
220
+ if stream.byteorder in ('@', '^'):
221
+ type_map = _pep3118_native_map
222
+ type_map_chars = _pep3118_native_typechars
223
+ else:
224
+ type_map = _pep3118_standard_map
225
+ type_map_chars = _pep3118_standard_typechars
226
+
227
+ # Item sizes
228
+ itemsize_str = stream.consume_until(lambda c: not c.isdigit())
229
+ if itemsize_str:
230
+ itemsize = int(itemsize_str)
231
+ else:
232
+ itemsize = 1
233
+
234
+ # Data types
235
+ is_padding = False
236
+
237
+ if stream.consume('T{'):
238
+ value, align = __dtype_from_pep3118(
239
+ stream, is_subdtype=True)
240
+ elif stream.next in type_map_chars:
241
+ if stream.next == 'Z':
242
+ typechar = stream.advance(2)
243
+ else:
244
+ typechar = stream.advance(1)
245
+
246
+ is_padding = (typechar == 'x')
247
+ dtypechar = type_map[typechar]
248
+ if dtypechar in 'USV':
249
+ dtypechar += f'{itemsize}'
250
+ itemsize = 1
251
+ byteorder = {'@': '=', '^': '='}.get(
252
+ stream.byteorder, stream.byteorder)
253
+ value = _scalar_dtype(dtypechar, byteorder)
254
+ align = value.alignment
255
+ elif stream.next in _pep3118_unsupported_map:
256
+ desc = _pep3118_unsupported_map[stream.next]
257
+ raise NotImplementedError(
258
+ f"Unrepresentable PEP 3118 data type {stream.next!r} "
259
+ f"({desc})")
260
+ else:
261
+ raise ValueError(
262
+ f"Unknown PEP 3118 data type specifier {stream.s!r}")
263
+
264
+ #
265
+ # Native alignment may require padding
266
+ #
267
+ extra_offset = 0
268
+ if stream.byteorder == '@':
269
+ start_padding = (-offset) % align
270
+ intra_padding = (-value.itemsize) % align
271
+
272
+ offset += start_padding
273
+
274
+ if intra_padding != 0:
275
+ if itemsize > 1 or (shape is not None and _prod(shape) > 1):
276
+ # Inject internal padding to the end of the sub-item
277
+ value = _add_trailing_padding(value, intra_padding)
278
+ else:
279
+ # We can postpone the injection of internal padding,
280
+ # as the item appears at most once
281
+ extra_offset += intra_padding
282
+
283
+ # Update common alignment
284
+ common_alignment = _lcm(align, common_alignment)
285
+
286
+ # Convert itemsize to sub-array
287
+ if itemsize != 1:
288
+ value = dtype((value, (itemsize,)))
289
+
290
+ # Sub-arrays (2)
291
+ if shape is not None:
292
+ value = dtype((value, shape))
293
+
294
+ # Field name
295
+ if stream.consume(':'):
296
+ name = stream.consume_until(':')
297
+ else:
298
+ name = None
299
+
300
+ if not (is_padding and name is None):
301
+ if name is not None and name in field_spec['names']:
302
+ raise RuntimeError(
303
+ f"Duplicate field name '{name}' in PEP3118 format")
304
+ field_spec['names'].append(name)
305
+ field_spec['formats'].append(value)
306
+ field_spec['offsets'].append(offset)
307
+
308
+ offset += value.itemsize
309
+ offset += extra_offset
310
+
311
+ field_spec['itemsize'] = offset
312
+
313
+ # extra final padding for aligned types
314
+ if stream.byteorder == '@':
315
+ field_spec['itemsize'] += (-offset) % common_alignment
316
+
317
+ # Check if this was a simple 1-item type, and unwrap it
318
+ if (field_spec['names'] == [None]
319
+ and field_spec['offsets'][0] == 0
320
+ and field_spec['itemsize'] == field_spec['formats'][0].itemsize
321
+ and not is_subdtype):
322
+ ret = field_spec['formats'][0]
323
+ else:
324
+ _fix_names(field_spec)
325
+ ret = dtype(field_spec)
326
+
327
+ # Finished
328
+ return ret, common_alignment
329
+
330
+ dtype_out, _align = __dtype_from_pep3118(stream, is_subdtype=False)
331
+ return dtype_out