summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-05-08 21:20:04 +0200
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-05-08 21:20:04 +0200
commit330ef2953f31dd7a5b64ff72919c4d7d696c7a83 (patch)
tree72afccbf8c359f536475319d7757c0865b3636a5
parent876d5d0fca204adce5335c5486bf6fb3d2188f22 (diff)
imporved multistring symbol support
-rw-r--r--modules/language/python/dict.scm25
-rw-r--r--modules/language/python/dir.scm176
-rw-r--r--modules/language/python/number.scm6
-rw-r--r--modules/oop/pf-objects.scm5
4 files changed, 130 insertions, 82 deletions
diff --git a/modules/language/python/dict.scm b/modules/language/python/dict.scm
index 18420dd..c5d56e5 100644
--- a/modules/language/python/dict.scm
+++ b/modules/language/python/dict.scm
@@ -18,7 +18,7 @@
py-popitem py-setdefault py-update py-clear
py-hash-ref dict pyhash-listing
weak-key-dict weak-value-dict
- py-hash-ref py-hash-set!
+ py-hash-ref py-hash-set! dictNs dictRNs
make-py-weak-key-hashtable
make-py-weak-value-hashtable
))
@@ -602,6 +602,11 @@
__init__)))
+(define (renorm k)
+ (if (symbol? k)
+ k
+ (string->symbol k)))
+
(define (norm k)
(if (symbol? k)
(symbol->string k)
@@ -609,6 +614,7 @@
(define fail (list 'fail))
+(define-syntax-rule (mkwrap dictNs norm renorm)
(define-python-class dictNs ()
(define __getitem__
(lambda (self k)
@@ -620,7 +626,10 @@
(define __iter__
(lambda (self)
- (wrap-in (ref self '_dict))))
+ ((make-generator ()
+ (lambda (yield)
+ (for ((k v : (ref self '_dict))) ()
+ (yield (renorm k) v)))))))
(define pop
(lambda (self k . l)
@@ -648,7 +657,9 @@
(define items
(lambda (self)
- (py-items (ref self '_dict))))
+ (for ((k v : (ref self '_dict))) ((l '()))
+ (cons (list (renorm k) v) l)
+ #:final (reverse l))))
(define __repr__
(lambda (self)
@@ -662,9 +673,13 @@
r))))
(define __init__
- (lambda (self d) (set self '_dict d))))
+ (lambda (self d) (set self '_dict d)))))
+
+(mkwrap dictNs norm renorm)
+(mkwrap dictRNs renorm norm)
-(set! (@@ (oop pf-objects) dictNs) dictNs)
+(set! (@@ (oop pf-objects) dictNs) dictNs)
+(set! (@@ (oop pf-objects) dictRNs) dictRNs)
(define-python-class weak-key-dict (<py> <py-hashtable>)
(define __init__
diff --git a/modules/language/python/dir.scm b/modules/language/python/dir.scm
index b74ca94..457c28f 100644
--- a/modules/language/python/dir.scm
+++ b/modules/language/python/dir.scm
@@ -13,7 +13,20 @@
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
-(define-method (dir) (pylist))
+(define in-p (make-fluid #f))
+(define-method (dir x) (py-list))
+(define (cont l1 l2)
+ (let ((h (make-hash-table))
+ (l (py-list)))
+ (for ((x : l1)) ()
+ (hash-set! h x #t))
+ (for ((x : l2)) ()
+ (hash-set! h x #t))
+ (for ((k v : h)) ()
+ (pylist-append! l k))
+ (pylist-sort! l)
+
+ l))
(define (p x) (if (symbol? x) (symbol->string x) x))
(define (chash-for-each t c)
@@ -29,11 +42,10 @@
h))))
(define (find-in o h c)
- (chash-for-each h c)
(aif it (and o (find-in-class c '__dir__ #f))
(for ((k : (it o))) ()
(hash-set! h (p k) #t))
- #f))
+ (chash-for-each h c)))
(define (find-in-mro o h l)
(let lp ((l l))
@@ -44,92 +56,112 @@
(define-method (dir (o <p>))
- (let ((h (make-hash-table)))
- (find-in-mro #f h (find-in-class o '__mro__ (list o)))
- (aif cl (find-in-class o '__class__ #f)
- (find-in-mro o h (find-in-class cl '__mro__ (list cl)))
- #f)
- (let ((l (py-list)))
- (hash-for-each
- (lambda (k v)
- (pylist-append! l k))
- h)
- (pylist-sort! l)
- l)))
+ (if (fluid-ref in-p)
+ (next-method)
+ (with-fluids ((in-p #t))
+ (cont
+ (next-method)
+ (let ((h (make-hash-table)))
+ (find-in-mro #f h (find-in-class o '__mro__ (list o)))
+ (aif cl (find-in-class o '__class__ #f)
+ (find-in-mro o h (find-in-class cl '__mro__ (list cl)))
+ #f)
+ (let ((l (py-list)))
+ (hash-for-each
+ (lambda (k v)
+ (pylist-append! l k))
+ h)
+ (pylist-sort! l)
+ l))))))
(define-method (dir (o <py-list>))
- (let ((l1 (pk (pylist-listing))))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pk (pylist-listing))))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-hashtable>))
- (let ((l1 (pyhash-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pyhash-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-string>))
- (let ((l1 (pystring-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pystring-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-int>))
- (let ((l1 (pyint-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pyint-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-float>))
- (let ((l1 (pyfloat-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pyfloat-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-complex>))
- (let ((l1 (pycomplex-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pycomplex-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-bytes>))
- (let ((l1 (pybytes-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pybytes-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
(define-method (dir (o <py-bytearray>))
- (let ((l1 (pybytesarray-listing)))
- (if (is-a? o <p>)
- (let* ((l2 (next-method))
- (l (+ l1 l2)))
- (pylist-sort! l)
- l)
- l1)))
+ (cont
+ (next-method)
+ (let ((l1 (pybytesarray-listing)))
+ (if (is-a? o <p>)
+ (let* ((l2 (next-method))
+ (l (+ l1 l2)))
+ (pylist-sort! l)
+ l)
+ l1))))
-
(define-method (dir (o <hashtable> )) (pyhash-listing))
(define-method (dir (o <string> )) (pystring-listing))
(define-method (dir (o <complex> )) (pycomplex-listing))
diff --git a/modules/language/python/number.scm b/modules/language/python/number.scm
index 845a155..db8a0c8 100644
--- a/modules/language/python/number.scm
+++ b/modules/language/python/number.scm
@@ -65,11 +65,11 @@
(define-method (op (o1 <py-complex>) o2)
(op (slot-ref o1 'x) o2))
(define-method (op o2 (o1 <py-int>))
- (op (slot-ref o1 'x) o2))
+ (op o2 (slot-ref o1 'x)))
(define-method (op o2 (o1 <py-complex>))
- (op (slot-ref o1 'x) o2))
+ (op o2 (slot-ref o1 'x)))
(define-method (op o2 (o1 <py-float>))
- (op (slot-ref o1 'x) o2))))
+ (op o2 (slot-ref o1 'x)))))
(define-syntax-rule (mk-biop1 mk-biop0 op r1)
(begin
diff --git a/modules/oop/pf-objects.scm b/modules/oop/pf-objects.scm
index 67dbaa0..25c4960 100644
--- a/modules/oop/pf-objects.scm
+++ b/modules/oop/pf-objects.scm
@@ -36,7 +36,8 @@ explicitly tell it to not update etc.
|#
;; this is mutated by the dict class
-(define dictNs '(dictNs))
+(define dictNs '(dictNs))
+(define dictRNs '(dictRNs))
#;
(define (pkk . l)
@@ -1529,4 +1530,4 @@ explicitly tell it to not update etc.
(define-method (py-dict (o <p>))
(aif it (ref o '__dict__)
it
- (slot-ref o 'h)))
+ (dictRNs (slot-ref o 'h))))