summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-02-23 16:00:31 +0100
committerStefan Israelsson Tampe <stefan.itampe@gmail.com>2018-02-23 16:00:31 +0100
commit745a1ab1736c6e917f639959c584d7d5134e4e24 (patch)
tree3739c6c4433f49a10c1decbaf902c693a9ef6d35 /modules
parentf702592cc6d6914c171f44db663a64f1665754df (diff)
weak dicts
Diffstat (limited to 'modules')
-rw-r--r--modules/language/python/dict.scm59
1 files changed, 58 insertions, 1 deletions
diff --git a/modules/language/python/dict.scm b/modules/language/python/dict.scm
index a57b771..b9f6dd3 100644
--- a/modules/language/python/dict.scm
+++ b/modules/language/python/dict.scm
@@ -16,6 +16,7 @@
py-iterkeys py-itervalues py-keys py-values
py-popitem py-setdefault py-update py-clear
py-hash-ref dict pyhash-listing
+ weak-key-dict weak-value-dict
))
(define-syntax-rule (aif it p x y) (let ((it p)) (if it x y)))
@@ -70,6 +71,24 @@
(slot-set! o 'n 0)
o))
+(define (make-py-weak-key-hashtable)
+ (let* ((o (make <py-hashtable>))
+ (t (make-weak-key-hash-table))
+ (h H))
+ (slot-set! o 't t)
+ (slot-set! o 'h h)
+ (slot-set! o 'n 0)
+ o))
+
+(define (make-py-weak-value-hashtable)
+ (let* ((o (make <py-hashtable>))
+ (t (make-weak-value-hash-table))
+ (h H))
+ (slot-set! o 't t)
+ (slot-set! o 'h h)
+ (slot-set! o 'n 0)
+ o))
+
(define miss (list 'miss))
(define-method (pylist-ref (o <hashtable>) x)
(let ((r (py-hash-ref o x miss)))
@@ -528,9 +547,47 @@
(pylist-set! self k v))
(slot-ref x 't)))))))
__init__)))
-
(name-object dict)
+(define-python-class weak-key-dict (<py-hashtable>)
+ (define __init__
+ (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))))
+ ((self x)
+ (__init__ self)
+ (if (is-a? x <py-hashtable>)
+ (hash-for-each
+ (lambda (k v)
+ (pylist-set! self k v))
+ (slot-ref x 't)))))))
+ __init__)))
+(name-object weak-key-dict)
+
+(define-python-class weak-value-dict (<py-hashtable>)
+ (define __init__
+ (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))))
+ ((self x)
+ (__init__ self)
+ (if (is-a? x <py-hashtable>)
+ (hash-for-each
+ (lambda (k v)
+ (pylist-set! self k v))
+ (slot-ref x 't)))))))
+ __init__)))
+
+(name-object weak-value-dict)
+
(define (pyhash-listing)
(let ((l (to-pylist
(map symbol->string