2018-02-19 11:41:46 -05:00
|
|
|
USING: alien alien.c-types alien.data alien.libraries
|
|
|
|
arrays assocs command-line fry
|
2014-07-06 13:18:27 -04:00
|
|
|
hashtables init io.encodings.utf8 kernel namespaces
|
|
|
|
python.errors python.ffi python.objects sequences
|
|
|
|
specialized-arrays strings vectors ;
|
2014-01-19 17:45:25 -05:00
|
|
|
IN: python
|
|
|
|
QUALIFIED: math
|
|
|
|
|
2014-10-02 11:19:05 -04:00
|
|
|
ERROR: python-error type message traceback ;
|
|
|
|
|
2014-02-03 05:28:43 -05:00
|
|
|
SPECIALIZED-ARRAY: void*
|
|
|
|
|
|
|
|
! Borrowed from unix.utilities
|
|
|
|
: strings>alien ( strings encoding -- array )
|
|
|
|
'[ _ malloc-string ] void*-array{ } map-as f suffix ;
|
|
|
|
|
2014-01-27 15:59:59 -05:00
|
|
|
! Initialization and finalization
|
|
|
|
: py-initialize ( -- )
|
2014-02-03 05:28:43 -05:00
|
|
|
Py_IsInitialized [
|
|
|
|
Py_Initialize
|
|
|
|
! Encoding must be 8bit on Windows I think, so
|
|
|
|
! native-string-encoding (utf16n) doesn't work.
|
|
|
|
(command-line) [ length ] [ utf8 strings>alien ] bi 0 PySys_SetArgvEx
|
|
|
|
] unless ;
|
2014-01-27 15:59:59 -05:00
|
|
|
|
|
|
|
: py-finalize ( -- )
|
|
|
|
Py_IsInitialized [ Py_Finalize ] when ;
|
|
|
|
|
2014-01-19 17:45:25 -05:00
|
|
|
! Importing
|
2014-03-04 12:39:02 -05:00
|
|
|
: py-import ( str -- module )
|
2014-01-31 09:59:04 -05:00
|
|
|
PyImport_ImportModule check-new-ref ;
|
2014-01-30 11:24:58 -05:00
|
|
|
|
2014-01-21 12:10:24 -05:00
|
|
|
! Unicodes
|
2014-01-27 15:59:59 -05:00
|
|
|
: py-ucs-size ( -- n )
|
|
|
|
"maxunicode" PySys_GetObject PyInt_AsLong 0xffff = 2 4 ? ;
|
|
|
|
|
2014-01-21 12:10:24 -05:00
|
|
|
: py-unicode>utf8 ( uni -- str )
|
2014-01-27 15:59:59 -05:00
|
|
|
py-ucs-size 4 =
|
|
|
|
[ PyUnicodeUCS4_AsUTF8String ]
|
2014-01-31 09:59:04 -05:00
|
|
|
[ PyUnicodeUCS2_AsUTF8String ] if (check-ref)
|
|
|
|
PyString_AsString (check-ref) ;
|
2014-01-21 12:10:24 -05:00
|
|
|
|
2014-01-27 15:59:59 -05:00
|
|
|
: utf8>py-unicode ( str -- uni )
|
|
|
|
py-ucs-size 4 =
|
|
|
|
[ PyUnicodeUCS4_FromString ]
|
|
|
|
[ PyUnicodeUCS2_FromString ] if ;
|
2014-01-21 12:10:24 -05:00
|
|
|
|
2014-01-19 17:45:25 -05:00
|
|
|
! Data marshalling to Python
|
2014-01-28 13:19:57 -05:00
|
|
|
: array>py-tuple ( arr -- py-tuple )
|
|
|
|
[ length <py-tuple> dup ] keep
|
|
|
|
[ rot py-tuple-set-item ] with each-index ;
|
|
|
|
|
2014-01-30 11:24:58 -05:00
|
|
|
: vector>py-list ( vec -- py-list )
|
|
|
|
[ length <py-list> dup ] keep
|
|
|
|
[ rot py-list-set-item ] with each-index ;
|
|
|
|
|
2014-01-28 18:31:43 -05:00
|
|
|
: py-tuple>array ( py-tuple -- arr )
|
2017-06-01 17:59:35 -04:00
|
|
|
dup py-tuple-size <iota> [ py-tuple-get-item ] with map ;
|
2014-01-28 18:31:43 -05:00
|
|
|
|
2014-01-30 14:46:55 -05:00
|
|
|
: py-list>vector ( py-list -- vector )
|
2017-06-01 17:59:35 -04:00
|
|
|
dup py-list-size <iota> [ py-list-get-item ] with V{ } map-as ;
|
2014-01-30 14:46:55 -05:00
|
|
|
|
2014-10-23 19:18:57 -04:00
|
|
|
DEFER: >py
|
|
|
|
|
2014-10-27 14:33:59 -04:00
|
|
|
GENERIC: >py ( obj -- py-obj )
|
2014-10-26 19:23:54 -04:00
|
|
|
M: string >py
|
|
|
|
utf8>py-unicode check-new-ref ;
|
|
|
|
M: math:fixnum >py
|
|
|
|
PyLong_FromLong check-new-ref ;
|
|
|
|
M: math:float >py
|
|
|
|
PyFloat_FromDouble check-new-ref ;
|
|
|
|
M: array >py
|
|
|
|
[ >py ] map array>py-tuple ;
|
|
|
|
M: hashtable >py
|
2014-01-19 17:45:25 -05:00
|
|
|
<py-dict> swap dupd [
|
2014-10-26 19:23:54 -04:00
|
|
|
swapd [ >py ] bi@ py-dict-set-item
|
2014-01-19 17:45:25 -05:00
|
|
|
] with assoc-each ;
|
2014-10-26 19:23:54 -04:00
|
|
|
M: vector >py
|
2014-10-23 19:18:57 -04:00
|
|
|
[ >py ] map vector>py-list ;
|
2014-10-26 19:23:54 -04:00
|
|
|
M: f >py
|
|
|
|
drop <none> ;
|
2014-01-19 17:45:25 -05:00
|
|
|
|
|
|
|
! Data marshalling to Factor
|
|
|
|
SYMBOL: py-type-dispatch
|
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
DEFER: py>
|
2014-01-19 17:45:25 -05:00
|
|
|
|
|
|
|
: init-py-type-dispatch ( -- table )
|
|
|
|
H{
|
|
|
|
{ "NoneType" [ drop f ] }
|
2014-01-27 17:49:00 -05:00
|
|
|
{ "bool" [ PyObject_IsTrue 1 = ] }
|
2014-03-04 12:39:02 -05:00
|
|
|
{ "dict" [ PyDict_Items (check-ref) py> >hashtable ] }
|
2014-01-19 17:45:25 -05:00
|
|
|
{ "int" [ PyInt_AsLong ] }
|
2014-03-04 12:39:02 -05:00
|
|
|
{ "list" [ py-list>vector [ py> ] map ] }
|
2014-01-19 17:45:25 -05:00
|
|
|
{ "long" [ PyLong_AsLong ] }
|
2014-01-31 09:59:04 -05:00
|
|
|
{ "str" [ PyString_AsString (check-ref) ] }
|
2014-03-04 12:39:02 -05:00
|
|
|
{ "tuple" [ py-tuple>array [ py> ] map ] }
|
2014-01-27 15:59:59 -05:00
|
|
|
{ "unicode" [ py-unicode>utf8 ] }
|
2014-01-19 17:45:25 -05:00
|
|
|
} clone ;
|
|
|
|
|
|
|
|
py-type-dispatch [ init-py-type-dispatch ] initialize
|
|
|
|
|
|
|
|
ERROR: missing-type type ;
|
|
|
|
|
2014-03-04 12:39:02 -05:00
|
|
|
: py> ( py-obj -- obj )
|
2014-01-19 17:45:25 -05:00
|
|
|
dup "__class__" getattr "__name__" getattr PyString_AsString
|
|
|
|
py-type-dispatch get ?at [ call( x -- x ) ] [ missing-type ] if ;
|
2014-07-06 13:18:27 -04:00
|
|
|
|
2014-10-23 19:01:15 -04:00
|
|
|
! Callbacks
|
|
|
|
: quot>py-callback ( quot: ( args kw -- ret ) -- alien )
|
|
|
|
'[
|
|
|
|
[ nip ] dip
|
|
|
|
[ [ py> ] [ { } ] if* ] bi@ @ >py
|
|
|
|
] PyCallback ; inline
|
|
|
|
|
|
|
|
: with-quot>py-cfunction ( alien quot -- )
|
|
|
|
'[ <py-cfunction> @ ] with-callback ; inline
|
|
|
|
|
2018-02-19 11:41:46 -05:00
|
|
|
[ "PyIsInitialized" "python2.7" library-dll dlsym? [ py-initialize ] when ] "python" add-startup-hook
|
2014-12-24 11:10:52 -05:00
|
|
|
[ py-finalize ] "python" add-shutdown-hook
|