forked from pinterface/burgled-batteries
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffi-interface.lisp
1360 lines (1238 loc) · 74.4 KB
/
ffi-interface.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
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(in-package #:python.cffi)
;;;; FFI Library
;; Much of what we do below requires it be loaded during macroexpansion time.
(eval-when (:compile-toplevel :load-toplevel :execute)
(define-foreign-library python-library
(:darwin (:framework "Python"))
(:unix (:or "libpython2.7.so.1.0" "libpython2.6.so.1.0" "libpython2.5.so.1.0" "libpython2.4.so.1.0" "libpython2.3.so.1.0"))
(:windows (:or "python27.dll" "python26.dll" "python25.dll" "python24.dll" "python23.dll"))
(t (:default "libpython")))
(use-foreign-library python-library))
;;;; C-level macros (as read-time conditionals)
;;; Automatically detected by their effects.
(eval-when (:compile-toplevel :load-toplevel)
;; Py_TRACE_REFS
(if (or (foreign-symbol-pointer "Py_InitModule4TraceRefs")
(foreign-symbol-pointer "Py_InitModule4TraceRefs_64"))
(pushnew 'trace-refs *features*)
(removef *features* 'trace-refs)))
;;;; Basic Handling of the PyObject struct
(defcstruct* %object ()
#+cpython:trace-refs (-ob-next object)
#+cpython:trace-refs (-ob-prev object)
(refcnt ssize-t)
(type :pointer type))
(defun %object.type-check-exact (o type)
(and (not (null-pointer-p o))
(pointer-eq (%object.type* o) type)))
(defcstruct* %var (%object)
(size ssize-t))
;;;; Functions Related to Embedding CPython
(in-python-docs "/c-api/init.html")
(defpyfun "Py_Initialize" :void ()
(:documentation "Initialize the Python interpreter. This, or .INITIALIZE-EX, must be called before Python may be used."))
(defpyfun "Py_InitializeEx" :void ((initsigs :int))
(:documentation "Like .INITIALIZE if INITSIGS is 1. If INITSIGS is 0, skips registration of signal handlers."))
(defpyfun "Py_IsInitialized" :boolean ()
(:documentation "Returns true if the Python interpreter has been initialize, false otherwise."))
(defpyfun "Py_Finalize" :void ())
(defpyfun "Py_SetProgramName" :void ((name :string)))
(defpyfun "Py_GetProgramName" :string ())
(defpyfun "Py_GetPrefix" :string ()) ; FIXME: directory pathname?
(defpyfun "Py_GetExecPrefix" :string ()) ; FIXME: directory pathname?
(defpyfun "Py_GetProgramFullPath" :string ()) ; FIXME: return a pathname?
(defpyfun "Py_GetPath" :string ()) ; FIXME?: Split on #\: or #\; and return a list of pathnames?
(defpyfun "Py_GetVersion" :string ())
(defpyfun "Py_GetPlatform" :string ())
(defpyfun "Py_GetCopyright" :string ())
(defpyfun "Py_GetCompiler" :string ())
(defpyfun "Py_GetBuildInfo" :string ())
#+requires-CHAR*-ARRAY-support (defpyfun "PySys_SetArgvEx" :void ((argc :int) (argv (:array :string)) (updatepath :int))
(:implementation (sys.set-argv argc argv)))
#+requires-CHAR*-ARRAY-support (defpyfun "PySys_SetArgv" :void ((argc :int) (argv (:array :string))))
(defpyfun "Py_SetPythonHome" :void ((home :string))) ; WARNING: requires static string!
(defpyfun "Py_GetPythonHome" :string ())
(in-python-docs nil)
;;;; Definitions Relating to Python Types
;;; PyObject is the root of all other types
(defpytype ("PyObject" "PyBaseObject")
(:to (value type)
(loop :for (lisp-name . type-parser) :in *type-map*
:for foreign-type := (funcall type-parser reference-type argument-type)
:when (lisp-is-convertable-to-foreign-p value foreign-type)
:do (cl:return (translate-to-foreign value foreign-type))
:finally (cl:return value)))
(:from (value type)
(cond
((null-pointer-p value) nil)
((%none.check value) (values)) ; treat Py_None as returning nothing
(t
;; NECESSARY! If we return the pointer, the later .DEC-REF in tff will
;; be too early; if we convert to a subtype of PyObject, that conversion
;; will also trigger a .DEC-REF, taking us to zero before we're ready.
(unless (borrowed-reference-p type) (.inc-ref value))
(loop :for (lisp-name . type-parser) :in *type-map*
:for foreign-type := (funcall type-parser reference-type argument-type)
:when (foreign-is-convertable-to-type-p value foreign-type)
:do (cl:return (translate-from-foreign value foreign-type))
;; This should probably occur in T-F-F for F-P-T, but then we
;; have multiple values and recursion from the above T-F-F to
;; deal with, and that would be much less straightforward.
:finally (cl:return (translate-unknown-pointer value type)))))))
;; Avoid recursion within object translator
(defmethod foreign-is-convertable-to-type-p (value (type foreign-python-object-type))
nil)
;; *sigh* If only C had introspection.
(defcstruct* %type (%var)
(name :string)
(basicsize ssize-t) (itemsize ssize-t)
;; standard operations
(dealloc :pointer) (print :pointer) (getattr :pointer)
(setattr :pointer) (compare :pointer) (repr :pointer)
;; generic methods
(as-number :pointer) (as-sequence :pointer) (as-mapping :pointer)
;; more standard ops
(hash :pointer) (call :pointer) (str :pointer)
(getattro :pointer) (setattro :pointer)
;; as buffer
(as-buffer :pointer)
;; flags marking optional/expanded features
(flags #.(cffi::canonicalize-foreign-type 'type-flags) type-flags)
;; docstring
(doc :string)
;; mo' functions
(traverse :pointer) (clear :pointer) (richcompare :pointer)
;; weak references
(weaklistoffset ssize-t)
;; iterators
(iter :pointer) (iternext :pointer)
;; attribute descriptors and subclassing
(methods :pointer)
(members :pointer)
(getset :pointer)
(base :pointer)
(dict :pointer dict)
(descr-get :pointer) (descr-set :pointer)
(dictoffset :long)
(init :pointer) (alloc :pointer) (new :pointer) (free :pointer)
(is-gc :pointer)
(bases :pointer tuple)
(mro :pointer tuple)
(cache :pointer)
(subclasses :pointer)
(weaklist :pointer))
(defpytype "PyType")
;; Docs say use Py_None, but _Py_NoneStruct is what's exported
(defpyvar "&_Py_NoneStruct" +None+)
(defun %none.check (o) (pointer-eq +None+ o))
;;; Numeric Types
(defpytype "PyInt"
(:type integer)
(:to (value type) (int.from-long* value))
(:from (value type) (int.as-long value)))
(defpyvar "&_Py_ZeroStruct" +False+)
(defpyvar "&_Py_TrueStruct" +True+)
(defpytype "PyBool"
(:superclass "PyInt")
(:type boolean)
(:to (value type) (if value +True+ +False+))
(:from (value type)
(cond
((pointer-eq +True+ value) t)
((pointer-eq +False+ value) nil)
(t (error "Not a boolean!")))))
(defpytype "PyLong"
(:to (value type) (long.from-long* value))
(:from (value type) (long.as-long value)))
(defpytype "PyFloat"
(:type cl:float)
(:to (value type) (float.from-double* value))
(:from (value type) (float.as-double value)))
;; FIXME: Actually using %complex will require FSBV integration (coming soon to a CFFI near you!)
(defcstruct %complex (real :double) (imag :double))
(defpytype "PyComplex"
(:type cl:complex)
(:to (value type)
(complex.from-doubles* (realpart value) (imagpart value)))
(:from (value type)
(cl:complex (complex.real-as-double value) (complex.imag-as-double value))))
;;; Sequence Types
;; ByteArray is not fundamentally different from String/Bytes, except we don't
;; bother trying to pretend it's got characters at all.
(defpytype "PyByteArray"
(:type (array (unsigned-byte 8)))
(:to (value type) (byte-array.from-string-and-size* value (length value)))
(:from (value type) (byte-array.as-string value)))
;; WARNING! PyString conversion will fail when there are disagreements with
;; encodings, or embedded NULs; and the inability to ask a string what
;; encoding it is means this isn't really fixable in practice. If you
;; want reliable string conversion you should use PyUnicode, and
;; PyByteArray for reliable octet-array conversion.
(defpytype "PyString"
(:to (value type) (string.from-string* value))
(:from (value type) (string.as-string value)))
#+(or) ;; FIXME: we'll need the bytes.whatever functions, and a way to alias the
;; PyString bits that make up PyBytes in Python 2
(defpytype "PyBytes"
(:to (value type) (bytes.from-string* value))
(:from (value type) (bytes.as-string value)))
;; Shorthand types for unicode strings
(defctype utf8-string (:string :encoding :utf-8))
(defctype ucs2-string (:string :encoding #+little-endian :ucs-2/le #+big-endian :ucs-2/be))
(defctype ucs4-string (:string :encoding #+little-endian :ucs-4/le #+big-endian :ucs-4/be))
;; PyUnicode should be preferred to PyString because it avoids the issue of
;; encodings.
(defpytype "PyUnicode"
(:type cl:string)
(:to (value type) (unicode.from-unicode* value (length value)))
(:from (value type) (unicode.as-unicode value)))
#+(or) ;; Skipping old-style buffers
(defpytype "PyBuffer"
(:to (value type) …)
(:from (value type) …))
(defpytype "PyTuple"
(:type cl:list)
(:to (value type)
(let* ((len (length value))
(tup (tuple.new* len))
(i 0))
(dolist (e value tup)
(tuple.set-item tup i e)
(incf i))))
(:from (value type)
(cond
((null-pointer-p value) (cl:list))
(t (loop :for i :from 0 :to (1- (tuple.size value))
:collect (tuple.get-item value i))))))
(defpytype "PyList"
(:type (and vector (not cl:string)))
(:to (value type)
(let ((list (list.new* (length value))))
(dotimes (i (length value) list)
(list.set-item list i (aref value i)))))
(:from (value type)
(cond
((null-pointer-p value) (make-array 0 :adjustable t :fill-pointer t))
(t (let ((array (make-array (list.size value) :adjustable t :fill-pointer t)))
(dotimes (i (list.size value) array)
(setf (aref array i) (list.get-item value i))))))))
;;; Mapping Types
(defpytype "PyDict"
(:type hash-table)
(:to (value type)
(let ((dict (dict.new*)))
(maphash (lambda (k v) (dict.set-item dict k v)) value)
dict))
(:from (value type)
(cond
((null-pointer-p value) (make-hash-table :test #'equal))
(t ;; Probably premature optimization, but the below avoids the
;; translation machinery for the bits we'd just GC anyway
(let* ((dict (make-hash-table :test #'equal))
(items (dict.items* value)))
(unwind-protect
(dotimes (i (list.size items) dict)
(let* ((item (list.get-item* items i))
(key (tuple.get-item item 0))
(value (tuple.get-item item 1)))
(setf (gethash key dict) value)))
(.dec-ref items)))))))
;;;; Other Objects
;;; Class and Instance Objects
;; Skipped because according to the docs they are going away in Python 3
;;; Function Objects
;; NOTE: Conceivably, we could turn Python functions into lambdas which when
;; called ran the Python function, and use an #'eq hash to go the other
;; way. However, I don't see an obvious way to introspect PyFunctions to
;; grab the argument list, so it'd be a pretty poor implementation. Also,
;; going from a Lisp-function to a Python-function is unlikely to be
;; supported any time soon. Rather than suffer that, we punt and just do
;; pointers for now.
(defpytype "PyFunction")
;;; Method Objects
;; see note for Function Objects
(defpytype "PyMethod")
;;; TODO File Objects
;; NOTE: Full support (meaning gray-streams) for Python file objects may not be
;; possible.
;;; Module Objects
;; NOTE: Because Python Modules don't really have any meaning in Lisp land, we
;; just pass them around as pointers and call it good.
(defpytype "PyModule")
;;; TODO Iterator Objects
;;; TODO Descriptor Objects
;;; TODO Slice Objects
;;; TODO Weak Reference Objects
;;; TODO Capsules
;;; CObjects (skipped; deprecated)
;;; TODO Cell Objects
;;; TODO Generator Objects
;;; DateTime Objects (skipped; requires extra C header)
;;; TODO Set Objects
;;; Code Objects
;; NOTE: Because Python Code Objects don't really have any meaning in Lisp land,
;; we just pass them around as pointers and call it good.
(defpytype "PyCode")
;;;; API Functions Relating to Threads, Interpreter State, and Debugging
(in-python-docs "/c-api/init.html")
;;; Threads
(defctype interpreter-state :pointer)
(defctype thread-state :pointer)
(defpyfun "PyEval_InitThreads" :void () (:requires :python-threads))
(defpyfun "PyEval_ThreadsInitialized" :boolean ())
(defpyfun "PyEval_SaveThread" thread-state ())
(defpyfun "PyEval_RestoreThread" :void ((tstate thread-state)))
(defpyfun "PyThreadState_Get" thread-state ())
(defpyfun "PyThreadState_Swap" thread-state ((tstate thread-state)))
(defpyfun "PyEval_ReInitThreads" :void ())
(defcenum (gilstate.state :int) :locked :unlocked)
(defpyfun "PyGILState_Ensure" gilstate.state ())
(defpyfun "PyGILState_Release" :void ((state gilstate.state)))
(defpyfun "PyGILState_GetThisThreadState" thread-state ())
(defmacro with-thread-state (&body body)
"The Lispy equivalent of surrounding code with Py_BEGIN_ALLOW_THREADS ... Py_END_ALLOW_THREADS."
(with-unique-names (thread-state)
`(let ((,thread-state (eval.save-thread)))
(unwind-protect (progn ,@body)
(eval.restore-thread ,thread-state)))))
(defpyfun "PyInterpreterState_New" interpreter-state () (:requires :python-threads))
(defpyfun "PyInterpreterState_Clear" :void ((interp interpreter-state)) (:requires :python-threads))
(defpyfun "PyInterpreterState_Delete" :void ((interp interpreter-state)) (:requires :python-threads))
(defpyfun "PyThreadState_New" thread-state ((interp interpreter-state)) (:requires :python-threads))
(defpyfun "PyThreadState_Clear" :void ((tstate thread-state)) (:requires :python-threads))
(defpyfun "PyThreadState_Delete" :void ((tstate thread-state)) (:requires :python-threads))
(defpyfun "PyThreadState_GetDict" (dict :borrowed) () (:requires :python-threads))
(defpyfun "PyThreadState_SetAsyncExc" :int ((id :long) (exc object)) (:requires :python-threads))
(defpyfun "PyEval_AcquireThread" :void ((tstate thread-state)) (:requires :python-threads))
(defpyfun "PyEval_ReleaseThread" :void ((tstate thread-state)) (:requires :python-threads))
(defpyfun "PyEval_AcquireLock" :void () (:requires :python-threads))
(defpyfun "PyEval_ReleaseLock" :void () (:requires :python-threads))
;;; Sub-Interpreters
(defpyfun "Py_NewInterpreter" thread-state ())
(defpyfun "Py_EndInterpreter" :void ((tstate thread-state)))
;;; Async Notifications
;; TODO: Add define-notification-callback macro like define-trace-function below?
(defpyfun "Py_AddPendingCall" 0-on-success ((callback :pointer) (arg :pointer))
(:requires "Python 2.7 (or newer)"))
;;; Profiling and Tracing
(defctype frame-object :pointer)
(defctype .tracefunc :pointer)
;; FIXME: should we call this define-trace-callback instead? define-trace-function-callback?
(defmacro define-trace-function (name (object frame what arg) &body body)
`(defcallback ,name :int ((,object object) (,frame frame-object) (,what :int) (,arg object))
,@body))
(defpyfun "PyEval_SetProfile" :void ((func .tracefunc) (obj object)))
(defpyfun "PyEval_SetTrace" :void ((func .tracefunc) (obj object)))
(defpyfun "PyEval_GetCallStats" object ((self object)) (:requires :python-call-profile))
(defconstant +pcall-all+ 0) ; Are these really necessary, or should
(defconstant +pcall-function+ 1) ; we instead split out the tuple into
(defconstant +pcall-fast-function+ 2) ; something like an alist or a
(defconstant +pcall-faster-function+ 3) ; call-stats struct?
(defconstant +pcall-method+ 4)
(defconstant +pcall-bound-method+ 5)
(defconstant +pcall-cfunction+ 6)
(defconstant +pcall-type+ 7)
(defconstant +pcall-generator+ 8)
(defconstant +pcall-other+ 9)
(defconstant +pcall-pop+ 10)
;;; Advanced Debugger Support
(defpyfun "PyInterpreterState_Head" interpreter-state ())
(defpyfun "PyInterpreterState_Next" interpreter-state ((interp interpreter-state)))
(defpyfun "PyInterpreterState_ThreadHead" thread-state ((interp interpreter-state)))
(defpyfun "PyThreadState_Next" thread-state ((tstate thread-state)))
;;;; High-level Interpreter Functions
(in-python-docs "/c-api/veryhigh.html")
(defcstruct compiler-flags (flags :int))
#+requires-CHAR*-ARRAY-support (defpyfun "Py_Main" :int ((argc :int) (argv (:array :string)))) ;canerr
;; NOTE: To support FILE*, we'll need to either get the FILE* for an open stream
;; (if the lisp provides access to it), or get the FD and call fdopen(fd,
;; mode). If the Lisp doesn't provide access to the FILE pointer or
;; file-descriptor, then we're out of luck. It does, however, appear that
;; fdopen and the other C f* functions are available for use via
;; cffi:defcfun without us having to create wrappers, though.
#+requires-FILE*-support (defpyfun "PyRun_AnyFile" 0-on-success/no-fetch ((fp :file) (filename :string)))
#+requires-FILE*-support (defpyfun "PyRun_AnyFileFlags" 0-on-success/no-fetch ((fp :file) (filename :string) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_AnyFileEx" 0-on-success/no-fetch ((fp :file) (filename :string) (closeit :int)))
#+requires-FILE*-support (defpyfun "PyRun_AnyFileExFlags" 0-on-success/no-fetch ((fp :file) (filename :string) (closeit :int) (flags compiler-flags)))
(defpyfun "PyRun_SimpleString" 0-on-success/no-fetch ((command :string)))
(defpyfun "PyRun_SimpleStringFlags" 0-on-success/no-fetch ((command :string) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_SimpleFile" 0-on-success/no-fetch ((fp :file) (filename :string)))
#+requires-FILE*-support (defpyfun "PyRun_SimpleFileFlags" 0-on-success/no-fetch ((fp :file) (filename :string) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_SimpleFileEx" 0-on-success/no-fetch ((fp :file) (filename :string) (closeit :int)))
#+requires-FILE*-support (defpyfun "PyRun_SimpleFileExFlags" 0-on-success/no-fetch ((fp :file) (filename :string) (closeit :int) (flags compiler-flags)))
;; FIXME: The Python docs leave me uncertain as to whether these are /no-fetch
#+requires-FILE*-support (defpyfun "PyRun_InteractiveOne" 0-on-success ((fp :file) (filename :string)))
#+requires-FILE*-support (defpyfun "PyRun_InteractiveOneFlags" 0-on-success ((fp :file) (filename :string) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_InteractiveLoop" 0-on-success ((fp :file) (filename :string)))
#+requires-FILE*-support (defpyfun "PyRun_InteractiveLoopFlags" 0-on-success ((fp :file) (filename :string) (flags compiler-flags)))
(defctype .node (can-error :pointer))
(defpyfun "PyParser_SimpleParseString" .node ((str :string) (start parser-context)))
(defpyfun "PyParser_SimpleParseStringFlags" .node ((str :string) (start parser-context) (flags :int)))
(defpyfun "PyParser_SimpleParseStringFlagsFilename" .node ((str :string) (filename :string) (start parser-context) (flags :int)))
#+requires-FILE*-support (defpyfun "PyParser_SimpleParseFile" .node ((fp :file) (filename :string) (start parser-context)))
#+requires-FILE*-support (defpyfun "PyParser_SimpleParseFileFlags" .node ((fp :file) (filename :string) (start parser-context) (flags :int)))
(defpyfun "PyRun_String" object! ((str :string) (start parser-context) (globals dict) (locals dict)))
(defpyfun "PyRun_StringFlags" object! ((str :string) (start parser-context) (globals dict) (locals dict) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_File" object! ((fp :file) (filename :string) (start parser-context) (globals dict) (locals dict)))
#+requires-FILE*-support (defpyfun "PyRun_FileEx" object! ((fp :file) (filename :string) (start parser-context) (globals dict) (locals dict) (closeit :int)))
#+requires-FILE*-support (defpyfun "PyRun_FileFlags" object! ((fp :file) (filename :string) (start parser-context) (globals dict) (locals dict) (flags compiler-flags)))
#+requires-FILE*-support (defpyfun "PyRun_FileExFlags" object! ((fp :file) (filename :string) (start parser-context) (globals dict) (locals dict) (closeit :int) (flags compiler-flags)))
(defpyfun "Py_CompileString" code! ((str :string) (filename :string) (start parser-context)))
(defpyfun "Py_CompileStringFlags" code! ((str :string) (filename :string) (start parser-context) (flags compiler-flags)))
(defpyfun "PyEval_EvalCode" object! ((co code) (globals dict) (locals dict)))
#+requires-POINTER-ARRAY-support (defpyfun "PyEval_EvalCodeEx" object! ((co code) (globals dict) (locals dict) (args (:array object)) (argcount :int) (kws (:array object)) (kwcount :int) (defs (:array object)) (defcount :int) (closure object)))
(defpyfun "PyEval_EvalFrame" object! ((f frame-object)))
(defpyfun "PyEval_EvalFrameEx" object! ((f frame-object) (throwflag :int)))
(defpyfun "PyEval_MergeCompilerFlags" :boolean ((cf compiler-flags)))
;;;; Memory Management
(in-python-docs "/c-api/refcounting.html")
;; These take pointers, rather than objects, because converting an object to
;; pass into these functions would be nonsensical.
(defpyfun "Py_IncRef" :void ((o :pointer)))
(defpyfun "Py_DecRef" :void ((o :pointer)))
;;;; Error Handling
(in-python-docs "/c-api/exceptions.html")
;;; Python Error API
(defctype always-error object!)
(defpyfun "PyErr_PrintEx" :void ((set-sys-last-vars :int)))
(defpyfun "PyErr_Print" :void ())
(defpyfun "PyErr_Occurred" (object :borrowed) ())
(defpyfun "PyErr_ExceptionMatches" :boolean ((exc object)))
(defpyfun "PyErr_GivenExceptionMatches" :boolean ((given object) (exc object)))
(defpyfun "PyErr_NormalizeException" :void ((exc (place object)) (val (place object)) (tb (place object))))
(defpyfun "PyErr_Clear" :void ())
(defpyfun "PyErr_Fetch" :void ((ptype (return object)) (pvalue (return object)) (ptraceback (return object))))
(defpyfun "PyErr_Restore" :void ((type object) (value object) (traceback object)))
(defpyfun "PyErr_SetString" :void ((type object) (message :string)))
(defpyfun "PyErr_SetObject" :void ((type object) (value object)))
(defpyfun "PyErr_Format" always-error ((exception object) (format :string) &rest))
(defpyfun "PyErr_SetNone" :void ((type object)))
(defpyfun "PyErr_BadArgument" :int ())
(defpyfun "PyErr_NoMemory" always-error ())
(defpyfun "PyErr_SetFromErrno" always-error ((type object)))
(defpyfun "PyErr_SetFromErrnoWithFilename" always-error ((type object) (filename :string)))
(defpyfun "PyErr_SetFromWindowsErr" always-error ((ierr :int)) (:requires :windows))
(defpyfun "PyErr_SetExcFromWindowsErr" always-error ((type object) (ierr :int)) (:requires :windows))
(defpyfun "PyErr_SetFromWindowsErrWithFilename" always-error ((ierr :int) (filename :string)) (:requires :windows))
(defpyfun "PyErr_SetExcFromWindowsErrWithFilename" always-error ((type object) (ierr :int) (filename :string)) (:requires :windows))
(defpyfun "PyErr_BadInternalCall" :void ())
(defpyfun "PyErr_WarnEx" 0-on-success ((category object) (message :string) (stacklevel :int)))
(defpyfun "PyErr_Warn" 0-on-success ((category object) (message :string)))
(defpyfun "PyErr_WarnExplicit" :int ((category object) (message :string) (filename :string) (lineno :int) (module :string) (registry object))) ; FIXME: what's the return value mean?
(defpyvar "Py_Py3kWarningFlag" *err.warn-py3k* :boolean)
(defpyvar "PyExc_DeprecationWarning")
(defpyfun "PyErr_WarnPy3k" 0-on-success ((message :string) (stacklevel :int))
(:implementation
(if *err.warn-py3k* (err.warn-ex +exc.deprecation-warning+ message stacklevel) 0)))
(defpyfun "PyErr_CheckSignals" 0-on-success ())
(defpyfun "PyErr_SetInterrupt" :void ())
(defpyfun "PySignal_SetWakeupFd" :int ((fd :int)) (:requires "Python 2.6 (or newer)")) ; FIXME: return value means...?
(defpyfun "PyErr_NewException" object ((name :string) (base object) (dict dict)))
(defpyfun "PyErr_NewExceptionWithDoc" object ((name :string) (doc :string) (base object) (dict dict))
(:implementation
(declare (ignore doc))
(err.new-exception* name base dict)))
(defpyfun "PyErr_WriteUnraisable" :void ((obj object)))
;;; Unicode Exceptions
(defpyfun "PyUnicodeDecodeError_Create" object ((encoding :string) (object :string) (length ssize-t) (start ssize-t) (end ssize-t) (reason :string)))
(defpyfun "PyUnicodeEncodeError_Create" object ((encoding :string) (object unicode) (length ssize-t) (start ssize-t) (end ssize-t) (reason :string)))
(defpyfun "PyUnicodeTranslateError_Create" object ((object unicode) (length ssize-t) (start ssize-t) (end ssize-t) (reason :string)))
(defpyfun "PyUnicodeDecodeError_GetEncoding" object ((exc object)))
(defpyfun "PyUnicodeEncodeError_GetEncoding" object ((exc object)))
(defpyfun "PyUnicodeDecodeError_GetObject" object ((exc object)))
(defpyfun "PyUnicodeEncodeError_GetObject" object ((exc object)))
(defpyfun "PyUnicodeTranslateError_GetObject" object ((exc object)))
(defpyfun "PyUnicodeDecodeError_GetStart" 0-on-success ((exc object) (start (return ssize-t))))
(defpyfun "PyUnicodeEncodeError_GetStart" 0-on-success ((exc object) (start (return ssize-t))))
(defpyfun "PyUnicodeTranslateError_GetStart" 0-on-success ((exc object) (start (return ssize-t))))
(defpyfun "PyUnicodeDecodeError_SetStart" 0-on-success ((exc object) (start ssize-t)))
(defpyfun "PyUnicodeEncodeError_SetStart" 0-on-success ((exc object) (start ssize-t)))
(defpyfun "PyUnicodeTranslateError_SetStart" 0-on-success ((exc object) (start ssize-t)))
(defpyfun "PyUnicodeDecodeError_GetEnd" 0-on-success ((exc object) (end (return ssize-t))))
(defpyfun "PyUnicodeEncodeError_GetEnd" 0-on-success ((exc object) (end (return ssize-t))))
(defpyfun "PyUnicodeTranslateError_GetEnd" 0-on-success ((exc object) (end (return ssize-t))))
(defpyfun "PyUnicodeDecodeError_SetEnd" 0-on-success ((exc object) (end ssize-t)))
(defpyfun "PyUnicodeEncodeError_SetEnd" 0-on-success ((exc object) (end ssize-t)))
(defpyfun "PyUnicodeTranslateError_SetEnd" 0-on-success ((exc object) (end ssize-t)))
(defpyfun "PyUnicodeDecodeError_GetReason" object ((exc object)))
(defpyfun "PyUnicodeEncodeError_GetReason" object ((exc object)))
(defpyfun "PyUnicodeTranslateError_GetReason" object ((exc object)))
(defpyfun "PyUnicodeDecodeError_SetReason" 0-on-success ((exc object) (reason :string)))
(defpyfun "PyUnicodeEncodeError_SetReason" 0-on-success ((exc object) (reason :string)))
(defpyfun "PyUnicodeTranslateError_SetReason" 0-on-success ((exc object) (reason :string)))
;; TODO: Probably just have a with-recursive-call macro, instead of these
;; functions. Of course, they'd still need to be defined for the
;; macro's use.
#+really-a-#define (defpyfun "Py_EnterRecursiveCall" 0-on-success ((where :string)))
#+really-a-#define (defpyfun "Py_LeaveRecursiveCall" :void ())
;;; Exception Variables
;; see ffi-conditions.lisp
;;;; TODO: Operating System Utilities, System Functions, Process Control
(in-python-docs nil)
;;;; Importing Modules
(in-python-docs "/c-api/import.html")
;; Technically, some of these may also return a top-level package instead of a
;; module. But it's all just pointers anyway.
(defctype initfunc :pointer) ; callback
(defcstruct .inittab (name :string) (initfunc initfunc))
(defpyfun "PyImport_ImportModule" module! ((name :string)))
(defpyfun "PyImport_ImportModuleNoBlock" module! ((name :string))
(:requires "Python 2.6 (or newer)"))
(defpyfun "PyImport_ImportModuleEx" module! ((name :string) (globals dict) (locals dict) (fromlist list))
(:implementation (import.import-module-level* name globals locals fromlist -1)))
(defpyfun "PyImport_ImportModuleLevel" module! ((name :string) (globals dict) (locals dict) (fromlist list) (level :int))
(:requires "Python 2.5 (or newer)"))
(defpyfun "PyImport_Import" module! ((name object)))
(defpyfun "PyImport_ReloadModule" module! ((m module)))
(defpyfun "PyImport_AddModule" (module! :borrowed) ((name :string)))
(defpyfun "PyImport_ExecCodeModule" module! ((name :string) (co code)))
(defpyfun "PyImport_ExecCodeModuleEx" module! ((name :string) (co code) (pathname :string)))
(defpyfun "PyImport_GetMagicNumber" :long ())
(defpyfun "PyImport_GetModuleDict" (dict! :borrowed) ())
(defpyfun "PyImport_GetImporter" object! ((path object))) ; Returns an "importer object". Whatever that is.
(defpyfun "PyImport_ImportFrozenModule" boolean! ((name :string)))
(defpyfun "PyImport_AppendInittab" 0-on-success ((name :string) (callback initfunc)))
(defpyfun "PyImport_ExtendInittab" 0-on-success ((newtab .inittab)))
;;;; TODO: Data Marshalling Support
(in-python-docs nil)
;;;; Passing Arguments and Building Values
(in-python-docs "/c-api/arg.html")
;;; NOTE: CFFI's type translation facilities mean we probably don't need any of
;;; this. (Users of this library, on the other hand, might.)
(defpyfun "PyArg_ParseTuple" 0-on-failure ((args tuple) (format :string) &rest))
#+requires-va_list-support (defpyfun "PyArg_VaParse" 0-on-failure ((args tuple) (format :string) (vargs va_list)))
#+requires-CHAR*-ARRAY-support (defpyfun "PyArg_ParseTupleAndKeywords" 0-on-failure ((args tuple) (kw dict) (format :string) (keywords (:array :string)) &rest))
#+requires-va_list-support/CHAR*-ARRAY-support (defpyfun "PyArg_VaParseTupleAndKeywords" 0-on-failure ((args tuple) (kw dict) (format :string) (keywords (:array :string)) (vargs va_list)))
;(defpyfun "PyArg_Parse" :int ((args object) (format :string) &rest)) ; deprecated?
(defpyfun "PyArg_UnpackTuple" 0-on-failure ((args tuple) (name :string) (min ssize-t) (max ssize-t) &rest))
(defpyfun "Py_BuildValue" object! ((format :string) &rest))
#+requires-va_list-support (defpyfun "Py_VaBuildValue" object! ((format :string) (vargs va_list)))
;;;; TODO: String Conversion and Formatting
(in-python-docs nil)
;;;; Reflection
(in-python-docs "/c-api/reflection.html")
(defpyfun "PyEval_GetBuiltins" (object! :borrowed) ())
(defpyfun "PyEval_GetLocals" (object! :borrowed) ())
(defpyfun "PyEval_GetGlobals" (object! :borrowed) ())
(defpyfun "PyEval_GetFrame" frame-object ())
;(defpyfun "PyFrame_GetLineNumber" :int ((frame frame-object)))
(defpyfun "PyEval_GetRestricted" :boolean ())
(defpyfun "PyEval_GetFuncName" :string ((func object)))
(defpyfun "PyEval_GetFuncDesc" :string ((func object)))
;;;; TODO: Codec Registry and Support Functions, Codec Lookup API, Registry API for Unicode Errors
(in-python-docs nil)
;;;; Abstract Objects
;;; Object Protocol
(in-python-docs "/c-api/object.html")
#+requires-FILE*-support (defpyfun "PyObject_Print" :int ((o object) (fp :file) (flags :int)))
(defpyfun "PyObject_HasAttr" :boolean ((o object) (attr-name object)))
(defpyfun "PyObject_HasAttrString" :boolean ((o object) (attr-name :string)))
(defpyfun "PyObject_GetAttr" object! ((o object) (attr-name object)))
(defpyfun "PyObject_GetAttrString" object! ((o object) (attr-name :string)))
(defpyfun "PyObject_GenericGetAttr" object! ((o object) (name object))) ; FIXME: docs don't specify canerr or new/borrowed
(defpyfun "PyObject_SetAttr" 0-on-success ((o object) (attr-name object) (v object)))
(defpyfun "PyObject_SetAttrString" 0-on-success ((o object) (attr-name :string) (v object)))
(defpyfun "PyObject_GenericSetAttr" 0-on-success ((o object) (name object) (value object)))
(defpyfun "PyObject_DelAttr" 0-on-success ((o object) (attr-name object))
(:implementation (object.set-attr o attr-name (null-pointer))))
(defpyfun "PyObject_DelAttrString" 0-on-success ((o object) (attr-name :string))
(:implementation (object.set-attr-string o attr-name (null-pointer))))
(defpyfun "PyObject_RichCompare" object! ((o1 object) (o2 object) (opid :int)))
(defpyfun "PyObject_RichCompareBool" boolean! ((o1 object) (o2 object) (opid :int)))
(defpyfun "PyObject_Cmp" 0-on-success ((o1 object) (o2 object) (result (return :int))))
(defpyfun "PyObject_Compare" boolean! ((o1 object) (o2 object)))
(defpyfun "PyObject_Repr" object! ((o object)))
(defpyfun "PyObject_Str" object! ((o object)))
(defpyfun "PyObject_Bytes" object! ((o object))
(:implementation (object.str* o)))
(defpyfun "PyObject_Unicode" object! ((o object)))
(defpyfun "PyObject_IsInstance" boolean! ((inst object) (cls object)))
(defpyfun "PyObject_IsSubclass" boolean! ((derived object) (cls object)))
(defpyfun "PyCallable_Check" :boolean ((o object)))
(defpyfun "PyObject_Call" object! ((callable-object object) (args tuple) (kw dict)))
(defpyfun "PyObject_CallObject" object! ((callable-object object) (args tuple)))
;; FIXME: how do we get conversion for &rest arguments?
(defpyfun "PyObject_CallFunction" object! ((callable object) (format :string) &rest))
(defpyfun "PyObject_CallMethod" object! ((o object) (method :string) (format :string) &rest))
#+requires-arg-after-&rest (defpyfun "PyObject_CallFunctionObjArgs" object! ((callable object) &rest #+(or) <NULL>))
#+requires-arg-after-&rest (defpyfun "PyObject_CallMethodObjArgs" object! ((o object) (name object) &rest #+(or) <NULL>))
(defpyfun "PyObject_Hash" (can-error :long) ((o object)))
(defpyfun "PyObject_HashNotImplemented" (can-error :long) ((o object)))
(defpyfun "PyObject_IsTrue" boolean! ((o object)))
(defpyfun "PyObject_Not" boolean! ((o object)))
(defpyfun "PyObject_Type" object! ((o object)))
(defpyfun "PyObject_TypeCheck" :boolean ((o object) (type type))
(:implementation (if (and (not (null-pointer-p o))
(or (%object.type-check-exact o type)
(type.is-subtype (%object.type* o) type)))
1 0)))
(defpyfun "PyObject_Length" ssize-t! ((o object)))
(defpyfun "PyObject_Size" ssize-t! ((o object)))
(defpyfun "PyObject_GetItem" object! ((o object) (key object)))
(defpyfun "PyObject_SetItem" 0-on-success ((o object) (key object) (v object)))
(defpyfun "PyObject_DelItem" 0-on-success ((o object) (key object)))
(defpyfun "PyObject_DelItemString" 0-on-success ((o object) (key :string)))
(defpyfun "PyObject_AsFileDescriptor" (can-error :int) ((o object)))
(defpyfun "PyObject_Dir" list! ((o object)))
(defpyfun "PyObject_GetIter" object! ((o object)))
;;; Number Protocol
(in-python-docs "/c-api/number.html")
(defpyfun "PyNumber_Check" :boolean ((o object)))
(defpyfun "PyNumber_Add" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Subtract" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Multiply" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Divide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_FloorDivide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_TrueDivide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Remainder" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Divmod" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Power" object! ((o1 object) (o2 object) (o3 object))) ; o3 optional; but must pass Py_None, not NULL
(defpyfun "PyNumber_Negative" object! ((o object)))
(defpyfun "PyNumber_Positive" object! ((o object)))
(defpyfun "PyNumber_Absolute" object! ((o object)))
(defpyfun "PyNumber_Invert" object! ((o object)))
(defpyfun "PyNumber_Lshift" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Rshift" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_And" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Xor" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Or" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceAdd" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceSubtract" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceMultiply" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceDivide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceFloorDivide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceTrueDivide" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceRemainder" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlacePower" object! ((o1 object) (o2 object) (o3 object))) ; o3 optional; pass Py_None not NULL
(defpyfun "PyNumber_InPlaceLshift" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceRshift" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceAnd" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceXor" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_InPlaceOr" object! ((o1 object) (o2 object)))
(defpyfun "PyNumber_Coerce" 0-on-success ((p1 (place object)) (p2 (place object))))
(defpyfun "PyNumber_CoerceEx" 0-on-success ((p1 (place object)) (p2 (place object))))
(defpyfun "PyNumber_Int" object! ((o object)))
(defpyfun "PyNumber_Long" object! ((o object)))
(defpyfun "PyNumber_Float" object! ((o object)))
(defpyfun "PyNumber_Index" object! ((o object)))
(defpyfun "PyNumber_ToBase" object! ((n object) (base :int)))
(defpyfun "PyNumber_AsSsize_t" ssize-t ((o object) (exc object)))
(defpyfun "PyIndex_Check" :boolean ((o object))
(:implementation (if (number.check o) 1 0)))
;;; Sequence Protocol
(in-python-docs "/c-api/sequence.html")
(defpyfun ("PySequence_Check" %sequence.check) :boolean ((o object)))
(defun sequence.check (o)
;; FIXME: rather than doing this horribly awkward read-time eval thing, push
;; some sort of wrapping facility into defpyfun? Though we'd only use it for
;; this and the below byte-array.as-string, so maybe not worth the extra
;; complication in defpyfun. (But...maybe worth its own defpyfun-wrapper?)
#.(with-unique-names (o)
(cffi::translate-objects (cl:list o) '(o)
'(object) :void
`(and (not (null-pointer-p ,o))
(%sequence.check ,o)))))
(eval-when (:compile-toplevel :load-toplevel :execute)
(export 'sequence.check))
(defpyfun "PySequence_Size" ssize-t! ((o object)))
(defpyfun "PySequence_Length" ssize-t! ((o object)))
(defpyfun "PySequence_Concat" object! ((o1 object) (o2 object)))
(defpyfun "PySequence_Repeat" object! ((o object) (count ssize-t)))
(defpyfun "PySequence_InPlaceConcat" object! ((o1 object) (o2 object)))
(defpyfun "PySequence_InPlaceRepeat" object! ((o object) (count ssize-t)))
(defpyfun "PySequence_GetItem" object! ((o object) (i ssize-t)))
(defpyfun "PySequence_GetSlice" object! ((o object) (i1 ssize-t) (i2 ssize-t)))
(defpyfun "PySequence_SetItem" 0-on-success ((o object) (i ssize-t) (v object)))
(defpyfun "PySequence_DelItem" 0-on-success ((o object) (i ssize-t)))
(defpyfun "PySequence_SetSlice" 0-on-success ((o object) (i1 ssize-t) (i2 ssize-t) (v object)))
(defpyfun "PySequence_DelSlice" 0-on-success ((o object) (i1 ssize-t) (i2 ssize-t)))
(defpyfun "PySequence_Count" ssize-t! ((o object) (value object)))
(defpyfun "PySequence_Contains" boolean! ((o object) (value object)))
(defpyfun "PySequence_Index" ssize-t! ((o object) (value object)))
(defpyfun "PySequence_List" object! ((o object)))
(defpyfun "PySequence_Tuple" object! ((o object)))
(defpyfun "PySequence_Fast" object! ((o object) (m :string)))
(defpyfun "PySequence_Fast_GET_ITEM" object! ((o object) (i ssize-t))
(:implementation (cond ((list.check o) (list.get-item* o i))
((tuple.check o) (tuple.get-item* o i))
(t (sequence.get-item* o i)))))
#+requires-POINTER-ARRAY-support (defpyfun "PySequence_Fast_ITEMS" (:array object) ((o object)))
(defpyfun "PySequence_ITEM" object! ((o object) (i ssize-t))
(:implementation (sequence.get-item* o i)))
(defpyfun "PySequence_Fast_GET_SIZE" ssize-t! ((o object))
(:implementation (cond ((list.check o) (list.size o))
((tuple.check o) (tuple.size o))
(t (sequence.size o)))))
;;; Mapping Protocol
(in-python-docs "/c-api/mapping.html")
(defpyfun "PyMapping_Check" :boolean ((o object)))
(defpyfun "PyMapping_Size" ssize-t! ((o object)))
(defpyfun "PyMapping_Length" ssize-t! ((o object)))
(defpyfun "PyMapping_DelItemString" 0-on-success ((o object) (key :string))
(:implementation (object.del-item-string o key)))
(defpyfun "PyMapping_DelItem" 0-on-success ((o object) (key object))
(:implementation (object.del-item o key)))
(defpyfun "PyMapping_HasKeyString" :boolean ((o object) (key :string)))
(defpyfun "PyMapping_HasKey" :boolean ((o object) (key object)))
(defpyfun "PyMapping_Keys" list! ((o object))
(:implementation (object.call-method* o "keys" (null-pointer))))
(defpyfun "PyMapping_Values" list! ((o object))
(:implementation (object.call-method* o "values" (null-pointer))))
(defpyfun "PyMapping_Items" list! ((o object))
(:implementation (object.call-method* o "items" (null-pointer))))
(defpyfun "PyMapping_GetItemString" object! ((o object) (key :string)))
(defpyfun "PyMapping_SetItemString" 0-on-success ((o object) (key :string) (v object)))
;;; Iterator Protocol
(in-python-docs "/c-api/iter.html")
(defpyfun "PyIter_Check" :boolean ((o object))
(:implementation
(if (and (not (null-pointer-p o))
(type.has-feature (%object.type* o) :have-iter)
(not (null-pointer-p (%type.iternext o))))
1 0)))
(defpyfun "PyIter_Next" object? ((o object)))
;;; Old Buffer Protocol (Skipped)
(in-python-docs nil)
;;;; Concrete Objects
;;;; Fundamental Objects
;;; Type Objects
(in-python-docs "/c-api/type.html")
(defpyfun "PyType_ClearCache" :uint ())
(defpyfun "PyType_Modified" :void ((type type)))
(defpyfun "PyType_HasFeature" :boolean ((o object) (feature type-flags))
(:implementation (logand feature (%type.flags* o))))
(defpyfun "PyType_IS_GC" :boolean ((o object))
(:implementation (if (type.has-feature o :have-gc) 1 0)))
(defpyfun "PyType_IsSubtype" :boolean ((a type) (b type)))
(defpyfun "PyType_GenericAlloc" object! ((type type) (nitems ssize-t)))
(defpyfun "PyType_GenericNew" object! ((type type) (args object) (kwds object)))
(defpyfun "PyType_Ready" 0-on-success ((type type)))
;;; The None Object
;;; (no functions)
(in-python-docs "/c-api/none.html")
;;;; Numeric Objects
;;; Plain Integer Objects
(in-python-docs "/c-api/int.html")
;; FIXME: The docs suggest (int.from-string "123ham" 10) should return 123,
;; "ham" but I get a VALUE-ERROR instead. Am /I/ supposed to point pend
;; there?
(defpyfun "PyInt_FromString" object! ((str :string) (pend (return :string)) (base :int)))
(defpyfun "PyInt_FromLong" int! ((ival :long)))
(defpyfun "PyInt_FromSsize_t" int! ((ival ssize-t)))
(defpyfun "PyInt_FromSize_t" int! ((ival size-t)))
(defpyfun "PyInt_AsLong" :long ((io object)))
(defpyfun "PyInt_AsUnsignedLongMask" :ulong ((io object)))
(defpyfun "PyInt_AsUnsignedLongLongMask" :unsigned-long-long ((io object)))
(defpyfun "PyInt_AsSsize_t" ssize-t ((io object)))
(defpyfun "PyInt_GetMax" :long ())
(defpyfun "PyInt_ClearFreeList" :int ())
;;; Boolean Objects
(in-python-docs "/c-api/bool.html")
(defpyfun "PyBool_FromLong" bool! ((v :long)))
;;; Long Integer Objects
(in-python-docs "/c-api/long.html")
(defpyfun "PyLong_FromLong" long! ((v :long)))
(defpyfun "PyLong_FromUnsignedLong" long! ((v :ulong)))
(defpyfun "PyLong_FromSsize_t" long! ((v ssize-t)))
(defpyfun "PyLong_FromSize_t" long! ((v size-t)))
(defpyfun "PyLong_FromLongLong" long! ((v :long-long)))
(defpyfun "PyLong_FromUnsignedLongLong" long! ((v :unsigned-long-long)))
(defpyfun "PyLong_FromDouble" long! ((v :double)))
(defpyfun "PyLong_FromString" long! ((str :string) (pend (return :string)) (base :int)))
(defpyfun "PyLong_FromUnicode" long! ((u unicode) (length ssize-t) (base :int)))
#+requires-VOID*-support (defpyfun "PyLong_FromVoidPtr" long! ((p (:pointer :void))))
(defpyfun "PyLong_AsLong" (soft-error :long) ((pylong object)))
(defpyfun "PyLong_AsLongAndOverflow" (soft-error :long) ((pylong object) (overflow (return :int)))
(:requires "Python 2.7 (or newer)"))
(defpyfun "PyLong_AsLongLongAndOverflow" (soft-error :long-long) ((pylong object) (overflow (return :int)))
(:requires "Python 2.7 (or newer)"))
(defpyfun "PyLong_AsSsize_t" (soft-error ssize-t) ((pylong object)))
(defpyfun "PyLong_AsUnsignedLong" (soft-error :ulong) ((pylong object)))
(defpyfun "PyLong_AsLongLong" (soft-error :long-long) ((pylong object)))
(defpyfun "PyLong_AsUnsignedLongLong" (soft-error :unsigned-long-long) ((pylong object)))
(defpyfun "PyLong_AsUnsignedLongMask" :ulong ((io object)))
(defpyfun "PyLong_AsUnsignedLongLongMask" :unsigned-long-long ((io object)))
(defpyfun "PyLong_AsDouble" (soft-error :double) ((pylong object)))
#+requires-VOID*-support (defpyfun "PyLong_AsVoidPtr" (can-error (:pointer :void)) ((pylong object)))
;;; Floating Point Objects
(in-python-docs "/c-api/float.html")
(defpyfun "PyFloat_FromString" object! ((str object) (pend (return :string))))
(defpyfun "PyFloat_FromDouble" float! ((v :double)))
(defpyfun "PyFloat_AsDouble" (soft-error :double) ((pyfloat object)))
(defpyfun "PyFloat_GetInfo" object! ())
(defpyfun "PyFloat_GetMax" :double ())
(defpyfun "PyFloat_GetMin" :double ())
(defpyfun "PyFloat_ClearFreeList" :int ())
;;; Complex Number Objects
(in-python-docs "/c-api/complex.html")
#+requires-FSBV-support (defpyfun "_Py_c_sum" %complex ((left %complex) (right %complex)))
#+requires-FSBV-support (defpyfun "_Py_c_diff" %complex ((left %complex) (right %complex)))
#+requires-FSBV-support (defpyfun "_Py_c_neg" %complex ((complex %complex)))
#+requires-FSBV-support (defpyfun "_Py_c_prod" %complex ((left %complex) (right %complex)))
#+requires-FSBV-support (defpyfun "_Py_c_quot" %complex ((dividend %complex) (divisor %complex)))
#+requires-FSBV-support (defpyfun "_Py_c_pow" %complex ((num %complex) (exp %complex)))
#+requires-FSBV-support (defpyfun "PyComplex_FromCComplex" object! ((v %complex)))
(defpyfun "PyComplex_FromDoubles" complex! ((real :double) (imag :double)))
(defpyfun "PyComplex_RealAsDouble" (soft-error :double) ((op object)))
(defpyfun "PyComplex_ImagAsDouble" (soft-error :double) ((op object)))
#+requires-FSBV-support (defpyfun "PyComplex_AsCComplex" (soft-error %complex!) ((op object)))
;;;; Sequence Objects
;;; Byte Array Objects
(in-python-docs "/c-api/bytearray.html")
(defpyfun "PyByteArray_FromObject" byte-array! ((o object)))
(defpyfun "PyByteArray_FromStringAndSize" byte-array! ((string octet-array) (len ssize-t)))
(defpyfun "PyByteArray_Concat" byte-array! ((a object) (b object)))
(defpyfun "PyByteArray_Size" ssize-t ((bytearray object)))
(defpyfun ("PyByteArray_AsString" %byte-array.as-string) octet-array ((bytearray byte-array)))
(defpyfun "PyByteArray_Resize" :int ((bytearray object) (len ssize-t)))
;; We need the size to correctly convert a Python byte-array to a lisp array,
;; but can't get at it directly from within translate-from-foreign.
(defun byte-array.as-string (bytearray)
;; FIXME: if bytearray is a Lisp object, it will get translated twice, once
;; for .size and again for .as-string
(let ((*byte-array.size* (byte-array.size bytearray)))
(declare (special *byte-array.size*))
(%byte-array.as-string bytearray)))
(eval-when (:compile-toplevel :load-toplevel :execute)
(export 'byte-array.as-string))
;;; String/Bytes Objects
(in-python-docs "/c-api/string.html")
(defpyfun "PyString_FromString" string! ((v :string)))
(defpyfun "PyString_FromStringAndSize" string! ((v :string) (len ssize-t))) ; size is BYTES, not characters!
(defpyfun "PyString_FromFormat" string! ((format :string) &rest))
#+requires-va_list-support (defpyfun "PyString_FromFormatV" string! ((format :string) (vargs va_list)))
(defpyfun "PyString_Size" ssize-t! ((string object)))
(defpyfun "PyString_AsString" :string ((string object)))
#+requires-call-by-reference-support (defpyfun "PyString_AsStringAndSize" 0-on-success ((obj object) (buffer (:ref :string)) (length (:pointer ssize-t))))
#+requires-call-by-reference-support (defpyfun "PyString_Concat" :void ((string (:ref object)) (newpart object)))
#+requires-call-by-reference-support (defpyfun "PyString_ConcatAndDel" :void ((string (:ref object)) (newpart object)))
#+requires-call-by-reference-support (defpyfun "_PyString_Resize" 0-on-success ((string (:ref object)) (newsize ssize-t)))
(defpyfun "PyString_Format" string! ((format object) (args tuple)))
;; Not in Python 3
;#+requires-call-by-reference-support (defpyfun "PyString_InternInPlace" :void ((string (:ref object))))
;(defpyfun "PyString_InternFromString" string! ((v :string)))
;(defpyfun "PyString_Decode" string! ((s :string) (size ssize-t) (encoding :string) (errors :string)))
;(defpyfun "PyString_AsDecodedObject" string! ((str object) (encoding :string) (errors :string)))
;(defpyfun "PyString_Encode" string! ((s :string) (size ssize-t) (encoding :string) (errors :string)))
;(defpyfun "PyString_AsEncodedObject" string! ((str object) (encoding :string) (errors :string)))
;; TODO PyBytes are #define-aliased to PyString in Python 2
;(defpyfun "PyBytes_FromString" bytes! ((v :string)))
;(defpyfun "PyBytes_FromStringAndSize" bytes! ((v :string) (len ssize-t)))
;(defpyfun "PyBytes_FromFormat" bytes! ((format :string) &rest))
;#+requires-va_list-support (defpyfun "PyBytes_FromFormatV" bytes! ((format :string) (vargs va_list)))
;(defpyfun "PyBytes_Size" ssize-t! ((string object)))
;(defpyfun "PyBytes_AsString" :string ((string object)))
;#+requires-call-by-reference-support (defpyfun "PyBytes_AsStringAndSize" :int ((obj object) (buffer (:ref :string)) (length (:pointer ssize-t))))
;#+requires-call-by-reference-support (defpyfun "PyBytes_Concat" :void ((string (:ref object)) (newpart object)))
;#+requires-call-by-reference-support (defpyfun "PyBytes_ConcatAndDel" :void ((string (:ref object)) (newpart object)))
;#+requires-call-by-reference-support (defpyfun "_PyBytes_Resize" :int ((string (:ref object)) (newsize ssize-t)))
;(defpyfun "PyBytes_Format" object! ((format object) (args tuple)))
;;; Unicode Objects
(in-python-docs "/c-api/unicode.html")
(defpyfun* unicode.clear-free-list
(("PyUnicodeUCS2_ClearFreelist" :int ())
("PyUnicodeUCS4_ClearFreelist" :int ()))
(:requires "Python 2.6 (or newer)"))
;; Py_UNICODE_IS* and Py_UNICODE_TO* macros are left out to avoid trying to
;; figure out which actual C function is in use. If you actually want these
;; things, you'll have to call into Python or something.
(defpyfun* unicode.from-unicode
(("PyUnicodeUCS2_FromUnicode" unicode! ((u ucs2-string) (size ssize-t)))
("PyUnicodeUCS4_FromUnicode" unicode! ((u ucs4-string) (size ssize-t)))))
(defpyfun* unicode.from-string-and-size
(("PyUnicodeUCS2_FromStringAndSize" unicode! ((u utf8-string) (size ssize-t)))
("PyUnicodeUCS4_FromStringAndSize" unicode! ((u utf8-string) (size ssize-t))))
(:requires "Python 2.6 (or newer)"))
(defpyfun* unicode.from-string
(("PyUnicodeUCS2_FromString" unicode! ((u utf8-string)))
("PyUnicodeUCS4_FromString" unicode! ((u utf8-string))))
(:requires "Python 2.6 (or newer)"))
(defpyfun* unicode.from-format
(("PyUnicodeUCS2_FromFormat" unicode! ((format :string) &rest))
("PyUnicodeUCS4_FromFormat" unicode! ((format :string) &rest)))
(:requires "Python 2.6 (or newer)"))
#+requires-va_list-support
(defpyfun* unicode.from-format-v
(("PyUnicodeUCS2_FromFormatV" unicode! ((format :string) (vargs va_list)))
("PyUnicodeUCS4_FromFormatV" unicode! ((format :string) (vargs va_list))))
(:requires "Python 2.6 (or newer)"))
(defpyfun* unicode.as-unicode ; WARNING! Do NOT free returned buffer!
(("PyUnicodeUCS2_AsUnicode" (can-error ucs2-string) ((unicode object)))
("PyUnicodeUCS4_AsUnicode" (can-error ucs4-string) ((unicode object)))))
(defpyfun* unicode.get-size
(("PyUnicodeUCS2_GetSize" ssize-t! ((unicode object)))
("PyUnicodeUCS4_GetSize" ssize-t! ((unicode object)))))
(defpyfun* unicode.from-encoded-object
(("PyUnicodeUCS2_FromEncodedObject" unicode! ((obj object) (encoding :string) (errors :string)))
("PyUnicodeUCS4_FromEncodedObject" unicode! ((obj object) (encoding :string) (errors :string)))))
(defpyfun* unicode.from-object
(("PyUnicodeUCS2_FromObject" unicode! ((obj object)))
("PyUnicodeUCS4_FromObject" unicode! ((obj object)))))
#+requires-wchar_t-support
(defpyfun* unicode.from-wide-char
(("PyUnicodeUCS2_FromWideChar" unicode! ((w wchar-string) (size ssize-t)))
("PyUnicodeUCS4_FromWideChar" unicode! ((w wchar-string) (size ssize-t)))))
#+requires-wchar_t-support
(defpyfun* unicode.as-wide-char
(("PyUnicodeUCS2_AsWideChar" ssize-t! ((unicode unicode) (w wchar-string) (size ssize-t)))
("PyUnicodeUCS4_AsWideChar" ssize-t! ((unicode unicode) (w wchar-string) (size ssize-t)))))
;;; Unicode Codecs
;; WARNING! decode size parameters for the codec functions are bytes, not characters!
(defpyfun* unicode.decode
(("PyUnicodeUCS2_Decode" unicode! ((s :string) (size ssize-t) (encoding :string) (errors :string)))
("PyUnicodeUCS4_Decode" unicode! ((s :string) (size ssize-t) (encoding :string) (errors :string)))))
(defpyfun* unicode.encode
(("PyUnicodeUCS2_Encode" string! ((s ucs2-string) (size ssize-t) (encoding :string) (errors :string)))
("PyUnicodeUCS4_Encode" string! ((s ucs4-string) (size ssize-t) (encoding :string) (errors :string)))))
(defpyfun* unicode.as-encoded-string
(("PyUnicodeUCS2_AsEncodedString" string! ((unicode unicode) (encoding :string) (errors :string)))
("PyUnicodeUCS4_AsEncodedString" string! ((unicode unicode) (encoding :string) (errors :string)))))
;; UTF-8 Codec
(defpyfun* unicode.decode-utf8
(("PyUnicodeUCS2_DecodeUTF8" unicode! ((s utf8-string) (size ssize-t) (errors :string)))
("PyUnicodeUCS4_DecodeUTF8" unicode! ((s utf8-string) (size ssize-t) (errors :string)))))
(defpyfun* unicode.decode-utf8-stateful
(("PyUnicodeUCS2_DecodeUTF8Stateful" unicode! ((s :string) (size ssize-t) (errors :string) (consumed (return ssize-t))))
("PyUnicodeUCS4_DecodeUTF8Stateful" unicode! ((s :string) (size ssize-t) (errors :string) (consumed (return ssize-t))))))
(defpyfun* unicode.encode-utf8
(("PyUnicodeUCS2_EncodeUTF8" string! ((s ucs2-string) (size ssize-t) (errors :string)))
("PyUnicodeUCS4_EncodeUTF8" string! ((s ucs4-string) (size ssize-t) (errors :string)))))
(defpyfun* unicode.as-utf8-string
(("PyUnicodeUCS2_AsUTF8String" string! ((unicode unicode)))
("PyUnicodeUCS4_AsUTF8String" string! ((unicode unicode)))))
;; UTF-32 Codec
#+requires-call-by-reference-support
(defpyfun* unicode.decode-utf32
(("PyUnicodeUCS2_DecodeUTF32" unicode! ((s ucs4-string) (size ssize-t) (errors :string) (byteorder (:ref :int))))
("PyUnicodeUCS4_DecodeUTF32" unicode! ((s ucs4-string) (size ssize-t) (errors :string) (byteorder (:ref :int)))))
(:requires "Python 2.6 (or newer)"))
#+requires-call-by-reference-support
(defpyfun* unicode.decode-utf32-stateful
(("PyUnicodeUCS2_DecodeUTF32Stateful" unicode! ((s ucs4-string) (size ssize-t) (errors :string) (byteorder (:ref :int)) (consumed (:ref ssize-t))))