@@ -360,7 +360,7 @@ def plugin_icon_small(plugin, _type=u'plugins'):
360
360
361
361
362
362
def import_plugin (plugin , _type = u'plugins' ):
363
- r """Imports plugin module.
363
+ """Imports plugin module.
364
364
365
365
Parameters
366
366
----------
@@ -373,21 +373,26 @@ def import_plugin(plugin, _type=u'plugins'):
373
373
-------
374
374
The imported module.
375
375
"""
376
- import imp
376
+ import importlib .util
377
+
377
378
plugin = str (plugin )
378
379
folder = plugin_folder (plugin , _type = _type )
379
380
for tmpl in src_templates :
380
381
if os .path .exists (os .path .join (folder , tmpl % plugin )):
381
382
path = os .path .join (folder , tmpl % plugin )
382
- if not py3 :
383
- path = safe_encode (path , enc = sys .getfilesystemencoding ())
384
- return imp .load_source (plugin , path )
383
+ # Create a module spec from the source file and load the module
384
+ spec = importlib .util .spec_from_file_location (plugin , path )
385
+ module = importlib .util .module_from_spec (spec )
386
+ spec .loader .exec_module (module )
387
+ return module
385
388
for tmpl in bytecode_templates :
386
389
if os .path .exists (os .path .join (folder , tmpl % plugin )):
387
390
path = os .path .join (folder , tmpl % plugin )
388
- if not py3 :
389
- path = safe_encode (path , enc = sys .getfilesystemencoding ())
390
- return imp .load_compiled (plugin , path )
391
+ # Similarly load the module from compiled bytecode
392
+ spec = importlib .util .spec_from_file_location (plugin , path )
393
+ module = importlib .util .module_from_spec (spec )
394
+ spec .loader .exec_module (module )
395
+ return module
391
396
392
397
393
398
def load_plugin (
@@ -490,17 +495,19 @@ def load_mod(path, mod, pkg=None):
490
495
-------
491
496
A module.
492
497
"""
493
- import imp
498
+ import importlib .util
499
+
494
500
path = safe_decode (path , enc = sys .getfilesystemencoding ())
495
501
if not os .path .isdir (path ):
496
502
path = os .path .dirname (path )
497
503
if pkg is not None :
498
504
path = os .path .join (path , pkg )
499
- path = os .path .join (path , mod + u '.py' )
505
+ path = os .path .join (path , mod + '.py' )
500
506
if not os .path .exists (path ):
501
- raise osexception (u'%s does not exist' % path )
502
- oslogger .debug (u'loading module from %s' % path )
503
- if not py3 :
504
- mod = safe_encode (mod , enc = sys .getfilesystemencoding ())
505
- path = safe_encode (path , enc = sys .getfilesystemencoding ())
506
- return imp .load_source (mod , path )
507
+ raise osexception ('%s does not exist' % path )
508
+ oslogger .debug ('loading module from %s' % path )
509
+ # Create a module spec from the source file and load the module
510
+ spec = importlib .util .spec_from_file_location (mod , path )
511
+ module = importlib .util .module_from_spec (spec )
512
+ spec .loader .exec_module (module )
513
+ return module
0 commit comments