我们从Python开源项目中,提取了以下43个代码示例,用于说明如何使用errno.ENOTSUP。
def _copyxattr(src, dst, *, follow_symlinks=True): """Copy extended filesystem attributes from `src` to `dst`. Overwrite existing attributes. If `follow_symlinks` is false, symlinks won't be followed. """ try: names = os.listxattr(src, follow_symlinks=follow_symlinks) except OSError as e: if e.errno not in (errno.ENOTSUP, errno.ENODATA): raise return for name in names: try: value = os.getxattr(src, name, follow_symlinks=follow_symlinks) os.setxattr(dst, name, value, follow_symlinks=follow_symlinks) except OSError as e: if e.errno not in (errno.EPERM, errno.ENOTSUP, errno.ENODATA): raise
def test_copystat_handles_harmless_chflags_errors(self): tmpdir = self.mkdtemp() file1 = os.path.join(tmpdir, 'file1') file2 = os.path.join(tmpdir, 'file2') self.write_file(file1, 'xxx') self.write_file(file2, 'xxx') def make_chflags_raiser(err): ex = OSError() def _chflags_raiser(path, flags): ex.errno = err raise ex return _chflags_raiser old_chflags = os.chflags try: for err in errno.EOPNOTSUPP, errno.ENOTSUP: os.chflags = make_chflags_raiser(err) shutil.copystat(file1, file2) # assert others errors break it os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP) self.assertRaises(OSError, shutil.copystat, file1, file2) finally: os.chflags = old_chflags
def test_copystat_handles_harmless_chflags_errors(self): tmpdir = self.mkdtemp() file1 = os.path.join(tmpdir, 'file1') file2 = os.path.join(tmpdir, 'file2') write_file(file1, 'xxx') write_file(file2, 'xxx') def make_chflags_raiser(err): ex = OSError() def _chflags_raiser(path, flags, *, follow_symlinks=True): ex.errno = err raise ex return _chflags_raiser old_chflags = os.chflags try: for err in errno.EOPNOTSUPP, errno.ENOTSUP: os.chflags = make_chflags_raiser(err) shutil.copystat(file1, file2) # assert others errors break it os.chflags = make_chflags_raiser(errno.EOPNOTSUPP + errno.ENOTSUP) self.assertRaises(OSError, shutil.copystat, file1, file2) finally: os.chflags = old_chflags
def copy_to_audiobook_folder(path): """ Copies the given path (folder or file) to the audio book folder. """ try: name = os.path.basename(os.path.normpath(path)) shutil.copytree(path, db.Settings.get().path + "/" + name) except OSError as exc: if exc.errno == errno.ENOTDIR: try: shutil.copy(path, db.Settings.get().path) except OSError as e: if e.errno == 95: log.error("Could not import file " + path) log.error(exc) else: log.error(e) elif exc.errno == errno.ENOTSUP: log.error("Could not import file " + path) log.error(exc) else: log.error("Could not import file " + path) log.error(exc)
def test_listen(self, tco): tco.listen(backlog=1) assert tco.state.LISTEN is True with pytest.raises(nfc.llcp.Error) as excinfo: tco.listen(1) assert excinfo.value.errno == errno.ENOTSUP tco.close() with pytest.raises(nfc.llcp.Error) as excinfo: tco.listen(1) assert excinfo.value.errno == errno.ESHUTDOWN
def listen(self, backlog): with self.lock: if self.state.SHUTDOWN: raise err.Error(errno.ESHUTDOWN) if not self.state.CLOSED: self.err("listen() but socket state is {0}".format(self.state)) raise err.Error(errno.ENOTSUP) self.state.LISTEN = True self.recv_buf = backlog
def copystat(src, dst, *, follow_symlinks=True): """Copy all stat info (mode bits, atime, mtime, flags) from src to dst. If the optional flag `follow_symlinks` is not set, symlinks aren't followed if and only if both `src` and `dst` are symlinks. """ def _nop(*args, ns=None, follow_symlinks=None): pass # follow symlinks (aka don't not follow symlinks) follow = follow_symlinks or not (os.path.islink(src) and os.path.islink(dst)) if follow: # use the real function if it exists def lookup(name): return getattr(os, name, _nop) else: # use the real function only if it exists # *and* it supports follow_symlinks def lookup(name): fn = getattr(os, name, _nop) if fn in os.supports_follow_symlinks: return fn return _nop st = lookup("stat")(src, follow_symlinks=follow) mode = stat.S_IMODE(st.st_mode) lookup("utime")(dst, ns=(st.st_atime_ns, st.st_mtime_ns), follow_symlinks=follow) try: lookup("chmod")(dst, mode, follow_symlinks=follow) except NotImplementedError: # if we got a NotImplementedError, it's because # * follow_symlinks=False, # * lchown() is unavailable, and # * either # * fchownat() is unavailable or # * fchownat() doesn't implement AT_SYMLINK_NOFOLLOW. # (it returned ENOSUP.) # therefore we're out of options--we simply cannot chown the # symlink. give up, suppress the error. # (which is what shutil always did in this circumstance.) pass if hasattr(st, 'st_flags'): try: lookup("chflags")(dst, st.st_flags, follow_symlinks=follow) except OSError as why: for err in 'EOPNOTSUPP', 'ENOTSUP': if hasattr(errno, err) and why.errno == getattr(errno, err): break else: raise _copyxattr(src, dst, follow_symlinks=follow)
def test_copyxattr(self): tmp_dir = self.mkdtemp() src = os.path.join(tmp_dir, 'foo') write_file(src, 'foo') dst = os.path.join(tmp_dir, 'bar') write_file(dst, 'bar') # no xattr == no problem shutil._copyxattr(src, dst) # common case os.setxattr(src, 'user.foo', b'42') os.setxattr(src, 'user.bar', b'43') shutil._copyxattr(src, dst) self.assertEqual(os.listxattr(src), os.listxattr(dst)) self.assertEqual( os.getxattr(src, 'user.foo'), os.getxattr(dst, 'user.foo')) # check errors don't affect other attrs os.remove(dst) write_file(dst, 'bar') os_error = OSError(errno.EPERM, 'EPERM') def _raise_on_user_foo(fname, attr, val, **kwargs): if attr == 'user.foo': raise os_error else: orig_setxattr(fname, attr, val, **kwargs) try: orig_setxattr = os.setxattr os.setxattr = _raise_on_user_foo shutil._copyxattr(src, dst) self.assertIn('user.bar', os.listxattr(dst)) finally: os.setxattr = orig_setxattr # the source filesystem not supporting xattrs should be ok, too. def _raise_on_src(fname, *, follow_symlinks=True): if fname == src: raise OSError(errno.ENOTSUP, 'Operation not supported') return orig_listxattr(fname, follow_symlinks=follow_symlinks) try: orig_listxattr = os.listxattr os.listxattr = _raise_on_src shutil._copyxattr(src, dst) finally: os.listxattr = orig_listxattr # test that shutil.copystat copies xattrs src = os.path.join(tmp_dir, 'the_original') write_file(src, src) os.setxattr(src, 'user.the_value', b'fiddly') dst = os.path.join(tmp_dir, 'the_copy') write_file(dst, dst) shutil.copystat(src, dst) self.assertEqual(os.getxattr(dst, 'user.the_value'), b'fiddly')
def test_copyxattr(self): tmp_dir = self.mkdtemp() src = os.path.join(tmp_dir, 'foo') write_file(src, 'foo') dst = os.path.join(tmp_dir, 'bar') write_file(dst, 'bar') # no xattr == no problem shutil._copyxattr(src, dst) # common case os.setxattr(src, 'user.foo', b'42') os.setxattr(src, 'user.bar', b'43') shutil._copyxattr(src, dst) self.assertEqual(sorted(os.listxattr(src)), sorted(os.listxattr(dst))) self.assertEqual( os.getxattr(src, 'user.foo'), os.getxattr(dst, 'user.foo')) # check errors don't affect other attrs os.remove(dst) write_file(dst, 'bar') os_error = OSError(errno.EPERM, 'EPERM') def _raise_on_user_foo(fname, attr, val, **kwargs): if attr == 'user.foo': raise os_error else: orig_setxattr(fname, attr, val, **kwargs) try: orig_setxattr = os.setxattr os.setxattr = _raise_on_user_foo shutil._copyxattr(src, dst) self.assertIn('user.bar', os.listxattr(dst)) finally: os.setxattr = orig_setxattr # the source filesystem not supporting xattrs should be ok, too. def _raise_on_src(fname, *, follow_symlinks=True): if fname == src: raise OSError(errno.ENOTSUP, 'Operation not supported') return orig_listxattr(fname, follow_symlinks=follow_symlinks) try: orig_listxattr = os.listxattr os.listxattr = _raise_on_src shutil._copyxattr(src, dst) finally: os.listxattr = orig_listxattr # test that shutil.copystat copies xattrs src = os.path.join(tmp_dir, 'the_original') write_file(src, src) os.setxattr(src, 'user.the_value', b'fiddly') dst = os.path.join(tmp_dir, 'the_copy') write_file(dst, dst) shutil.copystat(src, dst) self.assertEqual(os.getxattr(dst, 'user.the_value'), b'fiddly')