diff options
author | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-03-28 15:18:50 +0200 |
---|---|---|
committer | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-03-28 15:18:50 +0200 |
commit | fc6e3d19ce60950e7465d018ff9b4d59c035c38c (patch) | |
tree | 78e443255428533c4063956824910040e2f96d62 /modules/language/python/module/functools.scm | |
parent | 944fc50b8b36455b9749ad6b60f3020d466f901c (diff) |
abc
Diffstat (limited to 'modules/language/python/module/functools.scm')
-rw-r--r-- | modules/language/python/module/functools.scm | 73 |
1 files changed, 30 insertions, 43 deletions
diff --git a/modules/language/python/module/functools.scm b/modules/language/python/module/functools.scm index 5f2bd5e..e2a5ce1 100644 --- a/modules/language/python/module/functools.scm +++ b/modules/language/python/module/functools.scm @@ -9,7 +9,8 @@ #:use-module (language python module collections) #:use-module ((language python module python) #:select (iter getattr setattr repr isinstance callable - bool str int)) + bool str int enumerate reversed hasattr + issubclass any)) #:use-module (language python list) #:use-module (language python dict) #:use-module (language python set) @@ -509,13 +510,13 @@ (set wrapper 'cache_clear cache_clear) wrapper)) -#| + ;; single dispatch (define (_c3_merge sequences) (let lp ((result '())) (set! sequences (for ((s : sequences)) ((l '())) (if (bool s) - (cond s l) + (cons s l) l) #:final (reverse l))) (if (bool sequences) @@ -525,7 +526,7 @@ (let ((cand (pylist-ref (car s1) 0))) (let lp3 ((s2 sequences)) (if (pair? s2) - (if (in cand (pylist-slice! (car s2) 1 None None)) + (if (in cand (pylist-slice (car s2) 1 None None)) (lp2 (cdr s1)) (lp3 (cdr s2))) cand))) @@ -543,22 +544,6 @@ (py-list (reverse result))))) (def (_c3_mro cls (= abcs None)) - "Computes the method resolution order using extended C3 linearization. - - If no *abcs* are given, the algorithm works exactly like the built-in C3 - linearization used for method resolution. - - If given, *abcs* is a list of abstract base classes that should be inserted - into the resulting MRO. Unrelated ABCs are ignored and don't end up in the - result. The algorithm inserts ABCs where their functionality is introduced, - i.e. issubclass(cls, abc) returns True for the class itself but returns - False for all its direct base classes. Implicit ABCs for a given class - (either registered or inferred from the presence of a special method like - __len__) are inserted directly after the last ABC explicitly listed in the - MRO of said class. If two implicit ABCs end up next to each other in the - resulting MRO, their ordering depends on the order of types in *abcs*. - - " (define bases (ref cls '__bases__ '())) (define boundary (for ((i base : (enumerate (reversed bases)))) () @@ -576,7 +561,7 @@ (not (any (map (lambda (b) (issubclass b base)) bases)))) (pylist-append! abstract_bases base))) - (for ((base : abstract_bases)) + (for ((base : abstract_bases)) () (pylist-remove! abcs base)) (let* ((f (lambda (bases) @@ -594,7 +579,7 @@ abstract_c3_mros other_c3_mros (py-list explicit_bases) - (py-lit abstract_bases) + (py-list abstract_bases) (py-list other_bases))))) (define (_compose_mro cls types) @@ -616,7 +601,7 @@ (if (is_related n) (cons n l) l) - #final (reverse l))) + #:final (reverse l))) ;; Remove entries which are strict bases of other entries (they will end up ;; in the MRO anyway. @@ -631,7 +616,7 @@ (if (is_strict_base n) (cons n l) l) - #final (reverse l))) + #:final (reverse l))) ; Subclasses of the ABCs in *types* which are also implemented by ; *cls* can be used to stabilize ABC ordering. @@ -640,24 +625,24 @@ (for ((typ : types)) () (let ((found (py-list))) - (for ((sub in ((ref typ '__subclasses__ (lambda () '()))))) () + (for ((sub : ((ref typ '__subclasses__ (lambda () '()))))) () (if (and (not (in sub bases)) (issubclass cls sub)) - (pylist-append found - (for ((s in (ref sub '__mro__ '()))) - ((l '())) - (if (in s type_set) - (cons s l) - l) - #:final (py-list (reverse l)))))) - (f (not (bool found)) - (begin - (pylist-append! mro typ) - (pylist-sort! found #:key len #:reverse #t) - (for ((sub : found)) () - (for ((subcls : sub)) () - (if (not (in subcls mro)) - (pylist-append! mro subcls)))))))) + (pylist-append! found + (for ((s : (ref sub '__mro__ '()))) + ((l '())) + (if (in s type_set) + (cons s l) + l) + #:final (py-list (reverse l)))))) + (if (not (bool found)) + (begin + (pylist-append! mro typ) + (pylist-sort! found #:key len #:reverse #t) + (for ((sub : found)) () + (for ((subcls : sub)) () + (if (not (in subcls mro)) + (pylist-append! mro subcls)))))))) (_c3_mro cls #:abcs mro)) @@ -692,6 +677,8 @@ (py-get registry match)) +(define (get_cache_token) #t) + (define (singledispatch func) "Single-dispatch generic function decorator. @@ -718,7 +705,7 @@ (let ((current_token (get_cache_token))) (if (not (equal? cache_token current_token)) (begin - (pylist-clear! dispatch_cache) + (py-clear dispatch_cache) (set! cache_token current_token))))) (let ((impl (try @@ -742,7 +729,7 @@ (if (and (eq? cache_token None) (ref cls '__abstractmethods__)) (set! cache_token (get_cache_token))) - (pylist-clear! dispatch_cache) + (py-clear dispatch_cache) func))) (def (wrapper (* args) (** kw)) @@ -757,4 +744,4 @@ (update_wrapper wrapper func) wrapper) -|# + |