8000 Replace stat.ST_xxx usage with os.stat().st_xxx (#116501) · python/cpython@6183158 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6183158

Browse files
authored
Replace stat.ST_xxx usage with os.stat().st_xxx (#116501)
Modernize code to use the new API which avoids the usage of the stat module just to read os.stat() members. * Sort logging.handlers imports. * Rework reopenIfNeeded() code to make it easier to follow. * Replace "not self.stream" with "self.stream is None".
1 parent cca3023 commit 6183158

File tree

3 files changed

+40
-27
lines changed

3 files changed

+40
-27
lines changed

Lib/logging/handlers.py

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,17 @@
2323
To use, simply 'import logging.handlers' and log away!
2424
"""
2525

26-
import io, logging, socket, os, pickle, struct, time, re
27-
from stat import ST_DEV, ST_INO, ST_MTIME
26+
import copy
27+
import io
28+
import logging
29+
import os
30+
import pickle
2831
import queue
32+
import re
33+
import socket
34+
import struct
2935
import threading
30-
import copy
36+
import time
3137

3238
#
3339
# Some constants...
@@ -269,7 +275,7 @@ def __init__(self, filename, when='h', interval=1, backupCount=0,
269275
# path object (see Issue #27493), but self.baseFilename will be a string
270276
filename = self.baseFilename
271277
if os.path.exists(filename):
272-
t = os.stat(filename)[ST_MTIME]
278+
t = int(os.stat(filename).st_mtime)
273279
else:
274280
t = int(time.time())
275281
self.rolloverAt = self.computeRollover(t)
@@ -459,8 +465,7 @@ class WatchedFileHandler(logging.FileHandler):
459465
This handler is not appropriate for use under Windows, because
460466
under Windows open files cannot be moved or renamed - logging
461467
opens the files with exclusive locks - and so there is no need
462-
for such a handler. Furthermore, ST_INO is not supported under
463-
Windows; stat always returns zero for this value.
468+
for such a handler.
464469
465470
This handler is based on a suggestion and patch by Chad J.
466471
Schroeder.
@@ -476,9 +481,11 @@ def __init__(self, filename, mode='a', encoding=None, delay=False,
476481
self._statstream()
477482

478483
def _statstream(self):
479-
if self.stream:
480-
sres = os.fstat(self.stream.fileno())
481-
self.dev, self.ino = sres[ST_DEV], sres[ST_INO]
484+
if self.stream is None:
485+
return
486+
sres = os.fstat(self.stream.fileno())
8000
487+
self.dev = sres.st_dev
488+
self.ino = sres.st_ino
482489

483490
def reopenIfNeeded(self):
484491
"""
@@ -488,25 +495,33 @@ def reopenIfNeeded(self):
488495
has, close the old stream and reopen the file to get the
489496
current stream.
490497
"""
498+
if self.stream is None:
499+
return
500+
491501
# Reduce the chance of race conditions by stat'ing by path only
492502
# once and then fstat'ing our new fd if we opened a new log stream.
493503
# See issue #14632: Thanks to John Mulligan for the problem report
494504
# and patch.
495505
try:
496506
# stat the file by path, checking for existence
497507
sres = os.stat(self.baseFilename)
508+
509+
# compare file system stat with that of our stream file handle
510+
reopen = (sres.st_dev != self.dev or sres.st_ino != self.ino)
498511
except FileNotFoundError:
499-
sres = None
500-
# compare file system stat with that of our stream file handle
501-
if not sres or sres[ST_DEV] != self.dev or sres[ST_INO] != self.ino:
502-
if self.stream is not None:
503-
# we have an open file handle, clean it up
504-
self.stream.flush()
505-
self.stream.close()
506-
self.stream = None # See Issue #21742: _open () might fail.
507-
# open a new file handle and get new stat info from that fd
508-
self.stream = self._open()
509-
self._statstream()
512+
reopen = True
513+
514+
if not reopen:
515+
return
516+
517+
# we have an open file handle, clean it up
518+
self.stream.flush()
519+
self.stream.close()
520+
self.stream = None # See Issue #21742: _open () might fail.
521+
522+
# open a new file handle and get new stat info from that fd
523+
self.stream = self._open()
524+
self._statstream()
510525

511526
def emit(self, record):
512527
"""

Lib/test/test_largefile.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
"""
33

44
import os
5-
import stat
65
import sys
76
import unittest
87
import socket
@@ -29,7 +28,7 @@ def setUp(self):
2928
mode = 'w+b'
3029

3130
with self.open(TESTFN, mode) as f:
32-
current_size = os.fstat(f.fileno())[stat.ST_SIZE]
31+
current_size = os.fstat(f.fileno()).st_size
3332
if current_size == size+1:
3433
return
3534

@@ -40,13 +39,13 @@ def setUp(self):
4039
f.seek(size)
4140
f.write(b'a')
4241
f.flush()
43-
self.assertEqual(os.fstat(f.fileno())[stat.ST_SIZE], size+1)
42+
self.assertEqual(os.fstat(f.fileno()).st_size, size+1)
4443

4544
@classmethod
4645
def tearDownClass(cls):
4746
with cls.open(TESTFN, 'wb'):
4847
pass
49-
if not os.stat(TESTFN)[stat.ST_SIZE] == 0:
48+
if not os.stat(TESTFN).st_size == 0:
5049
raise cls.failureException('File was not truncated by opening '
5150
'with mode "wb"')
5251
unlink(TESTFN2)
@@ -67,7 +66,7 @@ def test_large_read(self, _size):
6766
self.assertEqual(f.tell(), size + 1)
6867

6968
def test_osstat(self):
70-
self.assertEqual(os.stat(TESTFN)[stat.ST_SIZE], size+1)
69+
self.assertEqual(os.stat(TESTFN).st_size, size+1)
7170

7271
def test_seek_read(self):
7372
with self.open(TESTFN, 'rb') as f:

Lib/test/test_mailbox.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -702,8 +702,7 @@ def _check_basics(self, factory=None):
702702
self.assertEqual(self._box._factory, factory)
703703
for subdir in '', 'tmp', 'new', 'cur':
704704
path = os.path.join(self._path, subdir)
705-
mode = os.stat(path)[stat.ST_MODE]
706-
self.assertTrue(stat.S_ISDIR(mode), "Not a directory: '%s'" % path)
705+
self.assertTrue(os.path.isdir(path), f"Not a directory: {path!r}")
707706

708707
def test_list_folders(self):
709708
# List folders

0 commit comments

Comments
 (0)
0