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

Fix PyJulia on Python 3.12 #538

Merged
merged 7 commits into from
Dec 16, 2023
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
4 changes: 2 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ jobs:
architecture: [x64, x86]
python-version:
- '3.9'
- '3.10'
- '3.12'
julia-version:
- '1.4'
- '1.6'
- '1.7'
- '1.8'
- '1.9'
exclude:
- os: ubuntu-latest
architecture: x86
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ def pyload(path):
'Programming Language :: Python :: 3.8',
'Programming Language :: Python :: 3.9',
'Programming Language :: Python :: 3.10',
'Programming Language :: Python :: 3.11',
'Programming Language :: Python :: 3.12',
],
url='http://julialang.org',
project_urls={
Expand Down
32 changes: 19 additions & 13 deletions src/julia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import textwrap
import warnings
from ctypes import c_char_p, c_void_p
from importlib.abc import Loader, MetaPathFinder
from importlib.machinery import ModuleSpec
from logging import getLogger # see `.logger`
from types import ModuleType # this is python 3.3 specific

Expand Down Expand Up @@ -189,7 +191,7 @@ def __try_getattr(self, name):
if self._julia.isamodule(jl_fullname):
realname = self._julia.fullname(self._julia.eval(jl_fullname))
if self._julia.isdefined(realname):
return self.__loader__.load_module("julia." + realname)
return self.__loader__.create_module(_find_spec_from_fullname("julia." + realname))
# Otherwise, it may be, e.g., "Main.anonymous", created by
# Module().

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


# add custom import behavior for the julia "module"
class JuliaImporter(object):
class JuliaImporter(MetaPathFinder):

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


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

class JuliaModuleLoader(Loader):
@property
def julia(self):
self.__class__.julia = julia = Julia()
return julia

# load module was deprecated in v3.4
def load_module(self, fullname):
def exec_module(self, module):
pass

def create_module(self, spec):
fullname = spec.name
juliapath = remove_prefix(fullname, "julia.")
if juliapath == 'Main':
return sys.modules.setdefault(fullname,
Expand Down
2 changes: 1 addition & 1 deletion src/julia/release.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# This file is executed via setup.py and imported via __init__.py

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