|
1 | 1 | import os |
2 | 2 | import sys |
3 | 3 |
|
| 4 | +from dataclasses import dataclass |
| 5 | + |
| 6 | +lazy import functools |
4 | 7 | lazy import shutil |
5 | 8 | lazy import subprocess |
6 | 9 |
|
| 10 | +lazy import annotationlib |
| 11 | +lazy from typing import Annotated, get_args, ClassVar, get_origin |
| 12 | +lazy from ctypes import Structure, BigEndianStructure, LittleEndianStructure |
| 13 | + |
7 | 14 | # find_library(name) returns the pathname of a library, or None. |
8 | 15 | if os.name == "nt": |
9 | 16 |
|
@@ -491,6 +498,82 @@ def dllist(): |
491 | 498 | ctypes.byref(ctypes.py_object(libraries))) |
492 | 499 | return libraries |
493 | 500 |
|
| 501 | + |
| 502 | +@dataclass(slots=True, frozen=True) |
| 503 | +class CFieldInfo: |
| 504 | + anonymous: bool = False |
| 505 | + bit_width: int | None = None |
| 506 | + |
| 507 | + |
| 508 | +def _process_struct(decorated_class, /, *, align, layout, endian, pack): |
| 509 | + fields = [] |
| 510 | + anonymous = [] |
| 511 | + if issubclass(decorated_class, Structure): |
| 512 | + fields.extend(decorated_class._fields_) |
| 513 | + anonymous.extend(decorated_class._anonymous_) |
| 514 | + |
| 515 | + for name, hint in annotationlib.get_annotations(decorated_class).items(): |
| 516 | + if get_origin(hint) is ClassVar: |
| 517 | + continue |
| 518 | + |
| 519 | + field = [name] |
| 520 | + if get_origin(hint) is Annotated: |
| 521 | + cls, field_info = get_args(hint) |
| 522 | + field.append(cls) |
| 523 | + if not isinstance(field_info, CFieldInfo): |
| 524 | + raise TypeError(f"expected CFieldInfo in Annotated, got {field_info!r}") |
| 525 | + |
| 526 | + if field_info.bit_width is not None: |
| 527 | + field.append(field_info.bit_width) |
| 528 | + |
| 529 | + if field_info.anonymous is True: |
| 530 | + anonymous.append(name) |
| 531 | + else: |
| 532 | + field.append(hint) |
| 533 | + |
| 534 | + fields.append(field) |
| 535 | + |
| 536 | + if endian == 'big': |
| 537 | + endian_class = BigEndianStructure |
| 538 | + elif endian == 'little': |
| 539 | + endian_class = LittleEndianStructure |
| 540 | + elif endian == 'native': |
| 541 | + endian_class = Structure |
| 542 | + else: |
| 543 | + raise ValueError(f"expected 'big', 'little', or 'native', but got {endian!r}") |
| 544 | + |
| 545 | + @functools.wraps(decorated_class, updated=()) |
| 546 | + class _Struct(endian_class): |
| 547 | + vars().update(vars(decorated_class)) |
| 548 | + if align is not None: |
| 549 | + _align_ = align |
| 550 | + if layout is not None: |
| 551 | + _layout_ = layout |
| 552 | + if pack is not None: |
| 553 | + _pack_ = pack |
| 554 | + _fields_ = fields |
| 555 | + _anonymous_ = anonymous |
| 556 | + |
| 557 | + return _Struct |
| 558 | + |
| 559 | + |
| 560 | +def struct(class_or_none=None, /, *, align=None, layout=None, endian='native', pack=None): |
| 561 | + process_the_struct = functools.partial( |
| 562 | + _process_struct, |
| 563 | + align=align, |
| 564 | + layout=layout, |
| 565 | + endian=endian, |
| 566 | + pack=pack |
| 567 | + ) |
| 568 | + |
| 569 | + if class_or_none is None: |
| 570 | + def inner(decorated_class): |
| 571 | + return process_the_struct(decorated_class) |
| 572 | + |
| 573 | + return inner |
| 574 | + |
| 575 | + return process_the_struct(class_or_none) |
| 576 | + |
494 | 577 | ################################################################ |
495 | 578 | # test code |
496 | 579 |
|
|
0 commit comments