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

Use importlib instead of imp #168

Merged
merged 3 commits into from
Aug 1, 2024
Merged
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
27 changes: 25 additions & 2 deletions snapshottest/module.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import codecs
import errno
import os
import imp
import sys
import importlib.util
from collections import defaultdict
import logging

Expand All @@ -17,6 +18,28 @@ def _escape_quotes(text):
return text.replace("'", "\\'")


def _load_source(module_name, filepath):
paulmelnikow marked this conversation as resolved.
Show resolved Hide resolved
"""
Replaces old imp.load_source() call.

The imp module was dropped in Python 3.12 in favor of the importlib.
See: https://docs.python.org/3.11/library/imp.html#imp.load_module

Following code was inspired by the importlib documentation example:
https://docs.python.org/3.12/library/importlib.html#importing-a-source-file-directly

This approach has been also encouraged in the official mailing lists:
https://discuss.python.org/t/how-do-i-migrate-from-imp/27885
"""
spec = importlib.util.spec_from_file_location(module_name, filepath)
module = importlib.util.module_from_spec(spec)
# As a performance optimization, store loaded module for further use.
# https://docs.python.org/3.11/library/sys.html#sys.modules
sys.modules[module_name] = module
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it necessary to write to sys.modules for this to work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, writing to sys.modules is not required for this to work (it just improves performance if the same python module is loaded multiple times)

Copy link
Contributor

@Waszker Waszker Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@paulmelnikow I'm not the owner of the PR, but I've found a similar problem and question on official Python mailing lists: https://discuss.python.org/t/how-do-i-migrate-from-imp/27885

It looks like writing to sys.modules is indeed correct, see this answer

EDIT: Didn't see the reply by @MarcellPerger1. My bad, sorry

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess my ask here is that it would be good to add some links to the imp documentation, mailing list, etc. to explain this implementation, which otherwise seems somewhat peculiar.

paulmelnikow marked this conversation as resolved.
Show resolved Hide resolved
spec.loader.exec_module(module)
return module


class SnapshotModule(object):
_snapshot_modules = {}

Expand All @@ -33,7 +56,7 @@ def __init__(self, module, filepath):

def load_snapshots(self):
try:
source = imp.load_source(self.module, self.filepath)
source = _load_source(self.module, self.filepath)
# except FileNotFoundError: # Python 3
except (IOError, OSError) as err:
if err.errno == errno.ENOENT:
Expand Down