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

WIP Python 3.12 support for removal of imp module #626

Merged
merged 1 commit into from
Feb 21, 2024
Merged
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
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -xuo pipefail

DOCKER_IMAGE=jmadler/python-future-builder
# XXX: TODO: Perhaps this version shouldn't be hardcoded
version=0.18.3
version=0.18.4

docker build . -t $DOCKER_IMAGE
docker push $DOCKER_IMAGE:latest
Expand Down
6 changes: 6 additions & 0 deletions docs/whatsnew.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
What's New
**********

What's new in version 0.18.4 (2023-10-10)
=========================================
This is a minor bug-fix release containing a number of fixes:

- Fix for Python 3.12's removal of the imp module

What's new in version 0.18.3 (2023-01-13)
=========================================
This is a minor bug-fix release containing a number of fixes:
Expand Down
2 changes: 1 addition & 1 deletion src/future/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@
__copyright__ = 'Copyright 2013-2019 Python Charmers Pty Ltd'
__ver_major__ = 0
__ver_minor__ = 18
__ver_patch__ = 3
__ver_patch__ = 4
__ver_sub__ = ''
__version__ = "%d.%d.%d%s" % (__ver_major__, __ver_minor__,
__ver_patch__, __ver_sub__)
11 changes: 7 additions & 4 deletions src/future/backports/test/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@
# import collections.abc # not present on Py2.7
import re
import subprocess
import imp
try:
from imp import cache_from_source
except ImportError:
from importlib.util import cache_from_source
import time
try:
import sysconfig
Expand Down Expand Up @@ -351,7 +354,7 @@ def make_legacy_pyc(source):
does not need to exist, however the PEP 3147 pyc file must exist.
:return: The file system path to the legacy pyc file.
"""
pyc_file = imp.cache_from_source(source)
pyc_file = cache_from_source(source)
up_one = os.path.dirname(os.path.abspath(source))
legacy_pyc = os.path.join(up_one, source + ('c' if __debug__ else 'o'))
os.rename(pyc_file, legacy_pyc)
Expand All @@ -370,8 +373,8 @@ def forget(modname):
# combinations of PEP 3147 and legacy pyc and pyo files.
unlink(source + 'c')
unlink(source + 'o')
unlink(imp.cache_from_source(source, debug_override=True))
unlink(imp.cache_from_source(source, debug_override=False))
unlink(cache_from_source(source, debug_override=True))
unlink(cache_from_source(source, debug_override=False))

# On some platforms, should not run gui test even if it is allowed
# in `use_resources'.
Expand Down
12 changes: 9 additions & 3 deletions src/future/standard_library/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@

import sys
import logging
import imp
try:
import importlib
except ImportError:
Comment on lines +65 to +67
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that

Suggested change
try:
import importlib
except ImportError:
if PY2:

and moving the block after from future.utils import PY2, PY3 should be enough (i.e. no change needed to _find_and_load_module()), since the RenameImport class is only instantiated under Python2.

import imp
import contextlib
import types
import copy
Expand Down Expand Up @@ -297,8 +300,11 @@ def _find_and_load_module(self, name, path=None):
flog.debug('What to do here?')

name = bits[0]
module_info = imp.find_module(name, path)
return imp.load_module(name, *module_info)
try:
module_info = imp.find_module(name, path)
return imp.load_module(name, *module_info)
except AttributeError:
return importlib.import_module(name, path)


class hooks(object):
Expand Down
5 changes: 4 additions & 1 deletion src/past/translation/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@
Inspired by and based on ``uprefix`` by Vinay M. Sajip.
"""

import imp
try:
import imp
except ImportError:
import importlib
Comment on lines +35 to +38
Copy link

Choose a reason for hiding this comment

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

When both modules are available, it would be better to prefer importlib to avoid logging deprecation warnings from imp.

Copy link
Contributor

Choose a reason for hiding this comment

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

@aij Have a look at #632 .

import logging
import marshal
import os
Expand Down
11 changes: 8 additions & 3 deletions tests/test_future/test_standard_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,9 +447,14 @@ def test_reload(self):
"""
reload has been moved to the imp module
"""
import imp
imp.reload(imp)
self.assertTrue(True)
try:
import imp
imp.reload(imp)
self.assertTrue(True)
except ImportError:
import importlib
importlib.reload(importlib)
self.assertTrue(True)
Comment on lines +451 to +457
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import imp
imp.reload(imp)
self.assertTrue(True)
except ImportError:
import importlib
importlib.reload(importlib)
self.assertTrue(True)
import importlib
importlib.reload(importlib)
except ImportError:
import imp
imp.reload(imp)
self.assertTrue(True)


def test_install_aliases(self):
"""
Expand Down