Skip to content

Commit

Permalink
Deprecate Julia.__getattr__ (and make it lazy)
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed May 16, 2018
1 parent 98c7e7e commit baec81a
Showing 1 changed file with 9 additions and 36 deletions.
45 changes: 9 additions & 36 deletions julia/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import keyword
import subprocess
import time
import warnings

from ctypes import c_void_p as void_p
from ctypes import c_char_p as char_p
Expand Down Expand Up @@ -200,36 +201,6 @@ def isafunction(julia, julia_name, mod_name=""):
return False


def module_functions(julia, module):
"""Compute the function names in the julia module"""
bases = {}
names = julia.eval("names(%s)" % module)
for name in names:
if (ismacro(name) or
isoperator(name) or
isprotected(name) or
notascii(name)):
continue
try:
# skip undefined names
if not julia.eval("isdefined(:%s)" % name):
continue
# skip modules for now
if isamodule(julia, name):
continue
if name.startswith("_"):
continue
attr_name = name
if name.endswith("!"):
attr_name = name.replace("!", "_b")
if keyword.iskeyword(name):
attr_name = "jl".join(name)
julia_func = julia.eval(name)
bases[attr_name] = julia_func
except:
pass
return bases

def determine_if_statically_linked():
"""Determines if this python executable is statically linked"""
# Windows and OS X are generally always dynamically linked
Expand Down Expand Up @@ -422,13 +393,16 @@ def __init__(self, init_julia=True, jl_runtime_path=None, jl_init_path=None,
# reloads.
_julia_runtime[0] = self.api

self.add_module_functions("Base")

sys.meta_path.append(JuliaImporter(self))

def add_module_functions(self, module):
for name, func in iteritems(module_functions(self, module)):
setattr(self, name, func)
def __getattr__(self, name):
from julia import Main
warnings.warn(
"Accessing `Julia().<name>` to obtain Julia objects is"
" deprecated. Use `from julia import Main; Main.<name>` or"
" `jl = Julia(); jl.eval('<name>')`.",
DeprecationWarning)
return getattr(Main, name)

def _debug(self, msg):
"""
Expand Down Expand Up @@ -504,4 +478,3 @@ def eval(self, src):
def using(self, module):
"""Load module in Julia by calling the `using module` command"""
self.eval("using %s" % module)
self.add_module_functions(module)

0 comments on commit baec81a

Please sign in to comment.