|
| 1 | +import sys |
1 | 2 | import unittest
|
2 | 3 | from test.test_ctypes import need_symbol
|
3 | 4 | import test.support
|
@@ -243,6 +244,58 @@ def test_parameter_repr(self):
|
243 | 244 | self.assertRegex(repr(c_wchar_p.from_param('hihi')), r"^<cparam 'Z' \(0x[A-Fa-f0-9]+\)>$")
|
244 | 245 | self.assertRegex(repr(c_void_p.from_param(0x12)), r"^<cparam 'P' \(0x0*12\)>$")
|
245 | 246 |
|
| 247 | + @test.support.cpython_only |
| 248 | + @unittest.skipUnless(sys.platform == "win32", 'Windows-specific test') |
| 249 | + def test_from_param_result_refcount(self): |
| 250 | + # Issue #99952 |
| 251 | + import sys |
| 252 | + import _ctypes_test |
| 253 | + from ctypes import PyDLL, c_int, c_void_p, py_object, Structure |
| 254 | + |
| 255 | + class X(Structure): |
| 256 | + _fields_ = [("a", c_void_p)] |
| 257 | + |
| 258 | + def __del__(self): |
| 259 | + trace.append(4) |
| 260 | + |
| 261 | + @classmethod |
| 262 | + def from_param(cls, value): |
| 263 | + trace.append(2) |
| 264 | + return cls() |
| 265 | + |
| 266 | + PyList_Append = PyDLL("python", handle=sys.dllhandle).PyList_Append |
| 267 | + PyList_Append.restype = c_int |
| 268 | + PyList_Append.argtypes = [py_object, py_object, X] |
| 269 | + |
| 270 | + trace = [] |
| 271 | + trace.append(1) |
| 272 | + PyList_Append(trace, 3, "dummy") |
| 273 | + trace.append(5) |
| 274 | + |
| 275 | + self.assertEqual(trace, [1, 2, 3, 4, 5]) |
| 276 | + |
| 277 | + class Y(Structure): |
| 278 | + _fields_ = [("a", c_void_p), ("b", c_void_p)] |
| 279 | + |
| 280 | + def __del__(self): |
| 281 | + trace.append(4) |
| 282 | + |
| 283 | + @classmethod |
| 284 | + def from_param(cls, value): |
| 285 | + trace.append(2) |
| 286 | + return cls() |
| 287 | + |
| 288 | + PyList_Append = PyDLL("python", handle=sys.dllhandle).PyList_Append |
| 289 | + PyList_Append.restype = c_int |
| 290 | + PyList_Append.argtypes = [py_object, py_object, Y] |
| 291 | + |
| 292 | + trace = [] |
| 293 | + trace.append(1) |
| 294 | + PyList_Append(trace, 3, "dummy") |
| 295 | + trace.append(5) |
| 296 | + |
| 297 | + self.assertEqual(trace, [1, 2, 3, 4, 5]) |
| 298 | + |
246 | 299 | ################################################################
|
247 | 300 |
|
248 | 301 | if __name__ == '__main__':
|
|
0 commit comments