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 define-python-class
9 #:use-module
(language python exceptions
)
10 #:use-module
((language python module string
) #:select
())
11 #:use-module
(language python def
)
12 #:use-module
(language python for
)
13 #:use-module
(language python try
)
14 #:use-module
(language python yield
)
15 #:use-module
(language python list
)
16 #:use-module
(language python dict
)
17 #:use-module
(language python set
)
18 #:use-module
(language python compile
)
19 #:use-module
(language python string
)
20 #:use-module
(language python bytes
)
21 #:use-module
(language python set
)
22 #:use-module
(language python number
)
23 #:use-module
(language python dir
)
24 #:use-module
(language python hash
)
25 #:use-module
(language python property
)
26 #:use-module
(language python range
)
27 #:use-module
(language python tuple
)
28 #:use-module
(language python eval
)
29 #:use-module
(language python bool
)
31 #:replace
(list abs min max hash round format
)
33 #:re-export
(StopIteration GeneratorExit RuntimeError
34 Exception ValueError TypeError
35 IndexError KeyError AttributeError
36 send sendException next
37 GeneratorExit sendClose RuntimeError
39 len dir next dict None property range
40 tuple bytes bytearray eval locals globals
41 compile exec type object
44 #:export
(print repr complex float int str
45 set all any bin callable reversed
46 chr classmethod staticmethod objectmethod
47 divmod enumerate filter open
48 getattr hasattr setattr hex isinstance issubclass
49 iter map sum id input oct ord pow super
51 ClassMethod StaticMethod Funcobj
))
53 (define-syntax-rule (aif it p x y
) (let ((it p
)) (if it x y
)))
55 (define (path-it path
)
56 (aif it
(ref path
'__fspath__
)
62 (() ((@ (guile) format
) #t
"~%"))
63 ((x) ((@ (guile) format
) #t
"~s~%" x
))
64 (l ((@ (guile) format
) #t
"~s~%" l
))))
66 (define (repr x
) ((@ (guile) format
) #f
"~a" x
))
69 (define complex py-complex
)
70 (define float py-float
)
72 (define round py-round
)
77 (define divmod py-divmod
)
78 (define format py-format
)
82 (define-method (callable x
) #f
)
83 (define-method (callable (x <procedure
> )) #t
)
84 (define-method (callable (x <procedure-class
> )) #t
)
85 (define-method (callable (x <applicable
> )) #t
)
86 (define-method (callable (x <primitive-generic
>)) #t
)
87 (define-method (callable (x <p
>))
90 (define chr integer-
>char
)
92 (define objectmethod object-method
)
93 (define classmethod class-method
)
94 (define staticmethod static-method
)
99 (for ((x : l
)) ((i 0))
110 (define miss
((@ (guile) list
) 'miss
))
112 (define* (getattr a b
#:optional
(k miss
))
113 (let ((r (ref a
(if (string? b
) (string->symbol b
) b
) k
)))
115 (raise AttributeError
"object/class ~a is missing attribute ~a" a b
)
118 (define (setattr a k v
)
119 (set a
(if (string? k
) (string->symbol k
) k
) v
))
121 (define (hasattr a b
)
122 (let ((r (ref a
(symbol->string b
) miss
)))
125 (define-method (issubclass x y
) #f
)
126 (define-method (issubclass (sub <p
>) (cls <p
>))
127 (aif it
(ref cls
'__subclasscheck__
)
131 (is-a?
(ref sub
'__goops__
) (ref cls
'__goops__
)))))
133 (define-method (isinstance x y
)
137 (or (isinstance x
(car y
))
138 (isinstance x
(cdr y
)))
141 (define-method (isinstance (i <integer
>) y
)
142 (if (issubclass y int
)
145 (or (isinstance i
(car y
))
146 (isinstance i
(cdr y
)))
149 (define-method (isinstance (i <real
>) y
)
150 (if (issubclass y float
)
153 (or (isinstance i
(car y
))
154 (isinstance i
(cdr y
)))
157 (define-method (isinstance (i <pair
>) y
)
158 (if (issubclass y tuple
)
161 (or (isinstance i
(car y
))
162 (isinstance i
(cdr y
)))
165 (define-method (isinstance (i <string
>) y
)
166 (if (issubclass y str
)
169 (or (isinstance i
(car y
))
170 (isinstance i
(cdr y
)))
173 (define-method (isinstance (i <bytevector
>) y
)
174 (if (issubclass y bytes
)
177 (or (isinstance i
(car y
))
178 (isinstance i
(cdr y
)))
182 (define-method (isinstance o
(cl <p
>))
188 (define-method (isinstance (o <p
>) (cl <p
>))
190 ((ref cl
'__instancecheck__
) =>
195 (isinstance o
(car cl
))
196 (isinstance o
(cdr cl
))))
198 (is-a? o
(ref cl
'__goops__
)))))
204 ((o) (aif it
(wrap-in o
)
206 (aif get
(ref o
'__getitem__
)
212 (raise TypeError
"not iterable" o
))))
228 (with-syntax (((x ...
) (generate-temporaries #'(a ...
))))
229 #'(for ((x : a
) ...
) ((l '()))
231 #:final
(py-list (reverse l
))))))))
233 (define* (sum i
#:optional
(start 0))
234 (for ((x : i
)) ((s start
))
240 (define (id x
) (object-address x
))
243 ((@ (guile) format
) #t str
)
248 (def (py-min (* l
) (= key idx
) (= default miss
))
252 (for ((x : it
)) ((s miss
) (b miss
))
261 (if (eq? default miss
)
263 "min does not work for zero length list")
266 (_ (lp ((@ (guile) list
) l
))))))
268 (def (py-max (* l
) (= key idx
) (= default miss
))
272 (for ((x : it
)) ((s miss
) (b miss
))
281 (if (eq? default miss
)
283 "min does not work for zero length list")
286 (_ (lp ((@ (guile) list
) l
))))))
288 (define (oct x
) (+ "0o" (number->string
(py-index x
) 8)))
289 (define (ord x
) (char->integer
(string-ref (pylist-ref x
0) 0)))
296 (py-mod (expt x y
) z
))))
298 (define-syntax-rule (super . l
) (py-super-mac . l
))
303 (define reversed py-reversed
)
304 (define (key-id x
) x
)
305 (define* (sorted it
#:key
(key key-id
) (reverse #f
))
306 (define l
(to-pylist '()))
307 (for ((x : it
)) () (pylist-append! l x
))
308 (pylist-sort! l
#:key key
#:reverse reverse
)
312 (let ((l ((@ (guile) map
) wrap-in l
)))
316 (let lp2
((l l
) (r '()))
318 (call-with-values (lambda () (next (car l
)))
320 (lp2 (cdr l
) (append (reverse z
) r
))))
325 (define DEFAULT_BUFFER_SIZE
4096)
335 (define modelist
(string->list mode
))
336 (define path
(path-it path
))
338 (filter (lambda (c) (not (eq? ch c
))) l
))
339 (let ((port (if (number? path
)
341 (if (member #\a modelist
)
342 (seek path
0 SEEK_END
))
343 (if (member #\x modelist
)
344 (error "cannot use mode 'x' for fd input"))
346 ((member #\r modelist
)
348 ((member #\w modelist
)
349 (fdes->outport path
))))
351 (if (member #\x modelist
)
352 (if (file-exists? path
)
353 (raise OSError
"mode='x' and file exists")
354 (set mode
(list->string
355 (clean #\x modelist
)))))
356 ((@ (guile) open-file
) (path-it path
) mode
)))))
360 (setvbuf port
'block DEFAULT_BUFFER_SIZE
))
362 (setvbuf port
'none
))
364 (setvbuf port
'line
))
366 (setvbuf port
'block buffering
)))
371 (define-python-class ClassMethod
())
372 (define-python-class StaticMethod
())
373 (define-python-class Funcobj
())