Skip to content

Commit 201e7ad

Browse files
committed
rest of requested changes
1 parent 8af6be7 commit 201e7ad

File tree

2 files changed

+41
-37
lines changed

2 files changed

+41
-37
lines changed

Lib/test/test_array.py

Lines changed: 40 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -473,50 +473,54 @@ def test_insert(self):
473473
def test_tofromfile(self):
474474
a = array.array(self.typecode, 2*self.example)
475475
self.assertRaises(TypeError, a.tofile)
476-
with os_helper.temp_dir() as temp_dir:
477-
temp_path = os.path.join(temp_dir, os_helper.TESTFN)
478-
try:
479-
with open(temp_path, 'wb') as f:
480-
a.tofile(f)
481-
b = array.array(self.typecode)
482-
with open(temp_path, 'rb') as f:
483-
self.assertRaises(TypeError, b.fromfile)
484-
b.fromfile(f, len(self.example))
485-
self.assertEqual(b, array.array(self.typecode, self.example))
486-
self.assertNotEqual(a, b)
487-
self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1)
488-
self.assertEqual(a, b)
489-
finally:
490-
os_helper.unlink(temp_path)
476+
os_helper.unlink(os_helper.TESTFN)
477+
f = open(os_helper.TESTFN, 'wb')
478+
try:
479+
a.tofile(f)
480+
f.close()
481+
b = array.array(self.typecode)
482+
f = open(os_helper.TESTFN, 'rb')
483+
self.assertRaises(TypeError, b.fromfile)
484+
b.fromfile(f, len(self.example))
485+
self.assertEqual(b, array.array(self.typecode, self.example))
486+
self.assertNotEqual(a, b)
487+
self.assertRaises(EOFError, b.fromfile, f, len(self.example)+1)
488+
self.assertEqual(a, b)
489+
f.close()
490+
finally:
491+
if not f.closed:
492+
f.close()
493+
os_helper.unlink(os_helper.TESTFN)
491494

492495
def test_fromfile_ioerror(self):
493496
# Issue #5395: Check if fromfile raises a proper OSError
494497
# instead of EOFError.
495498
a = array.array(self.typecode)
496-
with os_helper.temp_dir() as temp_dir:
497-
temp_path = os.path.join(temp_dir, os_helper.TESTFN)
498-
try:
499-
with open(temp_path, 'wb') as f:
500-
self.assertRaises(OSError, a.fromfile, f, len(self.example))
501-
finally:
502-
os_helper.unlink(temp_path)
499+
f = open(os_helper.TESTFN, 'wb')
500+
try:
501+
self.assertRaises(OSError, a.fromfile, f, len(self.example))
502+
finally:
503+
f.close()
504+
os_helper.unlink(os_helper.TESTFN)
503505

504506
def test_filewrite(self):
505507
a = array.array(self.typecode, 2*self.example)
506-
with os_helper.temp_dir() as temp_dir:
507-
temp_path = os.path.join(temp_dir, os_helper.TESTFN)
508-
try:
509-
with open(temp_path, 'wb') as f:
510-
f.write(a)
511-
b = array.array(self.typecode)
512-
with open(temp_path, 'rb') as f:
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-
finally:
519-
os_helper.unlink(temp_path)
508+
f = open(os_helper.TESTFN, 'wb')
509+
try:
510+
f.write(a)
511+
f.close()
512+
b = array.array(self.typecode)
513+
f = open(os_helper.TESTFN, 'rb')
514+
b.fromfile(f, len(self.example))
515+
self.assertEqual(b, array.array(self.typecode, self.example))
516+
self.assertNotEqual(a, b)
517+
b.fromfile(f, len(self.example))
518+
self.assertEqual(a, b)
519+
f.close()
520+
finally:
521+
if not f.closed:
522+
f.close()
523+
os_helper.unlink(os_helper.TESTFN)
520524

521525
def test_tofromlist(self):
522526
a = array.array(self.typecode, 2*self.example)

Modules/arraymodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ enum machine_format_code {
154154
static arraydata *
155155
arraydata_alloc(Py_ssize_t size, int itemsize)
156156
{
157-
size_t bufsize = sizeof(arraydata) + size * itemsize;
157+
Py_ssize_t bufsize = sizeof(arraydata) + size * itemsize;
158158
arraydata *data = (arraydata *)PyMem_Malloc(bufsize);
159159
if (data == NULL) {
160160
return NULL;

0 commit comments

Comments
 (0)