cbridge 0.1.0b0__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.
@@ -0,0 +1,24 @@
1
+ Metadata-Version: 2.4
2
+ Name: cbridge
3
+ Version: 0.1.0b0
4
+ Summary: Creating ctype structure like dataclass
5
+ Author: ZhengYu, Xu
6
+ Author-email: ZhengYu, Xu <zen-xu@outlook.com>
7
+ License-Expression: MIT
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python
10
+ Classifier: Programming Language :: Python :: 3
11
+ Classifier: Programming Language :: Python :: 3 :: Only
12
+ Classifier: Programming Language :: Python :: 3.9
13
+ Classifier: Programming Language :: Python :: 3.10
14
+ Classifier: Programming Language :: Python :: 3.11
15
+ Classifier: Programming Language :: Python :: 3.12
16
+ Classifier: Programming Language :: Python :: 3.13
17
+ Requires-Dist: typing-extensions ; python_full_version < '3.12'
18
+ Requires-Python: >=3.9
19
+ Project-URL: homepage, https://github.com/zen-xu/cbridge
20
+ Project-URL: issues, https://github.com/zen-xu/cbridge/issues
21
+ Project-URL: repository, https://github.com/zen-xu/cbridge.git
22
+ Description-Content-Type: text/markdown
23
+
24
+ # cbridge
@@ -0,0 +1 @@
1
+ # cbridge
@@ -0,0 +1,66 @@
1
+ [project]
2
+ name = "cbridge"
3
+ version = "0.1.0b0"
4
+ authors = [{ name = "ZhengYu, Xu", email = "zen-xu@outlook.com" }]
5
+ description = "Creating ctype structure like dataclass"
6
+ requires-python = ">=3.9"
7
+ readme = "README.md"
8
+ license = "MIT"
9
+ classifiers = [
10
+ "License :: OSI Approved :: MIT License",
11
+ "Programming Language :: Python",
12
+ "Programming Language :: Python :: 3",
13
+ "Programming Language :: Python :: 3 :: Only",
14
+ "Programming Language :: Python :: 3.9",
15
+ "Programming Language :: Python :: 3.10",
16
+ "Programming Language :: Python :: 3.11",
17
+ "Programming Language :: Python :: 3.12",
18
+ "Programming Language :: Python :: 3.13",
19
+ ]
20
+ dependencies = ["typing-extensions; python_version< '3.12'"]
21
+
22
+ [project.urls]
23
+ homepage = "https://github.com/zen-xu/cbridge"
24
+ repository = "https://github.com/zen-xu/cbridge.git"
25
+ issues = "https://github.com/zen-xu/cbridge/issues"
26
+
27
+ [build-system]
28
+ requires = ["uv_build>=0.8,<0.9"]
29
+ build-backend = "uv_build"
30
+
31
+ [tool.ruff]
32
+ extend-exclude = ["docs/*"]
33
+ fix = true
34
+ line-length = 88
35
+ target-version = "py39"
36
+
37
+ [tool.ruff.lint]
38
+ extend-select = [
39
+ "B", # flake8-bugbear
40
+ "C4", # flake8-comprehensions
41
+ "ERA", # flake8-eradicate/eradicate
42
+ "I", # isort
43
+ "N", # pep8-naming
44
+ "PIE", # flake8-pie
45
+ "PGH", # pygrep
46
+ "RUF", # ruff checks
47
+ "SIM", # flake8-simplify
48
+ "TC", # flake8-type-checking
49
+ "TID", # flake8-tidy-imports
50
+ "UP", # pyupgrade
51
+ ]
52
+
53
+ [tool.ruff.lint.isort]
54
+ force-single-line = true
55
+ known-first-party = ["cbridge"]
56
+ lines-after-imports = 2
57
+ lines-between-types = 1
58
+
59
+ [tool.ruff.format]
60
+ docstring-code-format = true
61
+
62
+ [tool.mypy]
63
+ files = "src"
64
+ explicit_package_bases = true
65
+ namespace_packages = true
66
+ show_error_codes = true
@@ -0,0 +1,10 @@
1
+ from __future__ import annotations
2
+
3
+ from .cbridge import CStruct
4
+
5
+
6
+ __version__ = "0.1.0b0"
7
+ __authors__ = [
8
+ "ZhengYu, Xu <zen-xu@outlook.com>",
9
+ ]
10
+ __all__ = ["CStruct"]
@@ -0,0 +1,72 @@
1
+ from __future__ import annotations
2
+
3
+ import ctypes
4
+ import dataclasses as ds
5
+ import sys
6
+ import typing
7
+
8
+ from typing import TYPE_CHECKING
9
+ from typing import Any
10
+ from typing import TypeVar
11
+
12
+
13
+ if sys.version_info >= (3, 12):
14
+ from typing import dataclass_transform
15
+ else:
16
+ from typing_extensions import dataclass_transform
17
+
18
+ if TYPE_CHECKING:
19
+ from .types import CData
20
+
21
+ field = ds.field
22
+
23
+
24
+ _T = TypeVar("_T")
25
+
26
+
27
+ _BaseStructMeta: type = type(ctypes.Structure)
28
+
29
+
30
+ @dataclass_transform()
31
+ class CStructMeta(_BaseStructMeta):
32
+ def __new__(meta_self, name: str, bases: tuple[type, ...], attrs: dict[str, Any]):
33
+ annotations = attrs.get("__annotations__", {})
34
+
35
+ fields: list[tuple[str, type[CData]]] = []
36
+
37
+ def get_base_fields(cls):
38
+ fields = []
39
+ for base in reversed(cls.__mro__):
40
+ if hasattr(base, "_fields_"):
41
+ fields += list(base._fields_)
42
+ return fields
43
+
44
+ for base in bases:
45
+ fields += get_base_fields(base)
46
+
47
+ for f_name, field in annotations.items():
48
+ if getattr(field, "__origin__", None) is typing.ClassVar:
49
+ continue
50
+
51
+ if isinstance(field, tuple):
52
+ # field is array
53
+ ctype, length = field
54
+ if hasattr(length, "__args__"):
55
+ length = length.__args__[0]
56
+ fields.append((f_name, ctype * length))
57
+ else:
58
+ fields.append((f_name, field))
59
+
60
+ if fields:
61
+ attrs["_fields_"] = tuple(fields)
62
+ cls = super().__new__(meta_self, name, bases, attrs)
63
+ return ds.dataclass()(cls)
64
+
65
+
66
+ if TYPE_CHECKING:
67
+
68
+ @dataclass_transform(field_specifiers=(field,))
69
+ class CStruct(ctypes.Structure): ...
70
+ else:
71
+
72
+ class CStruct(ctypes.Structure, metaclass=CStructMeta): ...
File without changes
@@ -0,0 +1,94 @@
1
+ import ctypes
2
+
3
+ from typing import TYPE_CHECKING
4
+ from typing import Any
5
+ from typing import Generic
6
+ from typing import TypeVar
7
+ from typing import Union
8
+
9
+
10
+ if TYPE_CHECKING:
11
+ from _ctypes import _CData as CData
12
+
13
+ Bool = Union[bool, CData]
14
+ Byte = Union[bytes, CData]
15
+ Char = Union[bytes, CData]
16
+ Double = Union[float, CData]
17
+ Float = Union[float, CData]
18
+ UByte = Union[bytes, CData]
19
+ Int = Union[int, CData]
20
+ Int8 = Union[int, CData]
21
+ Int16 = Union[int, CData]
22
+ Int32 = Union[int, CData]
23
+ Int64 = Union[int, CData]
24
+ Long = Union[int, CData]
25
+ LongDouble = Union[float, CData]
26
+ LongLong = Union[float, CData]
27
+ Short = Union[int, CData]
28
+ SizeT = Union[int, CData]
29
+ SSizeT = Union[int, CData]
30
+ UInt = Union[int, CData]
31
+ UInt8 = Union[int, CData]
32
+ UInt16 = Union[int, CData]
33
+ UInt32 = Union[int, CData]
34
+ UInt64 = Union[int, CData]
35
+ ULong = Union[int, CData]
36
+ ULongLong = Union[int, CData]
37
+ UShort = Union[int, CData]
38
+ WChar = Union[str, CData]
39
+ VoidPtr = Union[ctypes.c_void_p, Any]
40
+ CharPtr = Union[ctypes.c_char_p, Any]
41
+ WCharPtr = Union[ctypes.c_wchar_p, Any]
42
+ else:
43
+ CData = object
44
+ Byte = ctypes.c_byte
45
+ Bool = ctypes.c_bool
46
+ Char = ctypes.c_char
47
+ Double = ctypes.c_double
48
+ Float = ctypes.c_float
49
+ UByte = ctypes.c_ubyte
50
+ Int = ctypes.c_int
51
+ Int8 = ctypes.c_int8
52
+ Int16 = ctypes.c_int16
53
+ Int32 = ctypes.c_int32
54
+ Int64 = ctypes.c_int64
55
+ Long = ctypes.c_long
56
+ LongDouble = ctypes.c_longdouble
57
+ LongLong = ctypes.c_longlong
58
+ Short = ctypes.c_short
59
+ SizeT = ctypes.c_size_t
60
+ SSizeT = ctypes.c_ssize_t
61
+ UInt = ctypes.c_uint
62
+ UInt8 = ctypes.c_uint8
63
+ UInt16 = ctypes.c_uint16
64
+ UInt32 = ctypes.c_uint32
65
+ UInt64 = ctypes.c_uint64
66
+ ULong = ctypes.c_ulong
67
+ ULongLong = ctypes.c_ulonglong
68
+ UShort = ctypes.c_ushort
69
+ WChar = ctypes.c_wchar
70
+ VoidPtr = ctypes.c_void_p
71
+ CharPtr = ctypes.c_char_p
72
+ WCharPtr = ctypes.c_wchar_p
73
+
74
+
75
+ CTimeT = ULong
76
+
77
+ _T = TypeVar("_T")
78
+ _Len = TypeVar("_Len")
79
+
80
+
81
+ if TYPE_CHECKING:
82
+
83
+ class _Array(Generic[_T, _Len]): ...
84
+
85
+ Array = Union[_Array[_T, _Len], list[_T]]
86
+ else:
87
+
88
+ class _Array:
89
+ def __getitem__(
90
+ self, type_length: tuple[type[CData], int]
91
+ ) -> tuple[type[CData], int]:
92
+ return type_length
93
+
94
+ Array = _Array()