1 (define-module (language python module python
)
2 #:use-module
(oop goops
)
3 #:use-module
(ice-9 match
)
4 #:use-module
(ice-9 readline
)
5 #:use-module
((oop pf-objects
) #:select
6 (<p
> <property
> class-method static-method ref
7 py-super-mac type object pylist-ref
))
8 #:use-module
(language python exceptions
)
9 #:use-module
((language python module string
) #:select
())
10 #:use-module
(language python def
)
11 #:use-module
(language python for
)
12 #:use-module
(language python try
)
13 #:use-module
(language python yield
)
14 #:use-module
(language python list
)
15 #:use-module
(language python dict
)
16 #:use-module
(language python set
)
17 #:use-module
(language python compile
)
18 #:use-module
(language python string
)
19 #:use-module
(language python bytes
)
20 #:use-module
(language python set
)
21 #:use-module
(language python number
)
22 #:use-module
(language python dir
)
23 #:use-module
(language python hash
)
24 #:use-module
(language python property
)
25 #:use-module
(language python range
)
26 #:use-module
(language python tuple
)
27 #:use-module
(language python eval
)
29 #:replace
(list abs min max hash round format
)
31 #:re-export
(StopIteration GeneratorExit RuntimeError
32 Exception ValueError TypeError
33 IndexError KeyError AttributeError
34 send sendException next
35 GeneratorExit sendClose RuntimeError
37 len dir next dict None property range
38 tuple bytes bytearray eval locals globals
39 compile exec type object
42 #:export
(print repr complex float int str
43 set all any bin callable reversed
44 chr classmethod staticmethod
45 divmod enumerate filter open
46 getattr hasattr hex isinstance issubclass
47 iter map sum id input oct ord pow super
50 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
52 (define (path-it path
)
53 (aif it
(ref path
'__fspath__
)
59 (() ((@ (guile) format
) #t
"~%"))
60 ((x) ((@ (guile) format
) #t
"~s~%" x
))
61 (l ((@ (guile) format
) #t
"~s~%" l
))))
63 (define (repr x
) ((@ (guile) format
) #f
"~a" x
))
66 (define complex py-complex
)
67 (define float py-float
)
69 (define round py-round
)
74 (define divmod py-divmod
)
75 (define format py-format
)
79 (define-method (callable x
) #f
)
80 (define-method (callable (x <procedure
> )) #t
)
81 (define-method (callable (x <procedure-class
> )) #t
)
82 (define-method (callable (x <applicable
> )) #t
)
83 (define-method (callable (x <primitive-generic
>)) #t
)
84 (define-method (callable (x <p
>))
87 (define chr integer-
>char
)
89 (define classmethod class-method
)
90 (define staticmethod static-method
)
95 (for ((x : l
)) ((i 0))
106 (define miss
((@ (guile) list
) 'miss
))
108 (define* (getattr a b
#:optional
(k miss
))
109 (let ((r (ref a
(symbol->string b
) k
)))
111 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
114 (define (hasattr a b
)
115 (let ((r (ref a
(symbol->string b
) miss
)))
118 (define-method (issubclass (sub <p
>) (cls <p
>))
119 (aif it
(ref cls
'__subclasscheck__
)
121 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
))))
123 (define-method (isinstance (o <p
>) (cl <p
>))
124 (aif it
(ref cl
'__instancecheck__
)
128 (isinstance o
(car cl
))
129 (isinstance o
(cdr cl
)))
130 (is-a? o
(ref cl
'__goops__
)))))
134 ((o) (aif it
(wrap-in o
)
136 (aif get
(ref o
'__getitem__
)
142 (raise TypeError
"not iterable" o
))))
158 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
159 #'(for ((x : a
) ...
) ((l '()))
161 #:final
(py-list (reverse l
))))))))
163 (define* (sum i
#:optional
(start 0))
164 (for ((x : i
)) ((s start
))
170 (define (id x
) (object-address x
))
178 (def (py-min (* l
) (= key idx
) (= default miss
))
182 (for ((x : it
)) ((s miss
) (b miss
))
191 (if (eq? default miss
)
193 "min does not work for zero length list")
196 (_ (lp ((@ (guile) list
) l
))))))
198 (def (py-max (* l
) (= key idx
) (= default miss
))
202 (for ((x : it
)) ((s miss
) (b miss
))
211 (if (eq? default miss
)
213 "min does not work for zero length list")
216 (_ (lp ((@ (guile) list
) l
))))))
218 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
219 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
226 (py-mod (expt x y
) z
))))
228 (define-syntax-rule (super . l
) (py-super-mac . l
))
233 (define reversed py-reversed
)
234 (define (key-id x
) x
)
235 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
236 (define l
(to-pylist '()))
237 (for ((x : it
)) () (pylist-append! l x
))
238 (pylist-sort! l
#:key key
#:reverse reverse
)
242 (let ((l ((@ (guile) map
) wrap-in l
)))
246 (let lp2
((l l
) (r '()))
248 (call-with-values (lambda () (next (car l
)))
250 (lp2 (cdr l
) (append (reverse z
) r
))))
255 (define DEFAULT_BUFFER_SIZE
4096)
265 (define modelist
(string->list mode
))
266 (define path
(path-it path
))
268 (filter (lambda (c) (not (eq? ch c
))) l
))
269 (let ((port (if (number? path
)
271 (if (member #\a modelist
)
272 (seek path
0 SEEK_END
))
273 (if (member #\x modelist
)
274 (error "cannot use mode 'x' for fd input"))
276 ((member #\r modelist
)
278 ((member #\w modelist
)
279 (fdes->outport path
))))
281 (if (member #\x modelist
)
282 (if (file-exists? path
)
283 (raise OSError
"mode='x' and file exists")
284 (set mode
(list->string
285 (clean #\x modelist
)))))
286 ((@ (guile) open-file
) (path-it path
) mode
)))))
290 (setvbuf port
'block DEFAULT_BUFFER_SIZE
))
292 (setvbuf port
'none
))
294 (setvbuf port
'line
))
296 (setvbuf port
'block buffering
)))