|
14 | 14 | _testcapi = import_helper.import_module('_testcapi') |
15 | 15 | _testinternalcapi = import_helper.import_module('_testinternalcapi') |
16 | 16 |
|
| 17 | +NULL = None |
17 | 18 | STDERR_FD = 2 |
18 | 19 |
|
19 | 20 |
|
@@ -250,39 +251,54 @@ def func(x): |
250 | 251 |
|
251 | 252 | func(object()) |
252 | 253 |
|
253 | | - def test_pyobject_dump(self): |
| 254 | + def pyobject_dump(self, obj, release_gil=False): |
254 | 255 | pyobject_dump = _testcapi.pyobject_dump |
255 | | - obj = 'test string' |
256 | | - |
257 | | - filename = os_helper.TESTFN |
258 | | - self.addCleanup(os_helper.unlink, filename) |
259 | 256 |
|
260 | 257 | try: |
261 | 258 | old_stderr = os.dup(STDERR_FD) |
262 | 259 | except OSError as exc: |
263 | 260 | # os.dup(STDERR_FD) is not supported on WASI |
264 | 261 | self.skipTest(f"os.dup() failed with {exc!r}") |
265 | 262 |
|
| 263 | + filename = os_helper.TESTFN |
266 | 264 | try: |
267 | | - with open(filename, "wb") as fp: |
268 | | - fd = fp.fileno() |
269 | | - os.dup2(fd, STDERR_FD) |
270 | | - pyobject_dump(obj) |
| 265 | + try: |
| 266 | + with open(filename, "wb") as fp: |
| 267 | + fd = fp.fileno() |
| 268 | + os.dup2(fd, STDERR_FD) |
| 269 | + pyobject_dump(obj, release_gil) |
| 270 | + finally: |
| 271 | + os.dup2(old_stderr, STDERR_FD) |
| 272 | + os.close(old_stderr) |
| 273 | + |
| 274 | + with open(filename) as fp: |
| 275 | + return fp.read().rstrip() |
271 | 276 | finally: |
272 | | - os.dup2(old_stderr, STDERR_FD) |
273 | | - os.close(old_stderr) |
274 | | - |
275 | | - with open(filename) as fp: |
276 | | - output = fp.read() |
| 277 | + os_helper.unlink(filename) |
277 | 278 |
|
| 279 | + def test_pyobject_dump(self): |
| 280 | + # test string object |
| 281 | + str_obj = 'test string' |
| 282 | + output = self.pyobject_dump(str_obj) |
278 | 283 | hex_regex = r'(0x)?[0-9a-fA-F]+' |
279 | | - self.assertRegex(output.rstrip(), |
| 284 | + regex = ( |
280 | 285 | fr"object address : {hex_regex}\n" |
281 | 286 | r"object refcount : [0-9]+\n" |
282 | 287 | fr"object type : {hex_regex}\n" |
283 | 288 | r"object type name: str\n" |
284 | 289 | r"object repr : 'test string'" |
285 | 290 | ) |
| 291 | + self.assertRegex(output, regex) |
| 292 | + |
| 293 | + # release the GIL |
| 294 | + output = self.pyobject_dump(str_obj, release_gil=True) |
| 295 | + self.assertRegex(output, regex) |
| 296 | + |
| 297 | + # test NULL object |
| 298 | + output = self.pyobject_dump(NULL) |
| 299 | + hex_regex = r'(0x)?[0-9a-fA-F]+' |
| 300 | + self.assertEqual(output, |
| 301 | + '<object at (nil) is freed>') |
286 | 302 |
|
287 | 303 |
|
288 | 304 | if __name__ == "__main__": |
|
0 commit comments