1 USING: accessors alien arrays assocs calendar continuations destructors
2 destructors.private fry kernel math memory namespaces python python.errors
3 python.ffi python.objects sequences strings tools.test ;
6 : py-test ( result quot -- )
7 '[ python-dll-loaded? [ _ [ _ with-destructors ] unit-test ] when ] call ; inline
11 "builtins" "None" py-import-from <none> =
15 { 1 } [ 33 >py drop always-destructors get length ] py-test
17 { 1 } [ f >py drop always-destructors get length ] py-test
19 ! The tuple steals the reference properly now.
20 { 1 } [ 33 >py <1py-tuple> drop always-destructors get length ] py-test
22 { 1 } [ { } >py drop always-destructors get length ] py-test
24 { 1 } [ V{ 1 2 3 4 } >py drop always-destructors get length ] py-test
27 99 >py V{ 1 2 3 4 } >py 2drop always-destructors get length
31 { { { 33 } } } >py drop always-destructors get length
34 { } [ 123 <alien> unsteal-ref ] unit-test
36 { t } [ Py_GetVersion string? ] py-test
38 [ "os" ] [ "os" py-import PyModule_GetName ] py-test
41 "os" py-import "getpid" getattr
42 { } >py call-object py> 0 >
45 { t } [ Py_IsInitialized ] py-test
48 [ { "ModuleNotFoundError" "No module named 'kolobi'" f } ] [
49 [ "kolobi" py-import ] [
50 [ type>> ] [ message>> ] [ traceback>> ] tri 3array
56 "sys" py-import "testit" [ 73 >py setattr ] [ getattr py> ] 2bi
60 { 2 } [ 2 <py-tuple> py-tuple-size ] py-test
62 : py-datepy> ( py-obj -- timestamp )
63 { "year" "month" "day" } [ getattr py> ] with map
64 first3 0 0 0 instant <timestamp> ;
67 { t } [ V{ 4 8 15 16 23 42 } dup >py py> = ] py-test
71 [ py-datepy> ] "date" py-type-dispatch get set-at
72 "datetime" py-import "date" getattr "today" getattr
73 { } >py call-object py>
74 today instant >>gmt-offset =
79 "os.path" py-import "basename" getattr { "غثههح" } >py call-object py>
84 "datetime" py-import "timedelta" getattr
85 { 7 } >py call-object "days" getattr py>
89 { 0 } [ <py-dict> py-dict-size ] py-test
91 ! Dictionary with object keys
93 <py-dict> dup 0 >py 33 >py py-dict-set-item py-dict-size
96 ! Dictionary with string keys
98 <py-dict> [ "foo" 33 >py py-dict-set-item-string ] [ py-dict-size ] bi
101 ! Get dictionary items
104 [ 33 >py py-dict-set-item-string ]
105 [ py-dict-get-item-string py> ] 2bi
111 [ <py-dict> py-dict-set-item-string ]
112 [ py-dict-get-item-string ] 2bi
118 1 <py-tuple> dup 0 3 <py-tuple> py-tuple-set-item
119 0 py-tuple-get-item py-tuple-size
123 { { "foo" { 99 77 } } }
124 [ { "foo" { 99 77 } } >py py> ] py-test
126 { H{ { "foo" "bar" } { 3 4 } } } [
127 H{ { "foo" "bar" } { 3 4 } } >py py>
132 "datetime" py-import "date" getattr
133 { } >py H{ { "year" 2014 } { "month" 10 } { "day" 22 } } >py
135 [ year>> ] [ month>> ] [ day>> ] tri
140 "os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
145 1234 <alien> "foo" f <PyMethodDef>
146 ml_flags>> METH_VARARGS METH_KEYWORDS bitor =
150 1234 <alien> <py-cfunction>
151 [ "__doc__" getattr py> ] [ PyCFunction_GetFlags ] bi
155 1234 <alien> <py-cfunction>
156 ! Force nursery flush
157 10000 [ 1000 0xff <array> drop ] times
158 "__name__" getattr py>
162 1234 <alien> <py-cfunction> drop always-destructors get length
166 : py-map ( -- alien )
167 "builtins" "map" py-import-from ;
169 : py-list ( -- alien )
170 "builtins" "list" py-import-from ;
172 : py-list-call ( alien -- seq )
173 py-list swap 1array array>py-tuple f call-object-full py> ;
175 : py-map-call ( alien-cb -- seq )
177 <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
179 ] with-callback py-list-call ;
181 : always-33-fun ( -- alien )
182 [ 3drop 33 >py ] PyCallback ;
184 { V{ 33 33 } } [ always-33-fun py-map-call ] py-test
186 : id-fun ( -- alien )
187 [ drop nip py> first >py ] PyCallback ;
189 { V{ 1 2 } } [ id-fun py-map-call ] py-test