Skip to content

Commit

Permalink
Added the function 'register_record_class' to add proper subclasses of
Browse files Browse the repository at this point in the history
'pythoncom.com_record' to the global map 'pythoncom.RecordClasses'.
The function does only add instantiable subclasses of 'pythoncom.com_record'
to the map if their GUID is not already contained in the map.
  • Loading branch information
geppi committed Jan 5, 2025
1 parent b4618a9 commit bb40a88
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions com/win32com/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,39 @@ def Record(name, object):
)


# Registration function for com_record subclasses.
def register_record_class(cls):
"""
Register a subclass of com_record to enable creation of the represented record objects.
A subclass of com_record requires the following class attributes to be instantiable:
TLBID : The GUID of the containing TypeLibrary as a string.
MJVER : The major version number of the TypeLibrary as an integer.
MNVER : The minor version number of the TypeLibrary as an integer.
LCID : The LCID of the TypeLibrary as an integer.
GUID : The GUID of the COM Record as a string.
with GUID strings in {xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx} notation.
To instantiate such a subclasses it has to be registered via this function.
"""
if cls not in pythoncom.com_record.__subclasses__():
raise TypeError("Only subclasses of 'com_record' can be registered.")
try:
_ = cls()
except:
raise TypeError(f"Class {cls.__name__} cannot be instantiated.")
# Since the class can be instantiated we know that it represents a valid COM Record
# in a properly registered TypeLibrary and that it has a 'GUID' class attribute.
if cls.GUID in pythoncom.RecordClasses:
raise ValueError(
f"Record class with same GUID {cls.GUID} "
f"is already registered with name '{pythoncom.RecordClasses[cls.GUID].__name__}'."
)
pythoncom.RecordClasses[cls.GUID] = cls


############################################
# The base of all makepy generated classes
############################################
Expand Down

0 comments on commit bb40a88

Please sign in to comment.