#:use-module (ice-9 readline)
#:use-module ((oop pf-objects) #:select
(<p> <property> class-method static-method ref
- py-super-mac type object pylist-ref))
+ py-super-mac type object pylist-ref define-python-class
+ object-method))
#:use-module (language python exceptions )
#:use-module ((language python module string ) #:select ())
+ #:use-module ((language python module io ) #:select (open))
#:use-module (language python def )
#:use-module (language python for )
#:use-module (language python try )
#:use-module (language python range )
#:use-module (language python tuple )
#:use-module (language python eval )
+ #:use-module (language python bool )
- #:replace (list abs min max hash round format)
+ #:replace (list abs min max hash round format map)
#:re-export (StopIteration GeneratorExit RuntimeError
Exception ValueError TypeError
IndexError KeyError AttributeError
send sendException next
GeneratorExit sendClose RuntimeError
- SyntaxError
+ SyntaxError bool
len dir next dict None property range
tuple bytes bytearray eval locals globals
- compile exec type object
+ compile exec type object open
)
#:export (print repr complex float int str
set all any bin callable reversed
- chr classmethod staticmethod
- divmod enumerate filter open
- getattr hasattr hex isinstance issubclass
- iter map sum id input oct ord pow super
- sorted zip))
+ chr classmethod staticmethod objectmethod
+ divmod enumerate filter
+ getattr hasattr setattr hex isinstance issubclass
+ iter sum id input oct ord pow super
+ sorted zip
+ ClassMethod StaticMethod Funcobj))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
-(define (path-it path)
- (aif it (ref path '__fspath__)
- (it)
- path))
-
(define print
(case-lambda
(() ((@ (guile) format) #t "~%"))
(define chr integer->char)
+(define objectmethod object-method)
(define classmethod class-method)
(define staticmethod static-method)
(define miss ((@ (guile) list) 'miss))
(define* (getattr a b #:optional (k miss))
- (let ((r (ref a (symbol->string b) k)))
+ (let ((r (ref a (if (string? b) (string->symbol b) b) k)))
(if (eq? r miss)
(raise AttributeError "object/class ~a is missing attribute ~a" a b)
r)))
+(define (setattr a k v)
+ (set a (if (string? k) (string->symbol k) k) v))
+
(define (hasattr a b)
(let ((r (ref a (symbol->string b) miss)))
(not (eq? r miss))))
+(define-method (issubclass x y) #f)
(define-method (issubclass (sub <p>) (cls <p>))
(aif it (ref cls '__subclasscheck__)
- (it sub)
- (is-a? (ref sub '__goops__) (ref cls '__goops__))))
-
+ (it cls sub)
+ (if (eq? sub cls)
+ #t
+ (is-a? (ref sub '__goops__) (ref cls '__goops__)))))
+
+(define-method (isinstance x y)
+ (if (null? y)
+ #f
+ (if (pair? y)
+ (or (isinstance x (car y))
+ (isinstance x (cdr y)))
+ (is-a? x y))))
+
+(define-method (isinstance (i <integer>) y)
+ (if (issubclass y int)
+ #t
+ (if (pair? y)
+ (or (isinstance i (car y))
+ (isinstance i (cdr y)))
+ (is-a? i y))))
+
+(define-method (isinstance (i <real>) y)
+ (if (issubclass y float)
+ #t
+ (if (pair? y)
+ (or (isinstance i (car y))
+ (isinstance i (cdr y)))
+ (is-a? i y))))
+
+(define-method (isinstance (i <pair>) y)
+ (if (issubclass y tuple)
+ #t
+ (if (pair? y)
+ (or (isinstance i (car y))
+ (isinstance i (cdr y)))
+ (is-a? i y))))
+
+(define-method (isinstance (i <string>) y)
+ (if (issubclass y str)
+ #t
+ (if (pair? y)
+ (or (isinstance i (car y))
+ (isinstance i (cdr y)))
+ (is-a? i y))))
+
+(define-method (isinstance (i <bytevector>) y)
+ (if (issubclass y bytes)
+ #t
+ (if (pair? y)
+ (or (isinstance i (car y))
+ (isinstance i (cdr y)))
+ (is-a? i y))))
+
+
+(define-method (isinstance o (cl <p>))
+ (cond
+ ((eq? cl py-list)
+ (is-a? o <py-list>))
+ (else #f)))
+
(define-method (isinstance (o <p>) (cl <p>))
- (aif it (ref cl '__instancecheck__)
- (it o)
- (if (pair? cl)
- (or
- (isinstance o (car cl))
- (isinstance o (cdr cl)))
- (is-a? (ref (ref o '__class__) '__goops__) cl))))
+ (cond
+ ((ref cl '__instancecheck__) =>
+ (lambda (it)
+ (it o)))
+ ((pair? cl)
+ (or
+ (isinstance o (car cl))
+ (isinstance o (cdr cl))))
+ (else
+ (is-a? o (ref cl '__goops__)))))
+
+
(define iter
(case-lambda
(syntax-case x ()
((map f a ...)
(with-syntax (((x ...) (generate-temporaries #'(a ...))))
- #'(make-generator map
- (lambda (yield)
- (for ((x : a) ...) () (yield (f x ...))))))))))
+ #'(for ((x : a) ...) ((l '()))
+ (cons (f x ...) l)
+ #:final (py-list (reverse l))))))))
(define* (sum i #:optional (start 0))
(for ((x : i)) ((s start))
(define (id x) (object-address x))
(define (input str)
- (format #t str)
+ ((@ (guile) format) #t str)
(readline))
(define (idx x) x)
(yield (reverse r))
(lp))))))))))
-(define DEFAULT_BUFFER_SIZE 4096)
-(def (open path
- (= mode "r")
- (= buffering -1 )
- (= encoding None)
- (= errors None)
- (= newline None)
- (= closefd #t)
- (= opener None))
-
- (define modelist (string->list mode))
- (define path (path-it path))
- (define (clean ch l)
- (filter (lambda (c) (not (eq? ch c))) l))
- (let ((port (if (number? path)
- (begin
- (if (member #\a modelist)
- (seek path 0 SEEK_END))
- (if (member #\x modelist)
- (error "cannot use mode 'x' for fd input"))
- (cond
- ((member #\r modelist)
- (fdes->inport path))
- ((member #\w modelist)
- (fdes->outport path))))
- (begin
- (if (member #\x modelist)
- (if (file-exists? path)
- (raise OSError "mode='x' and file exists")
- (set mode (list->string
- (clean #\x modelist)))))
- ((@ (guile) open-file) (path-it path) mode)))))
-
- (case buffering
- ((-1)
- (setvbuf port 'block DEFAULT_BUFFER_SIZE))
- ((0)
- (setvbuf port 'none))
- ((1)
- (setvbuf port 'line))
- (else
- (setvbuf port 'block buffering)))
-
- port))
-
-
-
+(define-python-class ClassMethod ())
+(define-python-class StaticMethod ())
+(define-python-class Funcobj ())