forked from digikar99/py4cl2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpy4cl.py
490 lines (423 loc) · 15.1 KB
/
py4cl.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# Python interface for py4cl
#
# This code handles messages from lisp, marshals and unmarshals data,
# and defines classes which forward all interactions to lisp.
#
# Should work with python 2.7 or python 3
# Do NOT use single quote in this file.
from __future__ import print_function
import sys
import numbers
import itertools
import inspect
import json
import os
import signal
import traceback as tb
numpy_is_installed = False
try:
# Use NumPy for multi-dimensional arrays
import numpy
numpy_is_installed = True
except:
pass
return_stream = sys.stdout
output_stream = sys.stderr
sys.stdout = sys.stderr
eval_globals = {}
config = {}
eval_globals["_py4cl_config_file_name"] = ".config"
def load_config():
config_file = sys.argv[1] + eval_globals["_py4cl_config_file_name"]
if os.path.exists(config_file):
with open(config_file) as conf:
global config
config = json.load(conf)
for k in config:
if config[k] == None:
config[k] = False
try:
eval_globals["_py4cl_config"] = config
except:
pass
else:
print(eval_globals["_py4cl_config_file_name"], "file not found!")
eval_globals["_py4cl_config"] = {}
load_config()
class Symbol(object):
"""
A wrapper around a string, representing a Lisp symbol.
"""
def __init__(self, name):
self._name = name
def __str__(self):
return self._name
def __repr__(self):
return "Symbol("+self._name+")"
class LispCallbackObject (object):
"""
Represents a lisp function which can be called.
An object is used rather than a lambda, so that the lifetime
can be monitoried, and the function removed from a hash map
"""
def __init__(self, handle):
"""
handle A number, used to refer to the object in Lisp
"""
self.handle = handle
def __del__(self):
"""
Delete this object, sending a message to Lisp
"""
return_stream.write("d")
send_value(self.handle)
def __call__(self, *args, **kwargs):
"""
Call back to Lisp
args Arguments to be passed to the function
"""
global return_values
# Convert kwargs into a sequence of ":keyword value" pairs
# appended to the positional arguments
allargs = args
for key, value in kwargs.items():
allargs += (Symbol(":"+str(key)), value)
old_return_values = return_values # Save to restore after
try:
return_values = 0
return_stream.write("c")
send_value((self.handle, allargs))
finally:
return_values = old_return_values
# Wait for a value to be returned.
# Note that the lisp function may call python before returning
return message_dispatch_loop()
class UnknownLispObject (object):
"""
Represents an object in Lisp, which could not be converted to Python
"""
__during_init = True # Do not send changes during __init__
def __init__(self, lisptype, handle):
"""
lisptype A string describing the type. Mainly for debugging
handle A number, used to refer to the object in Lisp
"""
self.lisptype = lisptype
self.handle = handle
self.__during_init = False # Further changes are sent to Lisp
def __del__(self):
"""
Delete this object, sending a message to Lisp
"""
try:
sys.stdout = return_stream
return_stream.write("d")
send_value(self.handle)
finally:
sys.stdout = output_stream
def __str__(self):
return "UnknownLispObject(\""+self.lisptype+"\", "+str(self.handle)+")"
def __getattr__(self, attr):
# Check if there is a slot with this name
try:
sys.stdout = return_stream
return_stream.write("s") # Slot read
send_value((self.handle, attr))
finally:
sys.stdout = output_stream
# Wait for the result
return message_dispatch_loop()
def __setattr__(self, attr, value):
if self.__during_init:
return object.__setattr__(self, attr, value)
try:
sys.stdout = return_stream
return_stream.write("S") # Slot write
send_value((self.handle, attr, value))
finally:
sys.stdout = output_stream
# Wait until finished, to syncronise
return message_dispatch_loop()
python_to_lisp_type = {
bool: "BOOLEAN",
type(None): "NULL",
int: "INTEGER",
float: "FLOAT",
complex: "COMPLEX",
list: "VECTOR",
dict: "HASH-TABLE",
str: "STRING",
}
try:
python_to_lisp_type[inspect._empty] = "NIL"
except:
pass
return_values = 0
##################################################################
# This code adapted from cl4py
#
# https://github.com/marcoheisig/cl4py
#
# Copyright (c) 2018 Marco Heisig <[email protected]>
# 2019 Ben Dudson <[email protected]>
def lispify_infnan_if_needed(lispified_float):
table = {
"infd0": "float-features:double-float-positive-infinity",
"-infd0": "float-features:double-float-negative-infinity",
"inf": "float-features:single-float-positive-infinity",
"-inf": "float-features:single-float-negative-infinity",
"nan": "float-features:single-float-nan",
"nand0": "float-features:double-float-nan",
}
if lispified_float in table: return table[lispified_float]
else: return lispified_float
lispifiers = {
bool : lambda x: "T" if x else "NIL",
type(None) : lambda x: "\"None\"",
int : str,
# floats in python are double-floats of common-lisp
float : lambda x: lispify_infnan_if_needed(str(x).replace("e", "d") if str(x).find("e") != -1 else str(x)+"d0"),
complex : lambda x: "#C(" + lispify(x.real) + " " + lispify(x.imag) + ")",
list : lambda x: "#(" + " ".join(lispify(elt) for elt in x) + ")",
tuple : lambda x: "\"()\"" if len(x)==0 else "(quote (" + " ".join(lispify(elt) for elt in x) + "))",
# Note: With dict -> hash table, use :test equal so that string keys work as expected
# TODO: Should test be equalp? Should users get an option to choose the test functions?
# Should we avoid using cl:make-hash-table and use custom hash-tables instead?
dict : lambda x: "#.(let ((table (make-hash-table :test (quote cl:equal)))) " + " ".join("(setf (gethash (quote {}) table) (quote {}))".format(lispify(key), lispify(value)) for key, value in x.items()) + " table)",
str : lambda x: "\"" + x.replace("\\", "\\\\").replace("\"", "\\\"") + "\"",
type : lambda x: "(quote " + python_to_lisp_type[x] + ")",
Symbol : str,
UnknownLispObject : lambda x: "#.(py4cl2::lisp-object {})".format(x.handle),
# there is another lispifier just below
}
if numpy_is_installed: #########################################################
NUMPY_PICKLE_INDEX = 0 # optional increment in lispify_ndarray and reset to 0
def load_pickled_ndarray(filename):
arr = numpy.load(filename, allow_pickle = True)
return arr
def delete_numpy_pickle_arrays():
global NUMPY_PICKLE_INDEX
while NUMPY_PICKLE_INDEX:
NUMPY_PICKLE_INDEX -= 1
numpy_pickle_location = config["numpyPickleLocation"] \
+ ".from." + str(NUMPY_PICKLE_INDEX)
if os.path.exists(numpy_pickle_location):
os.remove(numpy_pickle_location)
numpy_cl_type = {
numpy.dtype("int64"): "(cl:quote (cl:signed-byte 64))",
numpy.dtype("int32"): "(cl:quote (cl:signed-byte 32))",
numpy.dtype("int16"): "(cl:quote (cl:signed-byte 16))",
numpy.dtype("int8"): "(cl:quote (cl:signed-byte 8))",
numpy.dtype("uint64"): "(cl:quote (cl:unsigned-byte 64))",
numpy.dtype("uint32"): "(cl:quote (cl:unsigned-byte 32))",
numpy.dtype("uint16"): "(cl:quote (cl:unsigned-byte 16))",
numpy.dtype("uint8"): "(cl:quote (cl:unsigned-byte 8))",
numpy.dtype("bool_"): "(cl:quote cl:bit)",
numpy.dtype("float64"): "(cl:quote cl:double-float)",
numpy.dtype("float32"): "(cl:quote cl:single-float)",
numpy.dtype("object"): "cl:t",
}
def numpy_to_cl_type(numpy_type):
try:
return numpy_cl_type[numpy_type]
except KeyError:
raise Exception("Do not know how to convert " + str(numpy_type) + " to CL")
def lispify_ndarray(obj):
"""Convert a NumPy array to a string which can be read by lisp
Example:
array([[1, 2], => "#2A((1 2) (3 4))"
[3, 4]])
"""
global NUMPY_PICKLE_INDEX
if "numpyPickleLowerBound" in config and \
"numpyPickleLocation" in config and \
obj.size >= config["numpyPickleLowerBound"]:
numpy_pickle_location = config["numpyPickleLocation"] \
+ ".from." + str(NUMPY_PICKLE_INDEX)
NUMPY_PICKLE_INDEX += 1
with open(numpy_pickle_location, "wb") as f:
numpy.save(f, obj, allow_pickle = True)
array = "#.(numpy-file-format:load-array \"" + numpy_pickle_location + "\")"
return array
if obj.ndim == 0:
# Convert to scalar then lispify
return lispify(numpy.asscalar(obj))
array = "(cl:make-array " + str(obj.size) + " :initial-contents (cl:list " \
+ " ".join(map(lispify, numpy.ndarray.flatten(obj))) + ") :element-type " \
+ numpy_to_cl_type(obj.dtype) + ")"
array = "#.(cl:make-array (cl:quote " + lispify(obj.shape) + ") :element-type " \
+ numpy_to_cl_type(obj.dtype) + " :displaced-to " + array + " :displaced-index-offset 0)"
return array
# Register the handler to convert Python -> Lisp strings
lispifiers.update({
numpy.ndarray: lispify_ndarray,
numpy.float64: lambda x : lispify_infnan_if_needed(str(x).replace("e", "d") if str(x).find("e") != -1 else str(x)+"d0"),
numpy.float32: lambda x : lispify_infnan_if_needed(str(x)),
numpy.bool_ : lambda x : "1" if x else "0"
# The case for integers is handled inside lispify function. At best, you would want a way to compare / check for subtypes to avoid casing on u/int64/32/16/8.
})
# end of "if numpy_is_installed" ###############################################
def lispify_handle(obj):
"""
Store an object in a dictionary, and return a handle
"""
handle = next(python_handle)
python_objects[handle] = obj
return "#.(py4cl2::customize (py4cl2::make-python-object-finalize :type \""+str(type(obj))+"\" :handle "+str(handle)+"))"
def lispify(obj):
"""
Turn a python object into a string which can be parsed by Lisp reader.
If return_values is false then always creates a handle
"""
if return_values > 0:
if isinstance(obj, Exception): return str(obj)
else: return lispify_handle(obj)
try:
if isinstance(obj, Exception):
return ("".join(tb.format_exception(type(obj), obj, obj.__traceback__))
if config["printPythonTraceback"] else str(obj))
elif numpy_is_installed and isinstance(obj, numpy.integer):
return str(obj)
else:
return lispifiers[type(obj)](obj)
except KeyError:
# Unknown type. Return a handle to a python object
return lispify_handle(obj)
def generator(function, stop_value):
temp = None
while True:
temp = function()
if temp == stop_value: break
yield temp
##################################################################
def recv_string():
"""
Get a string from the input stream
"""
length = int(sys.stdin.readline())
return sys.stdin.read(length)
def recv_value():
"""
Get a value from the input stream
Return could be any type
"""
return eval(recv_string(), eval_globals)
def send_value(value):
"""
Send a value to stdout as a string, with length of string first
"""
try:
# if type(value) == str and return_values > 0:
# value_str = value # to handle stringified-errors along with remote-objects
# else:
value_str = lispify(value)
except Exception as e:
# At this point the message type has been sent,
# so we cannot change to throw an exception/signal condition
value_str = ("Lispify error: " + "".join(tb.format_exception(type(e), e, e.__traceback__)) \
if config["printPythonTraceback"] else str(e))
excess_char_count = (0 if os.name != "nt" else value_str.count("\n"))
print(len(value_str)+excess_char_count, file = return_stream, flush=True)
return_stream.write(value_str)
return_stream.flush()
def return_value(value):
"""
Return value to lisp process, by writing to return_stream
"""
if isinstance(value, Exception):
return return_error(value)
return_stream.write("r")
return_stream.flush()
send_value(value)
def return_error(error):
return_stream.write("e")
send_value(error)
def pythonize(value): # assumes the symbol name is downcased by the lisp process
"""
Convertes the value (Symbol) to python conventioned strings.
In particular, replaces "-" with "_"
"""
return str(value)[1:].replace("-", "_")
def message_dispatch_loop():
"""
Wait for a message, dispatch on the type of message.
Message types are determined by the first character:
e Evaluate an expression (expects string)
x Execute a statement (expects string)
q Quit
"""
global return_values # Controls whether values or handles are returned
while True:
try:
output_stream.flush()
# Read command type
cmd_type = sys.stdin.read(1)
# It is possible that python would have finished sending the data to CL
# but CL would still not have finished processing. We will receive further
# instructions only after CL has finished processing, and therefore we can delete
# the arrays. (TODO: But how does this happen with callbacks?)
if numpy_is_installed: delete_numpy_pickle_arrays()
if cmd_type == "e": # Evaluate an expression
expr = recv_string()
# if expr not in cache:
# print("Adding " + expr + " to cache")
# cache[expr] = eval("lambda : " + expr, eval_globals)
# result = cache[expr]()
result = eval(expr, eval_globals)
return_value(result)
elif cmd_type == "x": # Execute a statement
exec(recv_string(), eval_globals)
return_value(None)
elif cmd_type == "q":
exit(0)
elif cmd_type == "r": # return value from lisp function
return recv_value()
elif cmd_type == "O": # Return only handles
return_values += 1
elif cmd_type == "o": # Return values when possible (default)
return_values -= 1
else:
return_error("Unknown message type \"{0}\"".format(cmd_type))
except KeyboardInterrupt as e: # to catch SIGINT
# output_stream.write("Python interrupted!\n")
return_value(None)
except Exception as e:
return_error(e)
# Store for python objects which cannot be translated to Lisp objects
python_objects = {}
python_handle = itertools.count(0)
# Make callback function accessible to evaluation
eval_globals["_py4cl_LispCallbackObject"] = LispCallbackObject
eval_globals["_py4cl_Symbol"] = Symbol
eval_globals["_py4cl_UnknownLispObject"] = UnknownLispObject
eval_globals["_py4cl_objects"] = python_objects
eval_globals["_py4cl_generator"] = generator
# These store the environment used when eval-ing strings from Lisp
eval_globals["_py4cl_config"] = config
eval_globals["_py4cl_load_config"] = load_config
if numpy_is_installed:
# NumPy is used for Lisp -> Python conversion of multidimensional arrays
eval_globals["_py4cl_numpy"] = numpy
eval_globals["_py4cl_load_pickled_ndarray"] \
= load_pickled_ndarray
# Handle fractions (RATIO type)
# Lisp will pass strings containing "_py4cl_fraction(n,d)"
# where n and d are integers.
import fractions
eval_globals["_py4cl_fraction"] = fractions.Fraction
# Turn a Fraction into a Lisp RATIO
lispifiers[fractions.Fraction] = str
# Lisp-side customize-able lispifiers
# FIXME: Is there a better way than going to each of the above and doing manually?
old_lispifiers = lispifiers.copy()
for key in lispifiers.keys():
lispifiers[key] = eval(
"""
lambda x: "#.(py4cl2::customize " + old_lispifiers[{0}](x) + ")"
""".format(("" if key.__module__ == "builtins" or key.__module__ == "__main__" \
else key.__module__ + ".") + key.__name__ if key.__name__ != "NoneType" \
else "type(None)"))
async_results = {} # Store for function results. Might be Exception
async_handle = itertools.count(0) # Running counter
# Main loop
message_dispatch_loop()