From 7e2221a7dd9aa67816a924d7e43d1d7b243de9f2 Mon Sep 17 00:00:00 2001 From: Avasam Date: Sat, 19 Apr 2025 13:19:26 -0400 Subject: [PATCH] Respect subclass names in repr --- CHANGES.txt | 10 ++++++++++ Pythonwin/pywin/tools/browser.py | 2 +- com/win32com/client/__init__.py | 4 ++-- com/win32com/client/build.py | 4 ++-- com/win32com/server/exception.py | 4 +++- com/win32comext/axdebug/debugger.py | 2 +- com/win32comext/axscript/client/error.py | 3 --- com/win32comext/axscript/client/pyscript.py | 2 +- win32/Lib/win32pdhquery.py | 2 +- win32/Lib/win32rcparser.py | 2 +- 10 files changed, 22 insertions(+), 13 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 5c915caa07..cc88da6c2a 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -16,6 +16,16 @@ Coming in build 311, as yet unreleased * Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi) * Fixed a memory leak when SafeArrays are used as out parameters (@the-snork) * Fixed dispatch handling for properties (@the-snork) +* The following classes will now use the correct subclass name in `repr`: (#2570, @Avasam) + * `pywin.tools.browser.HLIPythonObject` + * `win32com.client.VARIANT` + * `win32com.client.build.MapEntry` + * `win32com.server.exception.COMException` + * `win32comext.axdebug.debugger.ModuleTreeNode` + * `win32comext.axscript.client.pyscript.NamedScriptAttribute` + * `win32comext.axscript.client.error.AXScriptException` + * `win32pdhquery.QueryError` + * `win32rcparser.StringDef` Build 310, released 2025/03/16 ------------------------------ diff --git a/Pythonwin/pywin/tools/browser.py b/Pythonwin/pywin/tools/browser.py index 8346d7d019..12908f3777 100644 --- a/Pythonwin/pywin/tools/browser.py +++ b/Pythonwin/pywin/tools/browser.py @@ -49,7 +49,7 @@ def __repr__(self): type = self.GetHLIType() except: type = "Generic" - return f"HLIPythonObject({type}) - name: {self.name} object: {self.myobject!r}" + return f"{self.__class__.__name__}({type}) - name: {self.name} object: {self.myobject!r}" def GetText(self): try: diff --git a/com/win32com/client/__init__.py b/com/win32com/client/__init__.py index 84e59a2fa2..d072221cb9 100644 --- a/com/win32com/client/__init__.py +++ b/com/win32com/client/__init__.py @@ -678,7 +678,7 @@ def __bool__(self): # A very simple VARIANT class. Only to be used with poorly-implemented COM # objects. If an object accepts an arg which is a simple "VARIANT", but still -# is very pickly about the actual variant type (eg, isn't happy with a VT_I4, +# is very picky about the actual variant type (eg, isn't happy with a VT_I4, # which it would get from a Python integer), you can use this to force a # particular VT. class VARIANT: @@ -700,4 +700,4 @@ def _del_value(self): value = property(_get_value, _set_value, _del_value) def __repr__(self): - return f"win32com.client.VARIANT({self.varianttype!r}, {self._value!r})" + return f"{self.__class__.__module__}.{self.__class__.__name__}({self.varianttype!r}, {self._value!r})" diff --git a/com/win32com/client/build.py b/com/win32com/client/build.py index 4b7628438e..ecf3a5645f 100644 --- a/com/win32com/client/build.py +++ b/com/win32com/client/build.py @@ -70,7 +70,7 @@ class NotSupportedException(Exception): class MapEntry: - "Simple holder for named attibutes - items in a map." + """Simple holder for named attributes - items in a map.""" def __init__( self, @@ -99,7 +99,7 @@ def __init__( def __repr__(self): return ( - "MapEntry(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, " + "{s.__class__.__name__}(dispid={s.dispid}, desc={s.desc}, names={s.names}, doc={s.doc!r}, " "resultCLSID={s.resultCLSID}, resultDocumentation={s.resultDocumentation}, " "wasProperty={s.wasProperty}, hidden={s.hidden}" ).format(s=self) diff --git a/com/win32com/server/exception.py b/com/win32com/server/exception.py index 43c08fcac1..ae5491e41b 100644 --- a/com/win32com/server/exception.py +++ b/com/win32com/server/exception.py @@ -78,7 +78,9 @@ def __init__( pythoncom.com_error.__init__(self, scode, self.description, None, -1) def __repr__(self): - return f"" + return ( + f"<{self.__class__.__name__} - scode={self.scode}, desc={self.description}>" + ) def IsCOMException(t=None): diff --git a/com/win32comext/axdebug/debugger.py b/com/win32comext/axdebug/debugger.py index d0585b9b6f..9e5bce674a 100644 --- a/com/win32comext/axdebug/debugger.py +++ b/com/win32comext/axdebug/debugger.py @@ -19,7 +19,7 @@ def __init__(self, module): self.cont = codecontainer.SourceModuleContainer(module) def __repr__(self): - return f"" + return f"<{self.__class__.__name__} wrapping {self.module}>" def Attach(self, parentRealNode): self.realNode.Attach(parentRealNode) diff --git a/com/win32comext/axscript/client/error.py b/com/win32comext/axscript/client/error.py index 41711abdd8..31f28c9e7a 100644 --- a/com/win32comext/axscript/client/error.py +++ b/com/win32comext/axscript/client/error.py @@ -218,9 +218,6 @@ def ExtractTracebackInfo(self, tb: TracebackType, site: COMScript): line = None return filename, lineno, name, line - def __repr__(self): - return "AXScriptException Object with description:" + self.description - def ProcessAXScriptException( scriptingSite: AXSite, diff --git a/com/win32comext/axscript/client/pyscript.py b/com/win32comext/axscript/client/pyscript.py index 91d3ed2c1e..90bd0fb788 100644 --- a/com/win32comext/axscript/client/pyscript.py +++ b/com/win32comext/axscript/client/pyscript.py @@ -109,7 +109,7 @@ def __init__(self, scriptItem): self.__dict__["_scriptItem_"] = scriptItem def __repr__(self): - return f"" + return f"<{self.__class__.__name__}{self._scriptItem_!r}>" def __getattr__(self, attr): # If a known subitem, return it. diff --git a/win32/Lib/win32pdhquery.py b/win32/Lib/win32pdhquery.py index e356f89142..dc1db54be7 100644 --- a/win32/Lib/win32pdhquery.py +++ b/win32/Lib/win32pdhquery.py @@ -565,6 +565,6 @@ def __init__(self, query: BaseQuery): self.query = query def __repr__(self): - return f"" + return f"<{self.__class__.__name__} in {self.query!r}>" __str__ = __repr__ diff --git a/win32/Lib/win32rcparser.py b/win32/Lib/win32rcparser.py index 183f075b1c..2792995832 100644 --- a/win32/Lib/win32rcparser.py +++ b/win32/Lib/win32rcparser.py @@ -169,7 +169,7 @@ def __init__(self, id, idNum, value): self.value = value def __repr__(self): - return f"StringDef({self.id!r}, {self.idNum!r}, {self.value!r})" + return f"{self.__class__.__name__}({self.id!r}, {self.idNum!r}, {self.value!r})" class RCParser: