Issue #16706: get rid of os.error

This commit is contained in:
Andrew Svetlov 2012-12-18 22:02:39 +02:00
parent a191959849
commit ad28c7f9da
33 changed files with 4089 additions and 4123 deletions

View File

@ -200,7 +200,7 @@ def open(file, mode="r", buffering=-1, encoding=None, errors=None,
buffering = DEFAULT_BUFFER_SIZE
try:
bs = os.fstat(raw.fileno()).st_blksize
except (os.error, AttributeError):
except (OSError, AttributeError):
pass
else:
if bs > 1:

View File

@ -949,7 +949,7 @@ def print_directory():
print("<H3>Current Working Directory:</H3>")
try:
pwd = os.getcwd()
except os.error as msg:
except OSError as msg:
print("os.error:", html.escape(str(msg)))
else:
print(html.escape(pwd))

View File

@ -38,7 +38,7 @@ def compile_dir(dir, maxlevels=10, ddir=None, force=False, rx=None,
print('Listing {!r}...'.format(dir))
try:
names = os.listdir(dir)
except os.error:
except OSError:
print("Can't list {!r}".format(dir))
names = []
names.sort()

View File

@ -100,12 +100,12 @@ class _Database(collections.MutableMapping):
try:
self._os.unlink(self._bakfile)
except self._os.error:
except OSError:
pass
try:
self._os.rename(self._dirfile, self._bakfile)
except self._os.error:
except OSError:
pass
f = self._io.open(self._dirfile, 'w', encoding="Latin-1")

View File

@ -148,7 +148,7 @@ def setup (**attrs):
dist.run_commands()
except KeyboardInterrupt:
raise SystemExit("interrupted")
except (IOError, os.error) as exc:
except (IOError, OSError) as exc:
error = grok_environment_error(exc)
if DEBUG:

View File

@ -124,7 +124,7 @@ def copy_tree(src, dst, preserve_mode=1, preserve_times=1,
"cannot copy tree '%s': not a directory" % src)
try:
names = os.listdir(src)
except os.error as e:
except OSError as e:
(errno, errstr) = e
if dry_run:
names = []

View File

@ -27,26 +27,26 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
try:
try:
fsrc = open(src, 'rb')
except os.error as e:
except OSError as e:
raise DistutilsFileError("could not open '%s': %s" % (src, e.strerror))
if os.path.exists(dst):
try:
os.unlink(dst)
except os.error as e:
except OSError as e:
raise DistutilsFileError(
"could not delete '%s': %s" % (dst, e.strerror))
try:
fdst = open(dst, 'wb')
except os.error as e:
except OSError as e:
raise DistutilsFileError(
"could not create '%s': %s" % (dst, e.strerror))
while True:
try:
buf = fsrc.read(buffer_size)
except os.error as e:
except OSError as e:
raise DistutilsFileError(
"could not read from '%s': %s" % (src, e.strerror))
@ -55,7 +55,7 @@ def _copy_file_contents(src, dst, buffer_size=16*1024):
try:
fdst.write(buf)
except os.error as e:
except OSError as e:
raise DistutilsFileError(
"could not write to '%s': %s" % (dst, e.strerror))
finally:
@ -193,7 +193,7 @@ def move_file (src, dst,
copy_it = False
try:
os.rename(src, dst)
except os.error as e:
except OSError as e:
(num, msg) = e
if num == errno.EXDEV:
copy_it = True
@ -205,11 +205,11 @@ def move_file (src, dst,
copy_file(src, dst, verbose=verbose)
try:
os.unlink(src)
except os.error as e:
except OSError as e:
(num, msg) = e
try:
os.unlink(dst)
except os.error:
except OSError:
pass
raise DistutilsFileError(
"couldn't move '%s' to '%s' by copy/delete: "

View File

@ -324,8 +324,10 @@ class FileInput:
if self._inplace:
self._backupfilename = (
self._filename + (self._backup or ".bak"))
try: os.unlink(self._backupfilename)
except os.error: pass
try:
os.unlink(self._backupfilename)
except OSError:
pass
# The next few lines may raise IOError
os.rename(self._filename, self._backupfilename)
self._file = open(self._backupfilename, self._mode)

View File

@ -16,7 +16,7 @@ def exists(path):
"""Test whether a path exists. Returns False for broken symbolic links"""
try:
os.stat(path)
except os.error:
except OSError:
return False
return True
@ -27,7 +27,7 @@ def isfile(path):
"""Test whether a path is a regular file"""
try:
st = os.stat(path)
except os.error:
except OSError:
return False
return stat.S_ISREG(st.st_mode)
@ -39,7 +39,7 @@ def isdir(s):
"""Return true if the pathname refers to an existing directory."""
try:
st = os.stat(s)
except os.error:
except OSError:
return False
return stat.S_ISDIR(st.st_mode)

View File

@ -55,7 +55,7 @@ def glob1(dirname, pattern):
dirname = os.curdir
try:
names = os.listdir(dirname)
except os.error:
except OSError:
return []
if pattern[0] != '.':
names = [x for x in names if x[0] != '.']

View File

@ -732,7 +732,7 @@ class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
"""
try:
list = os.listdir(path)
except os.error:
except OSError:
self.send_error(404, "No permission to list directory")
return None
list.sort(key=lambda a: a.lower())
@ -1123,7 +1123,7 @@ class CGIHTTPRequestHandler(SimpleHTTPRequestHandler):
try:
try:
os.setuid(nobody)
except os.error:
except OSError:
pass
os.dup2(self.rfile.fileno(), 0)
os.dup2(self.wfile.fileno(), 1)

View File

@ -90,11 +90,11 @@ class StdoutRefactoringTool(refactor.MultiprocessRefactoringTool):
if os.path.lexists(backup):
try:
os.remove(backup)
except os.error as err:
except OSError as err:
self.log_message("Can't remove backup %s", backup)
try:
os.rename(filename, backup)
except os.error as err:
except OSError as err:
self.log_message("Can't rename %s to %s", filename, backup)
# Actually write the new file
write = super(StdoutRefactoringTool, self).write_file

View File

@ -534,12 +534,12 @@ class RefactoringTool(object):
"""
try:
f = _open_with_encoding(filename, "w", encoding=encoding)
except os.error as err:
except OSError as err:
self.log_error("Can't create %s: %s", filename, err)
return
try:
f.write(_to_system_newlines(new_text))
except os.error as err:
except OSError as err:
self.log_error("Can't write %s: %s", filename, err)
finally:
f.close()

View File

@ -53,7 +53,7 @@ def main():
for dir in sys.path:
try:
names = os.listdir(dir)
except os.error:
except OSError:
continue
print("Scanning", dir, "...", file=sys.stderr)
for name in names:

View File

@ -59,7 +59,7 @@ def checkcache(filename=None):
continue # no-op for files loaded via a __loader__
try:
stat = os.stat(fullname)
except os.error:
except OSError:
del cache[filename]
continue
if size != stat.st_size or mtime != stat.st_mtime:
@ -118,7 +118,7 @@ def updatecache(filename, module_globals=None):
try:
stat = os.stat(fullname)
break
except os.error:
except OSError:
pass
else:
return []

View File

@ -127,7 +127,7 @@ def lexists(path):
try:
st = os.lstat(path)
except os.error:
except OSError:
return False
return True

View File

@ -228,7 +228,7 @@ class ModuleFinder:
for dir in m.__path__:
try:
names = os.listdir(dir)
except os.error:
except OSError:
self.msg(2, "can't list directory", dir)
continue
for name in names:

View File

@ -111,7 +111,7 @@ if sys.platform != 'win32':
if self.returncode is None:
try:
pid, sts = os.waitpid(self.pid, flag)
except os.error:
except OSError:
# Child process not yet created. See #1731717
# e.errno == errno.ECHILD == 10
return None

View File

@ -321,7 +321,7 @@ def islink(path):
"""
try:
st = os.lstat(path)
except (os.error, AttributeError):
except (OSError, AttributeError):
return False
return stat.S_ISLNK(st.st_mode)
@ -331,7 +331,7 @@ def lexists(path):
"""Test whether a path exists. Returns True for broken symbolic links"""
try:
st = os.lstat(path)
except (os.error, WindowsError):
except (OSError, WindowsError):
return False
return True

View File

@ -338,7 +338,7 @@ def walk(top, topdown=True, onerror=None, followlinks=False):
By default errors from the os.listdir() call are ignored. If
optional arg 'onerror' is specified, it should be a function; it
will be called with one argument, an os.error instance. It can
will be called with one argument, an OSError instance. It can
report the error to continue with the walk, or raise the exception
to abort the walk. Note that the filename is available as the
filename attribute of the exception object.

View File

@ -316,7 +316,7 @@ def linux_distribution(distname='', version='', id='',
"""
try:
etc = os.listdir('/etc')
except os.error:
except OSError:
# Probably not a Unix system
return distname,version,id
etc.sort()
@ -424,10 +424,10 @@ def _syscmd_ver(system='', release='', version='',
pipe = popen(cmd)
info = pipe.read()
if pipe.close():
raise os.error('command failed')
raise OSError('command failed')
# XXX How can I suppress shell errors from being written
# to stderr ?
except os.error as why:
except OSError as why:
#print 'Command %s failed: %s' % (cmd,why)
continue
except IOError as why:
@ -906,7 +906,7 @@ def _syscmd_uname(option,default=''):
return default
try:
f = os.popen('uname %s 2> %s' % (option, DEV_NULL))
except (AttributeError,os.error):
except (AttributeError, OSError):
return default
output = f.read().strip()
rc = f.close()
@ -932,7 +932,7 @@ def _syscmd_file(target,default=''):
proc = subprocess.Popen(['file', target],
stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
except (AttributeError,os.error):
except (AttributeError, OSError):
return default
output = proc.communicate()[0].decode('latin-1')
rc = proc.wait()

View File

@ -162,7 +162,7 @@ def islink(path):
"""Test whether a path is a symbolic link"""
try:
st = os.lstat(path)
except (os.error, AttributeError):
except (OSError, AttributeError):
return False
return stat.S_ISLNK(st.st_mode)
@ -172,7 +172,7 @@ def lexists(path):
"""Test whether a path exists. Returns True for broken symbolic links"""
try:
os.lstat(path)
except os.error:
except OSError:
return False
return True
@ -220,7 +220,7 @@ def ismount(path):
else:
parent = join(path, '..')
s2 = os.lstat(parent)
except os.error:
except OSError:
return False # It doesn't exist -- so not a mount point :-)
dev1 = s1.st_dev
dev2 = s2.st_dev

View File

@ -57,17 +57,17 @@ def _open_terminal():
try:
tty_name, master_fd = sgi._getpty(os.O_RDWR, 0o666, 0)
except IOError as msg:
raise os.error(msg)
raise OSError(msg)
return master_fd, tty_name
for x in 'pqrstuvwxyzPQRST':
for y in '0123456789abcdef':
pty_name = '/dev/pty' + x + y
try:
fd = os.open(pty_name, os.O_RDWR)
except os.error:
except OSError:
continue
return (fd, '/dev/tty' + x + y)
raise os.error('out of pty devices')
raise OSError('out of pty devices')
def slave_open(tty_name):
"""slave_open(tty_name) -> slave_fd

View File

@ -356,24 +356,24 @@ def _rmtree_unsafe(path, onerror):
names = []
try:
names = os.listdir(path)
except os.error:
except OSError:
onerror(os.listdir, path, sys.exc_info())
for name in names:
fullname = os.path.join(path, name)
try:
mode = os.lstat(fullname).st_mode
except os.error:
except OSError:
mode = 0
if stat.S_ISDIR(mode):
_rmtree_unsafe(fullname, onerror)
else:
try:
os.unlink(fullname)
except os.error:
except OSError:
onerror(os.unlink, fullname, sys.exc_info())
try:
os.rmdir(path)
except os.error:
except OSError:
onerror(os.rmdir, path, sys.exc_info())
# Version using fd-based APIs to protect against races
@ -464,7 +464,7 @@ def rmtree(path, ignore_errors=False, onerror=None):
_rmtree_safe_fd(fd, path, onerror)
try:
os.rmdir(path)
except os.error:
except OSError:
onerror(os.rmdir, path, sys.exc_info())
else:
try:

View File

@ -196,7 +196,7 @@ def addsitedir(sitedir, known_paths=None):
known_paths.add(sitedircase)
try:
names = os.listdir(sitedir)
except os.error:
except OSError:
return
names = [name for name in names if name.endswith(".pth")]
for name in sorted(names):

View File

@ -532,7 +532,7 @@ class ForkingMixIn:
# children.
try:
pid, status = os.waitpid(0, 0)
except os.error:
except OSError:
pid = None
if pid not in self.active_children: continue
self.active_children.remove(pid)
@ -545,7 +545,7 @@ class ForkingMixIn:
for child in self.active_children:
try:
pid, status = os.waitpid(child, os.WNOHANG)
except os.error:
except OSError:
pid = None
if not pid: continue
try:

View File

@ -1444,7 +1444,7 @@ class Popen(object):
def _internal_poll(self, _deadstate=None, _waitpid=os.waitpid,
_WNOHANG=os.WNOHANG, _os_error=os.error):
_WNOHANG=os.WNOHANG):
"""Check if child process has terminated. Returns returncode
attribute.
@ -1457,7 +1457,7 @@ class Popen(object):
pid, sts = _waitpid(self.pid, _WNOHANG)
if pid == self.pid:
self._handle_exitstatus(sts)
except _os_error as e:
except OSError as e:
if _deadstate is not None:
self.returncode = _deadstate
elif e.errno == errno.ECHILD:

View File

@ -665,7 +665,6 @@ class TemporaryDirectory(object):
_islink = staticmethod(_os.path.islink)
_remove = staticmethod(_os.remove)
_rmdir = staticmethod(_os.rmdir)
_os_error = OSError
_warn = _warnings.warn
def _rmtree(self, path):
@ -675,16 +674,16 @@ class TemporaryDirectory(object):
fullname = self._path_join(path, name)
try:
isdir = self._isdir(fullname) and not self._islink(fullname)
except self._os_error:
except OSError:
isdir = False
if isdir:
self._rmtree(fullname)
else:
try:
self._remove(fullname)
except self._os_error:
except OSError:
pass
try:
self._rmdir(path)
except self._os_error:
except OSError:
pass

View File

@ -35,7 +35,7 @@ def randfloats(n):
if fp:
try:
os.unlink(fn)
except os.error:
except OSError:
pass
except IOError as msg:
print("can't write", fn, ":", msg)

View File

@ -82,7 +82,7 @@ class SocketServerTest(unittest.TestCase):
for fn in self.test_files:
try:
os.remove(fn)
except os.error:
except OSError:
pass
self.test_files[:] = []

View File

@ -188,7 +188,7 @@ class TestCandidateTempdirList(BaseTestCase):
try:
dirname = os.getcwd()
except (AttributeError, os.error):
except (AttributeError, OSError):
dirname = os.curdir
self.assertIn(dirname, cand)
@ -924,7 +924,7 @@ class TestTemporaryDirectory(BaseTestCase):
# (noted as part of Issue #10188)
with tempfile.TemporaryDirectory() as nonexistent:
pass
with self.assertRaises(os.error):
with self.assertRaises(OSError):
tempfile.TemporaryDirectory(dir=nonexistent)
def test_explicit_cleanup(self):

View File

@ -11,7 +11,7 @@ try:
try:
os.write(fd, b"blat")
except os.error:
except OSError:
# Success -- could not write to fd.
sys.exit(0)
else:

File diff suppressed because it is too large Load Diff