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 '[ _ with-destructors ] unit-test ; inline
13 { t } [ getpid py> integer? ] py-test
15 ! Automatic tuple unpacking
16 [ "hello.doc" ] [ "/some/path/hello.doc" >py basename py> ] py-test
18 [ { "hello" ".doc" } ] [
19 "hello.doc" >py splitext 2array [ py> ] s:map
22 [ ] [ 0 >py sleep ] py-test
24 ! Module variables are bound as zero-arg functions
25 [ t ] [ $path py> s:sequence? ] py-test
27 [ t ] [ $path len int py> 5 > ] py-test
29 [ 10 ] [ 10 >py range len py> ] py-test
33 "os" py-import "getpid" getattr
34 [ callable ] [ PyCallable_Check 1 = ] bi and
38 [ 1 ] [ 3 <py-tuple> getrefcount py> ] py-test
41 H{ { "foo" 33 } { "bar" 44 } } >py
42 [ "foo" py-dict-get-item-string getrefcount py> ]
45 500 [ _ "foo" py-dict-get-item-string drop ] times
48 [ "foo" py-dict-get-item-string getrefcount py> ] tri -
52 "abcd" >py <1py-tuple>
53 [ 0 py-tuple-get-item getrefcount py> ]
55 [ 100 [ swap 0 py-tuple-get-item drop ] with times ] with-destructors
57 [ 0 py-tuple-get-item getrefcount py> ] tri -
62 [ getrefcount py> 1 - ]
63 [ always-destructors get [ alien>> = ] with s:count ] bi =
67 "python-file" temp-file >py "wb" >py open
68 [ tell ] [ fileno ] [ close ] tri
69 [ py> integer? ] both?
74 "hello there" >py title 20 >py zfill "00" >py startswith py>
77 [ { "hello" "=" "there" } ] [
78 "hello=there" >py "=" >py partition 3array [ py> ] s:map
83 func_code ( func -- code ) ;
86 co_argcount ( code -- n ) ;
88 [ 1 ] [ $splitext $func_code $co_argcount py> ] py-test
92 $path "test" >py [ append ] [ drop py> ] [ remove ] 2tri
97 [ "datetime.timedelta(4, 10800)" ] [
98 H{ { "hours" 99 } } >py timedelta repr py>
105 "--foo" >py H{ { "help" "badger" } } >py add_argument
107 ] with-destructors [ blank? ] s:trim " " split "badger" swap in?
111 [ 987 >py basename ] [ traceback>> ] recover s:length 0 >
114 ! Test if exceptions leak references. If so, the test will leak a few
115 ! hundred megs of memory. Enough to be noticed but not to slow down
116 ! the tests too much.
119 [ [ 987 >py basename drop ] ignore-errors ] with-destructors
126 [ { 9 8 7 6 5 4 3 2 1 } >py ] with-destructors drop
131 PY-QUALIFIED-FROM: types => UnicodeType ( -- ) ;
134 types:$UnicodeType $__name__ py>
138 PY-QUALIFIED-FROM: __builtin__ =>
140 map ( func seq -- seq' )
141 reduce ( func seq -- seq' ) ;
144 __builtin__:$None { 1 2 3 } >py __builtin__:map py>
147 : double-fun ( -- alien )
148 [ drop s:first 2 * ] quot>py-callback ;
150 { V{ 2 4 16 2 4 68 } } [
152 { 1 2 8 1 2 34 } >py __builtin__:map py>
153 ] with-quot>py-cfunction
156 : reduce-func ( -- alien )
157 [ drop s:first2 + ] quot>py-callback ;
161 { 1 2 8 1 2 34 } >py __builtin__:reduce py>
162 ] with-quot>py-cfunction