Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ea69bd3

Browse files
committed
Issue #16169: Fix ctypes.WinError()'s confusion between errno and winerror
1 parent 38714d6 commit ea69bd3

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

Lib/ctypes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ def WinError(code=None, descr=None):
456456
code = GetLastError()
457457
if descr is None:
458458
descr = FormatError(code).strip()
459-
return WindowsError(code, descr)
459+
return WindowsError(None, descr, None, code)
460460

461461
if sizeof(c_uint) == sizeof(c_void_p):
462462
c_size_t = c_uint

Lib/ctypes/test/test_win32.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ def test_COMError(self):
6767
self.assertEqual(ex.text, "text")
6868
self.assertEqual(ex.details, ("details",))
6969

70+
class TestWinError(unittest.TestCase):
71+
def test_winerror(self):
72+
# see Issue 16169
73+
import errno
74+
ERROR_INVALID_PARAMETER = 87
75+
msg = FormatError(ERROR_INVALID_PARAMETER).strip()
76+
args = (errno.EINVAL, msg, None, ERROR_INVALID_PARAMETER)
77+
78+
e = WinError(ERROR_INVALID_PARAMETER)
79+
self.assertEqual(e.args, args)
80+
self.assertEqual(e.errno, errno.EINVAL)
81+
self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)
82+
83+
windll.kernel32.SetLastError(ERROR_INVALID_PARAMETER)
84+
try:
85+
raise WinError()
86+
except OSError as exc:
87+
e = exc
88+
self.assertEqual(e.args, args)
89+
self.assertEqual(e.errno, errno.EINVAL)
90+
self.assertEqual(e.winerror, ERROR_INVALID_PARAMETER)
91+
7092
class Structures(unittest.TestCase):
7193

7294
def test_struct_by_value(self):

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ Core and Builtins
3737
Library
3838
-------
3939

40+
- Issue #16169: Fix ctypes.WinError()'s confusion between errno and winerror.
41+
4042
- Issue #16089: Allow ElementTree.TreeBuilder to work again with a non-Element
4143
element_factory (fixes a regression in SimpleTAL).
4244

0 commit comments

Comments
 (0)