Skip to content

Commit 468aad2

Browse files
committed
Avoiding load errors
1 parent 071debd commit 468aad2

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

pluginloader/__init__.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
__description__ = 'Library to manage plugins/extensions in your applications.'
55

66
import os
7+
import logging
8+
9+
logger = logging.getLogger('pluginloader')
710

811

912
class PluginFactory(object):
@@ -19,14 +22,17 @@ def __init__(self):
1922
self.plugins = {}
2023

2124
def load_file(self, filename, onlyif=None, context=None):
22-
onlyif = self._default_condition if onlyif is None else onlyif
23-
context = context or {}
24-
with open(filename) as fd:
25-
exec(fd.read(), context)
26-
27-
for name, clazz in context.items():
28-
if (self._apply_condition(onlyif, name, clazz)):
29-
self.plugins[name] = PluginFactory(clazz)
25+
try:
26+
onlyif = self._default_condition if onlyif is None else onlyif
27+
context = context or {}
28+
with open(filename) as fd:
29+
exec(fd.read(), context)
30+
31+
for name, clazz in context.items():
32+
if (self._apply_condition(onlyif, name, clazz)):
33+
self.plugins[name] = PluginFactory(clazz)
34+
except:
35+
logger.exception('Error loading file %s. Ignored' % filename)
3036

3137
def load_directory(self, path, onlyif=None, recursive=False, context=None):
3238
for filename in os.listdir(path):

tests/integration/test_file_loader.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,12 @@ class Pattern(object):
121121

122122
self.assertEqual(sorted(['Foo', 'Bar']),
123123
sorted(list(sut.plugins.keys())))
124+
125+
def test_binary_files_are_ignored(self):
126+
self.plugin_file.write('\0\1\2\3\4\5\6\7')
127+
self.plugin_file.flush()
128+
sut = PluginLoader()
129+
130+
sut.load_file(self.plugin_file.name)
131+
132+
self.assertEqual([], list(sut.plugins.keys()))

0 commit comments

Comments
 (0)