Skip to content

Commit

Permalink
Remove dependencies on imp, which is removed in Python 3.12
Browse files Browse the repository at this point in the history
- Bump to 4.0.30
  • Loading branch information
smathot committed Nov 11, 2024
1 parent b19f07c commit 45726e5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 48 deletions.
2 changes: 1 addition & 1 deletion libopensesame/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from distutils.version import StrictVersion
import sys

__version__ = '4.0.29'
__version__ = '4.0.30'
strict_version = StrictVersion(__version__)
# The version without the prerelease (if any): e.g. 3.0.0
main_version = '.'.join([str(i) for i in strict_version.version])
Expand Down
46 changes: 15 additions & 31 deletions libopensesame/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,45 +235,29 @@ def opensesame_folder():
The OpenSesame folder or None if the os is not Windows.
"""
# Determines the directory name of the script or the directory name
# of the executable after being packaged with py2exe. This has to be
# done so the child process can find all relevant modules too.
# See http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe
# of the executable
#
# There are three scenarios:
# There are two scenarios:
#
# - OpenSesame is run from a frozen state, in which case the OpenSesame
# folder is the folder containing the
# - OpenSesame is run from source, in which case we go to the OpenSesame
# folder by going a levels up from the __file__ folder.
# - OpenSesame is run in Anaconda, in which case we need to go two levels
# up to get out of the site-packages folder.
if platform.system() == u'Darwin':
if platform.system() == 'Darwin':
return os.getcwd()
elif platform.system() == u'Windows':
import imp
if (
hasattr(sys, u'frozen') or
hasattr(sys, u'importers') or
imp.is_frozen(u'__main__')
):
path = safe_decode(
os.path.dirname(sys.executable),
enc=sys.getfilesystemencoding()
)
else:
# To get the opensesame folder, simply jump to levels up
path = safe_decode(
os.path.dirname(__file__),
enc=sys.getfilesystemencoding()
)
# The current should not be the site-packages folder, which
# happens on Anaconda if launched in multiprocess mode.
path = os.path.normpath(os.path.join(path, u'..'))
if path.endswith(u'Lib\\site-packages'):
path = os.path.normpath(os.path.join(path, u'..', u'..'))
if platform.system() == 'Windows':
# To get the opensesame folder, simply jump to levels up
path = safe_decode(
os.path.dirname(__file__),
enc=sys.getfilesystemencoding()
)
# The current should not be the site-packages folder, which
# happens on Anaconda if launched in multiprocess mode.
path = os.path.normpath(os.path.join(path, '..'))
if path.endswith('Lib\\site-packages'):
path = os.path.normpath(os.path.join(path, '..', '..'))
return path
else:
return None
return None


def open_url(url):
Expand Down
39 changes: 23 additions & 16 deletions libopensesame/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def plugin_icon_small(plugin, _type=u'plugins'):


def import_plugin(plugin, _type=u'plugins'):
r"""Imports plugin module.
"""Imports plugin module.
Parameters
----------
Expand All @@ -373,21 +373,26 @@ def import_plugin(plugin, _type=u'plugins'):
-------
The imported module.
"""
import imp
import importlib.util

plugin = str(plugin)
folder = plugin_folder(plugin, _type=_type)
for tmpl in src_templates:
if os.path.exists(os.path.join(folder, tmpl % plugin)):
path = os.path.join(folder, tmpl % plugin)
if not py3:
path = safe_encode(path, enc=sys.getfilesystemencoding())
return imp.load_source(plugin, path)
# Create a module spec from the source file and load the module
spec = importlib.util.spec_from_file_location(plugin, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module
for tmpl in bytecode_templates:
if os.path.exists(os.path.join(folder, tmpl % plugin)):
path = os.path.join(folder, tmpl % plugin)
if not py3:
path = safe_encode(path, enc=sys.getfilesystemencoding())
return imp.load_compiled(plugin, path)
# Similarly load the module from compiled bytecode
spec = importlib.util.spec_from_file_location(plugin, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module


def load_plugin(
Expand Down Expand Up @@ -490,17 +495,19 @@ def load_mod(path, mod, pkg=None):
-------
A module.
"""
import imp
import importlib.util

path = safe_decode(path, enc=sys.getfilesystemencoding())
if not os.path.isdir(path):
path = os.path.dirname(path)
if pkg is not None:
path = os.path.join(path, pkg)
path = os.path.join(path, mod+u'.py')
path = os.path.join(path, mod + '.py')
if not os.path.exists(path):
raise osexception(u'%s does not exist' % path)
oslogger.debug(u'loading module from %s' % path)
if not py3:
mod = safe_encode(mod, enc=sys.getfilesystemencoding())
path = safe_encode(path, enc=sys.getfilesystemencoding())
return imp.load_source(mod, path)
raise osexception('%s does not exist' % path)
oslogger.debug('loading module from %s' % path)
# Create a module spec from the source file and load the module
spec = importlib.util.spec_from_file_location(mod, path)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
return module

0 comments on commit 45726e5

Please sign in to comment.