Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q&D fix of the import imp deprecation in 3.12 #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions about.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Version history:
1.17.13b - 29 April 2024
• Fix: Quick & dirty fix of the import imp deprecation.

1.17.13 - 19 July 2022
• Fix: Qt6 compatibility - Yet another enum issue.

Expand Down
16 changes: 12 additions & 4 deletions action.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
__copyright__ = '2013, Greg Riker <[email protected]>, 2014-2020 additions by David Forrester <[email protected]>'
__docformat__ = 'restructuredtext en'

import imp, inspect, os, re, sys, tempfile, threading, types
import importlib, inspect, os, re, sys, tempfile, threading, types

# calibre Python 3 compatibility.
try:
Expand Down Expand Up @@ -877,7 +877,15 @@ def library_index_complete(self):
self.library_indexed = True
self.indexed_library = self.gui.current_db
self.library_last_modified = self.gui.current_db.last_modified()


def load_module(self, module_name, filename):
loader = importlib.machinery.SourceFileLoader(module_name, filename)
module = types.ModuleType(loader.name)
module.__file__ = filename
sys.modules[module.__name__] = module
loader.exec_module(module)
return module

def load_dynamic_reader_classes(self):
'''
Load reader classes dynamically from readers/ folder in plugin zip file
Expand All @@ -900,7 +908,7 @@ def load_dynamic_reader_classes(self):
with open(tmp_file, 'wb') as tf:
tf.write(get_resources(rac))
self._log(" loading built-in class '%s'" % name)
imp.load_source(name, tmp_file)
self.load_module(name, tmp_file)
os.remove(tmp_file)

# Load locally defined classes specified in config file
Expand All @@ -916,7 +924,7 @@ def load_dynamic_reader_classes(self):
name = re.sub('_', '', name)
self._log(" loading external class '%s'" % name)
try:
imp.load_source(name, ac)
self.load_module(name, ac)
except:
# If additional_class fails to import, exit
import traceback
Expand Down