Skip to content

Commit 68f384a

Browse files
committed
*: Replace imp with importlib on py3
@Qubitium notes (#1): Python 3.12 no longer support the imp module (fully deprecated) with note to use importlib as replacement. Please support python 3.12 by migrating from imp code to importlib. Thanks. and indeed, even trying to build pygolang fails on py3.12: (py312.venv) kirr@deca:~/src/tools/go/pygolang-master$ python setup.py build_ext -i Traceback (most recent call last): File "/home/kirr/src/tools/go/pygolang-master/setup.py", line 40, in <module> exec(readfile('trun'), trun) File "<string>", line 41, in <module> ModuleNotFoundError: No module named 'imp' -> Rework the code to use importlib instead, but keep using imp on py2 where there is practically no importlib functionality. /reported-by @Qubitium (github) /reviewed-by @jerome /reviewed-on https://lab.nexedi.com/nexedi/pygolang/-/merge_requests/23
1 parent 6446a0b commit 68f384a

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

golang/_gopath.py

+31-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2018-2019 Nexedi SA and Contributors.
1+
# Copyright (C) 2018-2024 Nexedi SA and Contributors.
22
# Kirill Smelkov <kirr@nexedi.com>
33
#
44
# This program is free software: you can Use, Study, Modify and Redistribute
@@ -34,11 +34,7 @@
3434

3535
import os, os.path
3636
import sys
37-
38-
import warnings
39-
with warnings.catch_warnings():
40-
warnings.simplefilter('ignore', DeprecationWarning)
41-
import imp
37+
import six
4238

4339
# _gopathv returns $GOPATH vector.
4440
def _gopathv():
@@ -51,11 +47,25 @@ def _gopathv():
5147

5248
# gimport imports python module or package from fully-qualified module name under $GOPATH.
5349
def gimport(name):
54-
imp.acquire_lock()
50+
_gimport_lock()
5551
try:
5652
return _gimport(name)
5753
finally:
58-
imp.release_lock()
54+
_gimport_unlock()
55+
56+
# on py2 there is global import lock
57+
# on py3 we need to organize our own gimport synchronization
58+
if six.PY2:
59+
import imp
60+
_gimport_lock = imp.acquire_lock
61+
_gimport_unlock = imp.release_lock
62+
else:
63+
from importlib import machinery as imp_machinery
64+
from importlib import util as imp_util
65+
from golang import sync
66+
_gimport_mu = sync.Mutex()
67+
_gimport_lock = _gimport_mu.lock
68+
_gimport_unlock = _gimport_mu.unlock
5969

6070
def _gimport(name):
6171
# we will register imported module into sys.modules with adjusted path.
@@ -93,4 +103,16 @@ def _gimport(name):
93103

94104

95105
# https://stackoverflow.com/a/67692
96-
return imp.load_source(modname, modpath)
106+
return _imp_load_source(modname, modpath)
107+
108+
def _imp_load_source(modname, modpath):
109+
if six.PY2:
110+
return imp.load_source(modname, modpath)
111+
112+
# https://docs.python.org/3/whatsnew/3.12.html#imp
113+
loader = imp_machinery.SourceFileLoader(modname, modpath)
114+
spec = imp_util.spec_from_file_location(modname, modpath, loader=loader)
115+
mod = imp_util.module_from_spec(spec)
116+
sys.modules[modname] = mod
117+
loader.exec_module(mod)
118+
return mod

trun

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#!/usr/bin/env python
2-
# Copyright (C) 2019-2020 Nexedi SA and Contributors.
2+
# Copyright (C) 2019-2024 Nexedi SA and Contributors.
33
# Kirill Smelkov <kirr@nexedi.com>
44
#
55
# This program is free software: you can Use, Study, Modify and Redistribute
@@ -34,12 +34,13 @@ trun cares to run python with LD_PRELOAD set appropriately to /path/to/libtsan.s
3434

3535
from __future__ import print_function, absolute_import
3636

37-
import os, sys, re, subprocess, pkgutil
38-
import warnings
39-
with warnings.catch_warnings():
40-
warnings.simplefilter('ignore', DeprecationWarning)
41-
import imp
37+
import os, sys, re, subprocess, types
4238
PY3 = (bytes is not str)
39+
if PY3:
40+
from importlib import machinery as imp_machinery
41+
else:
42+
import imp, pkgutil
43+
4344

4445
# env_prepend prepends value to ${name} environment variable.
4546
#
@@ -64,12 +65,15 @@ def grep1(pattern, text): # -> re.Match|None
6465
# to import e.g. golang.pyx.build, or locate golang._golang, without built/working golang.
6566
def ximport_empty_golangmod():
6667
assert 'golang' not in sys.modules
67-
golang = imp.new_module('golang')
68+
golang = types.ModuleType('golang')
6869
golang.__package__ = 'golang'
6970
golang.__path__ = ['golang']
7071
golang.__file__ = 'golang/__init__.py'
71-
golang.__loader__ = pkgutil.ImpLoader('golang', None, 'golang/__init__.py',
72-
[None, None, imp.PY_SOURCE])
72+
if PY3:
73+
golang.__loader__ = imp_machinery.SourceFileLoader('golang', 'golang/__init__.py')
74+
else:
75+
golang.__loader__ = pkgutil.ImpLoader('golang', None, 'golang/__init__.py',
76+
[None, None, imp.PY_SOURCE])
7377
sys.modules['golang'] = golang
7478

7579

0 commit comments

Comments
 (0)