diff --git a/CHANGES.txt b/CHANGES.txt index 67dd45c52..fd9c14bd8 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,4 @@ -Notable changes in recent builds. +Notable changes in recent builds. Maintained by hand, so what's "notable" is subjective! Contributors are encouraged to add entries for their work. @@ -13,6 +13,7 @@ https://mhammond.github.io/pywin32_installers.html . Coming in build 311, as yet unreleased -------------------------------------- +* pywin32's own warnings will now refer to the caller, rather than to the internal source of warning itself (#2594, @Avasam) * Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi) * Fixed `TypeError: cannot unpack non-iterable NoneType object` when registering an axscript client ScriptItem (#2513, @Avasam) * Fixed a memory leak when SafeArrays are used as out parameters (@the-snork) diff --git a/Pythonwin/pywin/framework/app.py b/Pythonwin/pywin/framework/app.py index 931a00787..561ed35d4 100644 --- a/Pythonwin/pywin/framework/app.py +++ b/Pythonwin/pywin/framework/app.py @@ -357,7 +357,9 @@ def OnInitDialog(self): except OSError: ver = None if not ver: - warnings.warn(f"Could not read pywin32's version from '{version_path}'") + warnings.warn( + f"Could not read pywin32's version from '{version_path}'", stacklevel=2 + ) self.SetDlgItemText(win32ui.IDC_ABOUT_VERSION, ver) self.HookCommand(self.OnButHomePage, win32ui.IDC_BUTTON1) diff --git a/Pythonwin/pywin/framework/dbgcommands.py b/Pythonwin/pywin/framework/dbgcommands.py index 72b99a567..cf32394cd 100644 --- a/Pythonwin/pywin/framework/dbgcommands.py +++ b/Pythonwin/pywin/framework/dbgcommands.py @@ -105,7 +105,9 @@ def OnAdd(self, msg, code): ## for the toolbar button IDC_DBG_ADD fails, since MFC falls back to ## sending a normal command if the UI update command fails. ## win32ui.MessageBox('There is no active window - no breakpoint can be added') - warnings.warn("There is no active window - no breakpoint can be added") + warnings.warn( + "There is no active window - no breakpoint can be added", stacklevel=2 + ) return None pathName = doc.GetPathName() lineNo = view.LineFromChar(view.GetSel()[0]) + 1 diff --git a/adodbapi/adodbapi.py b/adodbapi/adodbapi.py index 8f590c466..b850623bd 100644 --- a/adodbapi/adodbapi.py +++ b/adodbapi/adodbapi.py @@ -52,7 +52,7 @@ except ImportError: import warnings - warnings.warn("pywin32 package required for adodbapi.", ImportWarning) + warnings.warn("pywin32 package required for adodbapi.", ImportWarning, stacklevel=2) def getIndexedValue(obj, index): diff --git a/com/win32comext/axscript/client/error.py b/com/win32comext/axscript/client/error.py index 31f28c9e7..b731397e2 100644 --- a/com/win32comext/axscript/client/error.py +++ b/com/win32comext/axscript/client/error.py @@ -101,6 +101,7 @@ def __init__( warnings.warn( "`exc_type` and `exc_traceback` were redundant and are now unused.", category=DeprecationWarning, + stacklevel=2, ) # And my other values... diff --git a/ruff.toml b/ruff.toml index bc9c615e4..1a58fe8dc 100644 --- a/ruff.toml +++ b/ruff.toml @@ -7,6 +7,7 @@ line-ending = "cr-lf" [lint] select = [ + "B028", # no-explicit-stacklevel "C4", # flake8-comprehensions "F811", # redefined-while-unused "I", # isort diff --git a/win32/Lib/afxres.py b/win32/Lib/afxres.py index 6053d6236..ff8e84dc4 100644 --- a/win32/Lib/afxres.py +++ b/win32/Lib/afxres.py @@ -5,4 +5,5 @@ warnings.warn( "Importing the global `afxres` module is deprecated. Import from `pywin.mfc.afxres` instead.", category=DeprecationWarning, + stacklevel=2, ) diff --git a/win32/Lib/regcheck.py b/win32/Lib/regcheck.py index f3bc0a0cd..7ceb1b39a 100644 --- a/win32/Lib/regcheck.py +++ b/win32/Lib/regcheck.py @@ -6,6 +6,7 @@ warnings.warn( "The regcheck module has been deprecated and pending removal since build 210", category=DeprecationWarning, + stacklevel=2, ) import os diff --git a/win32/Lib/win2kras.py b/win32/Lib/win2kras.py index 9e3724811..90e4a3d52 100644 --- a/win32/Lib/win2kras.py +++ b/win32/Lib/win2kras.py @@ -11,4 +11,4 @@ from win32ras import * # nopycln: import -warnings.warn(str(__doc__), category=DeprecationWarning) +warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2) diff --git a/win32/Lib/win32gui_struct.py b/win32/Lib/win32gui_struct.py index d3e5e8306..1f9d15b65 100644 --- a/win32/Lib/win32gui_struct.py +++ b/win32/Lib/win32gui_struct.py @@ -122,7 +122,10 @@ def PackMENUITEMINFO( if dwTypeData is not None: import warnings - warnings.warn("PackMENUITEMINFO: please use dwItemData instead of dwTypeData") + warnings.warn( + "PackMENUITEMINFO: please use dwItemData instead of dwTypeData", + stacklevel=2, + ) if dwItemData is None: dwItemData = dwTypeData or 0 diff --git a/win32/Lib/win32serviceutil.py b/win32/Lib/win32serviceutil.py index 759e96b51..8df954ba3 100644 --- a/win32/Lib/win32serviceutil.py +++ b/win32/Lib/win32serviceutil.py @@ -261,7 +261,9 @@ def InstallService( ) except (win32service.error, NotImplementedError): ## delayed start only exists on Vista and later - warn only when trying to set delayed to True - warnings.warn("Delayed Start not available on this system") + warnings.warn( + "Delayed Start not available on this system", stacklevel=2 + ) win32service.CloseServiceHandle(hs) finally: win32service.CloseServiceHandle(hscm) @@ -345,7 +347,9 @@ def ChangeServiceConfig( ## doensn't exist. On Win2k and XP, will fail with ERROR_INVALID_LEVEL ## Warn only if trying to set delayed to True if delayedstart: - warnings.warn("Delayed Start not available on this system") + warnings.warn( + "Delayed Start not available on this system", stacklevel=2 + ) finally: win32service.CloseServiceHandle(hs) finally: diff --git a/win32/Lib/winerror.py b/win32/Lib/winerror.py index 4499bf5f2..90dc1c220 100644 --- a/win32/Lib/winerror.py +++ b/win32/Lib/winerror.py @@ -40,11 +40,10 @@ def __getattr__(name: str) -> int: "CERTDB_E_JET_ERROR": -2146873344, }.get(name): warnings.warn( - DeprecationWarning( - f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. " - + f"If you believe this is incorrect or are still using '{name}', " - + "please raise an issue at https://github.com/mhammond/pywin32/issues" - ), + f"Constant '{name}' is no longer part of Windows' SDK and may be removed eventually. " + + f"If you believe this is incorrect or are still using '{name}', " + + "please raise an issue at https://github.com/mhammond/pywin32/issues", + DeprecationWarning, stacklevel=2, ) return attr diff --git a/win32/winxpgui.py b/win32/winxpgui.py index 84924114a..493379601 100644 --- a/win32/winxpgui.py +++ b/win32/winxpgui.py @@ -10,4 +10,4 @@ ) from win32gui import * # nopycln: import -warnings.warn(str(__doc__), category=DeprecationWarning) +warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2)