From 77e4e51a919c50e2847527aaffe67e8e19b970ae Mon Sep 17 00:00:00 2001 From: Stefan Israelsson Tampe Date: Sun, 15 Apr 2018 22:29:50 +0200 Subject: progressively imporoving the conformance with python3 --- modules/language/python/compile.scm | 234 +++++++++++++++++------------- modules/language/python/def.scm | 7 +- modules/language/python/dict.scm | 66 +++++---- modules/language/python/exceptions.scm | 3 +- modules/language/python/module/enum.py | 50 ++++--- modules/language/python/module/python.scm | 4 +- modules/language/python/number.scm | 60 +++++++- modules/language/python/set.scm | 16 ++ 8 files changed, 284 insertions(+), 156 deletions(-) (limited to 'modules/language') diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm index fc4a1c7..a54dab8 100644 --- a/modules/language/python/compile.scm +++ b/modules/language/python/compile.scm @@ -44,7 +44,9 @@ (define-syntax-rule (use a ...) (catch #t (lambda () (use-modules a ...)) - (lambda x (raise (ImportError '(a ...)))))) + (lambda x + (warn "failed to load " x) + (raise (ImportError '(a ...)))))) (define s/d 'set!) @@ -213,21 +215,36 @@ (union vs (list (exp '() (if as as (car ids))))))) vs))) - ((#:expr-stmt l (#:assign u)) - (union (fold (lambda (x s) - (match x - ((#:test (#:power v2 v1 () . _) . _) - (if v2 - (union - (union (list (exp '() v1)) - (list (exp '() v2))) - s) - (union (list (exp '() v1)) s))) - (_ s))) - '() - l) - vs)) + ((#:expr-stmt l (#:assign u ... v)) + (union + (fold (lambda (l s) + (union + s + (fold (lambda (x s) + (match x + ((#:test (#:power v2 v1 () . _) . _) + (if v2 + (union + (union (list (exp '() v1)) + (list (exp '() v2))) + s) + (union (list (exp '() v1)) s))) + (_ s))) + '() + l))) + '() + (cons l u)) + vs)) + + ((#:for es in code . final) + (let ((vs (let lp ((es es)) + (match es + (((#:power #f (#:tuple . l) . _)) + (lp l)) + (_ (union vs (map (g vs exp) es))))))) + (scope final (scope code vs)))) + ((#:expr-stmt l (#:assign k . u)) (union (union (fold (lambda (x s) @@ -248,7 +265,7 @@ ((x . y) (scope y (scope x vs))) (_ vs))) - + (define (defs x vs) (match x ((#:def (#:identifier f) . _) @@ -515,14 +532,14 @@ #:final (reverse l))) -(define (arglist->pkw . l) +(define (arglist->pkw l) (let lp ((l l) (r '())) (if (pair? l) (let ((x (car l))) - (if (keyword? x) - (cons (reverse r) l) - (lp (cdr l) (cons x r)))) - (cons (reverse l) '())))) + (if (keyword? x) + (list (G 'cons) `(,(G 'list) ,@(reverse r)) `(,(G 'list) ,@l)) + (lp (cdr l) (cons x r)))) + (list (G 'cons) `(,(G 'list) ,@(reverse r)) ''())))) (define (get-addings vs x) (match x @@ -892,6 +909,12 @@ ((_ class parents code) (with-fluids ((is-class? #t)) (let () + (define (clean l) + (match l + (((#:apply . l). u) (append (clean l) (clean u))) + (((`= x v ) . l) (cons* (symbol->keyword x) v (clean l))) + ((x . l) (cons x (clean l))) + (() '()))) (let* ((decor (let ((r (fluid-ref decorations))) (fluid-set! decorations '()) r)) @@ -911,7 +934,7 @@ (,(C 'mk-p-class2) ,class ,(if parents - `(,(C 'ref-x) ,(C 'arglist->pkw) ,@parents) + (arglist->pkw (clean parents)) `(,(G 'cons) '() '())) ,(map (lambda (x) `(define ,x #f)) ls) ,(exp vs code)))))))))) @@ -1150,6 +1173,7 @@ (dd* (map cadr *f)) (**f (get-args** vs args)) (dd** (map cadr **f)) + (aa `(,@arg_ ,@*f ,@arg= ,@**f)) (ab (gensym "ab")) (vs (union dd** (union dd* (union dd= (union args vs))))) (ns (scope code vs)) @@ -1174,19 +1198,19 @@ `(set! ,f (,(C 'def-decor) ,decor (,(C 'def-wrap) ,y? ,f ,ab - (,(D 'lam) (,@arg_ ,@*f ,@arg= ,@**f) + (,(D 'lam) ,aa (,(C 'with-return) ,r ,(mk `(let ,(map (lambda (x) (list x #f)) ls) - (,(C 'with-self) ,c? ,args + (,(C 'with-self) ,c? ,aa ,(with-fluids ((return r)) (exp ns code)))))))))) `(set! ,f (,(C 'def-decor) ,decor - (,(D 'lam) (,@arg_ ,@*f ,@arg= ,@**f) + (,(D 'lam) ,aa (,(C 'with-return) ,r ,(mk `(let ,(map (lambda (x) (list x #f)) ls) - (,(C 'with-self) ,c? ,args + (,(C 'with-self) ,c? ,aa ,(with-fluids ((return r)) (exp ns code)))))))))) @@ -1194,19 +1218,19 @@ `(set! ,f (,(C 'def-decor) ,decor (,(C 'def-wrap) ,y? ,f ,ab - (,(D 'lam) (,@arg_ ,@*f ,@arg= ,@**f) + (,(D 'lam) ,aa (,(C 'with-return) ,r (let ,(map (lambda (x) (list x #f)) ls) - (,(C 'with-self) ,c? ,args + (,(C 'with-self) ,c? ,aa ,(with-fluids ((return r)) (mk (exp ns code)))))))))) `(set! ,f (,(C 'def-decor) ,decor - (,(D 'lam) (,@arg_ ,@*f ,@arg= ,@**f) + (,(D 'lam) ,aa (,(C 'with-return) ,r (let ,(map (lambda (x) (list x #f)) ls) - (,(C 'with-self) ,c? ,args + (,(C 'with-self) ,c? ,aa ,(with-fluids ((return r)) (exp ns code)))))))))))))) @@ -1274,10 +1298,10 @@ (car l) `(,(G 'values) ,@l)))) - ((_ l (#:assign x y . u)) + ((_ a (#:assign b c . u)) (let ((z (gensym "x"))) - `(let ((,x ,(exp vs `(#:expr-stmt1 ((#:verb ,z)) (#:assign ,y . ,u))))) - ,(exp vs `(#:expr-stmt ,x (#:assign ((#:verb ,z)))))))) + `(let ((,z ,(exp vs `(#:expr-stmt1 ,b (#:assign ,c . ,u))))) + ,(exp vs `(#:expr-stmt ,a (#:assign ((#:verb ,z)))))))) ((_ l type) (=> fail) @@ -1340,11 +1364,11 @@ (#:expr-stmt1 - ((_ l (#:assign x y . u)) + ((_ a (#:assign b c . u)) (let ((z (gensym "x"))) - `(let ((,x ,(exp vs `(#:expr-stmt1 ((#:verb ,z)) - (#:assign ,y . ,u))))) - ,(exp vs `(#:expr-stmt ,x (#:assign ((#:verb ,z)))))))) + `(let ((,z ,(exp vs `(#:expr-stmt1 ,b + (#:assign ,c . ,u))))) + ,(exp vs `(#:expr-stmt1 ,a (#:assign ((#:verb ,z)))))))) ((_ l type) (=> fail) @@ -1706,7 +1730,8 @@ (if (pair? a) (let lp ((l a)) (if (pair? l) - (let ((x (car l))) + (begin + (set! x (car l)) (with-sp ((continue (lp (cdr l))) (break (values))) code @@ -1720,10 +1745,10 @@ (if (pair? l) (begin (let/ec continue-ret - (let ((x (car l))) - (with-sp ((continue (continue-ret)) - (break (break-ret))) - code))) + (set! x (car l)) + (with-sp ((continue (continue-ret)) + (break (break-ret))) + code)) (lp (cdr l)))))) (for/adv1 (x) (a) code #f #t))) @@ -1731,12 +1756,13 @@ (if (pair? a) (let/ec break-ret (let ((x (let lp ((l a) (old #f)) - (if (pair? l) - (let ((x (car l))) + (if (pair? l) + (begin + (set! x (car l)) (let/ec continue-ret (with-sp ((continue (continue-ret)) (break (break-ret))) - code)) + code)) (lp (cdr l))) old)))) next)) @@ -1749,13 +1775,15 @@ (lambda (x) (syntax-case x () ((_ (x ...) (in) code #f #f) - (with-syntax ((inv (gentemp #'in))) + (with-syntax ((inv (gentemp #'in)) + ((xx ...) (generate-temporaries #'(x ...)))) #'(let ((inv (wrap-in in))) (catch StopIteration (lambda () (let lp () (call-with-values (lambda () (next inv)) - (lambda (x ...) + (lambda (xx ...) + (set! x xx) ... (with-sp ((break (values)) (continue (values))) code @@ -1763,28 +1791,32 @@ (lambda z (values)))))) ((_ (x ...) (in ...) code #f #f) - (with-syntax (((inv ...) (generate-temporaries #'(in ...)))) + (with-syntax (((inv ...) (generate-temporaries #'(in ...))) + ((xx ...) (generate-temporaries #'(x ...)))) #'(let ((inv (wrap-in in)) ...) (catch StopIteration (lambda () (let lp () (call-with-values (lambda () (values (next inv) ...)) - (lambda (x ...) + (lambda (xx ...) + (set! x xx) ... (with-sp ((break (values)) (continue (values))) - code - (lp)))))) + code + (lp)))))) (lambda z (values)))))) ((_ (x ...) (in) code #f #t) - (with-syntax ((inv (gentemp #'in))) + (with-syntax ((inv (gentemp #'in)) + ((xx ...) (generate-temporaries #'(x ...)))) #'(let ((inv (wrap-in in))) (let lp () (let/ec break-ret (catch StopIteration (lambda () (call-with-values (lambda () (next inv)) - (lambda (x ...) + (lambda (xx ...) + (set! x xx) ... (let/ec continue-ret (with-sp ((break (break-ret)) (continue (continue-ret))) @@ -1793,14 +1825,16 @@ (lambda z (values)))))))) ((_ (x ...) (in ...) code #f #t) - (with-syntax (((inv ...) (generate-temporaries #'(in ...)))) + (with-syntax (((inv ...) (generate-temporaries #'(in ...))) + ((xx ...) (generate-temporaries #'(x ...)))) #'(let ((inv (wrap-in in)) ...) (let lp () (let/ec break-ret (catch StopIteration (lambda () (call-with-values (lambda () (values (next inv) ...)) - (lambda (x ...) + (lambda (xx ...) + (set! x xx) ... (let/ec continue-ret (with-sp ((break (break-ret)) (continue (continue-ret))) @@ -1831,71 +1865,67 @@ (if (syntax->datum #'p) #'(let ((inv (wrap-in in))) (let/ec break-ret - (let ((x #f) ...) - (catch StopIteration - (lambda () - (let lp () - (call-with-values (lambda () (next inv)) - (lambda (xx ...) - (set! x xx) ... - (let/ec continue-ret - (with-sp ((break (break-ret)) - (continue (continue-ret))) - code)) - (lp))))) - (lambda q else))))) - - #'(let ((inv (wrap-in in))) - (let ((x #f) ...) - (let/ec break-ret - (catch StopIteration - (lambda () - (let lp () - (call-with-values (lambda () (next inv)) - (lambda (xx ...) - (set! x xx) ... - (with-sp ((break (break-ret)) - (continue (values))) - code) - (lp))))) - (lambda e else))))))))) - - ((_ (x ...) (in ...) code else p) - (with-syntax (((inv ...) (generate-temporaries #'(in ...)))) - (with-syntax ((get (gen #'(inv ...) #'(x ...))) - ((xx ...) (generate-temporaries #'(x ...)))) - (if (syntax->datum #'p) - #'(let ((inv (wrap-in in)) ...) - (let/ec break-ret - (let ((x #f) ...) (catch StopIteration (lambda () (let lp () - (call-with-values (lambda () get) + (call-with-values (lambda () (next inv)) (lambda (xx ...) (set! x xx) ... (let/ec continue-ret (with-sp ((break (break-ret)) (continue (continue-ret))) - code)) + code)) (lp))))) - (lambda q else))))) + (lambda q else)))) - #'(let ((inv (wrap-in in)) ...) - (let ((x #f) ...) + #'(let ((inv (wrap-in in))) (let/ec break-ret (catch StopIteration (lambda () (let lp () - (call-with-values (lambda () get) + (call-with-values (lambda () (next inv)) (lambda (xx ...) (set! x xx) ... (with-sp ((break (break-ret)) (continue (values))) code) (lp))))) - (lambda e else)))))))))))) - + (lambda e else)))))))) + + ((_ (x ...) (in ...) code else p) + (with-syntax (((inv ...) (generate-temporaries #'(in ...)))) + (with-syntax ((get (gen #'(inv ...) #'(x ...))) + ((xx ...) (generate-temporaries #'(x ...)))) + (if (syntax->datum #'p) + #'(let ((inv (wrap-in in)) ...) + (let/ec break-ret + (catch StopIteration + (lambda () + (let lp () + (call-with-values (lambda () get) + (lambda (xx ...) + (set! x xx) ... + (let/ec continue-ret + (with-sp ((break (break-ret)) + (continue (continue-ret))) + code)) + (lp))))) + (lambda q else)))) + + #'(let ((inv (wrap-in in)) ...) + (let/ec break-ret + (catch StopIteration + (lambda () + (let lp () + (call-with-values (lambda () get) + (lambda (xx ...) + (set! x xx) ... + (with-sp ((break (break-ret)) + (continue (values))) + code) + (lp))))) + (lambda e else))))))))))) + (define-syntax def-wrap (lambda (x) (syntax-case x () @@ -1941,13 +1971,15 @@ (define-syntax ref-x (lambda (x) - (syntax-case x () + (syntax-case x (quote __dict__) ((_ v) #'v) ((_ v (#:fastfkn-ref f _) . l) #'(ref-x (lambda x (if (pyclass? v) (apply f x) (apply f v x))) . l)) ((_ v (#:fast-id f _) . l) #'(ref-x (f v) . l)) + ((_ v (#:identifier '__dict__) . l) + #'(ref-x (py-dict v) . l)) ((_ v (#:identifier x) . l) #'(ref-x (wr x (ref v x miss)) . l)) ((_ v (#:call-obj x) . l) diff --git a/modules/language/python/def.scm b/modules/language/python/def.scm index 5c83b6f..fa4cbc5 100644 --- a/modules/language/python/def.scm +++ b/modules/language/python/def.scm @@ -82,7 +82,12 @@ (ww- (fold get-ww '() #'(arg ...))) (kv (fold get-kv '() #'(arg ...)))) (if (and-map null? (list kw ww- kv)) - #`(object-method (lambda #,as code ...)) + #`(object-method + (lambda (#,@as . u12345678) + (if (and (pair? u12345678) + (not (keyword? (car u12345678)))) + (raise (ArgumentError "too many arguments to function"))) + code ...)) (with-syntax ((kw (if (null? kw) (datum->syntax x (gensym "kw")) (car kw))) diff --git a/modules/language/python/dict.scm b/modules/language/python/dict.scm index 260aa0d..977d5e1 100644 --- a/modules/language/python/dict.scm +++ b/modules/language/python/dict.scm @@ -44,13 +44,13 @@ (define H (hash 1333674836 complexity)) -(define-class () t h n) +(define-class () t hash n) (name-object ) (cpit (o (lambda (o h n a) - (slot-set! o 'h h) + (slot-set! o 'hash h) (slot-set! o 'n n) (slot-set! o 't (let ((t (make-hash-table))) @@ -62,7 +62,7 @@ t))) (let ((t (slot-ref o 't))) (list - (slot-ref o 'h) + (slot-ref o 'hash) (slot-ref o 'n) (hash-fold (lambda (k v s) (cons (cons k v) s)) '() t))))) @@ -71,7 +71,7 @@ (t (make-hash-table)) (h H)) (slot-set! o 't t) - (slot-set! o 'h h) + (slot-set! o 'hash h) (slot-set! o 'n 0) o)) @@ -80,7 +80,7 @@ (t (make-weak-key-hash-table)) (h H)) (slot-set! o 't t) - (slot-set! o 'h h) + (slot-set! o 'hash h) (slot-set! o 'n 0) o)) @@ -89,7 +89,7 @@ (t (make-weak-value-hash-table)) (h H)) (slot-set! o 't t) - (slot-set! o 'h h) + (slot-set! o 'hash h) (slot-set! o 'n 0) o)) @@ -152,14 +152,14 @@ (define-method (pyhash-rem! (o ) k) (let ((t (slot-ref o 't)) (n (slot-ref o 'n)) - (h (slot-ref o 'h))) + (h (slot-ref o 'hash))) (let ((ret (py-hash-ref t k miss))) (if (eq? ret miss) (values) (begin (py-hash-remove! t k) - (slot-set! o 'n (- n 1)) - (slot-set! o 'h (logxor h (xy (py-hash k) (py-hash ret)))) + (slot-set! o 'n (- n 1)) + (slot-set! o 'hash (logxor h (xy (py-hash k) (py-hash ret)))) (values)))))) (define-method (pylist-pop! (o ) k . l) @@ -187,16 +187,16 @@ (define-method (pylist-set! (o ) key val) (let ((t (slot-ref o 't)) (n (slot-ref o 'n)) - (h (slot-ref o 'h))) + (h (slot-ref o 'hash))) (let ((ret (py-hash-ref t key miss))) (if (eq? ret miss) (begin (py-hash-set! t key val) (slot-set! o 'n (+ n 1)) - (slot-set! o 'h (logxor (xy (py-hash key) (py-hash val)) h))) + (slot-set! o 'hash (logxor (xy (py-hash key) (py-hash val)) h))) (begin (py-hash-set! t key val) - (slot-set! o 'h + (slot-set! o 'hash (logxor (xy (py-hash key) (py-hash val)) (logxor (xy (py-hash key) (py-hash ret)) @@ -242,7 +242,7 @@ ( (let ((r (make ))) - (slot-set! r 'h (slot-ref o 'h)) + (slot-set! r 'hash (slot-ref o 'hash)) (slot-set! r 'n (slot-ref o 'n)) (slot-set! r 't (py-copy (slot-ref o 't))) r))) @@ -275,7 +275,7 @@ (let ((elseval (match l (() None) ((v) v)))) - (let ((ret (py-hash-ref o k miss))) + (let ((ret (ref o k miss))) (if (eq? ret miss) elseval ret)))) @@ -284,7 +284,7 @@ (let ((elseval (match l (() None) ((v) v)))) - (let ((ret (py-hash-ref (slot-ref o 't) k miss))) + (let ((ret (ref (slot-ref o 't) k miss))) (if (eq? ret miss) elseval ret))))) @@ -453,7 +453,7 @@ (let ((t (slot-ref o 't))) (hash-clear! t) (slot-set! o 'n 0) - (slot-set! o 'h H) + (slot-set! o 'hash H) (values)))) #| @@ -492,9 +492,9 @@ (define-method (py-equal? (o1 ) (o2 )) (and - (equal? (slot-ref o1 'n) (slot-ref o2 'n)) - (equal? (slot-ref o1 'h) (slot-ref o2 'h)) - (e? (slot-ref o1 't) (slot-ref o2 't)))) + (equal? (slot-ref o1 'n) (slot-ref o2 'n)) + (equal? (slot-ref o1 'hash) (slot-ref o2 'hash)) + (e? (slot-ref o1 't) (slot-ref o2 't)))) (define (e? t1 t2) (let/ec ret @@ -548,12 +548,12 @@ (letrec ((__init__ (case-lambda ((self) - (let ((r (make-py-hashtable))) - (slot-set! self 't (slot-ref r 't)) - (slot-set! self 'h (slot-ref r 'h)) - (slot-set! self 'n (slot-ref r 'n)))) + (let ((r (make-hash-table))) + (slot-set! self 't r) + (slot-set! self 'hash H) + (slot-set! self 'n 0))) ((self x) - (__init__ self) + (__init__ self) (catch #t (lambda () (for ((k v : x)) () @@ -572,10 +572,11 @@ (letrec ((__init__ (case-lambda ((self) - (let ((r (make-py-weak-key-hashtable))) - (slot-set! self 't (slot-ref r 't)) - (slot-set! self 'h (slot-ref r 'h)) - (slot-set! self 'n (slot-ref r 'n)))) + (let ((r (make-hash-table))) + (slot-set! self 't r) + (slot-set! self 'hash H) + (slot-set! self 'n 0))) + ((self x) (__init__ self) (if (is-a? x ) @@ -590,10 +591,11 @@ (letrec ((__init__ (case-lambda ((self) - (let ((r (make-py-weak-value-hashtable))) - (slot-set! self 't (slot-ref r 't)) - (slot-set! self 'h (slot-ref r 'h)) - (slot-set! self 'n (slot-ref r 'n)))) + (let ((r (make-hash-table))) + (slot-set! self 't r) + (slot-set! self 'hash H) + (slot-set! self 'n 0))) + ((self x) (__init__ self) (if (is-a? x ) diff --git a/modules/language/python/exceptions.scm b/modules/language/python/exceptions.scm index 93ce54d..9d51116 100644 --- a/modules/language/python/exceptions.scm +++ b/modules/language/python/exceptions.scm @@ -3,7 +3,7 @@ #:use-module (oop goops) #:export (StopIteration GeneratorExit RuntimeError Exception ValueError TypeError - IndexError KeyError AttributeError + IndexError KeyError AttributeError ArgumentError SyntaxError SystemException OSError ProcessLookupError PermissionError None NotImplemented NotImplementedError @@ -40,6 +40,7 @@ (define-er SystemException 'SystemException) (define-er RuntimeError 'RuntimeError) (define-er IndexError 'IndexError) +(define-er ArgumentError 'IndexError) (define-er ValueError 'ValueError) (define None 'None) (define-er KeyError 'KeyError) diff --git a/modules/language/python/module/enum.py b/modules/language/python/module/enum.py index 89047cd..1549862 100644 --- a/modules/language/python/module/enum.py +++ b/modules/language/python/module/enum.py @@ -11,7 +11,6 @@ try: except ImportError: from collections import OrderedDict - __all__ = [ 'EnumMeta', 'Enum', 'IntEnum', 'Flag', 'IntFlag', @@ -50,6 +49,8 @@ def _make_class_unpicklable(cls): cls.__module__ = '' _auto_null = object() + + class auto: """ Instances are replaced with an appropriate value in Enum class suites. @@ -117,10 +118,14 @@ class EnumMeta(type): def __prepare__(metacls, cls, bases): # create the namespace dict enum_dict = _EnumDict() + pk('got dict') + # inherit previous flags and _generate_next_value_ function member_type, first_enum = metacls._get_mixins_(bases) + if first_enum is not None: enum_dict['_generate_next_value_'] = getattr(first_enum, '_generate_next_value_', None) + return enum_dict def __new__(metacls, cls, bases, classdict): @@ -128,42 +133,45 @@ class EnumMeta(type): # cannot be mixed with other types (int, float, etc.) if it has an # inherited __new__ unless a new __new__ is defined (or the resulting # class will fail). + pk('new enum meta') member_type, first_enum = metacls._get_mixins_(bases) __new__, save_new, use_args = metacls._find_new_(classdict, member_type, first_enum) - + pk(1) # save enum items into separate mapping so they don't get baked into # the new class enum_members = {k: classdict[k] for k in classdict._member_names} for name in classdict._member_names: del classdict[name] - + pk(2) # adjust the sunders _order_ = classdict.pop('_order_', None) - + pk(3) # check for illegal enum names (any others?) invalid_names = set(enum_members) & {'mro', } if invalid_names: raise ValueError('Invalid enum member name: {0}'.format( ','.join(invalid_names))) - + pk(4) # create a default docstring if one has not been provided if '__doc__' not in classdict: classdict['__doc__'] = 'An enumeration.' - + pk(5) # create our new Enum type enum_class = super().__new__(metacls, cls, bases, classdict) + enum_class._member_names_ = [] # names in definition order enum_class._member_map_ = OrderedDict() # name->value map enum_class._member_type_ = member_type - + pk(6) # save attributes from super classes so we know if we can take # the shortcut of storing members in the class dict + base_attributes = {a for b in enum_class.mro() for a in b.__dict__} # Reverse value->name map for hashable values. enum_class._value2member_map_ = {} - + pk(7) # If a custom type is mixed into the Enum, and it does not know how # to pickle itself, pickle.dumps will succeed but pickle.loads will # fail. Rather than have the error show up later and possibly far @@ -180,7 +188,7 @@ class EnumMeta(type): '__reduce_ex__', '__reduce__') if not any(m in member_type.__dict__ for m in methods): _make_class_unpicklable(enum_class) - + pk(8) # instantiate them, checking for duplicates as we go # we instantiate first instead of checking for duplicates first in case # a custom __new__ is doing something funky with the values -- such as @@ -230,7 +238,7 @@ class EnumMeta(type): enum_class._value2member_map_[value] = enum_member except TypeError: pass - + pk(9) # double check that repr and friends are not the mixin's or various # things break (such as pickle) for name in ('__repr__', '__str__', '__format__', '__reduce_ex__'): @@ -239,7 +247,7 @@ class EnumMeta(type): enum_method = getattr(first_enum, name, None) if obj_method is not None and obj_method is class_method: setattr(enum_class, name, enum_method) - + pk(10) # replace any other __new__ with our own (as long as Enum is not None, # anyway) -- again, this is to support pickle if Enum is not None: @@ -248,14 +256,14 @@ class EnumMeta(type): if save_new: enum_class.__new_member__ = __new__ enum_class.__new__ = Enum.__new__ - + pk(11) # py3 support for definition order (helps keep py2/py3 code in sync) if _order_ is not None: if isinstance(_order_, str): _order_ = _order_.replace(',', ' ').split() if _order_ != enum_class._member_names_: raise TypeError('member order does not match _order_') - + pk(12) return enum_class def __bool__(self): @@ -424,9 +432,10 @@ class EnumMeta(type): bases: the tuple of bases that was given to __new__ """ + pk('bases',bases) if not bases: return object, Enum - + pk(2) # double check that we are not subclassing a class with existing # enumeration members; while we're at it, see if any other data # type has been mixed in so we can use the correct __new__ @@ -436,6 +445,9 @@ class EnumMeta(type): issubclass(base, Enum) and base._member_names_): raise TypeError("Cannot extend enumerations") + pk(3) + pk(base) + pk(bases) # base is now the last base in bases if not issubclass(base, Enum): raise TypeError("new enumerations must be created as " @@ -473,11 +485,12 @@ class EnumMeta(type): # now find the correct __new__, checking to see of one was defined # by the user; also check earlier enum classes in case a __new__ was # saved as __new_member__ + pk(0) __new__ = classdict.get('__new__', None) - + pk(1) # should __new__ be saved as __new_member__ later? save_new = __new__ is not None - + pk(2) if __new__ is None: # check all possibles for __new_member__ before falling back to # __new__ @@ -496,7 +509,7 @@ class EnumMeta(type): break else: __new__ = object.__new__ - + pk(3) # if a non-object.__new__ is used then whatever value/tuple was # assigned to the enum member name will be passed to __new__ and to the # new enum member's __init__ @@ -504,7 +517,7 @@ class EnumMeta(type): use_args = False else: use_args = True - + pk(4) return __new__, save_new, use_args class Enum(metaclass=EnumMeta): @@ -636,6 +649,7 @@ class Enum(metaclass=EnumMeta): module_globals[name] = cls return cls +pk(6) class IntEnum(int, Enum): """Enum where members are also (and must be) ints""" diff --git a/modules/language/python/module/python.scm b/modules/language/python/module/python.scm index 3398dbb..ef42cc6 100644 --- a/modules/language/python/module/python.scm +++ b/modules/language/python/module/python.scm @@ -29,7 +29,7 @@ #: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 @@ -47,7 +47,7 @@ chr classmethod staticmethod objectmethod divmod enumerate filter getattr hasattr setattr hex isinstance issubclass - iter map sum id input oct ord pow super + iter sum id input oct ord pow super sorted zip ClassMethod StaticMethod Funcobj)) diff --git a/modules/language/python/number.scm b/modules/language/python/number.scm index 6d93435..845a155 100644 --- a/modules/language/python/number.scm +++ b/modules/language/python/number.scm @@ -74,7 +74,7 @@ (define-syntax-rule (mk-biop1 mk-biop0 op r1) (begin (mk-biop0 op) - (define-method (op v (o

)) + (define-method (op (o

) v) (aif it (ref o 'r1) (it v) (next-method))))) @@ -95,6 +95,7 @@ (define-method (op o2 (o1 )) (op o2 (slot-ref o1 'x))))) + (mk-biop2 b0 r+ + __add__ __radd__) (mk-biop2 b0 r- - __sub__ __rsub__) (mk-biop2 b0 r* * __mul__ __rmul__) @@ -106,6 +107,7 @@ (mk-biop2 b0 rexpt expt __pow__ __rpow__) (b0 py-equal?) + (define-method (py-lshift (o1 ) (o2 )) (ash o1 o2)) (define-method (py-rshift (o1 ) (o2 )) @@ -123,6 +125,62 @@ (define-method (py-lognot (o1 )) (lognot o1)) +(define-method (py-logand o1 (o2 )) + (py-logand o1 (slot-ref o2 'x))) + +(define-method (py-logand (o1 ) o2) + (py-logand (slot-ref o1 'x) o2)) + +(define-method (py-logior o1 (o2 )) + (py-logior o1 (slot-ref o2 'x))) + +(define-method (py-logior (o1 ) o2) + (py-logior (slot-ref o1 'x) o2)) + +(define-method (py-logxor o1 (o2 )) + (py-logxor o1 (slot-ref o2 'x))) + +(define-method (py-logxor (o1 ) o2) + (py-logxor (slot-ref o1 'x) o2)) + +(define-method (py-lognot (o1 )) + (lognot (slot-ref o1 'x))) + +(define-method (py-logand (o1

) o2) + (aif it (ref o1 '__and__) + (it o2) + (next-method))) + +(define-method (py-logand o1 (o2

)) + (aif it (ref o1 '__rand__) + (it o2) + (next-method))) + +(define-method (py-logior (o1

) o2) + (aif it (ref o1 '__or__) + (it o2) + (next-method))) + +(define-method (py-logior o1 (o2

)) + (aif it (ref o1 '__ror__) + (it o2) + (next-method))) + +(define-method (py-logxor (o1

) o2) + (aif it (ref o1 '__xor__) + (it o2) + (next-method))) + +(define-method (py-logxor o1 (o2

)) + (aif it (ref o1 '__rxor__) + (it o2) + (next-method))) + +(define-method (py-lognot (o1

)) + (aif it (ref o1 '__not__) + (it) + (next-method))) + (define-method (py-/ (o1 ) (o2 )) (/ o1 (exact->inexact o2))) diff --git a/modules/language/python/set.scm b/modules/language/python/set.scm index 5582d36..2f3b7cc 100644 --- a/modules/language/python/set.scm +++ b/modules/language/python/set.scm @@ -224,6 +224,22 @@ (t (slot-ref d 't))) (not (eq? miss (py-hash-ref t x miss)))))) + (define __and__ + (lambda (self op) + (intersection self op))) + + (define __or__ + (lambda (self op) + (union self op))) + + (define __sub__ + (lambda (self op) + (difference self op))) + + (define __xor__ + (lambda (self op) + (symmetric_difference self op))) + (define __eq__ (lambda (self x) (and -- cgit v1.2.3