|
4 | 4 |
|
5 | 5 | import collections.abc |
6 | 6 | import io |
| 7 | +import os |
7 | 8 | import unittest |
8 | 9 | from test import support |
9 | 10 | from test.support import import_helper |
@@ -104,7 +105,10 @@ def test_empty(self): |
104 | 105 | class ArrayReconstructorTest(unittest.TestCase): |
105 | 106 |
|
106 | 107 | def setUp(self): |
107 | | - self.enterContext(warnings.catch_warnings()) |
| 108 | + if (not support.Py_GIL_DISABLED or |
| 109 | + any('"parallel_threads": null' in a for a in sys.argv) or |
| 110 | + all('parallel_threads' not in a for a in sys.argv)): |
| 111 | + self.enterContext(warnings.catch_warnings()) |
108 | 112 | warnings.filterwarnings( |
109 | 113 | "ignore", |
110 | 114 | message="The 'u' type code is deprecated and " |
@@ -220,7 +224,10 @@ class BaseTest: |
220 | 224 | # minitemsize: the minimum guaranteed itemsize |
221 | 225 |
|
222 | 226 | def setUp(self): |
223 | | - self.enterContext(warnings.catch_warnings()) |
| 227 | + if (not support.Py_GIL_DISABLED or |
| 228 | + any('"parallel_threads": null' in a for a in sys.argv) or |
| 229 | + all('parallel_threads' not in a for a in sys.argv)): |
| 230 | + self.enterContext(warnings.catch_warnings()) |
224 | 231 | warnings.filterwarnings( |
225 | 232 | "ignore", |
226 | 233 | message="The 'u' type code is deprecated and " |
@@ -472,54 +479,59 @@ def test_insert(self): |
472 | 479 | def test_tofromfile(self): |
473 | 480 | a = array.array(self.typecode, 2*self.example) |
474 | 481 | self.assertRaises(TypeError, a.tofile) |
475 | | - os_helper.unlink(os_helper.TESTFN) |
476 | | - f = open(os_helper.TESTFN, 'wb') |
477 | | - try: |
478 | | - a.tofile(f) |
479 | | - f.close() |
480 | | - b = array.array(self.typecode) |
481 | | - f = open(os_helper.TESTFN, 'rb') |
482 | | - self.assertRaises(TypeError, b.fromfile) |
483 | | - b.fromfile(f, len(self.example)) |
484 | | - self.assertEqual(b, array.array(self.typecode, self.example)) |
485 | | - self.assertNotEqual(a, b) |
486 | | - self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1) |
487 | | - self.assertEqual(a, b) |
488 | | - f.close() |
489 | | - finally: |
490 | | - if not f.closed: |
| 482 | + with os_helper.temp_dir() as temp_dir: |
| 483 | + temp_path = os.path.join(temp_dir, os_helper.TESTFN) |
| 484 | + f = open(temp_path, 'wb') |
| 485 | + try: |
| 486 | + a.tofile(f) |
491 | 487 | f.close() |
492 | | - os_helper.unlink(os_helper.TESTFN) |
| 488 | + b = array.array(self.typecode) |
| 489 | + f = open(temp_path, 'rb') |
| 490 | + self.assertRaises(TypeError, b.fromfile) |
| 491 | + b.fromfile(f, len(self.example)) |
| 492 | + self.assertEqual(b, array.array(self.typecode, self.example)) |
| 493 | + self.assertNotEqual(a, b) |
| 494 | + self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1) |
| 495 | + self.assertEqual(a, b) |
| 496 | + f.close() |
| 497 | + finally: |
| 498 | + if not f.closed: |
| 499 | + f.close() |
| 500 | + os_helper.unlink(temp_path) |
493 | 501 |
|
494 | 502 | def test_fromfile_ioerror(self): |
495 | 503 | # Issue #5395: Check if fromfile raises a proper OSError |
496 | 504 | # instead of EOFError. |
497 | 505 | a = array.array(self.typecode) |
498 | | - f = open(os_helper.TESTFN, 'wb') |
499 | | - try: |
500 | | - self.assertRaises(OSError, a.fromfile, f, len(self.example)) |
501 | | - finally: |
502 | | - f.close() |
503 | | - os_helper.unlink(os_helper.TESTFN) |
| 506 | + with os_helper.temp_dir() as temp_dir: |
| 507 | + temp_path = os.path.join(temp_dir, os_helper.TESTFN) |
| 508 | + f = open(temp_path, 'wb') |
| 509 | + try: |
| 510 | + self.assertRaises(OSError, a.fromfile, f, len(self.example)) |
| 511 | + finally: |
| 512 | + f.close() |
| 513 | + os_helper.unlink(temp_path) |
504 | 514 |
|
505 | 515 | def test_filewrite(self): |
506 | 516 | a = array.array(self.typecode, 2*self.example) |
507 | | - f = open(os_helper.TESTFN, 'wb') |
508 | | - try: |
509 | | - f.write(a) |
510 | | - f.close() |
511 | | - b = array.array(self.typecode) |
512 | | - f = open(os_helper.TESTFN, 'rb') |
513 | | - b.fromfile(f, len(self.example)) |
514 | | - self.assertEqual(b, array.array(self.typecode, self.example)) |
515 | | - self.assertNotEqual(a, b) |
516 | | - b.fromfile(f, len(self.example)) |
517 | | - self.assertEqual(a, b) |
518 | | - f.close() |
519 | | - finally: |
520 | | - if not f.closed: |
| 517 | + with os_helper.temp_dir() as temp_dir: |
| 518 | + temp_path = os.path.join(temp_dir, os_helper.TESTFN) |
| 519 | + f = open(temp_path, 'wb') |
| 520 | + try: |
| 521 | + f.write(a) |
| 522 | + f.close() |
| 523 | + b = array.array(self.typecode) |
| 524 | + f = open(temp_path, 'rb') |
| 525 | + b.fromfile(f, len(self.example)) |
| 526 | + self.assertEqual(b, array.array(self.typecode, self.example)) |
| 527 | + self.assertNotEqual(a, b) |
| 528 | + b.fromfile(f, len(self.example)) |
| 529 | + self.assertEqual(a, b) |
521 | 530 | f.close() |
522 | | - os_helper.unlink(os_helper.TESTFN) |
| 531 | + finally: |
| 532 | + if not f.closed: |
| 533 | + f.close() |
| 534 | + os_helper.unlink(temp_path) |
523 | 535 |
|
524 | 536 | def test_tofromlist(self): |
525 | 537 | a = array.array(self.typecode, 2*self.example) |
@@ -1201,6 +1213,7 @@ def test_obsolete_write_lock(self): |
1201 | 1213 | a = array.array('B', b"") |
1202 | 1214 | self.assertRaises(BufferError, _testcapi.getbuffer_with_null_view, a) |
1203 | 1215 |
|
| 1216 | + @unittest.skipIf(support.Py_GIL_DISABLED, 'not freed if GIL disabled') |
1204 | 1217 | def test_free_after_iterating(self): |
1205 | 1218 | support.check_free_after_iterating(self, iter, array.array, |
1206 | 1219 | (self.typecode,)) |
@@ -1255,6 +1268,7 @@ def test_issue17223(self): |
1255 | 1268 | self.assertRaises(ValueError, a.tounicode) |
1256 | 1269 | self.assertRaises(ValueError, str, a) |
1257 | 1270 |
|
| 1271 | + @unittest.skipIf(support.Py_GIL_DISABLED, 'warning stuff is not free-thread safe yet') |
1258 | 1272 | def test_typecode_u_deprecation(self): |
1259 | 1273 | with self.assertWarns(DeprecationWarning): |
1260 | 1274 | array.array("u") |
|
0 commit comments