Skip to content

Commit 8377cd4

Browse files
asottilegiampaolo
authored andcommitted
Clean up code which checked presence of os.{stat,lstat,chmod} (#11643)
1 parent 9c3f284 commit 8377cd4

18 files changed

+21
-70
lines changed

Lib/dbm/dumb.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -278,8 +278,7 @@ def close(self):
278278
__del__ = close
279279

280280
def _chmod(self, file):
281-
if hasattr(self._os, 'chmod'):
282-
self._os.chmod(file, self._mode)
281+
self._os.chmod(file, self._mode)
283282

284283
def __enter__(self):
285284
return self

Lib/fileinput.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,7 @@ def _readline(self):
357357
fd = os.open(self._filename, mode, perm)
358358
self._output = os.fdopen(fd, "w")
359359
try:
360-
if hasattr(os, 'chmod'):
361-
os.chmod(self._filename, perm)
360+
os.chmod(self._filename, perm)
362361
except OSError:
363362
pass
364363
self._savestdout = sys.stdout

Lib/shutil.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,10 +290,8 @@ def copymode(src, dst, *, follow_symlinks=True):
290290
stat_func, chmod_func = os.lstat, os.lchmod
291291
else:
292292
return
293-
elif hasattr(os, 'chmod'):
294-
stat_func, chmod_func = _stat, os.chmod
295293
else:
296-
return
294+
stat_func, chmod_func = _stat, os.chmod
297295

298296
st = stat_func(src)
299297
chmod_func(dst, stat.S_IMODE(st.st_mode))

Lib/tarfile.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,10 +1787,9 @@ def gettarinfo(self, name=None, arcname=None, fileobj=None):
17871787
tarinfo = self.tarinfo()
17881788
tarinfo.tarfile = self # Not needed
17891789

1790-
# Use os.stat or os.lstat, depending on platform
1791-
# and if symlinks shall be resolved.
1790+
# Use os.stat or os.lstat, depending on if symlinks shall be resolved.
17921791
if fileobj is None:
1793-
if hasattr(os, "lstat") and not self.dereference:
1792+
if not self.dereference:
17941793
statres = os.lstat(name)
17951794
else:
17961795
statres = os.stat(name)
@@ -2235,11 +2234,10 @@ def chown(self, tarinfo, targetpath, numeric_owner):
22352234
def chmod(self, tarinfo, targetpath):
22362235
"""Set file permissions of targetpath according to tarinfo.
22372236
"""
2238-
if hasattr(os, 'chmod'):
2239-
try:
2240-
os.chmod(targetpath, tarinfo.mode)
2241-
except OSError:
2242-
raise ExtractError("could not change mode")
2237+
try:
2238+
os.chmod(targetpath, tarinfo.mode)
2239+
except OSError:
2240+
raise ExtractError("could not change mode")
22432241

22442242
def utime(self, tarinfo, targetpath):
22452243
"""Set modification time of targetpath according to tarinfo.

Lib/tempfile.py

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -70,20 +70,10 @@
7070

7171
_once_lock = _allocate_lock()
7272

73-
if hasattr(_os, "lstat"):
74-
_stat = _os.lstat
75-
elif hasattr(_os, "stat"):
76-
_stat = _os.stat
77-
else:
78-
# Fallback. All we need is something that raises OSError if the
79-
# file doesn't exist.
80-
def _stat(fn):
81-
fd = _os.open(fn, _os.O_RDONLY)
82-
_os.close(fd)
8373

8474
def _exists(fn):
8575
try:
86-
_stat(fn)
76+
_os.lstat(fn)
8777
except OSError:
8878
return False
8979
else:

Lib/test/libregrtest/runtest.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,10 +249,8 @@ def cleanup_test_droppings(testname, verbose):
249249
if verbose:
250250
print("%r left behind %s %r" % (testname, kind, name))
251251
try:
252-
# if we have chmod, fix possible permissions problems
253-
# that might prevent cleanup
254-
if (hasattr(os, 'chmod')):
255-
os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
252+
# fix possible permissions problems that might prevent cleanup
253+
os.chmod(name, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO)
256254
nuker(name)
257255
except Exception as msg:
258256
print(("%r left behind %s %r and it couldn't be "

Lib/test/pickletester.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,11 +1568,10 @@ def test_structseq(self):
15681568
s = self.dumps(t, proto)
15691569
u = self.loads(s)
15701570
self.assert_is_copy(t, u)
1571-
if hasattr(os, "stat"):
1572-
t = os.stat(os.curdir)
1573-
s = self.dumps(t, proto)
1574-
u = self.loads(s)
1575-
self.assert_is_copy(t, u)
1571+
t = os.stat(os.curdir)
1572+
s = self.dumps(t, proto)
1573+
u = self.loads(s)
1574+
self.assert_is_copy(t, u)
15761575
if hasattr(os, "statvfs"):
15771576
t = os.statvfs(os.curdir)
15781577
s = self.dumps(t, proto)

Lib/test/test_compileall.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ def timestamp_metadata(self):
5757
compare = struct.pack('<4sll', importlib.util.MAGIC_NUMBER, 0, mtime)
5858
return data, compare
5959

60-
@unittest.skipUnless(hasattr(os, 'stat'), 'test needs os.stat()')
6160
def recreation_check(self, metadata):
6261
"""Check that compileall recreates bytecode when the new metadata is
6362
used."""

Lib/test/test_dbm_dumb.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ def test_dumbdbm_creation(self):
4040
f.close()
4141

4242
@unittest.skipUnless(hasattr(os, 'umask'), 'test needs os.umask()')
43-
@unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()')
4443
def test_dumbdbm_creation_mode(self):
4544
try:
4645
old_umask = os.umask(0o002)
@@ -275,7 +274,6 @@ def test_invalid_flag(self):
275274
"'r', 'w', 'c', or 'n'"):
276275
dumbdbm.open(_fname, flag)
277276

278-
@unittest.skipUnless(hasattr(os, 'chmod'), 'test needs os.chmod()')
279277
def test_readonly_files(self):
280278
with support.temp_dir() as dir:
281279
fname = os.path.join(dir, 'db')

Lib/test/test_fileinput.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,6 @@ def test_readline_os_fstat_raises_OSError(self):
428428
self.assertTrue(os_fstat_replacement.invoked,
429429
"os.fstat() was not invoked")
430430

431-
@unittest.skipIf(not hasattr(os, "chmod"), "os.chmod does not exist")
432431
def test_readline_os_chmod_raises_OSError(self):
433432
"""Tests invoking FileInput.readline() when os.chmod() raises OSError.
434433
This exception should be silently discarded."""

0 commit comments

Comments
 (0)