Skip to content

Commit e57a738

Browse files
authored
Avoid forgetting about stacklevel in warnings (#2594)
1 parent 33d4238 commit e57a738

File tree

12 files changed

+24
-12
lines changed

12 files changed

+24
-12
lines changed

CHANGES.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ https://mhammond.github.io/pywin32_installers.html .
1313

1414
Coming in build 311, as yet unreleased
1515
--------------------------------------
16+
* pywin32's own warnings will now refer to the caller, rather than to the internal source of warning itself (#2594, @Avasam)
1617
* Fixed a regression that broke special __dunder__ methods with CoClass. (#1870, #2493, @Avasam, @geppi)
1718
* Fixed `TypeError: cannot unpack non-iterable NoneType object` when registering an axscript client ScriptItem (#2513, @Avasam)
1819
* Fixed a memory leak when SafeArrays are used as out parameters (@the-snork)

Pythonwin/pywin/framework/app.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,9 @@ def OnInitDialog(self):
354354
except OSError:
355355
ver = None
356356
if not ver:
357-
warnings.warn(f"Could not read pywin32's version from '{version_path}'")
357+
warnings.warn(
358+
f"Could not read pywin32's version from '{version_path}'", stacklevel=1
359+
)
358360
self.SetDlgItemText(win32ui.IDC_ABOUT_VERSION, ver)
359361
self.HookCommand(self.OnButHomePage, win32ui.IDC_BUTTON1)
360362

Pythonwin/pywin/framework/dbgcommands.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def OnAdd(self, msg, code):
105105
## for the toolbar button IDC_DBG_ADD fails, since MFC falls back to
106106
## sending a normal command if the UI update command fails.
107107
## win32ui.MessageBox('There is no active window - no breakpoint can be added')
108-
warnings.warn("There is no active window - no breakpoint can be added")
108+
warnings.warn(
109+
"There is no active window - no breakpoint can be added", stacklevel=1
110+
)
109111
return None
110112
pathName = doc.GetPathName()
111113
lineNo = view.LineFromChar(view.GetSel()[0]) + 1

adodbapi/adodbapi.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
except ImportError:
5353
import warnings
5454

55-
warnings.warn("pywin32 package required for adodbapi.", ImportWarning)
55+
warnings.warn("pywin32 package required for adodbapi.", ImportWarning, stacklevel=2)
5656

5757

5858
def getIndexedValue(obj, index):

com/win32comext/axscript/client/error.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ def __init__(
101101
warnings.warn(
102102
"`exc_type` and `exc_traceback` were redundant and are now unused.",
103103
category=DeprecationWarning,
104+
stacklevel=2,
104105
)
105106

106107
# And my other values...

ruff.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ line-ending = "cr-lf"
77

88
[lint]
99
select = [
10+
"B028", # no-explicit-stacklevel
1011
"C4", # flake8-comprehensions
1112
"F811", # redefined-while-unused
1213
"I", # isort

win32/Lib/afxres.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@
55
warnings.warn(
66
"Importing the global `afxres` module is deprecated. Import from `pywin.mfc.afxres` instead.",
77
category=DeprecationWarning,
8+
stacklevel=2,
89
)

win32/Lib/regcheck.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
warnings.warn(
77
"The regcheck module has been deprecated and pending removal since build 210",
88
category=DeprecationWarning,
9+
stacklevel=2,
910
)
1011

1112
import os

win32/Lib/win2kras.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111

1212
from win32ras import * # nopycln: import
1313

14-
warnings.warn(str(__doc__), category=DeprecationWarning)
14+
warnings.warn(str(__doc__), category=DeprecationWarning, stacklevel=2)

win32/Lib/win32serviceutil.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,9 @@ def InstallService(
261261
)
262262
except (win32service.error, NotImplementedError):
263263
## delayed start only exists on Vista and later - warn only when trying to set delayed to True
264-
warnings.warn("Delayed Start not available on this system")
264+
warnings.warn(
265+
"Delayed Start not available on this system", stacklevel=2
266+
)
265267
win32service.CloseServiceHandle(hs)
266268
finally:
267269
win32service.CloseServiceHandle(hscm)
@@ -345,7 +347,9 @@ def ChangeServiceConfig(
345347
## doensn't exist. On Win2k and XP, will fail with ERROR_INVALID_LEVEL
346348
## Warn only if trying to set delayed to True
347349
if delayedstart:
348-
warnings.warn("Delayed Start not available on this system")
350+
warnings.warn(
351+
"Delayed Start not available on this system", stacklevel=2
352+
)
349353
finally:
350354
win32service.CloseServiceHandle(hs)
351355
finally:

0 commit comments

Comments
 (0)