Skip to content

Commit ffeae3b

Browse files
committed
Use a non-string key for the ctypes callback error test
1 parent c4ca44f commit ffeae3b

1 file changed

Lines changed: 14 additions & 10 deletions

File tree

‎Lib/test/test_ctypes/test_callbacks.py‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,16 @@ def __del__(self):
146146
def test_argument_conversion_error_does_not_leak(self):
147147
import weakref
148148

149-
class PoisonedKey(str):
150-
__hash__ = str.__hash__
149+
marker = '_needs_com_addref_'
150+
151+
class PoisonedKey:
152+
def __hash__(self):
153+
return hash(marker)
151154

152155
def __eq__(self, other):
153-
if str(other) == '_needs_com_addref_':
156+
if type(other) is str and other == marker:
154157
raise RuntimeError('injected lookup failure')
155-
return str.__eq__(self, other)
158+
return NotImplemented
156159

157160
refs = []
158161
finalized_values = []
@@ -167,15 +170,16 @@ def finalize(arg):
167170
if getattr(arg, 'temporary', False):
168171
finalized_values.append(arg.value)
169172

170-
Arg = type('Arg', (ctypes.c_void_p,), {
171-
'__init__': init,
172-
'__del__': finalize,
173-
PoisonedKey('_needs_com_addref_'): True,
174-
})
173+
with self.assertWarnsRegex(RuntimeWarning, 'non-string key'):
174+
Arg = type('Arg', (ctypes.c_void_p,), {
175+
'__init__': init,
176+
'__del__': finalize,
177+
PoisonedKey(): True,
178+
})
175179

176180
# Verify that the COM marker lookup can fail.
177181
with self.assertRaises(RuntimeError):
178-
'_needs_com_addref_' in vars(Arg)
182+
marker in vars(Arg)
179183

180184
calls = 0
181185

0 commit comments

Comments
 (0)