def test_fails_if_file_is_not_open(self): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) test_file = os.fdopen(fd, "r") test_file.close() with self.assertRaises(InputError): handle_from_file(test_file)
def test_fails_if_file_is_not_open(self): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) test_file = os.fdopen(fd, "r") test_file.close() with self.assertRaises(InputError): handle_from_file(test_file)
def create_handle(self): fd, path = tempfile.mkstemp() self.addCleanup(os.remove, path) file_ = os.fdopen(fd, "w") self.addCleanup(file_.close) handle = handle_from_file(file_) return handle, path
def create_handle(self): fd, path = tempfile.mkstemp() self.addCleanup(os.remove, path) file_ = os.fdopen(fd, "w") self.addCleanup(file_.close) handle = handle_from_file(file_) return handle, path
def test_get_handle_info_file(self): _, library = dist.load() # can't use mkstemp: not inheritable on Python < 3.4 tempdir = tempfile.mkdtemp() self.addCleanup(os.rmdir, tempdir) filename = os.path.join(tempdir, "test_file") with open(filename, "w") as test_file: self.addCleanup(os.unlink, filename) test_file.write("data") file_handle = handle_from_file(test_file) handle_flags = GetHandleInformation(file_handle) inherit = handle_flags & library.HANDLE_FLAG_INHERIT expected = self._expected_inheritance() self.assertEqual(inherit, expected)
def _set_handle_info_file(self, inherit, check=False): _, library = dist.load() tempdir = tempfile.mkdtemp() self.addCleanup(os.rmdir, tempdir) filename = os.path.join(tempdir, "test_file") with open(filename, "w") as test_file: self.addCleanup(os.unlink, filename) test_file.write("data") file_handle = handle_from_file(test_file) SetHandleInformation(file_handle, library.HANDLE_FLAG_INHERIT, inherit) if check: result = GetHandleInformation(file_handle) self.assertEqual(inherit, result)
def test_get_handle_info_file(self): _, library = dist.load() # can't use mkstemp: not inheritable on Python < 3.4 tempdir = tempfile.mkdtemp() self.addCleanup(os.rmdir, tempdir) filename = os.path.join(tempdir, "test_file") with open(filename, "w") as test_file: self.addCleanup(os.unlink, filename) test_file.write("data") file_handle = handle_from_file(test_file) handle_flags = GetHandleInformation(file_handle) inherit = handle_flags & library.HANDLE_FLAG_INHERIT expected = self._expected_inheritance() self.assertEqual(inherit, expected)
def test_file_rename_after_spawn(self): _, library = dist.load() tempdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True) filename = os.path.join(tempdir, "original_name") with open(filename, "w") as test_file: test_file.write("data") file_handle = handle_from_file(test_file) # prevent file_handle inheritance SetHandleInformation(file_handle, library.HANDLE_FLAG_INHERIT, 0) # spawn child while test_file is open child = self._spawn_child() self.addCleanup(self._cleanup_child, child) newfilename = os.path.join(tempdir, "new_name") # works as long as file is closed and not inherited by child os.rename(filename, newfilename)
def test_file_rename_after_spawn(self): _, library = dist.load() tempdir = tempfile.mkdtemp() self.addCleanup(shutil.rmtree, tempdir, ignore_errors=True) filename = os.path.join(tempdir, "original_name") with open(filename, "w") as test_file: test_file.write("data") file_handle = handle_from_file(test_file) # prevent file_handle inheritance SetHandleInformation(file_handle, library.HANDLE_FLAG_INHERIT, 0) # spawn child while test_file is open child = self._spawn_child() self.addCleanup(self._cleanup_child, child) newfilename = os.path.join(tempdir, "new_name") # works as long as file is closed and not inherited by child os.rename(filename, newfilename)
def _set_handle_info_file(self, inherit, check=False): _, library = dist.load() tempdir = tempfile.mkdtemp() self.addCleanup(os.rmdir, tempdir) filename = os.path.join(tempdir, "test_file") with open(filename, "w") as test_file: self.addCleanup(os.unlink, filename) test_file.write("data") file_handle = handle_from_file(test_file) SetHandleInformation( file_handle, library.HANDLE_FLAG_INHERIT, inherit ) if check: result = GetHandleInformation(file_handle) self.assertEqual(inherit, result)
def test_opens_correct_file_handle(self): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) os.close(fd) test_file = open(path, "w") handle = handle_from_file(test_file) CloseHandle(handle) # If CloseHandle() was passed the same handle # that test_file is trying to write to the file # and/or flushing it should fail. try: test_file.write("foo") test_file.flush() except (OSError, IOError, WindowsError) as error: # EBADF == Bad file descriptor (because CloseHandle closed it) self.assertEqual(error.errno, EBADF) else: self.fail("Expected os.close(%r) to fail" % fd)
def test_opens_correct_file_handle(self): fd, path = tempfile.mkstemp() self.addCleanup(os.unlink, path) os.close(fd) test_file = open(path, "w") handle = handle_from_file(test_file) CloseHandle(handle) # If CloseHandle() was passed the same handle # that test_file is trying to write to the file # and/or flushing it should fail. try: test_file.write("foo") test_file.flush() except (OSError, IOError, WindowsError) as error: # EBADF == Bad file descriptor (because CloseHandle closed it) self.assertEqual(error.errno, EBADF) else: self.fail("Expected os.close(%r) to fail" % fd)
def test_fails_if_not_a_file(self): with self.assertRaises(InputError): handle_from_file(0)
def test_fails_if_not_a_file(self): with self.assertRaises(InputError): handle_from_file(0)