Skip to content

Commit 1f08cc4

Browse files
authored
Fix PyJulia on Python 3.12 (#538)
* Fix importlib deprecated API * Test Python 3.12 * Test Julia 1.9 * Fix semantics of exec_module vs create_module * Ensure we can find spec from JuliaModule * Bump version with python 3.12 compat * Declare compat for 3.11 and 3.12
1 parent bdefc8b commit 1f08cc4

File tree

4 files changed

+24
-16
lines changed

4 files changed

+24
-16
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ jobs:
2020
architecture: [x64, x86]
2121
python-version:
2222
- '3.9'
23-
- '3.10'
23+
- '3.12'
2424
julia-version:
2525
- '1.4'
26-
- '1.6'
2726
- '1.7'
2827
- '1.8'
28+
- '1.9'
2929
exclude:
3030
- os: ubuntu-latest
3131
architecture: x86

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ def pyload(path):
6767
'Programming Language :: Python :: 3.8',
6868
'Programming Language :: Python :: 3.9',
6969
'Programming Language :: Python :: 3.10',
70+
'Programming Language :: Python :: 3.11',
71+
'Programming Language :: Python :: 3.12',
7072
],
7173
url='http://julialang.org',
7274
project_urls={

src/julia/core.py

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import textwrap
2525
import warnings
2626
from ctypes import c_char_p, c_void_p
27+
from importlib.abc import Loader, MetaPathFinder
28+
from importlib.machinery import ModuleSpec
2729
from logging import getLogger # see `.logger`
2830
from types import ModuleType # this is python 3.3 specific
2931

@@ -189,7 +191,7 @@ def __try_getattr(self, name):
189191
if self._julia.isamodule(jl_fullname):
190192
realname = self._julia.fullname(self._julia.eval(jl_fullname))
191193
if self._julia.isdefined(realname):
192-
return self.__loader__.load_module("julia." + realname)
194+
return self.__loader__.create_module(_find_spec_from_fullname("julia." + realname))
193195
# Otherwise, it may be, e.g., "Main.anonymous", created by
194196
# Module().
195197

@@ -220,27 +222,31 @@ def __setattr__(self, name, value):
220222

221223

222224
# add custom import behavior for the julia "module"
223-
class JuliaImporter(object):
225+
class JuliaImporter(MetaPathFinder):
224226

225-
# find_module was deprecated in v3.4
226-
def find_module(self, fullname, path=None):
227-
if fullname.startswith("julia."):
228-
filename = fullname.split(".", 2)[1]
229-
filepath = os.path.join(os.path.dirname(__file__), filename)
230-
if os.path.isfile(filepath + ".py") or os.path.isdir(filepath):
231-
return
232-
return JuliaModuleLoader()
227+
def find_spec(self, fullname, path=None, target=None):
228+
return _find_spec_from_fullname(fullname)
233229

234230

235-
class JuliaModuleLoader(object):
231+
def _find_spec_from_fullname(fullname):
232+
if fullname.startswith("julia."):
233+
filename = fullname.split(".", 2)[1]
234+
filepath = os.path.join(os.path.dirname(__file__), filename)
235+
if os.path.isfile(filepath + ".py") or os.path.isdir(filepath):
236+
return
237+
return ModuleSpec(fullname, JuliaModuleLoader(), origin=filepath)
236238

239+
class JuliaModuleLoader(Loader):
237240
@property
238241
def julia(self):
239242
self.__class__.julia = julia = Julia()
240243
return julia
241244

242-
# load module was deprecated in v3.4
243-
def load_module(self, fullname):
245+
def exec_module(self, module):
246+
pass
247+
248+
def create_module(self, spec):
249+
fullname = spec.name
244250
juliapath = remove_prefix(fullname, "julia.")
245251
if juliapath == 'Main':
246252
return sys.modules.setdefault(fullname,

src/julia/release.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# This file is executed via setup.py and imported via __init__.py
22

3-
__version__ = "0.6.1"
3+
__version__ = "0.6.2"
44
# For Python versioning scheme, see:
55
# https://www.python.org/dev/peps/pep-0440/#version-scheme

0 commit comments

Comments
 (0)