Skip to content

Commit 45726e5

Browse files
committed
Remove dependencies on imp, which is removed in Python 3.12
- Bump to 4.0.30
1 parent b19f07c commit 45726e5

File tree

3 files changed

+39
-48
lines changed

3 files changed

+39
-48
lines changed

libopensesame/metadata.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from distutils.version import StrictVersion
2121
import sys
2222

23-
__version__ = '4.0.29'
23+
__version__ = '4.0.30'
2424
strict_version = StrictVersion(__version__)
2525
# The version without the prerelease (if any): e.g. 3.0.0
2626
main_version = '.'.join([str(i) for i in strict_version.version])

libopensesame/misc.py

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -235,45 +235,29 @@ def opensesame_folder():
235235
The OpenSesame folder or None if the os is not Windows.
236236
"""
237237
# Determines the directory name of the script or the directory name
238-
# of the executable after being packaged with py2exe. This has to be
239-
# done so the child process can find all relevant modules too.
240-
# See http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe
238+
# of the executable
241239
#
242-
# There are three scenarios:
240+
# There are two scenarios:
243241
#
244-
# - OpenSesame is run from a frozen state, in which case the OpenSesame
245-
# folder is the folder containing the
246242
# - OpenSesame is run from source, in which case we go to the OpenSesame
247243
# folder by going a levels up from the __file__ folder.
248244
# - OpenSesame is run in Anaconda, in which case we need to go two levels
249245
# up to get out of the site-packages folder.
250-
if platform.system() == u'Darwin':
246+
if platform.system() == 'Darwin':
251247
return os.getcwd()
252-
elif platform.system() == u'Windows':
253-
import imp
254-
if (
255-
hasattr(sys, u'frozen') or
256-
hasattr(sys, u'importers') or
257-
imp.is_frozen(u'__main__')
258-
):
259-
path = safe_decode(
260-
os.path.dirname(sys.executable),
261-
enc=sys.getfilesystemencoding()
262-
)
263-
else:
264-
# To get the opensesame folder, simply jump to levels up
265-
path = safe_decode(
266-
os.path.dirname(__file__),
267-
enc=sys.getfilesystemencoding()
268-
)
269-
# The current should not be the site-packages folder, which
270-
# happens on Anaconda if launched in multiprocess mode.
271-
path = os.path.normpath(os.path.join(path, u'..'))
272-
if path.endswith(u'Lib\\site-packages'):
273-
path = os.path.normpath(os.path.join(path, u'..', u'..'))
248+
if platform.system() == 'Windows':
249+
# To get the opensesame folder, simply jump to levels up
250+
path = safe_decode(
251+
os.path.dirname(__file__),
252+
enc=sys.getfilesystemencoding()
253+
)
254+
# The current should not be the site-packages folder, which
255+
# happens on Anaconda if launched in multiprocess mode.
256+
path = os.path.normpath(os.path.join(path, '..'))
257+
if path.endswith('Lib\\site-packages'):
258+
path = os.path.normpath(os.path.join(path, '..', '..'))
274259
return path
275-
else:
276-
return None
260+
return None
277261

278262

279263
def open_url(url):

libopensesame/plugins.py

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ def plugin_icon_small(plugin, _type=u'plugins'):
360360

361361

362362
def import_plugin(plugin, _type=u'plugins'):
363-
r"""Imports plugin module.
363+
"""Imports plugin module.
364364
365365
Parameters
366366
----------
@@ -373,21 +373,26 @@ def import_plugin(plugin, _type=u'plugins'):
373373
-------
374374
The imported module.
375375
"""
376-
import imp
376+
import importlib.util
377+
377378
plugin = str(plugin)
378379
folder = plugin_folder(plugin, _type=_type)
379380
for tmpl in src_templates:
380381
if os.path.exists(os.path.join(folder, tmpl % plugin)):
381382
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
385388
for tmpl in bytecode_templates:
386389
if os.path.exists(os.path.join(folder, tmpl % plugin)):
387390
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
391396

392397

393398
def load_plugin(
@@ -490,17 +495,19 @@ def load_mod(path, mod, pkg=None):
490495
-------
491496
A module.
492497
"""
493-
import imp
498+
import importlib.util
499+
494500
path = safe_decode(path, enc=sys.getfilesystemencoding())
495501
if not os.path.isdir(path):
496502
path = os.path.dirname(path)
497503
if pkg is not None:
498504
path = os.path.join(path, pkg)
499-
path = os.path.join(path, mod+u'.py')
505+
path = os.path.join(path, mod + '.py')
500506
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

Comments
 (0)