def teardown(self): self.tmpfp.close() self.data = None if IS_PYPY: break_cycles() break_cycles() shutil.rmtree(self.tempdir)
def test_write_roundtrip(realfile, mmap, rate, channels, dt_str, tmpdir): dtype = np.dtype(dt_str) if realfile: tmpfile = str(tmpdir.join('temp.wav')) else: tmpfile = BytesIO() data = np.random.rand(100, channels) if channels == 1: data = data[:, 0] if dtype.kind == 'f': # The range of the float type should be in [-1, 1] data = data.astype(dtype) else: data = (data * 128).astype(dtype) wavfile.write(tmpfile, rate, data) rate2, data2 = wavfile.read(tmpfile, mmap=mmap) assert_equal(rate, rate2) assert_(data2.dtype.byteorder in ('<', '=', '|'), msg=data2.dtype) assert_array_equal(data, data2) # also test writing (gh-12176) if realfile: data2[0] = 0 else: with pytest.raises(ValueError, match='read-only'): data2[0] = 0 if realfile and mmap and IS_PYPY and sys.platform == 'win32': # windows cannot remove a dead file held by a mmap but not collected # in PyPy; since the filename gets reused in this test, clean this up break_cycles() break_cycles()
def test_mem_seteventhook(self): # The actual tests are within the C code in # multiarray/_multiarray_tests.c.src import numpy.core._multiarray_tests as ma_tests with pytest.warns(DeprecationWarning, match='PyDataMem_SetEventHook is deprecated'): ma_tests.test_pydatamem_seteventhook_start() # force an allocation and free of a numpy array # needs to be larger then limit of small memory cacher in ctors.c a = np.zeros(1000) del a break_cycles() with pytest.warns(DeprecationWarning, match='PyDataMem_SetEventHook is deprecated'): ma_tests.test_pydatamem_seteventhook_end()
def test_memmap_roundtrip(): # Fixme: used to crash on windows if not (sys.platform == 'win32' or sys.platform == 'cygwin'): for arr in basic_arrays + record_arrays: if arr.dtype.hasobject: # Skip these since they can't be mmap'ed. continue # Write it out normally and through mmap. nfn = os.path.join(tempdir, 'normal.npy') mfn = os.path.join(tempdir, 'memmap.npy') with open(nfn, 'wb') as fp: format.write_array(fp, arr) fortran_order = (arr.flags.f_contiguous and not arr.flags.c_contiguous) ma = format.open_memmap(mfn, mode='w+', dtype=arr.dtype, shape=arr.shape, fortran_order=fortran_order) ma[...] = arr del ma if IS_PYPY: break_cycles() # Check that both of these files' contents are the same. with open(nfn, 'rb') as fp: normal_bytes = fp.read() with open(mfn, 'rb') as fp: memmap_bytes = fp.read() assert_equal_(normal_bytes, memmap_bytes) # Check that reading the file using memmap works. ma = format.open_memmap(nfn, mode='r') del ma if IS_PYPY: break_cycles()
def test_version_2_0_memmap(): # requires more than 2 byte for header dt = [(("%d" % i) * 100, float) for i in range(500)] d = np.ones(1000, dtype=dt) tf = tempfile.mktemp('', 'mmap', dir=tempdir) # 1.0 requested but data cannot be saved this way assert_raises(ValueError, format.open_memmap, tf, mode='w+', dtype=d.dtype, shape=d.shape, version=(1, 0)) ma = format.open_memmap(tf, mode='w+', dtype=d.dtype, shape=d.shape, version=(2, 0)) ma[...] = d del ma if IS_PYPY: break_cycles() with warnings.catch_warnings(record=True) as w: warnings.filterwarnings('always', '', UserWarning) ma = format.open_memmap(tf, mode='w+', dtype=d.dtype, shape=d.shape, version=None) assert_(w[0].category is UserWarning) ma[...] = d del ma if IS_PYPY: break_cycles() ma = format.open_memmap(tf, mode='r') assert_array_equal(ma, d) del ma if IS_PYPY: break_cycles()
def teardown(self): self.tmpfp.close() self.data = None if IS_PYPY: break_cycles() break_cycles()
def test_read_write_files(): # test round trip for example file cwd = os.getcwd() try: tmpdir = tempfile.mkdtemp() os.chdir(tmpdir) with make_simple('simple.nc', 'w') as f: pass # read the file we just created in 'a' mode with netcdf_file('simple.nc', 'a') as f: check_simple(f) # add something f._attributes['appendRan'] = 1 # To read the NetCDF file we just created:: with netcdf_file('simple.nc') as f: # Using mmap is the default (but not on pypy) assert_equal(f.use_mmap, not IS_PYPY) check_simple(f) assert_equal(f._attributes['appendRan'], 1) # Read it in append (and check mmap is off) with netcdf_file('simple.nc', 'a') as f: assert_(not f.use_mmap) check_simple(f) assert_equal(f._attributes['appendRan'], 1) # Now without mmap with netcdf_file('simple.nc', mmap=False) as f: # Using mmap is the default assert_(not f.use_mmap) check_simple(f) # To read the NetCDF file we just created, as file object, no # mmap. When n * n_bytes(var_type) is not divisible by 4, this # raised an error in pupynere 1.0.12 and scipy rev 5893, because # calculated vsize was rounding up in units of 4 - see # https://www.unidata.ucar.edu/software/netcdf/guide_toc.html with open('simple.nc', 'rb') as fobj: with netcdf_file(fobj) as f: # by default, don't use mmap for file-like assert_(not f.use_mmap) check_simple(f) # Read file from fileobj, with mmap with suppress_warnings() as sup: if IS_PYPY: sup.filter( RuntimeWarning, "Cannot close a netcdf_file opened with mmap=True.*") with open('simple.nc', 'rb') as fobj: with netcdf_file(fobj, mmap=True) as f: assert_(f.use_mmap) check_simple(f) # Again read it in append mode (adding another att) with open('simple.nc', 'r+b') as fobj: with netcdf_file(fobj, 'a') as f: assert_(not f.use_mmap) check_simple(f) f.createDimension('app_dim', 1) var = f.createVariable('app_var', 'i', ('app_dim', )) var[:] = 42 # And... check that app_var made it in... with netcdf_file('simple.nc') as f: check_simple(f) assert_equal(f.variables['app_var'][:], 42) finally: if IS_PYPY: # windows cannot remove a dead file held by a mmap # that has not been collected in PyPy break_cycles() break_cycles() os.chdir(cwd) shutil.rmtree(tmpdir)