Skip to content

Commit 573c39d

Browse files
authored
Merge pull request #313 from stdweird/py2_file_exceptions
add FileExists / FileNotFound os exception from py3
2 parents e96160c + 6bf4f11 commit 573c39d

File tree

12 files changed

+219
-192
lines changed

12 files changed

+219
-192
lines changed

examples/simple_option.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@
4242

4343
go = simple_option(options)
4444

45-
go.log.info("1st option %s" % go.options.long1)
46-
go.log.debug("DEBUG 1st option %s" % go.options.long1)
45+
go.log.info("1st option %s", go.options.long1)
46+
go.log.debug("DEBUG 1st option %s", go.options.long1)
4747

4848
logging.info("logging from logging.info (eg from 3rd party module)")
4949
logging.debug("logging with logging.root root %s", logging.root)

lib/vsc/utils/affinity.py

Lines changed: 29 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,9 @@
3535
@author: Stijn De Weirdt (Ghent University)
3636
"""
3737
import ctypes
38+
import logging
3839
import os
3940
from ctypes.util import find_library
40-
from vsc.utils.fancylogger import getLogger
41-
42-
_logger = getLogger("affinity")
4341

4442
_libc_lib = find_library('c')
4543
_libc = ctypes.cdll.LoadLibrary(_libc_lib)
@@ -113,7 +111,6 @@ class cpu_set_t(ctypes.Structure):
113111

114112
def __init__(self, *args, **kwargs):
115113
super(cpu_set_t, self).__init__(*args, **kwargs)
116-
self.log = getLogger(self.__class__.__name__)
117114
self.cpus = None
118115

119116
def __str__(self):
@@ -128,14 +125,17 @@ def convert_hr_bits(self, txt):
128125

129126
# sanity check
130127
if indices[1] < indices[0]:
131-
self.log.raiseException("convert_hr_bits: end is lower then start in '%s'" % rng)
128+
logging.error("convert_hr_bits: end is lower then start in '%s'", rng)
129+
raise Exception("convert_hr_bits: end is lower then start")
132130
elif indices[0] < 0:
133-
self.log.raiseException("convert_hr_bits: negative start in '%s'" % rng)
131+
logging.error("convert_hr_bits: negative start in '%s'", rng)
132+
raise Exception("convert_hr_bits: negative start")
134133
elif indices[1] > CPU_SETSIZE + 1: # also covers start, since end > start
135-
self.log.raiseException("convert_hr_bits: end larger then max %s in '%s'" % (CPU_SETSIZE, rng))
134+
logging.error("convert_hr_bits: end larger then max %s in '%s'", CPU_SETSIZE, rng)
135+
raise Exception("convert_hr_bits: end larger then max")
136136

137137
self.cpus[indices[0]:indices[1] + 1] = [1] * (indices[1] + 1 - indices[0])
138-
self.log.debug("convert_hr_bits: converted %s into cpus %s" % (txt, self.cpus))
138+
logging.debug("convert_hr_bits: converted %s into cpus %s", txt, self.cpus)
139139

140140
def convert_bits_hr(self):
141141
"""Convert __bits into human readable text"""
@@ -168,8 +168,8 @@ def set_cpus(self, cpus_list):
168168
"""Given list, set it as cpus"""
169169
nr_cpus = len(cpus_list)
170170
if nr_cpus > CPU_SETSIZE:
171-
self.log.warning("set_cpus: length cpu list %s is larger then cpusetsize %s. Truncating to cpusetsize" %
172-
(nr_cpus, CPU_SETSIZE))
171+
logging.warning("set_cpus: length cpu list %s is larger then cpusetsize %s. Truncating to cpusetsize",
172+
nr_cpus, CPU_SETSIZE)
173173
cpus_list = cpus_list[:CPU_SETSIZE]
174174
elif nr_cpus < CPU_SETSIZE:
175175
cpus_list.extend([0] * (CPU_SETSIZE - nr_cpus))
@@ -188,11 +188,12 @@ def set_bits(self, cpus=None):
188188
__bits[idx] = cpu_mask_t(sum(cpus))
189189
# sanity check
190190
if prev_cpus == self.get_cpus():
191-
self.log.debug("set_bits: new set to %s" % self.convert_bits_hr())
191+
logging.debug("set_bits: new set to %s", self.convert_bits_hr())
192192
else:
193193
# get_cpus() rescans
194-
self.log.raiseException("set_bits: something went wrong: previous cpus %s; current ones %s" %
195-
(prev_cpus[:20], self.cpus[:20]))
194+
logging.error("set_bits: something went wrong: previous cpus %s; current ones %s",
195+
prev_cpus[:20], self.cpus[:20])
196+
raise Exception("set_bits: something went wrong: previous cpus / current ones")
196197

197198
def str_cpus(self):
198199
"""Return a string representation of the cpus"""
@@ -234,9 +235,9 @@ def sched_getaffinity(cs=None, pid=None):
234235

235236
ec = _libc.sched_getaffinity(pid_t(pid), ctypes.sizeof(cpu_set_t), ctypes.pointer(cs))
236237
if ec == 0:
237-
_logger.debug("sched_getaffinity for pid %s returned cpuset %s" % (pid, cs))
238+
logging.debug("sched_getaffinity for pid %s returned cpuset %s", pid, cs)
238239
else:
239-
_logger.error("sched_getaffinity failed for pid %s ec %s" % (pid, ec))
240+
logging.error("sched_getaffinity failed for pid %s ec %s", pid, ec)
240241
return cs
241242

242243

@@ -250,9 +251,9 @@ def sched_setaffinity(cs, pid=None):
250251

251252
ec = _libc.sched_setaffinity(pid_t(pid), ctypes.sizeof(cpu_set_t), ctypes.pointer(cs))
252253
if ec == 0:
253-
_logger.debug("sched_setaffinity for pid %s and cpuset %s" % (pid, cs))
254+
logging.debug("sched_setaffinity for pid %s and cpuset %s", pid, cs)
254255
else:
255-
_logger.error("sched_setaffinity failed for pid %s cpuset %s ec %s" % (pid, cs, ec))
256+
logging.error("sched_setaffinity failed for pid %s cpuset %s ec %s", pid, cs, ec)
256257

257258

258259
# /* Get index of currently used CPU. */
@@ -290,13 +291,14 @@ def getpriority(which=None, who=None):
290291
if which is None:
291292
which = PRIO_PROCESS
292293
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
293-
_logger.raiseException("getpriority: which %s not in correct range" % which)
294+
logging.error("getpriority: which %s not in correct range", which)
295+
raise Exception("getpriority: which not in correct range")
294296
if who is None:
295297
who = 0 # current which-ever
296298
prio = _libc.getpriority(priority_which_t(which),
297299
id_t(who),
298300
)
299-
_logger.debug("getpriority prio %s for which %s who %s" % (prio, which, who))
301+
logging.debug("getpriority prio %s for which %s who %s", prio, which, who)
300302

301303
return prio
302304

@@ -315,23 +317,26 @@ def setpriority(prio, which=None, who=None):
315317
if which is None:
316318
which = PRIO_PROCESS
317319
elif which not in (PRIO_PROCESS, PRIO_PGRP, PRIO_USER,):
318-
_logger.raiseException("setpriority: which %s not in correct range" % which)
320+
logging.error("setpriority: which %s not in correct range", which)
321+
raise Exception("setpriority: which not in correct range")
319322
if who is None:
320323
who = 0 # current which-ever
321324

322325
try:
323326
prio = int(prio)
324327
except ValueError:
325-
_logger.raiseException("setpriority: failed to convert priority %s into int" % prio)
328+
logging.error("setpriority: failed to convert priority %s into int", prio)
329+
raise Exception("setpriority: failed to convert priority into int")
326330

327331
if prio < PRIO_MIN or prio > PRIO_MAX:
328-
_logger.raiseException("setpriority: prio not in allowed range MIN %s MAX %s" % (PRIO_MIN, PRIO_MAX))
332+
logging.error("setpriority: prio not in allowed range MIN %s MAX %s", PRIO_MIN, PRIO_MAX)
333+
raise Exception("setpriority: prio not in allowed range MIN MAX")
329334

330335
ec = _libc.setpriority(priority_which_t(which),
331336
id_t(who),
332337
ctypes.c_int(prio)
333338
)
334339
if ec == 0:
335-
_logger.debug("setpriority for which %s who %s prio %s" % (which, who, prio))
340+
logging.debug("setpriority for which %s who %s prio %s", which, who, prio)
336341
else:
337-
_logger.error("setpriority failed for which %s who %s prio %s" % (which, who, prio))
342+
logging.error("setpriority failed for which %s who %s prio %s", which, who, prio)

lib/vsc/utils/fancylogger.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,8 @@ def getLogger(name=None, fname=False, clsname=False, fancyrecord=None):
495495
l.fancyrecord = fancyrecord
496496
if _env_to_boolean('FANCYLOGGER_GETLOGGER_DEBUG'):
497497
print('FANCYLOGGER_GETLOGGER_DEBUG')
498-
print('name ' + name + ' fname ' + fname + ' fullname ' + fullname)
499-
print("getRootLoggerName: " + getRootLoggerName())
498+
print('name %s fname %s fullname %s' % (name, fname, fullname))
499+
print("getRootLoggerName: %s" % getRootLoggerName())
500500
if hasattr(l, 'get_parent_info'):
501501
print('parent_info verbose')
502502
print("\n".join(l.get_parent_info("FANCYLOGGER_GETLOGGER_DEBUG")))
@@ -668,7 +668,7 @@ def _logToSomething(handlerclass, handleropts, loggeroption,
668668
setattr(logger, loggeroption, handler)
669669
else:
670670
handler = getattr(logger, loggeroption)
671-
elif not enable:
671+
else:
672672
# stop logging to X
673673
if handler is None:
674674
if len(logger.handlers) == 1:
@@ -678,6 +678,10 @@ def _logToSomething(handlerclass, handleropts, loggeroption,
678678
zerohandler = logger.handlers[0]
679679
# no logging should be done with APOCALYPTIC, so silence happens
680680
zerohandler.setLevel(getLevelInt(APOCALYPTIC))
681+
if _env_to_boolean('FANCYLOGGER_LOGLEVEL_DEBUG'):
682+
print("FANCYLOGGER_LOGLEVEL_DEBUG DISABLE LAST HANDLER", zerohandler)
683+
print("\n".join(logger.get_parent_info("FANCYLOGGER_LOGLEVEL_DEBUG")))
684+
sys.stdout.flush()
681685
else: # remove the handler set with this loggeroption
682686
handler = getattr(logger, loggeroption)
683687
logger.removeHandler(handler)
@@ -760,7 +764,7 @@ def setLogLevel(level):
760764
logger = getLogger(fname=False, clsname=False)
761765
logger.setLevel(level)
762766
if _env_to_boolean('FANCYLOGGER_LOGLEVEL_DEBUG'):
763-
print("FANCYLOGGER_LOGLEVEL_DEBUG", level, logging.getLevelName(level))
767+
print("FANCYLOGGER_LOGLEVEL_DEBUG SETLOGLEVEL", level, logging.getLevelName(level))
764768
print("\n".join(logger.get_parent_info("FANCYLOGGER_LOGLEVEL_DEBUG")))
765769
sys.stdout.flush()
766770

0 commit comments

Comments
 (0)