Skip to content

Commit

Permalink
By default, use the eager flavor of the Julia registry from the Jul…
Browse files Browse the repository at this point in the history
…ia Pkg Servers (but try falling back to `conservative` if an exception is encountered)
  • Loading branch information
DilumAluthge committed Dec 5, 2024
1 parent 9d976e9 commit 2538d67
Showing 1 changed file with 69 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pysr/julia_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@
from types import ModuleType
from typing import cast


def get_registry_pref_env_var_name():
name = "JULIA_PKG_SERVER_REGISTRY_PREFERENCE"
return name


def restore_julia_registry_pref(old_value):
name = get_registry_pref_env_var_name()
if old_value is None:
# Delete the JULIA_PKG_SERVER_REGISTRY_PREFERENCE environment variable that we set:
os.environ.pop(name)
else:
# Restore the original value of the JULIA_PKG_SERVER_REGISTRY_PREFERENCE environment variable:
os.environ[name] = old_value
return None


# Check if JuliaCall is already loaded, and if so, warn the user
# about the relevant environment variables. If not loaded,
# set up sensible defaults.
Expand Down Expand Up @@ -42,10 +59,62 @@
# Deprecated; so just pass to juliacall
os.environ["PYTHON_JULIACALL_AUTOLOAD_IPYTHON_EXTENSION"] = autoload_extensions

if "JULIA_PKG_SERVER_REGISTRY_PREFERENCE" in os.environ:
old_julia_registry_pref = os.environ["JULIA_PKG_SERVER_REGISTRY_PREFERENCE"]
# If the user has set the JULIA_PKG_SERVER_REGISTRY_PREFERENCE environment variable, then
# we don't overwrite it.
pass
else:
# If the user has not set the JULIA_PKG_SERVER_REGISTRY_PREFERENCE environment variable, then
# we set it temporarily, and unset it when we are done.
old_julia_registry_pref = None
os.environ["JULIA_PKG_SERVER_REGISTRY_PREFERENCE"] = "eager"

try:
import juliacall
except:
error_msg = """\
ERROR: Encountered a network error.
Are you behind a firewall, or are there network restrictions that would prevent access
to certain websites or domains?
Try setting the `{env_var_name}` environment variable to `conservative`.
""".format(
env_var_name=get_registry_pref_env_var_name()
)
if old_julia_registry_pref is not None:
print()
print(error_msg)
print()
restore_julia_registry_pref(old_julia_registry_pref)
# In this case, we know that the user had set JULIA_PKG_SERVER_REGISTRY_PREFERENCE,
# and thus we respect that value, and we don't override it.
# So, in this case, we just rethrow the caught exception:
raise # rethrow the caught exception
else:
# In this case, the user had not set JULIA_PKG_SERVER_REGISTRY_PREFERENCE.
# So we had initially tried with JULIA_PKG_SERVER_REGISTRY_PREFERENCE=eager, but that
# resulted in an exception.
# So let us now try with JULIA_PKG_SERVER_REGISTRY_PREFERENCE=conservative
os.environ["JULIA_PKG_SERVER_REGISTRY_PREFERENCE"] = "conservative"
try:
import juliacall
except:
print()
print(error_msg)
print()
# Now, we just rethrow the caught exception:
restore_julia_registry_pref(old_julia_registry_pref)
raise # rethrow the caught exception


import juliacall
from juliacall import AnyValue # type: ignore
from juliacall import VectorValue # type: ignore
from juliacall import Main as jl # type: ignore

restore_julia_registry_pref(old_julia_registry_pref)


jl = cast(ModuleType, jl)


Expand Down

0 comments on commit 2538d67

Please sign in to comment.