19
19
import os
20
20
import sys
21
21
import keyword
22
+ import imp
22
23
23
24
from ctypes import c_void_p as void_p
24
25
from ctypes import c_char_p as char_p
25
26
from ctypes import py_object
26
27
28
+
27
29
# this is python 3.3 specific
28
30
from types import ModuleType , FunctionType
31
+
29
32
#-----------------------------------------------------------------------------
30
33
# Classes and funtions
31
34
#-----------------------------------------------------------------------------
@@ -40,7 +43,7 @@ class ModuleChainedDict(ChainMap, dict):
40
43
# http://code.activestate.com/recipes/305268/
41
44
import UserDict
42
45
43
- class ModuleChainedDict (UserDict .DictMixin ,dict ):
46
+ class ModuleChainedDict (UserDict .DictMixin , dict ):
44
47
"""Combine mulitiple mappings for seq lookup.
45
48
For example, to emulate PYthon's normal lookup sequence:"
46
49
import __builtin__
@@ -59,6 +62,34 @@ def __getitem__(self, key):
59
62
raise KeyError (key )
60
63
61
64
65
+ if python_version .major == 3 :
66
+ from io import StringIO
67
+
68
+ class JuliaOutput (list ):
69
+
70
+ def __enter__ (self ):
71
+ self ._stdout = sys .stdout
72
+ sys .stdout = self ._stringio = StringIO ()
73
+ return self
74
+
75
+ def __exit__ (self , * args ):
76
+ self .extend (self ._stringio .getvalue ().splitlines ())
77
+ sys .stdout = self ._stdout
78
+ else :
79
+ from cStringIO import StringIO
80
+
81
+ class JuliaOutput (list ):
82
+
83
+ def __enter__ (self ):
84
+ self ._stdout = sys .stdout
85
+ sys .stdout = self ._stringio = StringIO ()
86
+ return self
87
+
88
+ def __exit__ (self , * args ):
89
+ self .extend (self ._stringio .getvalue ().splitlines ())
90
+ sys .stdout = self ._stdout
91
+
92
+
62
93
class MetaJuliaModule (type ):
63
94
def __new__ (meta , name , bases , dict ):
64
95
mod = ModuleType (name , dict .get ("__doc__" ))
@@ -85,7 +116,7 @@ class JuliaError(JuliaObject, Exception):
85
116
pass
86
117
87
118
88
- class JuliaModule (JuliaObject ):
119
+ class JuliaModule (ModuleType ):
89
120
pass
90
121
91
122
@@ -193,11 +224,14 @@ def __init__(self, init_julia=True):
193
224
# reloads.
194
225
sys ._julia_runtime = api
195
226
basenames = []
196
- for name in self .names (self .Base ):
227
+ names = self .eval ("names(Base)" )
228
+ for name in names :
197
229
if ismacro (name ):
198
230
continue
199
231
if isoperator (name ):
200
232
continue
233
+ if name .startswith ("_" ):
234
+ continue
201
235
if name .endswith ("!" ):
202
236
name = name .rstrip ("!" ) + "_b"
203
237
if keyword .iskeyword (name ):
@@ -237,20 +271,27 @@ def help(self, name):
237
271
return None
238
272
self .eval ('help("{}")' .format (name ))
239
273
240
- def methods (self , name ):
241
- """
242
- return methods
243
- """
244
- if name is None :
245
- return None
246
- print (self .eval ("string(methods({}))" .format (name )))
247
-
248
274
def put (self , x ):
249
275
pass
250
276
251
277
def get (self , x ):
252
278
pass
253
279
280
+ def _ismodule (self , julia_name ):
281
+ return self .eval ("isa({}, Module)" .format (julia_name ))
282
+ #return self.isa(pycallobj, self.Module)
283
+
284
+ def _isfunction (self , pycallobj ):
285
+ return self .isa (pycallobj , self .Function )
286
+
287
+ def _isimmutabletype (self , pycallobj ):
288
+ return (self .isa (pycallobj , self .DataType ) and
289
+ self .isimmutable (pycallobj ))
290
+
291
+ def _isdatatype (self , pycallobj ):
292
+ return (self .isa (pycallobj , self .DataType ) and not
293
+ self .isimmutable (pycallobj ))
294
+
254
295
def __getattr__ (self , attr ):
255
296
if attr is None :
256
297
return None
@@ -266,10 +307,34 @@ def __getattr__(self, attr):
266
307
else :
267
308
julia_name = attr
268
309
try :
269
- obj = self .eval (julia_name )
270
- setattr (self , attr , obj )
271
- return obj
272
- except :
310
+ #import ipdb; ipdb.set_trace()
311
+ jobj = self .eval (julia_name )
312
+ #if self._isfunction(jobj):
313
+ # func = JuliaFunction(jobj)
314
+ # pyobj = jobj
315
+ #elif self._isdatatype(jobj):
316
+ # pyobj = jobj
317
+ #elif self._isimmutabletype(jobj):
318
+ # pyobj = jobj
319
+ if self ._ismodule (julia_name ):
320
+ names = self .eval ("names({})" .format (julia_name ))
321
+ #module = JuliaModule(julia_name)
322
+ module = imp .new_module (julia_name )
323
+ #for name in dir(jobj):
324
+ # try:
325
+ # setattr(module, name, getattr(jobj, name))
326
+ # except:
327
+ # pass
328
+ for name in names :
329
+ mobj = self .eval ("{}.{}" .format (julia_name , name ))
330
+ setattr (module , name , mobj )
331
+ #sys.modules["julia.{}".format(julia_name)] = module
332
+ return module
333
+
334
+ setattr (self , attr , jobj )
335
+ return jobj
336
+ except Exception as err :
337
+ #raise err
273
338
raise AttributeError (attr )
274
339
275
340
def eval (self , src ):
0 commit comments