def test_mode_pil_image(self): img = np.random.randint(0, 255, (5, 5, 3)).astype(np.uint8) result = Image.fromarray(img) result.save(self.temp('test_simple.jpg')) # PIL doesn't support append mode. So it will allways use binary read. with Image.open(self.temp('test_simple.jpg')) as fileobj: assert util.fileobj_mode(fileobj) == 'rb'
def test_mode_pil_image(self): img = np.random.randint(0, 255, (5, 5, 3)).astype(np.uint8) result = Image.fromarray(img) result.save(self.temp('test_simple.jpg')) # PIL doesn't support append mode. So it will allways use binary read. with Image.open(self.temp('test_simple.jpg')) as fileobj: assert util.fileobj_mode(fileobj) == 'rb'
def test_mode_normal_buffering(self): # Use the python IO with buffering parameter. Binary mode only: # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [(0, 'ab', 'ab'), (0, 'wb', 'wb'), (1, 'xb', 'xb'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp(f'test1{num}.dat') with open(filename, mode, buffering=0) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_normal_no_buffering(self): # Python IO without buffering # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [(0, 'a', 'a'), (0, 'ab', 'ab'), (0, 'w', 'w'), (0, 'wb', 'wb'), (1, 'x', 'x'), (1, 'r', 'r'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp(f'test2{num}.dat') with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_normal_no_buffering(self): # Python IO without buffering # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [(0, 'a', 'a'), (0, 'ab', 'ab'), (0, 'w', 'w'), (0, 'wb', 'wb'), (1, 'x', 'x'), (1, 'r', 'r'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp('test2{0}.dat'.format(num)) with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_normal_buffering(self): # Use the python IO with buffering parameter. Binary mode only: # see "test_mode_gzip" for explanation of tuple meanings. num_mode_resmode = [(0, 'ab', 'ab'), (0, 'wb', 'wb'), (1, 'xb', 'xb'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp('test1{0}.dat'.format(num)) with open(filename, mode, buffering=0) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_normalization(self): # Use the normal python IO in append mode with all possible permutation # of the "mode" letters. # Tuple gives a file name suffix, the given mode and the functions # return. The filenumber is only for consistency with the other # test functions. Append can deal with existing and not existing files. for num, mode, res in [(0, 'a', 'a'), (0, 'a+', 'a+'), (0, 'ab', 'ab'), (0, 'a+b', 'ab+'), (0, 'ab+', 'ab+')]: filename = self.temp(f'test3{num}.dat') with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_normalization(self): # Use the normal python IO in append mode with all possible permutation # of the "mode" letters. # Tuple gives a file name suffix, the given mode and the functions # return. The filenumber is only for consistency with the other # test functions. Append can deal with existing and not existing files. for num, mode, res in [(0, 'a', 'a'), (0, 'a+', 'a+'), (0, 'ab', 'ab'), (0, 'a+b', 'ab+'), (0, 'ab+', 'ab+')]: filename = self.temp('test3{0}.dat'.format(num)) with open(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_gzip(self): # Open a gzip in every possible (gzip is binary or "touch" only) way # and check if the mode was correctly identified. # The lists consist of tuples: filenumber, given mode, identified mode # The filenumber must be given because read expects the file to exist # and x expects it to NOT exist. num_mode_resmode = [(0, 'a', 'ab'), (0, 'ab', 'ab'), (0, 'w', 'wb'), (0, 'wb', 'wb'), (1, 'x', 'xb'), (1, 'r', 'rb'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp(f'test{num}.gz') with gzip.GzipFile(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_gzip(self): # Open a gzip in every possible (gzip is binary or "touch" only) way # and check if the mode was correctly identified. # The lists consist of tuples: filenumber, given mode, identified mode # The filenumber must be given because read expects the file to exist # and x expects it to NOT exist. num_mode_resmode = [(0, 'a', 'ab'), (0, 'ab', 'ab'), (0, 'w', 'wb'), (0, 'wb', 'wb'), (1, 'x', 'xb'), (1, 'r', 'rb'), (1, 'rb', 'rb')] for num, mode, res in num_mode_resmode: filename = self.temp('test{0}.gz'.format(num)) with gzip.GzipFile(filename, mode) as fileobj: assert util.fileobj_mode(fileobj) == res
def test_mode_strings(self): # A string signals that the file should be opened so the function # should return None, because it's simply not opened yet. assert util.fileobj_mode('tmp1.fits') is None
def test_mode_strings(self): # A string signals that the file should be opened so the function # should return None, because it's simply not opened yet. assert util.fileobj_mode('tmp1.fits') is None