1 USING: accessors arrays assocs continuations destructors destructors.private
2 fry io.files.temp kernel math namespaces python python.ffi
3 python.modules.__builtin__ python.modules.argparse python.modules.datetime
4 python.modules.os python.modules.os.path python.modules.sys
5 python.modules.time python.objects python.syntax sets splitting tools.test
7 QUALIFIED-WITH: sequences s
8 IN: python.syntax.tests
10 : py-test ( result quot -- )
11 '[ python-dll-loaded? [ _ [ _ with-destructors ] unit-test ] when ] call ; inline
13 : long-py-test ( result quot -- )
14 '[ python-dll-loaded? [ _ [ _ with-destructors ] long-unit-test ] when ] call ; inline
16 { t } [ getpid py> integer? ] py-test
18 ! Automatic tuple unpacking
19 [ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] py-test
21 [ { "hello" ".doc" } ] [
22 "hello.doc" >py splitext 2array [ py> ] s:map
25 [ ] [ 0 >py sleep ] py-test
27 ! Module variables are bound as zero-arg functions
28 { t } [ $path py> s:sequence? ] py-test
30 { t } [ $path len int py> 5 > ] py-test
32 [ 10 ] [ 10 >py range len py> ] py-test
36 "os" py-import "getpid" getattr
37 [ callable ] [ PyCallable_Check 1 = ] bi and
41 { 1 } [ 3 <py-tuple> getrefcount py> ] py-test
44 H{ { "foo" 33 } { "bar" 44 } } >py
45 [ "foo" py-dict-get-item-string getrefcount py> ]
48 500 [ _ "foo" py-dict-get-item-string drop ] times
51 [ "foo" py-dict-get-item-string getrefcount py> ] tri -
55 "abcd" >py <1py-tuple>
56 [ 0 py-tuple-get-item getrefcount py> ]
58 [ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
60 [ 0 py-tuple-get-item getrefcount py> ] tri -
65 [ getrefcount py> 1 - ]
66 [ always-destructors get [ alien>> = ] with s:count ] bi =
70 "python-file" temp-file >py "wb" >py open
71 [ tell ] [ fileno ] [ close ] tri
72 [ py> integer? ] both?
77 "hello there" >py title 20 >py zfill "00" >py startswith py>
80 [ { "hello" "=" "there" } ] [
81 "hello=there" >py "=" >py partition 3array [ py> ] s:map
86 func_code ( func -- code ) ;
89 co_argcount ( code -- n ) ;
91 { 1 } [ $splitext $func_code $co_argcount py> ] py-test
95 $path "test" >py [ append ] [ drop py> ] [ remove ] 2tri
100 [ "datetime.timedelta(4, 10800)" ] [
101 H{ { "hours" 99 } } >py timedelta repr py>
108 "--foo" >py H{ { "help" "badger" } } >py add_argument
110 ] with-destructors [ blank? ] s:trim " " split "badger" swap in?
114 [ 987 >py basename ] [ traceback>> ] recover s:length 0 >
117 ! Test if exceptions leak references. If so, the test will leak a few
118 ! hundred megs of memory. Enough to be noticed but not to slow down
119 ! the tests too much.
122 [ [ 987 >py basename drop ] ignore-errors ] with-destructors
129 [ { 9 8 7 6 5 4 3 2 1 } >py ] with-destructors drop
134 PY-QUALIFIED-FROM: types => UnicodeType ( -- ) ;
137 types:$UnicodeType $__name__ py>
141 PY-QUALIFIED-FROM: __builtin__ =>
143 map ( func seq -- seq' )
144 reduce ( func seq -- seq' ) ;
147 __builtin__:$None { 1 2 3 } >py __builtin__:map py>
150 : double-fun ( -- alien )
151 [ drop s:first 2 * ] quot>py-callback ;
153 { V{ 2 4 16 2 4 68 } } [
155 { 1 2 8 1 2 34 } >py __builtin__:map py>
156 ] with-quot>py-cfunction
159 : reduce-func ( -- alien )
160 [ drop s:first2 + ] quot>py-callback ;
164 { 1 2 8 1 2 34 } >py __builtin__:reduce py>
165 ] with-quot>py-cfunction