Skip to content

Commit

Permalink
open files by using sys.getfilesystemencoding() + refactor stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Aug 30, 2015
1 parent e229dbe commit 77119eb
Showing 1 changed file with 23 additions and 18 deletions.
41 changes: 23 additions & 18 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@
CLOCK_TICKS = os.sysconf("SC_CLK_TCK")
PAGESIZE = os.sysconf("SC_PAGE_SIZE")
BOOT_TIME = None # set later
DEFAULT_ENCODING = sys.getdefaultencoding()
if PY3:
FS_ENCODING = sys.getfilesystemencoding()
if enum is None:
AF_LINK = socket.AF_PACKET
else:
Expand Down Expand Up @@ -115,6 +116,16 @@ class IOPriority(enum.IntEnum):
TimeoutExpired = None


# --- utils

def open_text(fname):
"""On Python 3 opens a file in text mode by using fs encoding.
On Python 2 this is just an alias for open(name, 'rt').
"""
kw = dict(encoding=FS_ENCODING) if PY3 else dict()
return open(fname, "rt", **kw)


# --- named tuples

def _get_cputimes_fields():
Expand Down Expand Up @@ -263,7 +274,7 @@ def cpu_count_logical():
# try to parse /proc/stat as a last resort
if num == 0:
search = re.compile('cpu\d')
with open('/proc/stat', 'rt') as f:
with open_text('/proc/stat') as f:
for line in f:
line = line.split(' ')[0]
if search.match(line):
Expand Down Expand Up @@ -471,8 +482,7 @@ def process_inet(self, file, family, type_, inodes, filter_pid=None):
if file.endswith('6') and not os.path.exists(file):
# IPv6 not supported
return
kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
with open(file, "rt", **kw) as f:
with open_text(file) as f:
f.readline() # skip the first line
for line in f:
try:
Expand Down Expand Up @@ -505,8 +515,7 @@ def process_inet(self, file, family, type_, inodes, filter_pid=None):

def process_unix(self, file, family, inodes, filter_pid=None):
"""Parse /proc/net/unix files."""
kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
with open(file, "rt", **kw) as f:
with open_text(file) as f:
f.readline() # skip the first line
for line in f:
tokens = line.split()
Expand Down Expand Up @@ -577,7 +586,7 @@ def net_io_counters():
"""Return network I/O statistics for every network interface
installed on the system as a dict of raw tuples.
"""
with open("/proc/net/dev", "rt") as f:
with open_text("/proc/net/dev") as f:
lines = f.readlines()
retdict = {}
for line in lines[2:]:
Expand Down Expand Up @@ -628,7 +637,7 @@ def disk_io_counters():

# determine partitions we want to look for
partitions = []
with open("/proc/partitions", "rt") as f:
with open_text("/proc/partitions") as f:
lines = f.readlines()[2:]
for line in reversed(lines):
_, _, _, name = line.split()
Expand All @@ -645,7 +654,7 @@ def disk_io_counters():
partitions.append(name)
#
retdict = {}
with open("/proc/diskstats", "rt") as f:
with open_text("/proc/diskstats") as f:
lines = f.readlines()
for line in lines:
# http://www.mjmwired.net/kernel/Documentation/iostats.txt
Expand All @@ -671,7 +680,7 @@ def disk_io_counters():
def disk_partitions(all=False):
"""Return mounted disk partitions as a list of namedtuples"""
fstypes = set()
with open("/proc/filesystems", "r") as f:
with open_text("/proc/filesystems") as f:
for line in f:
line = line.strip()
if not line.startswith("nodev"):
Expand Down Expand Up @@ -750,9 +759,7 @@ def __init__(self, pid):

@wrap_exceptions
def name(self):
fname = "/proc/%s/stat" % self.pid
kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
with open(fname, "rt", **kw) as f:
with open_text("/proc/%s/stat" % self.pid) as f:
data = f.read()
# XXX - gets changed later and probably needs refactoring
return data[data.find('(') + 1:data.rfind(')')]
Expand Down Expand Up @@ -788,9 +795,7 @@ def exe(self):

@wrap_exceptions
def cmdline(self):
fname = "/proc/%s/cmdline" % self.pid
kw = dict(encoding=DEFAULT_ENCODING) if PY3 else dict()
with open(fname, "rt", **kw) as f:
with open_text("/proc/%s/cmdline" % self.pid) as f:
data = f.read()
if data.endswith('\x00'):
data = data[:-1]
Expand Down Expand Up @@ -900,7 +905,7 @@ def memory_maps(self):
Fields are explained in 'man proc'; here is an updated (Apr 2012)
version: http://goo.gl/fmebo
"""
with open("/proc/%s/smaps" % self.pid, "rt") as f:
with open_text("/proc/%s/smaps" % self.pid) as f:
first_line = f.readline()
current_block = [first_line]

Expand Down Expand Up @@ -1023,7 +1028,7 @@ def threads(self):

@wrap_exceptions
def nice_get(self):
# with open('/proc/%s/stat' % self.pid, 'r') as f:
# with open_text('/proc/%s/stat' % self.pid) as f:
# data = f.read()
# return int(data.split()[18])

Expand Down

0 comments on commit 77119eb

Please sign in to comment.