-
Notifications
You must be signed in to change notification settings - Fork 30
/
tests.lisp
758 lines (613 loc) · 23.5 KB
/
tests.lisp
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
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
(py4cl:import-module "math" :reload t)
(defpackage #:py4cl/tests
(:use #:cl #:clunit)
(:export #:run))
(in-package :py4cl/tests)
(defsuite tests ())
;; Tests which start and stop python
(defsuite pytests (tests))
(deffixture pytests (@body)
(py4cl:python-start)
@body
(py4cl:python-stop))
(defun run (&optional interactive?)
"Run all the tests for py4cl."
(run-suite 'tests :use-debugger interactive?))
;; Start and stop Python, check that python-alive-p responds
(deftest start-stop (tests)
(assert-false (py4cl:python-alive-p))
(py4cl:python-start)
(assert-true (py4cl:python-alive-p))
(py4cl:python-stop)
(assert-false (py4cl:python-alive-p)))
(deftest eval-integer (pytests)
(let ((result (py4cl:python-eval "1 + 2 * 3")))
(assert-true (typep result 'integer))
(assert-equalp 7 result)))
(deftest eval-malformed (pytests)
(assert-condition py4cl:python-error
(py4cl:python-eval "1 + ")))
(deftest eval-real (pytests)
(let ((result (py4cl:python-eval "1.3 + 2.2")))
(assert-true (typep result 'real))
(assert-equalp 3.5 result)))
(deftest eval-vector (pytests)
(let ((result (py4cl:python-eval "[i**2 for i in range(4)]")))
(assert-true (typep result 'array))
(assert-equalp #(0 1 4 9) result)))
(deftest eval-list (pytests)
(let ((result (py4cl:python-eval "(1,2,3)")))
(assert-true (typep result 'cons))
(assert-equalp '(1 2 3) result)))
;; Check passing strings, including quote characters which need to be escaped
(deftest eval-string (pytests)
(assert-equalp "say \"hello\" world"
(py4cl:python-eval "'say \"hello\"' + ' world'")))
(deftest eval-string-newline (pytests)
(let ((str "hello
world"))
(assert-equalp str (py4cl:python-eval (py4cl::pythonize str)))))
(deftest pythonize-format-string (tests)
(assert-equalp "\"foo\""
(py4cl::pythonize (format nil "foo"))))
(deftest eval-format-string (pytests)
(assert-equalp "foo"
(py4cl:python-eval
(py4cl::pythonize (format nil "foo")))))
;; This tests whether outputs to stdout mess up the return stream
(deftest eval-print (pytests)
(unless (= 2 (first (py4cl:python-version-info)))
;; Should return the result of print, not the string printed
(assert-equalp nil
(py4cl:python-eval "print(\"hello\")")
"This fails with python 2")
;; Should print the output to stdout
(assert-equalp "hello world
"
(with-output-to-string (*standard-output*)
(py4cl:chain (print "hello world"))))
;; Check that the output is cleared, by printing a shorter string
(assert-equalp "testing
"
(with-output-to-string (*standard-output*)
(py4cl:chain (print "testing"))))))
(deftest eval-params (pytests)
;; Values are converted into python values
(let ((a 4)
(b 7))
(assert-equalp 11
(py4cl:python-eval a "+" b)))
;; Arrays can also be passed
(assert-equalp #2A((1 2) (3 4))
(py4cl:python-eval #2A((1 2) (3 4))))
(assert-equalp #2A((2 4) (6 8))
(py4cl:python-eval #2A((1 2) (3 4)) "*" 2))
(assert-equalp #3A(((2 4) (7 8)) ((8 5) (1 6)))
(py4cl:python-eval #3A(((1 3) (6 7)) ((7 4) (0 5))) "+" 1))
;; Test handling of real numbers in arrays
(assert-equalp #(1.0 2.0)
(py4cl:python-eval (vector 1.0 2.0)))
;; Test empty arrays
(assert-equalp #()
(py4cl:python-eval #()))
;; Unless the values are strings
(let ((str "hello"))
(assert-condition py4cl:python-error
(py4cl:python-eval "len(" str ")")) ; "len(hello)"
;; To pass a string to python, run through pythonize:
(assert-equalp 5
(py4cl:python-eval "len(" (py4cl::pythonize str) ")"))))
(deftest complex-values (pytests)
;; Single values
(assert-equality #'= #C(1 2)
(py4cl:python-eval #C(1 2)))
(assert-equality #'= #C(1 -2)
(py4cl:python-eval #C(1 -2)))
(assert-equality #'= #C(-1 -2)
(py4cl:python-eval #C(-1 -2)))
;; Expressions. Tested using multiply to catch things like
;; "1+2j * 2+3j -> 1+7j rather than (-4+7j)
;; Note: Python doesn't have complex integers, so all returned
;; values could be floats
(assert-equality #'= #C(-4 7)
(py4cl:python-eval #C(1 2) "*" #C(2 3)))
(assert-equality #'= #C(4 7)
(py4cl:python-eval #C(1 -2) "*" #C(-2 3)))
;; Lists of complex numbers
(assert-equality #'= #C(6 9)
(py4cl:python-call "sum" (list #C(1 2) #C(2 3) #C(3 4)))))
(deftest exec-print (pytests)
(unless (= 2 (first (py4cl:python-version-info)))
;; Python 3
(assert-equalp nil
(py4cl:python-exec "print(\"hello\")")
"This fails with python 2")))
(deftest call-lambda-no-args (pytests)
(assert-equalp 3
(py4cl:python-call "lambda : 3")))
(deftest call-one-arg-int (pytests)
(assert-equalp 42
(py4cl:python-call "abs" -42)))
(deftest call-one-arg-list (pytests)
(assert-equalp 9
(py4cl:python-call "sum" '(3 2 4))))
(deftest call-one-arg-string (pytests)
(assert-equalp #("h" "e" "l" "l" "o")
(py4cl:python-call "list" "hello")))
(deftest call-dotted-function (pytests)
(py4cl:python-exec "import math")
(assert-equalp (sqrt 42)
(py4cl:python-call "math.sqrt" 42)))
(deftest call-lambda-function (pytests)
(assert-equalp 16
(py4cl:python-call "lambda x: x*x" 4)))
(deftest call-lambda-function-two-args (pytests)
(assert-equalp 10
(py4cl:python-call "lambda x, y: x*y - y" 3 5)))
(deftest call-lambda-keywords (pytests)
(assert-equalp -1
(py4cl:python-call "lambda a=0, b=1: a-b" :b 2 :a 1))
(assert-equalp 1
(py4cl:python-call "lambda a=0, b=1: a-b" :a 2 :b 1)))
(deftest call-with-lambda-callback (pytests)
;; Define a function in python which calls its argument
(py4cl:python-exec "runme = lambda f: f()")
;; Pass a lambda function to python-call
(assert-equalp 42
(py4cl:python-call "runme" (lambda () 42))))
(py4cl:import-function "sum")
(deftest import-function-sum (pytests)
(assert-equalp 6
(sum '(2 1 3))))
(deftest call-return-numpy-types (pytests)
(py4cl:python-exec "import numpy as np")
(assert-equalp 42.0
(py4cl:python-eval "np.float64(42.0)")))
;; Simple callback function
(defun test-func ()
42)
(deftest callback-no-args (pytests)
(py4cl:export-function #'test-func "test")
(assert-equalp 42
(py4cl:python-eval "test()")))
;; Even simpler function returning NIL
(defun nil-func ()
nil)
(deftest callback-no-args-return-nil (pytests)
(py4cl:export-function #'nil-func "test_nil")
(assert-equalp nil
(py4cl:python-eval "test_nil()")))
;; Python can't eval write-to-string's output "3.141592653589793d0"
(deftest callback-return-double (pytests)
(py4cl:export-function (lambda () pi) "test")
(assert-equalp 3.1415927
(py4cl:python-eval "test()")))
(deftest callback-one-arg (pytests)
(py4cl:export-function (lambda (x) (* 2 x)) "double")
(assert-equalp 4
(py4cl:python-eval "double(2)")))
(deftest callback-two-args (pytests)
(py4cl:export-function (lambda (x y) (/ x y)) "div")
(assert-equalp 3
(py4cl:python-eval "div(6, 2)")))
(deftest callback-many-args (pytests)
(py4cl:export-function #'+ "add")
(assert-equalp 15
(py4cl:python-eval "add(2, 4, 6, 3)")))
(deftest callback-seq-arg (pytests)
(py4cl:export-function #'reverse "reverse")
(assert-equalp '(3 1 2 4)
(py4cl:python-eval "reverse((4,2,1,3))"))
(assert-equalp #(3 1 2 4)
(py4cl:python-eval "reverse([4,2,1,3])")))
(deftest callback-keyword-arg (pytests)
(py4cl:export-function (lambda (&key setting) setting) "test")
(assert-equalp nil
(py4cl:python-eval "test()"))
(assert-equalp 42
(py4cl:python-eval "test(setting=42)")))
;; Call python during callback
(deftest python-during-callback (pytests)
(py4cl:export-function
(lambda () (py4cl:python-eval "42"))
"test")
(assert-equalp 42
(py4cl:python-eval "test()")))
;; Hash-table support
(deftest hash-table-empty (pytests)
(assert-equalp "{}"
(py4cl:python-call "str" (make-hash-table))))
(deftest hash-table-values (pytests)
(let ((table (make-hash-table)))
(setf (gethash "test" table) 3
(gethash "more" table) 42)
(assert-equalp 42
(py4cl:python-call "lambda d: d[\"more\"]" table))
(assert-equalp 3
(py4cl:python-call "lambda d: d[\"test\"]" table))
(assert-equalp 2
(py4cl:python-call "len" table))))
(deftest hash-table-from-dict (pytests)
;; Simple keys
(let ((table (py4cl:python-eval "{1:2, 2:3}")))
(assert-equalp 2
(gethash 1 table))
(assert-equalp 3
(gethash 2 table)))
;; Ensure values are being lispified
(let ((table (py4cl:python-eval "{1:[1,2,3]}")))
(assert-equalp #(1 2 3)
(gethash 1 table)))
;; Ensure keys are being lispified and string keys work
(let ((table (py4cl:python-eval "{\"test\":42}")))
(assert-equalp 42
(gethash "test" table))))
;; Generators
(deftest generator (pytests)
(assert-equalp (ecase (car (py4cl:python-version-info))
(3 "<class 'generator'>")
(2 "<type 'generator'>"))
(slot-value (py4cl:python-generator #'identity 3) 'type))
(py4cl:python-exec "
def foo(gen):
return list(gen)")
(assert-equalp #(1 2 3 4)
(let ((gen (py4cl:python-generator (let ((x 0)) (lambda () (incf x)))
5)))
(py4cl:python-call "foo" gen)))
(assert-equalp #(#\h #\e #\l #\l #\o)
(let ((gen (py4cl:python-generator (let ((str (make-string-input-stream "hello")))
(lambda () (read-char str nil)))
nil)))
(py4cl:python-call "foo" gen))))
;; Asyncronous functions
(deftest call-function-async (pytests)
(let ((thunk (py4cl:python-call-async "str" 42)))
;; returns a function which when called returns the result
(assert-equalp "42"
(funcall thunk))
;; And returns the same when called again
(assert-equalp "42"
(funcall thunk)))
;; Check if it handles errors
(let ((thunk (py4cl:python-call-async "len"))) ; TypeError
(assert-condition py4cl:python-error
(funcall thunk)))
;; Check that values can be requested out of order
(let ((thunk1 (py4cl:python-call-async "str" 23))
(thunk2 (py4cl:python-call-async "str" 12))
(thunk3 (py4cl:python-call-async "str" 7)))
(assert-equalp "12"
(funcall thunk2))
(assert-equalp "7"
(funcall thunk3))
(assert-equalp "23"
(funcall thunk1))))
(deftest python-objects (pytests)
;; Define a simple python class containing a value
(py4cl:python-exec
"class Test:
pass
a = Test()
a.value = 42")
;; Check that the variable has been defined
(assert-equalp 42
(py4cl:python-eval "a.value"))
;; Implementation detail: No objects stored in python dict
(assert-equalp 0
(py4cl:python-eval "len(_py4cl_objects)"))
;; Evaluate and return a python object
(let ((var (py4cl:python-eval "a")))
;; Implementation detail: Type of returned object
(assert-equalp 'PY4CL::PYTHON-OBJECT
(type-of var))
;; Implementation detail: Object is stored in a dictionary
(assert-equalp 1
(py4cl:python-eval "len(_py4cl_objects)"))
;; Can pass to eval to use dot accessor
(assert-equalp 42
(py4cl:python-eval var ".value"))
;; Can pass as argument to function
(assert-equal 84
(py4cl:python-call "lambda x : x.value * 2" var)))
;; Trigger a garbage collection so that VAR is finalized.
;; This should also delete the object in python
(tg:gc :full t)
;; Implementation detail: dict object store should be empty
;; Note: This is dependent on the CL implementation. Trivial-garbage
;; doesn't seem to support ccl
#-clozure (assert-equalp 0
(py4cl::python-eval "len(_py4cl_objects)")))
(deftest python-del-objects (tests)
;; Check that finalizing objects doesn't start python
(py4cl:python-start)
(py4cl:python-exec
"class Test:
pass
a = Test()")
(let ((var (py4cl:python-eval "a")))
;; Implementation detail: Type of returned object
(assert-equalp 'PY4CL::PYTHON-OBJECT
(type-of var))
(py4cl:python-stop)
(assert-false (py4cl:python-alive-p)))
;; VAR out of scope. Make sure it's finalized
(tg:gc :full t)
(assert-false (py4cl:python-alive-p)))
(deftest python-method (pytests)
(assert-equalp 3
(py4cl:python-method '(1 2 3) '__len__))
(assert-equalp "hello world"
(py4cl:python-method "hello {0}" 'format "world")))
;; Shorter more convenient slicing
(py4cl:import-function "slice")
(deftest chain (pytests)
(assert-equalp "Hello world"
(py4cl:chain "hello {0}" (format "world") (capitalize)))
(assert-equalp "hello world"
(let ((format-str "hello {0}")
(argument "world"))
(py4cl:chain format-str (format argument))))
(assert-equalp "result: 3"
(py4cl:chain "result: {0}" (format (+ 1 2))))
(assert-equalp 3
(py4cl:chain (slice 3) stop))
;; Anything not a list or a symbol is put between [] brackets (__getitem__)
(assert-equalp "o"
(py4cl:chain "hello" 4))
;; [] operator for indexing and slicing (alias for __getitem__)
(assert-equalp "l"
(py4cl:chain "hello" ([] 3)))
(assert-equalp 3
(py4cl:chain #2A((1 2) (3 4)) ([] 1 0)))
(assert-equalp #(4 5)
(py4cl:chain #2A((1 2 3) (4 5 6)) ([] 1 (slice 0 2))))
(let ((dict (py4cl:python-eval "{\"hello\":\"world\", \"ping\":\"pong\"}")))
(assert-equalp "world"
(py4cl:chain dict "hello"))
(assert-equalp "pong"
(py4cl:chain dict ([] "ping")))))
(deftest chain-keywords (pytests)
(py4cl:python-exec
"def test_fn(arg, key=1):
return arg * key")
(assert-equalp 3
(py4cl:chain (test_fn 3)))
(assert-equalp 6
(py4cl:chain (test_fn 3 :key 2)))
(py4cl:python-exec
"class testclass:
def run(self, dummy = 1, value = 42):
return value")
(assert-equalp 42
(py4cl:chain (testclass) (run)))
(assert-equalp 31
(py4cl:chain (testclass) (run :value 31))))
(deftest chain-strings (pytests)
(py4cl:python-exec
"class TestClass:
def doThing(self, dummy = 1, value = 42):
return value")
(assert-equalp 42
(py4cl:chain ("TestClass") ("doThing")))
(assert-equalp 31
(py4cl:chain ("TestClass") ("doThing" :value 31))))
(deftest remote-objects (pytests)
;; REMOTE-OBJECTS returns a handle
(assert-equalp 'py4cl::python-object
(type-of (py4cl:remote-objects (py4cl:python-eval "1+2"))))
;; REMOTE-OBJECTS* returns a value
(assert-equalp 3
(py4cl:remote-objects* (py4cl:python-eval "1+2")))
(assert-equalp 3
(py4cl:python-eval
(py4cl:remote-objects (py4cl:python-eval "1+2"))))
;; Nested remote-object environments
(assert-equalp 'py4cl::python-object
(type-of (py4cl:remote-objects
(py4cl:remote-objects (py4cl:python-eval "1+2"))
(py4cl:python-eval "1+2")))))
(deftest call-callable-object (pytests)
(assert-equalp 6
(py4cl:python-call (py4cl:python-eval "lambda x : 2*x") 3)))
(deftest setf-eval (pytests)
(setf (py4cl:python-eval "test_value") 42) ; Set a variable
(assert-equalp 42
(py4cl:python-eval "test_value")))
(deftest setf-chain (pytests)
(assert-equalp #(0 5 2 -1)
(py4cl:remote-objects*
(let ((list (py4cl:python-eval "[0, 1, 2, 3]")))
(setf (py4cl:chain list ([] 1)) 5
(py4cl:chain list ([] -1)) -1)
list)))
(assert-equalp "world"
(py4cl:remote-objects*
(let ((dict (py4cl:python-eval "{}")))
(setf (py4cl:chain dict ([] "hello")) "world")
(py4cl:chain dict ([] "hello")))))
;; Define an empty class which can be modified
(py4cl:python-exec "
class testclass:
pass")
(let ((obj (py4cl:chain (testclass))))
(setf (py4cl:chain obj data_attrib) 21)
(assert-equalp 21
(py4cl:chain obj data_attrib))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Passing unknown lisp objects to python
(defstruct test-struct
x y)
(deftest lisp-structs (pytests)
;; Create a struct and pass to Python
(let ((result (py4cl:python-call
"lambda x: x"
(make-test-struct :x 1 :y 2))))
;; Check we got back the structure
(assert-true (typep result 'test-struct))
(assert-equalp 1
(test-struct-x result))
(assert-equalp 2
(test-struct-y result))))
(defclass test-class ()
((value :initarg :value)
(thing :initarg :thing)))
;; Define a method to handle slot access from python
(defmethod py4cl:python-getattr ((object test-class) slot-name)
(cond
((string= slot-name "value")
(slot-value object 'value))
((string= slot-name "thing")
(slot-value object 'thing))
((string= slot-name "func")
(lambda (arg) (* 2 arg)))
(t (call-next-method))))
(deftest lisp-class-slots (pytests)
(let ((object (make-instance 'test-class :thing 23 :value 42)))
;; Slot access
(assert-equalp 23
(py4cl:python-call "lambda x : x.thing" object))
(assert-equalp 42
(py4cl:chain object value))
;; Function (method) call
(assert-equalp 42
(py4cl:chain object (func 21))))
;; The handler should work for other objects of the same class (class-of)
(let ((object2 (make-instance 'test-class :thing "hello" :value 314)))
(assert-equalp "hello"
(py4cl:chain object2 thing))))
;; Class inheriting from test-class
(defclass child-class (test-class)
((other :initarg :other)))
;; Define method which passes to the next method if slot not recognised
(defmethod py4cl:python-getattr ((object child-class) slot-name)
(cond
((string= slot-name "other")
(slot-value object 'other))
(t (call-next-method))))
(deftest lisp-class-inherit (pytests)
(let ((object (make-instance 'child-class :thing 23 :value 42 :other 3)))
(assert-equalp 23
(py4cl:python-call "lambda x : x.thing" object))
(assert-equalp 42
(py4cl:chain object value))
(assert-equalp 3
(py4cl:chain object other))))
;; Define a method to handle slot writing from python
(defmethod py4cl:python-setattr ((object test-class) slot-name set-to-value)
(cond
((string= slot-name "value")
(setf (slot-value object 'value) set-to-value))
((string= slot-name "thing")
(setf (slot-value object 'thing) set-to-value))
(t (call-next-method))))
(deftest lisp-class-set-slots (pytests)
(let ((object (make-instance 'test-class :thing 23 :value 42)))
;; Set value
(py4cl:python-exec object ".thing = 3")
(assert-equalp 3 (slot-value object 'thing))
(assert-equalp 3 (py4cl:python-eval object ".thing"))
;; Set again
(setf (py4cl:chain object thing) 72)
(assert-equalp 72 (slot-value object 'thing))
(assert-equalp 72 (py4cl:chain object thing))))
(deftest callback-in-remote-objects (pytests)
;; Callbacks send values to lisp in remote-objects environments
(assert-equalp 6
(py4cl:remote-objects*
(py4cl:python-call (lambda (x y) (* x y)) 2 3))))
(deftest unicode-string-type (pytests)
;; Python 2 and python 3 handle unicode differently
;; This just catches the use of unicode type strings in python2
;; not the use of unicode characters
(assert-equal "test unicode"
(py4cl:python-eval "u'test unicode'"))
(assert-equal 3
(gethash "pizza"
(py4cl:python-eval "{u'pizza': 3}"))))
;; ============================== PICKLE =======================================
(deftest transfer-multiple-arrays (pytests)
(when (and (py4cl:config-var 'py4cl:numpy-pickle-location)
(py4cl:config-var 'py4cl:numpy-pickle-lower-bound))
(let ((dimensions `((,(* 2 (py4cl:config-var 'py4cl:numpy-pickle-lower-bound)))
(,(* 5 (py4cl:config-var 'py4cl:numpy-pickle-lower-bound))))))
(assert-equalp dimensions
(mapcar #'array-dimensions
(py4cl:python-eval
(list (make-array (first dimensions) :element-type 'single-float)
(make-array (second dimensions) :element-type 'single-float))))
"No bound or location for pickling."))))
(deftest transfer-without-pickle (pytests)
(unless (and (py4cl:config-var 'py4cl:numpy-pickle-location)
(py4cl:config-var 'py4cl:numpy-pickle-lower-bound))
(assert-equalp '(100000)
(array-dimensions
(py4cl:python-eval (make-array 100000 :element-type 'single-float)))
"Pickle bound and location is present.")))
;; ==================== PROCESS-INTERRUPT ======================================
;; Unable to test on CCL:
;; Stream #<BASIC-CHARACTER-OUTPUT-STREAM UTF-8 (PIPE/36) #x3020019EE9AD> is private to #<PROCESS repl-thread(12) [Sleep] #x302000AC72FD>
#-ccl (deftest interrupt (pytests)
(unless (= 2 (first (py4cl:python-version-info)))
(let ((py4cl::*py4cl-tests* t))
(py4cl:python-stop)
(py4cl:python-exec "
class Foo():
def foo(self):
import time
import sys
sys.stdout.write('hello')
sys.stdout.flush()
time.sleep(5)
return")
(assert-equalp "hello"
(let* ((return-value nil)
(mon-thread (bt:make-thread
(lambda ()
(setq return-value
(with-output-to-string (*standard-output*)
(py4cl:python-call "Foo().foo")))))))
(sleep 1)
(py4cl:python-interrupt)
(bt:join-thread mon-thread)
return-value))
(assert-equalp "hello"
(let* ((return-value nil)
(mon-thread (bt:make-thread
(lambda ()
(setq return-value
(with-output-to-string (*standard-output*)
(py4cl:python-method (py4cl:python-call "Foo")
'foo)))))))
(sleep 1)
(py4cl:python-interrupt)
(bt:join-thread mon-thread)
return-value))
;; Check if no "residue" left
(assert-equalp 5 (py4cl:python-eval 5)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Rational numbers
(deftest ratios (pytests)
;; Ratios survive a round trip
(assert-equalp 1/2
(py4cl:python-eval 1/2))
;; Ratios (Fractions in python) can be manipulated
(assert-equalp 1/4
(py4cl:python-eval 1/2 "/" 2))
;; Complex ratios not supported in python so converts to floats
(assert-equality #'= #C(0.5 1.0)
(py4cl:python-eval #C(1 2) "*" 1/2)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Scope in python exec
(deftest python-exec-scope (pytests)
;; Local functions are retained in scope
;; This changed in python 3.x see e.g. https://stackoverflow.com/a/24734880
(assert-equalp "10
"
(with-output-to-string (*standard-output*)
(py4cl:python-exec "
def foo():
return 5
def bar():
return foo() + foo()
print(bar())"))))