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 '[ _ with-destructors ] unit-test ; inline
11 "__builtin__" py-import "None" getattr <none> =
14 ! Pretty sure the # of None references should stay constant.
17 "sys" py-import "getrefcount" getattr
18 <none> <1py-tuple> call-object py>
21 "sys" py-import "getrefcount" getattr
22 <none> <1py-tuple> call-object py>
26 { } [ { f f f } >py drop ] py-test
29 { 1 } [ 33 >py drop always-destructors get length ] py-test
31 { 1 } [ f >py drop always-destructors get length ] py-test
33 ! The tuple steals the reference properly now.
34 { 1 } [ 33 >py <1py-tuple> drop always-destructors get length ] py-test
36 { 1 } [ { } >py drop always-destructors get length ] py-test
38 { 1 } [ V{ 1 2 3 4 } >py drop always-destructors get length ] py-test
41 99 >py V{ 1 2 3 4 } >py 2drop always-destructors get length
45 { { { 33 } } } >py drop always-destructors get length
48 { } [ 123 <alien> unsteal-ref ] unit-test
50 [ t ] [ Py_GetVersion string? ] unit-test
52 [ "os" ] [ "os" py-import PyModule_GetName ] py-test
55 "os" py-import "getpid" getattr
56 { } >py call-object py> 0 >
59 [ t ] [ Py_IsInitialized ] py-test
62 [ { "ImportError" "No module named kolobi" f } ] [
63 [ "kolobi" py-import ] [
64 [ type>> ] [ message>> ] [ traceback>> ] tri 3array
70 "sys" py-import "testit" [ 73 >py setattr ] [ getattr py> ] 2bi
74 [ 2 ] [ 2 <py-tuple> py-tuple-size ] py-test
76 : py-datepy> ( py-obj -- timestamp )
77 { "year" "month" "day" } [ getattr py> ] with map
78 first3 0 0 0 instant <timestamp> ;
81 [ t ] [ V{ 4 8 15 16 23 42 } dup >py py> = ] py-test
85 [ py-datepy> ] "date" py-type-dispatch get set-at
86 "datetime" py-import "date" getattr "today" getattr
87 { } >py call-object py>
88 today instant >>gmt-offset =
93 "os.path" py-import "basename" getattr { "غثههح" } >py call-object py>
98 "datetime" py-import "timedelta" getattr
99 { 7 } >py call-object "days" getattr py>
103 [ 0 ] [ <py-dict> py-dict-size ] py-test
105 ! Dictionary with object keys
107 <py-dict> dup 0 >py 33 >py py-dict-set-item py-dict-size
110 ! Dictionary with string keys
112 <py-dict> [ "foo" 33 >py py-dict-set-item-string ] [ py-dict-size ] bi
115 ! Get dictionary items
118 [ 33 >py py-dict-set-item-string ]
119 [ py-dict-get-item-string py> ] 2bi
125 [ <py-dict> py-dict-set-item-string ]
126 [ py-dict-get-item-string ] 2bi
132 1 <py-tuple> dup 0 3 <py-tuple> py-tuple-set-item
133 0 py-tuple-get-item py-tuple-size
137 [ { "foo" { 99 77 } } ] [ { "foo" { 99 77 } } >py py> ] py-test
139 [ H{ { "foo" "bar" } { 3 4 } } ] [
140 H{ { "foo" "bar" } { 3 4 } } >py py>
145 "datetime" py-import "date" getattr
146 { } >py H{ { "year" 2014 } { "month" 10 } { "day" 22 } } >py
148 [ year>> ] [ month>> ] [ day>> ] tri
153 "os" py-import PyModule_GetDict dup Py_IncRef &Py_DecRef py-dict-size 100 >
158 1234 <alien> "foo" f <PyMethodDef>
159 ml_flags>> METH_VARARGS METH_KEYWORDS bitor =
163 1234 <alien> <py-cfunction>
164 [ "__doc__" getattr py> ] [ PyCFunction_GetFlags ] bi
168 1234 <alien> <py-cfunction>
169 ! Force nursery flush
170 10000 [ 1000 0xff <array> drop ] times
171 "__name__" getattr py>
175 1234 <alien> <py-cfunction> drop always-destructors get length
179 : py-map ( -- alien )
180 "__builtin__" py-import "map" getattr ;
182 : py-map-call ( alien-cb -- seq )
184 <py-cfunction> py-map swap { 1 2 } >py 2array array>py-tuple f
186 ] with-callback py> ;
188 : always-33-fun ( -- alien )
189 [ 3drop 33 >py ] PyCallback ;
191 { V{ 33 33 } } [ always-33-fun py-map-call ] py-test
193 : id-fun ( -- alien )
194 [ drop nip py> first >py ] PyCallback ;
196 { V{ 1 2 } } [ id-fun py-map-call ] py-test