improving the python import experience

parent ca4e3f91
......@@ -67,7 +67,8 @@ SOURCES = \
language/python/module/abc.scm \
language/python/module/threading.scm \
language/python/module/functools.scm \
language/python/module/types.scm \
language/python/module/_types.scm \
language/python/module/types.py \
language/python/module/enum.py \
language/python/module/time.scm \
language/python/module/warnings.scm \
......
......@@ -311,8 +311,8 @@ empty list."
(catch #t
(lambda ()
(fluid-set! %dont-warn-list
(cons v
(fluid-ref %dont-warn-list))))
(cons v
(fluid-ref %dont-warn-list))))
(lambda x (values))))
(define (dont-warn* v)
......@@ -480,8 +480,24 @@ empty list."
(union vs (list (exp '() (if as2 as2 (car ids2)))))))
(lp (cdr ids) (cdr as) vs)))
vs)))
((#:import (#:from (() () . nm) l))
;; Make sure to load the module in
(let* ((xl (map (lambda (nm) (exp vs nm)) nm))
(s.ss (find-symbols xl l)))
(for-each dont-warn (car s.ss))
(union vs (map cdr (cdr s.ss)))))
((#:expr-stmt l (#:assign u ... v))
((#:import (#:from (("." . nn) . nm) l))
;; Make sure to load the module in
(let* ((u (module-name (current-module)))
(u (reverse (list-cdr-ref (reverse (u-it u)) (length nn))))
(xl (append u (map (lambda (nm) (exp vs nm)) nm)))
(s.ss (find-symbols xl l)))
(for-each dont-warn (car s.ss))
(union vs (map cdr (cdr s.ss)))))
((#:expr-stmt l (#:assign u ... v))
(union
(fold (lambda (l s)
(union
......@@ -566,7 +582,24 @@ empty list."
(ids (car ids)))
(union vs (list (exp '() (if as as (car ids)))))))
vs)))
((#:import (#:from (() () . nm) l))
;; Make sure to load the module in
(let* ((xl (map (lambda (nm) (exp vs nm)) nm)))
(let ((s.ss (find-symbols xl l)))
(for-each dont-warn (car s.ss))
(union vs (map cdr (cdr s.ss))))))
((#:import (#:from (("." . nn) . nm) l))
;; Make sure to load the module in
(let* ((u (module-name (current-module)))
(u (reverse (list-cdr-ref (reverse (u-it u)) (length nn))))
(xl (append u (map (lambda (nm) (exp vs nm)) nm))))
(let ((s.ss (find-symbols xl l)))
(for-each dont-warn (car s.ss))
(union vs (map cdr (cdr s.ss))))))
((x . y)
(defs y (defs x vs)))
(_ vs)))
......@@ -1070,6 +1103,73 @@ empty list."
("notin" (list (G 'not) (list (L 'in) x y)))
("<>" (list (G 'not) (list (O 'equal?) x y)))))
(define (find-symbols mod syms)
(define (get-name x)
(define (f a) (exp '() a))
(match x
((a . #f)
(cons (f a) (f a)))
((a . b)
(cons (f a) (f b)))))
(catch #t
(lambda ()
(let* ((mod (Module (string-join (map symbol->string mod) "/")))
(e (list 'missing)))
(public mod)
(let lp ((l (map get-name syms)) (r '()) (rr '()))
(if (pair? l)
(let ((x (car l))
(l (cdr l)))
(catch #t
(lambda ()
(if (eq? e (ref mod (cdr x) e))
(lp l r (cons x rr))
(lp l (cons x r) rr)))
(lambda xx
(lp l r (cons x rr)))))
(cons (reverse r) (reverse rr))))))
(lambda x
(cons (map get-name syms) '()))))
(define-syntax @@values
(lambda (x)
(syntax-case x ()
((_ (a ...) m mm)
(let* ((l.ll (find-symbols (syntax->datum #'mm)
(syntax->datum #'(a ...))))
(l (car l.ll))
(ll (cdr l.ll)))
(define (f xx) (datum->syntax x xx))
(if (pair? l)
(if (pair? ll)
(with-syntax ((((b . c) ...) (f l))
(((bb . cc) ...) (f ll)))
#'(begin
(catch #t
(lambda ()
(set! cc (@@ m bb))
...)
(lambda x
(raise ImportError
'(m #:select ((bb . cc) ...)))))
(use #t () (m #:select ((b . c) ...)))))
(with-syntax ((((b . c) ...) (f l)))
#'(use #t () (m #:select ((b . c) ...)))))
(if (pair? ll)
(with-syntax ((((bb . cc) ...) (f ll)))
#'(catch #t
(lambda ()
(set! cc (@@ m bb))
...)
(lambda x
(raise ImportError '(m #:select ((bb . cc) ...))))))
#'(values))))))))
(gen-table x vs
(#:power
((_ _ (x) () . #f)
......@@ -1388,51 +1488,7 @@ empty list."
;; Make sure to load the module in
(let* ((xl (map (lambda (nm) (exp vs nm)) nm))
(ll `(language python module ,@xl)))
(define (get-imp x)
(define (f a) `(@@ ,ll ,(exp vs a)))
(match x
((a . #f)
(f a))
((a . b)
(f a))))
(define (get-name x)
(define (f a) (exp vs a))
(match x
((a . #f)
(f a))
((a . b)
(f b))))
`(define-values ,(map get-name l)
(,(G 'catch) #t
(,(G 'lambda) ()
(,(G 'values)
,@(map get-imp l)))
(,(G 'lambda) x123
(raise ImportError (,(G 'quote) ,ll)))))
#;
`(,(C 'use) #t ()
(,ll
#:select
,(map (lambda (x)
(match x
((a . #f)
(let ((s (exp vs a)))
(fluid-set! ignore
(cons s (fluid-ref ignore)))
(dont-warn s)
s))
((a . b)
(let ((s1 (exp vs a))
(s2 (exp vs b)))
(fluid-set! ignore
(cons s2
(fluid-ref ignore)))
(dont-warn s2)
(cons s1 s2)))))
l)))))
`(,(C '@@values) ,l ,ll ,xl)))
((_ (#:from (("." . nn) . nm) l))
;; Make sure to load the module in
......@@ -1440,53 +1496,7 @@ empty list."
(u (reverse (list-cdr-ref (reverse (u-it u)) (length nn))))
(xl (append u (map (lambda (nm) (exp vs nm)) nm)))
(ll `(language python module ,@xl)))
(define (get-imp x)
(define (f a) `(@@ ,ll ,(exp vs a)))
(match x
((a . #f)
(f a))
((a . b)
(f a))))
(define (get-name x)
(define (f a) (exp vs a))
(match x
((a . #f)
(f a))
((a . b)
(f b))))
`(define-values ,(map get-name l)
(,(G 'catch) #t
(,(G 'lambda) ()
(,(G 'values)
,@(map get-imp l)))
(,(G 'lambda) x123
(raise ImportError (,(G 'quote) ,ll)))))
#;
`(,(C 'use) #t ()
(,ll
#:select
,(map (lambda (x)
(match x
((a . #f)
(let ((s (exp vs a)))
(fluid-set! ignore
(cons s (fluid-ref ignore)))
(dont-warn s)
s))
((a . b)
(let ((s1 (exp vs a))
(s2 (exp vs b)))
(fluid-set! ignore
(cons s2
(fluid-ref ignore)))
(dont-warn s2)
(cons s1 s2)))))
l)))))
`(,(C '@@values) ,l ,ll ,xl)))
((_ (#:from ("." . nn) l))
;; Make sure to load the module in
......
......@@ -21,7 +21,9 @@
(lambda (x)
(syntax-case x ()
((g)
#'(M ((L env-module) (locals g)))))))
#'(M (current-module)))
((g gg)
#'(M ((L env-module) (locals gg)))))))
(define-syntax-rule (call- self item a ...)
(let ((class (ref self '_module)))
......@@ -95,12 +97,13 @@
(if (and (> (length mod) 3)
(eq? (car mod) 'language)
(eq? (cadr mod) 'python)
(eq? (caddr mod) 'module))
(eq? (caddr mod) 'module))
(set! mod (Module (reverse mod)
(reverse (cdddr mod))))
(set! mod (Module (reverse mod) (reverse mod))))
(GlobalModuleWrap mod))
(private mod)
mod #;(GlobalModuleWrap mod))
(define (local-eval x locals globals)
......
......@@ -22,9 +22,10 @@
(lambda () (current-language lan)))))
(define (private mod)
((ref mod '__setprivate__) #t))
((rawref mod '__setprivate__) mod #t))
(define (public mod)
((ref mod '__setprivate__) #f))
((rawref mod '__setprivate__) mod #f))
(define e (list 'e))
......@@ -45,6 +46,26 @@
it
(rawref self '_module)))
(define (get-derefs self)
(let* ((m (rawref self '_module)))
(let lp ((l (module-uses m)) (r '()))
(if (pair? l)
(let ((i (car l)))
(match (module-name i)
(('language 'python 'module a . u)
(if (not (member a '(python)))
(let ((ll '()))
(catch #t
(lambda ()
(module-for-each
(lambda (x v) (set! ll (cons (cons x v) ll))) i))
(lambda x (values)))
(lp (cdr l) (append ll r)))
(lp (cdr l) r)))
(_ (lp (cdr l) r))))
r))))
(define-python-class Module ()
(define _modules (make-hash-table))
(define __setprivate__
......@@ -79,8 +100,8 @@
((ref it '__update__) pre l nm)
(rawset self id (Module pre l nm)))
#f)))
(define __init__
(define __init__
(case-lambda
((self pre l nm)
(match l
......@@ -257,11 +278,13 @@
(let* ((h (slot-ref self 'h))
(l '())
(m (_m self))
(f (lambda (g) (lambda (x) (g (car x) (cdr x)))))
(add (lambda (k . u)
(if (not (in "-" (symbol->string k)))
(set! l (cons (symbol->string k) l))))))
(hash-for-each add h)
(if m (module-for-each add m))
(if (rawref self '_private) (for-each (f add) (get-derefs self)))
(aif it (rawref self '_export) (module-for-each add it))
(hash-for-each add (slot-ref self 'h))
(py-list l))))
......@@ -272,12 +295,14 @@
(let* ((h (slot-ref self 'h))
(l '())
(m (_m self))
(f (lambda (g) (lambda (x) (g (car x) (cdr x)))))
(add (lambda (k v)
(let ((k (symbol->string k)))
(if (and (not (in "-" k)) (variable-bound? v))
(set! l (cons (list k (variable-ref v))
(set! l (cons k
l)))))))
(module-for-each add m)
(if (rawref self '_private) (for-each (f add) (get-derefs self)))
(module-for-each add (rawref self '_export))
l)))
......
This diff is collapsed.
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment