Skip to content

Commit 06f53f5

Browse files
author
Jonathan Kamens
committed
Remove top-level side effects from importing libdelete
If the afs python modue isn't installed, then when libdelete is imported, this was getting displayed instead of the intended warning about afs not being available: >`No handlers could be found for logger "libdelete"` This is because libdelete was attempting to import afs to check for its availability at module scope, before the main script importing libdelete had configured logging. Fix this by changing `have_AFS` from a module-scope variable to a memoized function so that the check occurs after logging is configured.
1 parent 0e0d823 commit 06f53f5

File tree

1 file changed

+14
-8
lines changed

1 file changed

+14
-8
lines changed

libdelete.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,7 @@
1515
KILO = 1024
1616

1717
logger = logging.getLogger('libdelete')
18-
have_AFS = True
19-
20-
try:
21-
import afs.fs
22-
except ImportError:
23-
logger.warn("AFS support unavailable")
24-
have_AFS = False
18+
_have_AFS = None
2519

2620
class DeleteError(Exception):
2721
pass
@@ -52,10 +46,22 @@ def format_columns(items, singlecol=False, width=80):
5246
rv.append("".join(item.ljust(col_width + padding) for item in c))
5347
return "\n".join(rv)
5448

49+
def have_AFS():
50+
global _have_AFS, afs
51+
52+
if _have_AFS is None:
53+
try:
54+
import afs.fs
55+
_have_AFS = True
56+
except ImportError:
57+
logger.warn("AFS support unavailable")
58+
_have_AFS = False
59+
return _have_AFS
60+
5561
def is_mountpoint(path):
5662
if os.path.ismount(path):
5763
return True
58-
if have_AFS and afs.fs.inafs(os.path.abspath(path)):
64+
if have_AFS() and afs.fs.inafs(os.path.abspath(path)):
5965
afs.fs.whichcell(path)
6066
try:
6167
return afs.fs.lsmount(path) is not None

0 commit comments

Comments
 (0)