Skip to content

Commit

Permalink
better xfunctype decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
T-256 authored Oct 4, 2023
1 parent facc913 commit fd74b46
Showing 1 changed file with 22 additions and 17 deletions.
39 changes: 22 additions & 17 deletions win32more/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,6 @@ class ComPtrMeta(type(c_void_p)):
def __init__(cls, name, bases, attrs):
super().__init__(name, bases, attrs)

if "_ComPtrMeta" in attrs:
return

ComPtrMeta.registers[cls.__module__].append(cls)

@classmethod
Expand Down Expand Up @@ -139,9 +136,6 @@ class EasyCastMeta(type(Structure), type(Union)):
def __init__(cls, name, bases, attrs):
super().__init__(name, bases, attrs)

if "_EasyCastMeta" in attrs:
return

# FIXME: not work for Union.
# if hasattr(cls, "_fields_"):
if "_fields_" in dir(cls):
Expand Down Expand Up @@ -173,8 +167,6 @@ def commit(cls, name):


class EasyCastStructure(Structure, metaclass=EasyCastMeta):
_EasyCastMeta = True

def __setattr__(self, name, obj):
if name in self._hints_:
obj = easycast(obj, self._hints_[name])
Expand All @@ -194,8 +186,6 @@ def __getattribute__(self, name):


class EasyCastUnion(Union, metaclass=EasyCastMeta):
_EasyCastMeta = True

def __setattr__(self, name, obj):
if name in self._hints_:
obj = easycast(obj, self._hints_[name])
Expand Down Expand Up @@ -425,15 +415,30 @@ def wrapper(*args, **kwargs):
return decorator


class BaseFuncType:
registers = defaultdict(list)

def __init__(self, fn, kind):
self._fn = fn
self._kind = kind
self.registers[fn.__module__].append(self)

def __call__(self, *args, **kwargs):
return self._fn(*args, **kwargs)

@classmethod
def commit(cls, name):
for struct in cls.registers[name]:
types = list(get_type_hints(struct._fn).values())
types = types[-1:] + types[:-1]
struct._fn = struct._kind(*types)

del cls.registers[name]


def cfunctype_pointer(prototype):
types = list(get_type_hints(prototype).values())
types = types[-1:] + types[:-1]
return CFUNCTYPE(*types)
return BaseFuncType(prototype, CFUNCTYPE)


def winfunctype_pointer(prototype):
types = list(get_type_hints(prototype).values())
types = types[-1:] + types[:-1]
return WINFUNCTYPE(*types)

return BaseFuncType(prototype, WINFUNCTYPE)

0 comments on commit fd74b46

Please sign in to comment.