plt-spy-commits Mailing List for PLT Spy
Brought to you by:
aphexairlines
You can subscribe to this list here.
2004 |
Jan
|
Feb
|
Mar
|
Apr
(3) |
May
|
Jun
|
Jul
(4) |
Aug
(15) |
Sep
(7) |
Oct
|
Nov
(7) |
Dec
(9) |
---|---|---|---|---|---|---|---|---|---|---|---|---|
2005 |
Jan
|
Feb
(2) |
Mar
|
Apr
|
May
|
Jun
|
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
2007 |
Jan
|
Feb
|
Mar
|
Apr
|
May
|
Jun
(6) |
Jul
|
Aug
|
Sep
|
Oct
|
Nov
|
Dec
|
From: Daniel S. <aph...@us...> - 2007-06-11 05:32:16
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv22709 Added Files: generator-dsilva.ss Log Message: Generators. --- NEW FILE: generator-dsilva.ss --- (module generator-dsilva mzscheme (require (lib "class.ss")) (define iterator% (class object% (init-field initial-k) (define k initial-k) (define set-k! (lambda (c) (set! k c))) (define/public (next) (let/cc cc (raise (let/cc raise-cc (cc (k (list set-k! cc raise-cc))))))) (define/public (reset) (set! k initial-k)) (super-new))) (define-struct (exn:stop-iteration exn) ()) (define-syntax (define-gen stx) (syntax-case stx () [(_ (id args ...) bodies ...) (with-syntax ([yield (datum->syntax-object stx 'yield)] [return (datum->syntax-object stx 'return)]) #`(define (id args ...) (make-object iterator% (lambda (set-k!&ec&raise) (define-values (set-k! ec raise) (values #f #f #f)) (define (set-values the-set-k! the-ec the-raise) (set! set-k! the-set-k!) (set! ec the-ec) (set! raise the-raise)) (define (return) (raise (make-exn:stop-iteration "StopIteration" (current-continuation-marks)))) (define (yield x) (apply set-values (let/cc k (set-k! k) (ec x)))) (apply set-values set-k!&ec&raise) (with-handlers ([void raise]) (begin bodies ...) (return))))))])) #| Examples: (define-gen (iter lst) (for-each yield lst)) (define-gen (iter2 lst) (for-each yield lst) (raise "damn it")) (define (gen lst) (make-object iterator% (lambda (set-k!) (let/ec return (for-each (lambda (x) (let/cc k (set-k! k) (return x))) lst) (raise "stop iteration"))))) |# ) |
From: Daniel S. <aph...@us...> - 2007-06-11 05:30:43
|
Update of /cvsroot/plt-spy/python/examples In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21939/examples Added Files: example1.spy example2.spy example3.spy scheme-dcop-browser.ss scheme-dcop.ss Log Message: Spy examples --- NEW FILE: example2.spy --- (module example2 "../full-spy-language.ss" ;; Tuples and functions with rest arguments (define (f x . more) (+ more (mktuple 4 5))) (display "f(1,2,3) = ") (display (f 1 2 3)) (newline) (provide f)) --- NEW FILE: scheme-dcop-browser.ss --- (require "scheme-dcop.ss" (lib "class.ss") (lib "mred.ss" "mred")) (define frame (new frame% [label "DCOP Browser"])) (define vpanel (new vertical-panel% [parent frame])) (define toolbar (new horizontal-panel% [parent vpanel] [stretchable-height #f])) (define navigation-pane (new horizontal-panel% [parent vpanel])) (define results (new text-field% [label "Results"] [parent vpanel] [callback void])) (define refresh (new button% [label "Refresh"] [parent toolbar] [callback (lambda (obj evt) (refresh-apps))])) (define execute (new button% [label "Execute"] [parent toolbar] [callback (lambda (obj evt) (print-results (dcop-call (get-app) (get-obj) (get-method))))])) (define apps (new list-box% [label "Applications"] [choices null] [parent navigation-pane] [style '(single vertical-label)] [callback (lambda (obj evt) (on-select-app (get-app)))])) (define objs (new list-box% [label "Objects"] [choices null] [parent navigation-pane] [style '(single vertical-label)] [callback (lambda (obj evt) (on-select-obj (get-app) (get-obj)))])) (define methods (new list-box% [label "Methods"] [choices null] [parent navigation-pane] [style '(single vertical-label)] [callback void])) (define (refresh-apps) (send apps clear) (send objs clear) (send methods clear) (for-each (lambda (app) (send apps append app)) (app-list))) (define (on-select-app app-name) (send objs clear) (send methods clear) (for-each (lambda (obj) (send objs append obj)) (obj-list app-name))) (define (on-select-obj app-name obj-name) (send methods clear) (for-each (lambda (method) (send methods append method)) (method-list app-name obj-name))) (define (get-app) (send apps get-string-selection)) (define (get-obj) (send objs get-string-selection)) (define (get-method) (cadr (regexp-match #rx"[^ ]+ ([^(]+)\\(.*" (send methods get-string-selection)))) (define (print-results r) (send results set-value (format "~a" r))) (send frame show #t) --- NEW FILE: example3.spy --- (module example3 "../full-spy-language.ss" ;; Functions with keyword arguments (define (f x &all-keys kw) (display "x was ") (display x) (newline) (display "The keys were ") (display kw)) (display "calling f(1, b = 2, c = 3)") (newline) (f 1 :b 2 :c 3) (newline) (provide f)) --- NEW FILE: scheme-dcop.ss --- (module scheme-dcop mzscheme ;; Scheme bindings for DCOP based on Python's bindings (require "../runtime-support.ss" "../python.ss") ;; grab the functions we care about from Python (py-eval "import pcop") (define pcop (in-python (ns-get 'pcop))) (define pcop-app-list (py-get-attr/sym pcop 'app_list)) (define pcop-obj-list (py-get-attr/sym pcop 'obj_list)) (define pcop-method-list (py-get-attr/sym pcop 'method_list)) (define pcop-dcop-call (py-get-attr/sym pcop 'dcop_call)) ;; provide some Scheme wrappers (define (app-list) (map get-py-string (pylist->list (py-apply pcop-app-list)))) (define (obj-list app-name) (map get-py-string (pylist->list (py-apply pcop-obj-list (make-py-string app-name))))) (define (method-list app-name obj-name) (map get-py-string (pylist->list (py-apply pcop-method-list (make-py-string app-name) (make-py-string obj-name))))) (define (dcop-call app-name obj-name method-name . args) (define result (py-apply pcop-dcop-call (make-py-string app-name) (make-py-string obj-name) (make-py-string method-name) (list->pytuple (map scheme->python args)))) (python->scheme result)) (define (scheme->python x) (cond [(number? x) (make-py-number x)] [(string? x) (make-py-string x)] [(list? x) (list->pylist (map scheme->python x))] [else x])) (define (python->scheme x) (cond [(py-number? x) (get-py-number x)] [(py-string? x) (get-py-string x)] [(py-list? x) (map python->scheme (pylist->list x))] [(py-tuple? x) (map python->scheme (pytuple->list x))])) (require (lib "contract.ss")) (provide/contract [app-list (-> (listof string?))] [obj-list (string? . -> . (listof string?))] [method-list (string? string? . -> . (listof string?))] [dcop-call (string? string? string? . -> . any/c)])) --- NEW FILE: example1.spy --- (module example1 "../full-spy-language.ss" ;; Values and the identity function (define (f x) x) (define y 3) (display "y: ") (display y) (newline) (display "f(4) = ") (display (f 4)) (newline) (provide f y)) |
From: Daniel S. <aph...@us...> - 2007-06-11 05:30:43
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21939 Added Files: wrapper-test.scm Log Message: Spy examples --- NEW FILE: wrapper-test.scm --- (require (lib "class.ss") "convert.ss" "python.ss") (py-eval " class C(object): def m(self, x): print 'this is m' return x ") (define pyC (in-python (ns-get #cs'C))) (define scmC (wrap-pyclass pyC)) (define obj (make-object scmC)) (display "obj.m(2) = ") (display (send obj m 2)) (newline) (py-eval " class D(object): def n(self, x): return x ") (py-eval " class E(D): def o(self, x): return x ") (py-eval " class F(E, C): def p(self, x): print 'this is p' return x ") ;; should be (F (E (D (object proxy))) (C proxy)) ;;; but with one arg ;; (F (E (D (object (C proxy))))) (define f% (wrap-pyclass (in-python (ns-get #cs'F)))) (define f (new f%)) (printf "f.p(2) = ~a~n" (send f p 2)) (printf "f.o(2) = ~a~n" (send f o 2)) (printf "f.n(2) = ~a~n" (send f n 2)) (printf "f.m(2) = ~a~n" (send f m 2)) (py-eval " class G(object): x = 3 def printx(self): print self.x ") (define g% (wrap-pyclass (py-eval "G"))) (define g (new g%)) (printf "g.x = ~a~n" (send g x)) (printf "setting g.x to 5~n") (send g x 5) (printf "g.x = ~a~n" (send g x)) (py-eval "import gtk") (define pywin (py-eval "gtk.Window")) (define win% (wrap-pyclass pywin)) (define win (new win%)) (define pygtkmain (py-eval "gtk.main")) (define pygtkmainquit (py-eval "gtk.main_quit")) (send win connect "destroy" pygtkmainquit) (send win set_title "hello mz classes") (send win show) (define gtk-main (wrap-pyfun pygtkmain)) (gtk-main) |
From: Daniel S. <aph...@us...> - 2007-06-11 05:29:15
|
Update of /cvsroot/plt-spy/python/examples In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv21483/examples Log Message: Directory /cvsroot/plt-spy/python/examples added to the repository |
From: Daniel S. <aph...@us...> - 2007-06-11 05:18:06
|
Update of /cvsroot/plt-spy/python/embed In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16928/embed Modified Files: Makefile embed.c spy.h Log Message: Embedding Index: Makefile =================================================================== RCS file: /cvsroot/plt-spy/python/embed/Makefile,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- Makefile 14 Dec 2004 10:41:02 -0000 1.2 +++ Makefile 11 Jun 2007 05:18:05 -0000 1.3 @@ -2,7 +2,7 @@ #MZC=/home/dsilva/plt/bin/mzc #MZC=mzc #MZC=/home/dsilva/plt-299/bin/mzc -MZC=/home/dsilva/cvs/plt-nightly/plt/bin-nogc/mzc +MZC=/home/dsilva/cvs/plt/bin/mzc #MZC=/tmp/plt-299.22/bin/mzc CC=$(MZC) COPTIMIZE_FLAGS=++ccf -ftracer ++ccf -foptimize-sibling-calls @@ -10,7 +10,8 @@ #CFLAGS=++ccf -I. ++ccf -g ++ccf -finline-functions ++ccf -foptimize-sibling-calls ++ccf -mtail-call --cc CFLAGS= ++ccf -I. ++ccf -I/usr/include/python2.3 $(CWARNING_FLAGS) ++ccf -g3 ++ccf -fPIC ++ccf -Wl,-E $(COPTIMIZE_FLAGS) --cc -PY_EXT_DIR=/usr/lib/python2.3/site-packages +#PY_EXT_DIR=/usr/lib/python2.3/site-packages +PY_EXT_DIR=/dev/null ### hack, plus skip OpenGL bindings PY_EXTENSIONS=$(shell find $(PY_EXT_DIR) -name "*.so" | grep -v GL) ALL_RPATHS=$(foreach e,$(PY_EXTENSIONS),$(shell echo ++ldf -Wl,-rpath ++ldf $(shell dirname $(e)))) @@ -30,7 +31,8 @@ ALLOBJS=$(NEWOBJS) #$(OBJECTS) $(MODOBJS) $(CPYOBJS) $(PARSEROBJS) -LIBS=++ldf -lpthread ++ldf -lutil ++ldf -lpython2.3 ++ldf -lgtk-x11-2.0 /usr/lib/python2.3/site-packages/gtk-2.0/gobject.so $(PY_EXTENSIONS) +#LIBS=++ldf -lpthread ++ldf -lutil ++ldf -lpython2.3 ++ldf -lgtk-x11-2.0 /usr/lib/python2.3/site-packages/gtk-2.0/gobject.so $(PY_EXTENSIONS) +LIBS=++ldf -lpthread ++ldf -lutil ++ldf -lpython2.3 $(PY_EXTENSIONS) #MZLIBDIR=/home/dsilva/cvs/plt-nightly/plt/lib/ @@ -42,7 +44,7 @@ # echo MakeSpy: no errors. $(TARGET): $(ALLOBJS) - $(MZC) -v --linker /usr/bin/gcc --ld $(TARGET) ++ldf -g ++ldf -fPIC ++ldf -Xlinker ++ldf -export-dynamic ++ldf -Wl,-E $(RPATHS) $(LIBS) --ldf-show --ldl-show $(ALLOBJS) #/home/dsilva/incoming/modded-python/out/lib/libpython2.3.a + $(MZC) -v --linker /usr/bin/gcc --ld $(TARGET) ++ldf -g ++ldf -fPIC ++ldf -Xlinker ++ldf -export-dynamic ++ldf -Wl,-E $(RPATHS) $(LIBS) --ldf-show --ldl-show $(ALLOBJS) .c.o: $(CC) -d `dirname $@` $(CFLAGS) $< Index: spy.h =================================================================== RCS file: /cvsroot/plt-spy/python/embed/spy.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- spy.h 14 Dec 2004 10:41:02 -0000 1.3 +++ spy.h 11 Jun 2007 05:18:06 -0000 1.4 @@ -96,10 +96,11 @@ if (!p) { #ifdef DEBUG_SPY - fprintf(stderr, "SCM: got a null pointer."); +/* fprintf(stderr, "SCM: got a null pointer."); int* foo = NULL; int bar = (int) *foo; assert(0); +*/ #else scheme_signal_error("SCM: got a null pointer."); #endif Index: embed.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/embed.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- embed.c 14 Dec 2004 10:41:02 -0000 1.3 +++ embed.c 11 Jun 2007 05:18:06 -0000 1.4 @@ -3,6 +3,8 @@ Scheme_Env* spy_global_env; Scheme_Object* spy_none; +Scheme_Object* spy_true; +Scheme_Object* spy_false; //static Scheme_Object* spy_apply_argv[1024]; @@ -298,8 +300,11 @@ } } +#include <dlfcn.h> + Scheme_Object* scheme_initialize(Scheme_Env* enclosing_ns) { + dlopen("/usr/lib/libpython2.3.so", RTLD_LAZY | RTLD_GLOBAL); scheme_register_extension_global(&spy_tag, sizeof(Scheme_Object*)); // scheme_register_extension_global(&spy_apply_argv, sizeof(spy_apply_argv)); spy_tag = sym("spy-object"); @@ -307,32 +312,51 @@ Py_Initialize(); PRINTF("[scheme_initialize] Initialized libpython.\n"); PyType_Ready(&PySchemeFunction_Type); + spy_none = SCM(Py_None); + spy_true = SCM(Py_True); + spy_false = SCM(Py_False); + + spy_global_env = enclosing_ns; // spy_register_pytoken(ns); // if(1) { PyObject* warn = PyExc_Warning; } - { - Scheme_Env* ns = scheme_primitive_module(scheme_module_name(), enclosing_ns); - scheme_register_extension_global(&spy_global_env, sizeof(Scheme_Env*)); - scheme_register_extension_global(&spy_none, sizeof(spy_none)); - spy_global_env = enclosing_ns; - spy_none = SCM(Py_None); - scheme_add_global("__pytoken__", pytoken, ns); - scheme_add_global("cpy-none", spy_none, ns); - scheme_add_global("cpy-true", SCM(Py_True), ns); - scheme_add_global("cpy-false", SCM(Py_False), ns); - - scheme_load_prim(ns, scm_prim, sizeof(scm_prim)); - spy_load_builtin_types(ns, spy_builtin_types, sizeof(spy_builtin_types)); - - scheme_finish_primitive_module(ns); - } return scheme_reload(enclosing_ns); } +/* We must build the module every time reload is called. + Otherwise, this happens: + +$ mzscheme +Welcome to MzScheme version 299.30, Copyright (c) 2004-2005 PLT Scheme, Inc. +> (parameterize ([current-namespace (make-namespace 'initial)]) + (eval '(require "embed/embed.ss"))) +[scheme_initialize] Initializing libpython... +[scheme_initialize] Initialized libpython. +> (parameterize ([current-namespace (make-namespace 'initial)]) + (eval '(require "embed/embed.ss"))) +require: unknown module: |,/home/dsilva/.plt-scheme/299.30/collects/python/embed/embed| +> + +*/ + Scheme_Object* scheme_reload(Scheme_Env* enclosing_ns) { + { + Scheme_Env* ns = scheme_primitive_module(scheme_module_name(), enclosing_ns); + scheme_register_extension_global(&spy_global_env, sizeof(Scheme_Env*)); + scheme_register_extension_global(&spy_none, sizeof(spy_none)); + scheme_add_global("__pytoken__", pytoken, ns); + scheme_add_global("cpy-none", spy_none, ns); + scheme_add_global("cpy-true", spy_true, ns); + scheme_add_global("cpy-false", spy_false, ns); + + scheme_load_prim(ns, scm_prim, sizeof(scm_prim)); + spy_load_builtin_types(ns, spy_builtin_types, sizeof(spy_builtin_types)); + + scheme_finish_primitive_module(ns); + } return scheme_void; } |
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv16140 Modified Files: TODO base.ss compiler-expr.ss compiler-stmt.ss compiler.ss parser.ss python-import.ss python.ss runtime-support.ss tool.ss Added Files: callcc.py callec.py clazz.py convert.ss core-spy-language.ss define_y_and_f.py full-spy-language.ss generator_oleg.py inner_def.py p.py python-eval-aot.ss spy-primitives.ss spy.scm tail_try_except.py tiny_callcc.py tiny_callec.py Log Message: Spy module language. Index: runtime-support.ss =================================================================== RCS file: /cvsroot/plt-spy/python/runtime-support.ss,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- runtime-support.ss 14 Dec 2004 10:41:02 -0000 1.6 +++ runtime-support.ss 11 Jun 2007 05:15:41 -0000 1.7 @@ -1,5 +1,5 @@ (module runtime-support mzscheme - (require (lib "errortrace.ss" "errortrace")) +; (require (lib "errortrace.ss" "errortrace")) (require (lib "list.ss") (lib "etc.ss") ;"primitives.ss" @@ -163,6 +163,7 @@ (rename cpy-none py-none) py-number? py-list? + py-tuple? py-string? python-node? python-object? @@ -269,6 +270,7 @@ (define py-number? spy-number?) (define py-string? spy-string?) (define py-list? spy-list?) + (define py-tuple? spy-tuple?) (define python-get-type-name spy-get-type-name) (define (py-none? x) @@ -345,7 +347,7 @@ (define (python-initialize) (unless *initialized?* - (init-python-import) +; (init-python-import) (spy-initialize python-import-submodule-for-c) (set! *initialized?* #t))) (provide python-initialize) --- NEW FILE: tiny_callec.py --- def f(x): return callec( lambda ec: ec(x) ) print "f(2) is:" print f(2) Index: python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python.ss,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- python.ss 14 Dec 2004 10:41:02 -0000 1.6 +++ python.ss 11 Jun 2007 05:15:41 -0000 1.7 @@ -1,25 +1,25 @@ (module python mzscheme - (require (lib "errortrace.ss" "errortrace")) + ; (require (lib "errortrace.ss" "errortrace")) (require (lib "class.ss") (lib "list.ss") (lib "etc.ss") (lib "contract.ss") - ; "compiler.ss" + ; "compiler.ss" "python-node.ss" - ; "primitives.ss" ;; need py-object%->string - ; "read-python.ss" + ; "primitives.ss" ;; need py-object%->string + ; "read-python.ss" "compile-python.ss" "python-import.ss" ;"base.ss" "runtime-support.ss" - ; "c-bindings.ss" + ; "c-bindings.ss" ;"get-base.ss" ) - + (provide in-ns in-python) - + (define (py-gen-result? x) (or (syntax? x) (python-node? x))) -#| + #| (provide/contract [python (string? . -> . (listof string?))] [py-eval/asts ((listof syntax?) . -> . (listof string?))] @@ -33,13 +33,13 @@ [render-python-value ((union python-node? void?) port? procedure? . -> . void?)] [render-python-value/format ((union python-node? void?) port? procedure? . -> . void?)]) |# - + (provide python py-eval/asts/to-strs py-eval/asts py-eval py-eval0 py-eval* py-eval/to-strs py-gen ssd ssd* python-ns ns-set! ns-get render-python-value render-python-value/format) (define ssd syntax-object->datum) (define (ssd* lst) (map ssd lst)) - + (require (lib "pretty.ss")) (define (py-disp str) (pretty-print (ssd* (py-gen str)))) @@ -51,15 +51,15 @@ ;(namespace-require '(lib "primitives.ss" "python")) ;((namespace-variable-value 'py-object%->string) value)) (get-py-string (py-object->py-string value))) - ; ((dynamic-require '(lib "primitives.ss" "python") 'py-object%->string) value)) - + ; ((dynamic-require '(lib "primitives.ss" "python") 'py-object%->string) value)) + (define (none? py-value) (eqv? py-value py-none)) -; ((dynamic-require '(lib "primitives.ss" "python") 'py-none?) py-value)) - + ; ((dynamic-require '(lib "primitives.ss" "python") 'py-none?) py-value)) + (define (render-python-value/format value port port-write) (render-python-value value port port-write)) - + (define (render-python-value value port port-write) (unless (none? value) (let ([to-render (if (python-node? value) @@ -68,8 +68,8 @@ (if #f ;port-write (port-write to-render) (display to-render port))))) - -#| (require (lib "pconvert.ss")) + + #| (require (lib "pconvert.ss")) (install-converting-printer) (define hook current-print-convert-hook) (define old-hook (hook)) @@ -79,45 +79,45 @@ (old-hook value basic-convert sub-convert))) (hook?new-hook) |# - -; (current-directory "/home/dsilva/incoming/Python-2.3.4/Lib") -; (python-initialize) - (define pns ;(make-namespace)) - (make-python-namespace)) + + ; (current-directory "/home/dsilva/incoming/Python-2.3.4/Lib") + ; (python-initialize) + (define pns ; (make-namespace)) + (make-python-namespace)) ;(set-python-namespace-name! pns '__main__) - - ; (set-pyns! pns) - + + ; (set-pyns! pns) + (define python-ns (make-parameter pns)) (define python-ss-module-ns (current-namespace)) - + (define (get-py-symbol pystr) (string->symbol (get-py-string pystr))) - + (define pystring->symbol get-py-symbol) - + (define (load-builtins-into-ns ns) (define builtin (python-load-module/dotted "__builtin__")) (define pydict (py-get-attr/sym builtin '__dict__)) (define pykeys (python-method-call pydict 'keys)) (define keyvec (pylist->vector pykeys)) (define veclen (vector-length keyvec)) - (parameterize ([current-namespace pns]) - (let loop ([idx 0]) - (unless (= idx veclen) - (let* ([pyname (vector-ref keyvec idx)] - [name (pystring->symbol pyname)]) - ;; TODO: should we use dict[name] or module.name? - (unless (eq? '__name__ name) ;; skip the special symbols - (namespace-set-variable-value! name - (py-get-attr/sym builtin name))) - (loop (add1 idx))))))) - + (parameterize ([current-namespace pns]) + (let loop ([idx 0]) + (unless (= idx veclen) + (let* ([pyname (vector-ref keyvec idx)] + [name (pystring->symbol pyname)]) + ;; TODO: should we use dict[name] or module.name? + (unless (eq? '__name__ name) ;; skip the special symbols + (namespace-set-variable-value! name + (py-get-attr/sym builtin name))) + (loop (add1 idx))))))) + (load-builtins-into-ns pns) - -; (define (python-ns) -; pns) - + + ; (define (python-ns) + ; pns) + #| (define (load-ps) (parameterize ([current-namespace pns]) @@ -125,30 +125,30 @@ "c" "stringobject.so"))) (copy-namespace-bindings pns (current-namespace) #f #f)) |# - + (define (py-eval/asts/to-strs asts) (map convert-value (py-eval/asts asts))) - + (define (py-eval/asts asts) (eval-python asts (python-ns))) - + (define (py-eval* str) (py-eval/asts (py-gen str))) - + (define (py-eval/to-strs str) (map convert-value (py-eval* str))) - + (define (py-eval0 str) (car (py-eval* str))) - + (define (py-eval str) (let ([lst (py-eval* str)]) (if (null? lst) lst (car (last-pair lst))))) - + ;(require "base.ss") @@ -164,8 +164,8 @@ (unless (andmap syntax? sol) (error 'python "epexted syntax object list")) (eval-python sol (python-ns)))))) -; (eval-python (py-gen/file path) (python-ns))))) - + ; (eval-python (py-gen/file path) (python-ns))))) + (define-syntax (in-ns stx) (syntax-case stx () [(_ ns exprs ...) #`(parameterize ([current-namespace ns]) @@ -177,7 +177,7 @@ exprs ...)])) (define ns-set! namespace-set-variable-value!) - + (define ns-get namespace-variable-value) Index: tool.ss =================================================================== RCS file: /cvsroot/plt-spy/python/tool.ss,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- tool.ss 10 Apr 2004 18:42:08 -0000 1.1.1.1 +++ tool.ss 11 Jun 2007 05:15:41 -0000 1.2 @@ -82,6 +82,8 @@ (define lang% (class* object% (drscheme:language:language<%>) + + (define/public (get-comment-character) (values "'COMMENT'" #\#)) (define/public (config-panel parent) @@ -120,16 +122,19 @@ (define/public (front-end input settings) (set! front-end-ns (current-namespace)) (let-values ([(port name) - (if (string? input) - (values (open-input-file input) (path->complete-path input)) - (let ([text (drscheme:language:text/pos-text input)]) + (cond + [(string? input) + (values (open-input-file input) (path->complete-path input))] + [(input-port? input) + (values input "python tool.ss: FIXME: input name")] + [else (let ([text (drscheme:language:text/pos-text input)]) (values (open-input-string (send text get-text (drscheme:language:text/pos-start input) (drscheme:language:text/pos-end input))) - text)))]) + text))])]) (let ([ast-list (read-python-port port name)]) (close-input-port port) @@ -165,7 +170,9 @@ (add-annotation e) e))) (drscheme:debug:test-coverage-enabled (python-settings-test-coverage? settings)) - (init-python-namespace (current-namespace))))) + ;; TODO: init + ;(init-python-namespace (current-namespace)) + ))) #| (dynamic-require '(lib "base.ss" "python") #f) (let ([base-path ((current-module-name-resolver) '(lib "base.ss" "python") #f #f)] @@ -206,17 +213,17 @@ )))) |# - (define/public (render-value value settings port port-write) - (render-python-value value port port-write)) + (define/public (render-value value settings port) + (render-python-value value port write)) - (define/public (render-value/format value settings port port-write width) - (render-python-value/format value port port-write)) + (define/public (render-value/format value settings port width) + (render-python-value/format value port write)) ;; default implementation provided by Robby (define/public (order-manuals x) (values x #t)) - ;; TODO: fixme - (define/public (create-executable settings parent src-file) + ;; TODO: fixme, implement me? + (define/public (create-executable settings parent src-file teachpacks) (let ([dst-file (drscheme:language:put-executable parent src-file #f #f (string-constant save-a-mzscheme-stand-alone-executable))]) --- NEW FILE: callec.py --- def find5(lst): def proc(ec): for x in lst: if x == 5: ec(1) ec(0) return callec(proc) a = [2,4,6,8] b = [1,3,5,7] print "a:" print a print "b:" print b print "is 5 in a?" if find5(a): print "yep, 5 is in a" else: print "nope, 5 is not in a" print "is 5 in b?" if find5(b): print "yep, 5 is in b" else: print "nope, 5 is not in b" Index: compiler-stmt.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-stmt.ss,v retrieving revision 1.7 retrieving revision 1.8 diff -u -d -r1.7 -r1.8 --- compiler-stmt.ss 14 Dec 2004 10:41:02 -0000 1.7 +++ compiler-stmt.ss 11 Jun 2007 05:15:41 -0000 1.8 @@ -1,5 +1,5 @@ (module compiler-stmt mzscheme - (require (lib "errortrace.ss" "errortrace")) +; (require (lib "errortrace.ss" "errortrace")) (require (lib "class.ss") (lib "list.ss") @@ -1280,10 +1280,10 @@ ;;daniel - (inherit ->orig-so ->lex-so) + (inherit ->orig-so ->lex-so ->top-so) (inherit-field substatements) - (define/override (to-scheme) - (->orig-so (let ([proc-name (send name to-scheme)] + (define/override (to-scheme) + (->top-so (let ([proc-name (send name to-scheme)] [body-so (send body to-scheme return-symbol #t #t)]) `(,(if (top?) 'define 'set!) ,proc-name Index: base.ss =================================================================== RCS file: /cvsroot/plt-spy/python/base.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- base.ss 14 Dec 2004 10:41:02 -0000 1.4 +++ base.ss 11 Jun 2007 05:15:41 -0000 1.5 @@ -1,5 +1,5 @@ (module base mzscheme - (require (lib "errortrace.ss" "errortrace")) + ;(require (lib "errortrace.ss" "errortrace")) (require "runtime-support.ss" (lib "contract.ss") (lib "etc.ss") Index: python-import.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python-import.ss,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- python-import.ss 14 Dec 2004 10:41:02 -0000 1.6 +++ python-import.ss 11 Jun 2007 05:15:41 -0000 1.7 @@ -1,6 +1,6 @@ (module python-import mzscheme - (require (lib "errortrace.ss" "errortrace")) - +; (require (lib "errortrace.ss" "errortrace")) + (require (lib "list.ss") (lib "file.ss") (lib "etc.ss") @@ -15,7 +15,7 @@ python-load-module-path copy-namespace-bindings make-python-namespace ; note that make-python-namespace already calls init-... - init-python-namespace + ; init-python-namespace ;; eval-python-ns eval-python-inner-ns @@ -226,20 +226,27 @@ |# (define (init-python-namespace py-ns) - (unless base-path - (error 'init-python-namespace "Please initialize python-import.ss with (init-python-import) first.")) +; (unless base-path +; (error 'init-python-namespace "Please initialize python-import.ss with (init-python-import) first.")) (with-handlers ([void (lambda (e) (printf (exn-message e)))]) + (when (not base-path) + (set! spec `(file ,(path->string (build-path (this-expression-source-directory) + "full-spy-language.ss")))) + (set! base-path ((current-module-name-resolver) spec #f #f))) + (dynamic-require base-path #f) (parameterize ([current-namespace py-ns]) (namespace-attach-module outer-namespace base-path) - (namespace-require base-path)))) + (namespace-require spec)))) (define outer-namespace (current-namespace)) (define base-path #f) - + (define spec #f) + + #| (define (init-python-import) (define base-spec `(file ,(path->string (build-path (this-expression-source-directory) - "base.ss"))));'(lib "base.ss" "python") + "full-spy-language.ss"))));'(lib "base.ss" "python") (parameterize ([current-namespace outer-namespace]) (dynamic-require base-spec #f) @@ -247,9 +254,10 @@ (set! base-path ((current-module-name-resolver) base-spec #f #f)) (printf "base-path after: ~a~n" base-path))) - (init-python-import) +; (init-python-import) (provide init-python-import) + |# --- NEW FILE: convert.ss --- (module convert mzscheme (require (lib "list.ss") (lib "class.ss") (lib "etc.ss") (lib "pretty.ss") "runtime-support.ss" ) (provide scheme->python python->scheme wrap-pyfun ; mk-wrap-pymethod wrap-pyclass) ;; TODO: rewrite the cond as a jump table (define (scheme->python value) (cond [(integer? value) (integer->pyint value)] [(number? value) (make-py-number value)] [(symbol? value) (make-py-symbol value)] [(string? value) (make-py-string value)] [(vector? value) (let ([len (vector-length value)]) (if (or (zero? len) ;; HACK (python-object? (vector-ref value 0))) (vector->pylist value) ;; TODO: if all ints, intvector->pylist (let ([v (make-vector len)]) (let loop ([idx 0]) (unless (= len idx) (vector-set! v idx (scheme->python (vector-ref value idx))) (loop (add1 idx)))) v)))] [(list? value) (list->pylist (map scheme->python value))] [(python-object? value) value] [(void? value) py-none] [else (error 'scheme->python (format "Could not convert to a Python object: ~a" value))])) ;; TODO: rewrite cond as a jump table (define (python->scheme obj) (cond [(not (python-object? obj)) obj] [(py-number? obj) (get-py-number obj)] [(py-string? obj) (get-py-string obj)] [(py-list? obj) (map python->scheme (pylist->list obj))] [(py-none? obj) (void)] [else (error 'python->scheme (format "Could not convert to a Scheme object: ~a" (pyobject->string obj)))])) (define (pyobject->symbol pobj) (string->symbol (pyobject->string pobj))) ;; TODO: if (instanceof? pyfun py-schemefun) unwrap.. (define (wrap-pyfun pyfun) (lambda args (apply py-apply pyfun (map scheme->python args)))) ; (define (mk-wrap-pymethod pyfun) ; ;; we don't have the 'self' object until runtime, after instantiation ; (lambda (pyself) ; (lambda args ; (apply py-apply pyfun pyself ; (map scheme->python args))))) (define *wrapped-pyclasses* (make-hash-table)) (define *py-mixins* (make-hash-table)) (define python-proxy% (class object% (define/public (py-obj) (error 'python-proxy% "py-obj is abstract.")) (super-new))) (define (get-raw-class-attribute-value pyclass member-pyname) (define pydict (py-get-attr/sym pyclass '__dict__)) (define bases (pytuple->list (py-get-attr/sym pyclass '__bases__))) (with-handlers ([void (lambda _ (ormap (lambda (base) (get-raw-class-attribute-value base member-pyname)) bases))]) (py-get-item/obj pydict member-pyname))) ;; pyclass->mixin/dynamic is similar to pyclass->mixin/static, but it dynamically looks up Python methods at each method invocation. ;; Example - dynamic: #| (let ([C (py-eval "C")]) (lambda (super%) (class super% (define pyobj (py-apply C)) (define/public (m . args) (python->scheme (apply python-method-call pyobj 'm (map scheme->python args)))) (super-new)))) |# ;; Example - static: #| (let ([C:m (py-eval "C.__dict__['m']")]) (lambda (super%) (class super% (define pyobj (py-apply C)) (define/public (m . args) (python->scheme (apply spy-apply C:m pyobj (map scheme->python args)))) (super-new))) |# (define (pyclass->mixin/static pyclass) (define table-id (string->symbol (pyobject->string pyclass))) (hash-table-get *py-mixins* table-id (lambda () (define pydict (py-get-attr/sym pyclass '__dict__)) (define pybuiltin (python-load-module/dotted "__builtin__")) (define pydir (py-get-attr/sym pybuiltin 'dir)) ;; TODO: provide only this class's fields? pyclass.__dict__.keys() ? ;(define keys (pylist->list (py-apply pydir pyclass))) (define keys (pylist->list (python-method-call pydict 'keys))) (define orig-bases (pytuple->list (py-get-attr/sym pyclass '__bases__))) ;(define pyname (py-get-attr/sym pyclass '__name__)) (define (mixin %) (define expr `(class ,% ;; the asterisk is just a random symbol to make it an invalid python id (define pyobj* #f) (define/override (py-obj) (or pyobj* (begin (set! pyobj* (,py-apply ,pyclass)) pyobj*))) ;; cache (py-obj) ; (define/override (class-id) ; ( ,@(map (lambda (member-pyname) (define member-name (pyobject->symbol member-pyname)) (unless (member member-name '(__init__ __new__ __reduce__ __doc__ __reduce_ex__ __repr__ __str__ __class__ __delattr__ __setattr__ __getattr__ __dict__ __weakref__ __getattribute__ __hash__ __cmp__)) (let () ;; TODO: maybe use the dict? (py-get-item/obj pydict member-pyname) (define attribute (py-get-attr/obj pyclass member-pyname)) (define member-value (get-raw-class-attribute-value pyclass member-pyname)) (define (bases-list clazz) (define lst (pytuple->list (py-get-attr/sym pyclass '__bases__))) (if (null? lst) (list cpy-object) lst)) (define (bases-have-member? clazz) (ormap (lambda (base) (with-handlers ([void (lambda _ #f)]) (py-get-attr/obj base member-pyname))) ;(or (with-handlers ([void (lambda _ #f)]) ; (py-get-attr/obj base member-pyname)) ; (bases-have-member? base))) (bases-list clazz))) (define override? (bases-have-member? pyclass)) `(,(if override? 'define/override 'define/public) (,member-name . the-args) ;; TODO: staticmethod, classmethod support ,(if (py-callable? member-value) `(begin (printf "calling method ~a~n" ',member-name) (printf "member value: ~a~n" ,member-value) (printf "member value (str): ~a~n" ,(pyobject->string member-value)) (printf "result: ~a~n" (,apply ,py-apply ,member-value (py-obj) (,map ,scheme->python the-args))) (,python->scheme (,apply ,py-apply ,member-value (py-obj) (,map ,scheme->python the-args)))) `(if (null? the-args) (,python->scheme (,py-get-item/obj ,pydict ,member-pyname)) (,py-set-attr/obj ,pyclass ,member-pyname (,scheme->python (,car the-args))))))))) keys) (super-new))) (printf "Mixing in the following methods from ~a with ~a~n" table-id %) ;(pretty-print expr) ;; HACK (eval expr module-ns)) (define the-mixin (lambda (%) (foldr (lambda (next-mixin acc) (printf "next: ~a acc: ~a~n" next-mixin acc) (let ([new-acc (next-mixin acc)]) (printf "new acc: ~a~n" new-acc) new-acc) ) % (cons mixin (map pyclass->mixin orig-bases))))) (hash-table-put! *py-mixins* table-id the-mixin) the-mixin))) (define (wrap-pyclass pyclass) (unless (py-is-a? pyclass py-type) (error 'wrap-pyclass (format "Expected a Python type, got: ~a~n" (pyobject->string pyclass)))) (let ([table-id (string->symbol (pyobject->string pyclass))]) ; (define table-id (string->symbol (pyobject->string pyclass))) (hash-table-get *wrapped-pyclasses* table-id (lambda () (define mixin (pyclass->mixin pyclass)) (define orig-bases (pytuple->list (py-get-attr/sym pyclass '__bases__))) (define bases (if (null? orig-bases) (list python-proxy%) bases)) (define clazz (mixin python-proxy%)) (hash-table-put! *wrapped-pyclasses* table-id clazz) clazz)))) (define module-ns (current-namespace)) ) --- NEW FILE: full-spy-language.ss --- (module full-spy-language "core-spy-language.ss" (provide (all-from "core-spy-language.ss") (all-from "spy-primitives.ss")) (require "spy-primitives.ss") ) Index: compiler.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- compiler.ss 23 Nov 2004 12:57:15 -0000 1.4 +++ compiler.ss 11 Jun 2007 05:15:41 -0000 1.5 @@ -58,6 +58,9 @@ ;; same as ->orig-so, but with lexical information (define/public (->lex-so datum context) (datum->syntax-object context datum src-loc stx-orig-prop)) + + (define/public (->top-so datum) + (->lex-so datum (current-toplevel-context))) ;; set-bindings!: (or/f fales? (is-a?/c def%) (is-a?/c class%) (is-a?/c lambda%)) -> ;; Compute all the bindings this statement could introduce into --- NEW FILE: inner_def.py --- def f(): def g(): return 1 return g --- NEW FILE: tail_try_except.py --- def f(): try: return "hello world" except e: print "exn caught" raise e --- NEW FILE: generator_oleg.py --- # Implemetation of Generators in Python+callc. # Ported from Oleg's Scheme generators # http://okmij.org/ftp/Scheme/enumerators-callcc.html#Generators # Python has no DELAY form so we'll use thunks instead import __builtin__ False = __builtin__.False StopIteration = __builtin__.StopIteration len = __builtin__.len map = __builtin__.map def generative(generator): def f1(k_main): def suspend(val): def f2(k_reenter): def f3(k_new_main): global k_main k_main = k_new_main return k_reenter(False) return k_main([val, lambda: callcc(f3)]) return callcc(f2) generator(suspend) return k_main([]) return lambda: callcc(f1) class It(object): def __init__(self, init_cursor): self.init_cursor = init_cursor self.promise = init_cursor def next(self): if not self.promise: raise StopIteration def getres(): try: return self.promise() except e: self.promise = False raise e res = getres() if res: self.promise = res[1] return res[0] else: self.promise = False return self.next() def reset(self): self.promise = self.init_cursor def make_py_iterator(fun): return It(generative(fun)) # foreach algorithm: def foreach_algorithm(proc, *seqs): if seqs: for x in range(0, len(seqs[0])): args = map(lambda seq: seq[x], seqs) apply(proc, args) # hand-optimized foreach, about 1.5X speed: def foreach(proc, *seqs): if seqs: top = len(seqs[0]) seqs_len = len(seqs) # optimize for one sequence if seqs_len == 1: seq = seqs[0] x = 0 while x < top: proc(seq) x += 1 else: args = [seq[0] for seq in seqs] proc(*args) x = 1 while x < top: # map looks better but is slower in cpython #proc(*map(lambda seq: seq[x], seqs)) # list comprehensions are a bit faster in cpython #proc(*[seq[x] for seq in seqs]) # going manually is still faster, though.. #args = [] #for seq in seqs: args.append(seq) # actually, this is even faster, since it only allocates 'args' once y = 0 while y < seqs_len: args[y] = seqs[y][x] y += 1 proc(*args) x += 1 def measure(thunk): from time import clock before = clock() thunk() after = clock() print "clocked: ", after - before def list_iter(lst): def f(yiild): for x in lst: yiild(x) return make_py_iterator(f) #def list_iter(lst): # def f(yiild): # return foreach_algorithm(yiild, lst) # return make_py_iterator(f) #def list_iter(lst): # return make_py_iterator(lambda yiild: [yiild(x) for x in lst]) #def list_iter(lst): # return make_py_iterator(lambda yiild: foreach(yiild, lst)) lst = [1,2,3,4,5] print "lst:", lst it = list_iter(lst) print "it:", it print "next:", it.next() print "next:", it.next() print "next:", it.next() print "next:", it.next() print "next:", it.next() try: print "next:", it.next() except StopIteration: print "caught StopIteration as expected" print "resetting" it.reset() print "next:", it.next() print "next:", it.next() print "resetting" it.reset() print "next:", it.next() print "next:", it.next() print "next:", it.next() print "next:", it.next() Index: parser.ss =================================================================== RCS file: /cvsroot/plt-spy/python/parser.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- parser.ss 14 Dec 2004 10:41:02 -0000 1.4 +++ parser.ss 11 Jun 2007 05:15:41 -0000 1.5 @@ -1,6 +1,6 @@ (module parser mzscheme - (require (lib "errortrace.ss" "errortrace")) - +; (require (lib "errortrace.ss" "errortrace")) + (require (lib "lex.ss" "parser-tools") (lib "yacc.ss" "parser-tools") (lib "readerr.ss" "syntax") @@ -12,7 +12,7 @@ "compiler-stmt.ss" "compiler-target.ss") (require (prefix : (lib "lex-plt-v200.ss" "parser-tools"))) - + ; (provide build-ast) (provide/contract (build-ast (input-port? . -> . (listof (is-a?/c ast-node%)))) @@ -742,5 +742,8 @@ (start-pos $1-start-pos) (end-pos $2-end-pos))) ((if test list_iter) (instantiate list-if% ($2 $3) - (start-pos $1-start-pos) (end-pos $3-end-pos)))))))) + (start-pos $1-start-pos) (end-pos $3-end-pos))))))) + + + ) --- NEW FILE: python-eval-aot.ss --- (module python-eval-aot mzscheme (require "python.ss") (require-for-syntax "python.ss") (provide py-eval/aot) ;; eval it ahead of time (define-syntax (py-eval/aot stx) (syntax-case stx () [(_ str) (string? (syntax-e (syntax str))) #`(or #,(py-eval (syntax-e (syntax str))))])) ) --- NEW FILE: core-spy-language.ss --- (module core-spy-language mzscheme (provide (rename my-define define) provide require define-syntax ; (all-from "base.ss") (rename my-lambda lambda) (rename my-datum #%datum) (rename swindle:#%top #%top) (rename my-app #%app) ; (rename my-module-begin #%module-begin) #%module-begin ) (require "runtime-support.ss") (require-for-syntax "runtime-support.ss") (require-for-syntax (lib "etc.ss")) (require-for-syntax (prefix swindle: (lib "base.ss" "swindle"))) (require (prefix swindle: (lib "base.ss" "swindle"))) ;(require "embed/embed.ss") (provide ;#%provide ;#%define ;; not sure if it's ok to remove these ;(rename #%begin begin) ;(rename #%provide provide) ;(rename #%define define) ;(rename #%require require) ;(rename #%datum->syntax-object datum->syntax-object) ;syntax (all-from "runtime-support.ss") ;; types (rename cpy-object object) (rename cpy-str str) (rename cpy-list list) (rename cpy-type type) (rename cpy-tuple tuple) (rename cpy-dict dict) (rename py-none |None|) (rename cpy-int int) (rename cpy-long long) (rename cpy-float float) (rename cpy-bool bool) (rename cpy-complex complex) (rename cpy-unicode unicode) (rename cpy-slice slice) ;(rename cpy-buffer buffer) ) ; (require "base.ss") (define-syntax (my-app stx) (syntax-case stx () [(_ fn args ...) #`(let ([f fn]) (if (procedure? f) (f args ...) (swindle:#%app py-apply f args ...)))])) (define-syntax (my-datum stx) (syntax-case stx () [(_ . datum) (let ([atom (syntax-e #'datum)]) #`(#%datum . #,(if (string? atom) (make-py-string atom) (make-py-number atom))))])) (define-syntax (my-lambda stx) (syntax-case stx () [(_ (args ... . rest) bodies ...) #`(make-py-function (make-py-code 'python-lambda (swindle:lambda (args ... . rest) (let ([rest (list->pytuple rest)]) bodies ...)) #,(length (syntax->list #'(args ...))) ;; FIXME: the last argument is deprecated null))] [(_ (args ...) bodies ...) #`(make-py-function (make-py-code 'python-lambda (swindle:lambda (args ...) bodies ...) #,(length (syntax->list #'(args ...))) ;; FIXME: the last argument is deprecated null))])) (define-syntax (my-define stx) (syntax-case stx (&all-keys) [(_ (fn args ... . rest) bodies ...) #`(define fn (make-py-function (make-py-code 'fn (swindle:lambda (args ... . rest) (let ([rest (list->pytuple rest)]) bodies ...)) #,(length (syntax->list #'(args ...))) ;; FIXME: the last argument is deprecated null)))] [(_ (fn args ... &all-keys kw) bodies ...) #`(define fn (make-py-function (make-py-code 'fn (swindle:lambda (args ... &all-keys kw) (let ([kw (let ([d (make-py-dict null)]) (let loop ([keys kw]) (if (null? keys) d (begin (py-set-item/obj d (make-py-string (substring (symbol->string (car keys)) 1)) (cadr keys)) (loop (cddr keys))))))]) bodies ...)) #,(length (syntax->list #'(args ...))) ;; FIXME: the last argument is deprecated null)))] [(_ (fn args ...) bodies ...) #`(define fn (make-py-function (make-py-code 'fn (swindle:lambda (args ...) bodies ...) #,(length (syntax->list #'(args ...))) ;; FIXME: the last argument is deprecated null)))] [(_ id value) #`(define id value)])) #| (define-syntax (my-module-begin stx) (syntax-case stx () [(_ (init [reg value] ...) instructions ...) #`(#%plain-module-begin (require #,(build-path (this-expression-source-directory) "mips.ss")) (execute (list (addi reg r0 value) ... instructions ...) (make-store)))] [else (raise-syntax-error 'MIPS "A MIPS module body must have the shape (init [reg value] ...) instructions ..." stx)])) |# ) Index: compiler-expr.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-expr.ss,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- compiler-expr.ss 14 Dec 2004 10:41:02 -0000 1.6 +++ compiler-expr.ss 11 Jun 2007 05:15:41 -0000 1.7 @@ -1,5 +1,5 @@ (module compiler-expr mzscheme - (require (lib "errortrace.ss" "errortrace")) + ;(require (lib "errortrace.ss" "errortrace")) (require (lib "class.ss") (lib "list.ss") @@ -13,6 +13,7 @@ ; "runtime-support.ss" "runtime-context.ss" "embed/embed.ss" + ;"embed.ss" ) (provide (all-defined)) @@ -220,7 +221,7 @@ (define/public (get-dict) dict) ;;daniel - (inherit ->orig-so) + (inherit ->orig-so ->top-so) (define/override (to-scheme) (let ([Ps (map (lambda (p) ;; for tuple arguments, just use the id of the first item (send (first-atom p) to-scheme)) @@ -236,6 +237,12 @@ (send (first-atom k) to-scheme)) (reverse key))]) ;(printf "parameters%-to-scheme~n") + (->top-so (cond + [(and seq dict) `(,@Ps ,@Ks &all-keys ,(send dict to-scheme) . ,(send seq to-scheme))] + [seq `(,@Ps ,@Ks . ,(send seq to-scheme))] + [dict `(,@Ps ,@Ks &all-keys ,(send dict to-scheme))] + [else `(,@Ps ,@Ks)])))) + #| (->orig-so (cond [(and seq dict) (begin ;(printf "identifier binding for seq: ~a~n" (identifier-binding (send seq to-scheme))) @@ -249,7 +256,7 @@ [dict ;;`(,(send dict to-scheme) ,@Ps ,@Ks)] `(,@Ps ,@Ks ,(send dict to-scheme))] [else `(,@Ps ,@Ks)])))) - + |# @@ -305,12 +312,14 @@ (define/public (get-value) value) ;;daniel - (inherit ->orig-so) + (inherit ->orig-so ->lex-so) + (define/override (to-scheme) - (->orig-so - (if (string? value) - (make-py-string value) - (make-py-number value)))) + (->lex-so value (current-toplevel-context))) + ; (->orig-so +; (if (string? value) +; (make-py-string value) +; (make-py-number value)))) #| `(,(py-so (if (string? value) 'make-py-string 'make-py-number)) @@ -695,9 +704,10 @@ (send rhs set-bindings! enclosing-scope)) ;;daniel - (inherit ->orig-so) + (inherit ->orig-so ->lex-so) (define/override (to-scheme) - (cond + (->lex-so `(,op ,(send lhs to-scheme) ,(send rhs to-scheme)) (current-toplevel-context))) +#| (cond [(eq? op 'and) (->orig-so `(,(py-so 'py-if) ,(send lhs to-scheme) ,(send rhs to-scheme) ,(py-so 'py-none)))] [(eq? op 'or) @@ -707,7 +717,7 @@ (->orig-so (let ([lhs-so (send lhs to-scheme)] [rhs-so (send rhs to-scheme)]) `(,(py-so (case op - [(+) 'py-add/safe] + [(+) (->lex-so '+ (current-toplevel-context))];'py-add/safe] [(-) 'py-sub] [(*) 'py-mul] [(/) 'py-div] @@ -716,6 +726,7 @@ [else (raise (format "binary% op unsupported: ~a" op))])) ,lhs-so ,rhs-so)))])) + |# (super-instantiate ()))) Index: TODO =================================================================== RCS file: /cvsroot/plt-spy/python/TODO,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- TODO 10 Apr 2004 20:30:45 -0000 1.1 +++ TODO 11 Jun 2007 05:15:41 -0000 1.2 @@ -1,3 +1,12 @@ +TOP-PRIORITY: + Python source code must expand into Full Spy modules. + Full Spy modules expand into Core Spy modules. + The Full Spy language is written as macros in Core Spy. + Core Spy modules expand into Textual MzScheme modules. + Textual MzScheme modules expand into Core Scheme. + Core Scheme is evaluated by plt/bin/mzscheme to produce a value. + + Automated test suite! * command-line interface to Spy: spy foo.py @@ -13,3 +22,43 @@ * Implement refcounting APIs more fully so we don't have to get rid of the refcount hacks? +Compilation: + * Transform assignment% nodes into definition% and mutation% nodes. Then transform definition% nodes into + either define% (when at the top level) or let% (when inside functions) nodes. + * Maybe transform function-definition% nodes into definition% and letrec% nodes, with the body transformed + into a lambda% node. + * Compile into modules in a Python module-language. Eg, this: + def myfun(x): + return x + 1 + class MyClass: + def mymeth(self): + return 3 + z = 5 + into this: + (begin + (module ,(gensym) (lib "lang.ss" "python") + (define (myfun x) + (+ x 1)) + (define-class MyClass + (define (mymeth self) + 3) + ) + (define z 5)) + (require the-gensym) + (current-namespace (module->namespace the-gensym))) + where the module body expands into: + (begin + (define myfun + (make-python-function + (lambda (x) + (python+ x (make-python-number 1))))) + (define (set-myfun! x) (set! myfun x)) + (define MyClass + (make-python-class + ...)) + (define (set-MyClass! x) (set! MyClass x)) + (define z (make-python-number 5)) + (define (set-z! x) (set! z x))) + + + --- NEW FILE: callcc.py --- def find5(lst): def proc(cc): for x in lst: if x == 5: cc(1) cc(0) return callcc(proc) a = [2,4,6,8] b = [1,3,5,7] print "a:" print a print "b:" print b print "is 5 in a?" print find5(a) print "is 5 in b?" print find5(b) --- NEW FILE: p.py --- print "module P invoked" something = "some thing" --- NEW FILE: spy.scm --- #!/home/dsilva/bin/mzscheme -C (require (lib "rep.ss" "readline")) (require (lib "python.ss" "python")) (require (lib "pretty.ss")) (require (lib "runtime-support.ss" "python")) (define (main args) (python-initialize) (for-each python (cdr args)) (python-repl)) (define (python-repl) (display "> ") (let ([next (read-line)]) (unless (eof-object? next) (with-handlers ([exn? (lambda (e) (print-exn&ast e (py-gen next)))]) (let ([results (py-eval next)]) (for-each (lambda (result) (unless (void? result) (display result) (newline))) results))) (python-repl)))) (define (print-exn&ast e ast-l) (print e) (newline) (for-each pretty-print (ssd* ast-l))) ;mzscheme -L rep.ss readline -L python.ss python -e \(python\ \"$1\"\) --- NEW FILE: clazz.py --- class C(object): def m(self): return 1 --- NEW FILE: spy-primitives.ss --- (module spy-primitives mzscheme ;; We write the SPY-PRIMITIVES module in Scheme, ;; because the Core Spy language is implemented in Scheme, ;; just as Mz's primitives are written in its ;; implementation language: C. ;; TODO: should Core Spy be able to apply Scheme functions? (provide newline mktuple (rename my-display display) (rename py-add +) (rename py-sub -) (rename py-mul *) (rename py-div /)) (require "runtime-support.ss") (define (my-display obj) (display (pyobject->string obj)) py-none) (define (mktuple . args) (list->pytuple args)) ) --- NEW FILE: tiny_callcc.py --- def f(x): return callcc( lambda cc: cc(x) ) print "f(2) is:" print f(2) --- NEW FILE: define_y_and_f.py --- y = 3 def f(): print "y before the import: ", y import mutate_y print "y after the import: ", y |
From: Daniel S. <aph...@us...> - 2005-02-13 07:56:21
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30229 Added Files: HACKING Log Message: Initial check-in of hacking information. This first check-in lists the Scheme modules and their purpose. --- NEW FILE: HACKING --- Core Spy is a "Module Language" for PLT Scheme. Full Spy is a more usable Module Language for PLT Scheme. Full Spy is written in Core Spy. Full Spy adds some pleasant macros and provides the Spy primitives in addition to everything from Core Spy. Python source code compiles into Full Spy. Full Spy expands into Core Spy, which expands into Textual/Full Scheme. Files: full-spy-language.ss - Full Spy module language. core-spy-language.ss - Core Spy module language. spy-primitives.ss - Spy primitive functions. Written in Scheme, since Core Spy is implemented in Scheme. (analogy: MzScheme's primitives are written in its implementation language, C) compiler.ss - Base AST class. compiler-expr.ss - ASTs and Compile methods for expressions. compiler-stmt.ss - ASTs and Compile methods for statements. compiler-target.ss - ASTs and Compile methods for binding targets. bindings-mixin.ss - Shared mixin functionality for ASTs representing binding scopes: functions, classes, modules. tool.ss - DrScheme language tool to place Python in DrScheme's Languages menu. info.ss - Collection description for setup-plt. generator-oleg.ss - Oleg's implementation of Iterator Generators for Scheme. runtime-support.ss - Runtime support for Scheme code interfacing with Python. Also exports everything from the C glue module in the 'embed' directory. runtime-context.ss - Captures a context where the runtime support is available. Mainly for use by the compiler to generate code that refers to the runtime system -- see the 'py-so' (python syntax object) function. get-base.ss - Captures a context where the basic Python bindings are available to a Python program: object, str, repr, etc... read-python.ss - Provides a better interface to parse Python source text. python.ss - Main interface to the entire system. (provide python py-eval/asts/to-strs py-eval/asts py-eval py-eval0 py-eval* py-eval/to-strs py-gen python-ns render-python-value render-python-value/format) python-import.ss - Functions for creating namespaces in which to evaluate python programs, for importing other python files, and for evaluating generated code from our compiler. TODO: how does this fit into the new Core Spy world? compile-python.ss - Uses read-python.ss to create ASTs from Python source text, and compiles those ASTs into Core Spy syntax objects. convert.ss - Functions for converting between Python and Scheme values, including experimental support for wrapping a Python class as a (lib "class.ss") class. base.ss - Provides the basic Python bindings: object, str, repr, etc.. TODO: this module will probably change after we start using Core Spy. c-bindings.ss - Deprecated. Provided the bindings from the extension in the 'c' directory. The 'c' directory actually contained a modified CPython interpreter. CPython's C code uses reference counting, and MzScheme uses a garbage collector. That clearly did not work very well. empty-base.ss - Deprecated. A Module Language providing nothing beyond Core Scheme basics -- begin, top, app, datum, and so on. runtime-base.ss builds on this. built-in-exceptions.ss - Deprecated. Provided the basic Python exception types when the entire runtime system was written in Scheme. built-in.ss - Deprecated. Provided the basic Python toplevel identifiers when the entire runtime system was written in Scheme. builtin-types-uninitialized.ss - Deprecated. Support module for built-in.ss. built-in-os.ss - Deprecated. Beginnings of a Python 'os' module written in Scheme. python-node.ss - Deprecated. Defines the python-node struct type. Deprecated. runtime-base.ss - Deprecated. extension-modsupport.ss - Deprecated. Provided the beginnings of a Scheme implementation of CPython's modsupport.c when we were still hoping to implement most of Python and its libraries in pure Scheme. export-top.ss - Deprecated. empty-context.ss - Deprecated. Captures a context where only Core Scheme forms are available -- #%module-begin, #%app, #%top, #%datum, provide, require, begin. |
From: Daniel S. <aph...@us...> - 2005-02-13 07:55:18
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv29928 Added Files: HISTORY Log Message: Initial check-in of Spy's history. --- NEW FILE: HISTORY --- Sixth version: Re-targeting the compiler to produce Core Spy code. The compiler previously generated Scheme code that scripted the runtime system. Core Spy is a domain-specific language to script the runtime system. Fifth version: Dumped the 'c' directory containing the patched CPython code. Now talking to CPython across shared library boundaries, ie, embedding CPython. Glue (embedding) code in the 'embed' directory. Fourth version: Dumped all of the Scheme-based runtime and tried patching CPython to compile as a MzScheme C extension. The CPython memory model relied on reference counting and MzScheme uses a garbage collector. Therefore, the system often went down with segfaults after MzScheme's GC moved some of CPython's memory. Third version: Dumped more of the Scheme-based runtime in order to use CPython's code. Still uncomfortable, lots of unexpected segfaults. Second version: Dumped some of the libraries in favor of using CPython's standard library. CPython's C code and the Scheme-based runtime system did not play well together. Initial version: Entire Python runtime system (including class/object system) and libraries written in Scheme. Proved to be too much for a single person to write and maintain. |
From: Christopher A. <ra...@us...> - 2004-12-17 23:22:02
|
Update of /cvsroot/plt-spy/python/doc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3598/doc Added Files: beginner-python-grammar.tex Log Message: get rid of useless junk in the repository. move the grammar.tex to doc. etc. --- NEW FILE: beginner-python-grammar.tex --- % table column lengths were modified in: % list_for, compound_stmt, if_stmt, while_stmt, for_stmt, try_stmt, % and except_clause_list_plus \documentclass[12pt]{article} \title{The Beginner Python Language Level} \author{Daniel Silva} \date{\today} \usepackage{tex2page} \begin{document} \maketitle \tableofcontents \newpage \section{Program} The grammar for Beginner Python programs is presented. The empty string is represented by $\varepsilon$. The starting non-terminal is {\bf \verb+<+file\_input~\ref{filezzzinputb}\verb+>+}. If a non-terminal is named {\bf nonterm\_list\_plus}, it is assumed that it defines the regular expression {\bf nonterm+}, while {\bf nonterm\_list} usually defines the regular expression {\bf nonterm*}, though it might be {\bf (nonterm ,)*} as well. \subsection{file\_input} \label{filezzzinputb} The following non-terminal ({\bf file\_{}input}) is the result of the parser. \begin{tabular}{lcl} {\bf \verb+<+file\_input\verb+>+} & ::= & $\varepsilon$ \\ & \verb+|+ & {\bf \verb+<+file\_input~\ref{filezzzinputb}\verb+>+} NEWLINE \\ & \verb+|+ & {\bf \verb+<+file\_input~\ref{filezzzinputb}\verb+>+} {\bf \verb+<+stmt~\ref{stmtb}\verb+>+} \\ \end{tabular} \section{Statements} \subsection{stmt} \label{stmtb} \begin{tabular}{lcl} {\bf \verb+<+stmt\verb+>+} & ::= & {\bf \verb+<+simple\_stmt~\ref{simplezzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+compound\_stmt~\ref{compoundzzzstmtb}\verb+>+} \\ \end{tabular} \\ Simple statements (\ref{simplezzzstmtb}) span a single line (no new indentation levels). Compound statements (\ref{compoundzzzstmtb}) may span multiple lines. \\ \subsection{simple\_stmt} \label{simplezzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+simple\_stmt\verb+>+} & ::= & {\bf \verb+<+small\_stmt~\ref{smallzzzstmtb}\verb+>+} NEWLINE \\ & \verb+|+ & {\bf \verb+<+small\_stmt~\ref{smallzzzstmtb}\verb+>+} \verb|;| NEWLINE \\ & \verb+|+ & {\bf \verb+<+small\_stmt~\ref{smallzzzstmtb}\verb+>+} \verb|;| {\bf \verb+<+simple\_stmt~\ref{simplezzzstmtb}\verb+>+} \\ \end{tabular} \subsection{small\_stmt} \label{smallzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+small\_stmt\verb+>+} & ::= & {\bf \verb+<+expr\_stmt~\ref{exprzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+pass\_stmt~\ref{passzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+flow\_stmt~\ref{flowzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+import\_stmt~\ref{importzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+assert\_stmt~\ref{assertzzzstmtb}\verb+>+} \\ \end{tabular} \subsection{expr\_stmt} \label{exprzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+expr\_stmt\verb+>+} & ::= & {\bf \verb+<+testlist\_list\_plus~\ref{testlistzzzlistzzzplusb}\verb+>+} \\ \end{tabular} \\ An expression statement is a {\bf \verb+<+testlist\_list\_plus~\ref{testlistzzzlistzzzplusb}\verb+>+} . \subsection{testlist\_list\_plus} \label{testlistzzzlistzzzplusb} \begin{tabular}{lcl} {\bf \verb+<+testlist\_list\_plus\verb+>+} & ::= & {\bf \verb+<+tuple\_or\_test~\ref{tuplezzzorzzztestb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+tuple\_or\_test~\ref{tuplezzzorzzztestb}\verb+>+} \verb|=| {\bf \verb+<+testlist\_list\_plus~\ref{testlistzzzlistzzzplusb}\verb+>+} \\ \end{tabular} \\ A \verb|testlist_list_plus| is an assignment, which can define or mutate variables, or an expression (\ref{tuplezzzorzzztestb}), which will be displayed in the output of the interpreter. \subsection{pass\_stmt} \label{passzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+pass\_stmt\verb+>+} & ::= & pass \\ \end{tabular} A pass is a no-op. \subsection{flow\_stmt} \label{flowzzzstmtb} A flow statement directs or modifies program flow. \\ \begin{tabular}{lcl} {\bf \verb+<+flow\_stmt\verb+>+} & ::= & {\bf \verb+<+break\_stmt~\ref{breakzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+continue\_stmt~\ref{continuezzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+return\_stmt~\ref{returnzzzstmtb}\verb+>+} \\ \end{tabular} \subsection{break\_stmt} \label{breakzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+break\_stmt\verb+>+} & ::= & break \\ \end{tabular} \\ http://www.python.org/doc/current/ref/break.html \verb+break+ may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop. \subsection{continue\_stmt} \label{continuezzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+continue\_stmt\verb+>+} & ::= & continue \\ \end{tabular} \\ http://www.python.org/doc/current/ref/continue.html \verb|continue| may only occur syntactically nested in a for or while loop, but not nested in a function or class definition or try statement within that loop. \subsection{return\_stmt} \label{returnzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+return\_stmt\verb+>+} & ::= & return {\bf \verb+<+tuple\_or\_test~\ref{tuplezzzorzzztestb}\verb+>+} \\ & \verb+|+ & return \\ \end{tabular} \\ http://www.python.org/doc/current/ref/return.html \verb|return| may only occur syntactically nested in a function definition, not within a nested class definition (we do want these, as they're not that hard to grasp). In a generator function, the return statement is not allowed to include an expression list (\ref{tuplezzzorzzztestb}). In that context, a bare return indicates that the generator is done and will cause \verb|StopIteration| to be raised. \subsection{import\_stmt } \label{importzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+import\_stmt\verb+>+} & ::= & {\bf \verb+<+import\_stmt1~\ref{importzzzstmt1b}\verb+>+} \\ & \verb+|+ & from {\bf \verb+<+dotted\_name~\ref{dottedzzznameb}\verb+>+} import \verb|*| \\ & \verb+|+ & from {\bf \verb+<+dotted\_name~\ref{dottedzzznameb}\verb+>+} import {\bf \verb+<+import\_stmt2~\ref{importzzzstmt2b}\verb+>+} \\ \end{tabular} \\ http://www.python.org/doc/current/ref/import.html Modules are required to use any libraries or teachpacks, so \emph{import} is included. \subsection{import\_stmt1 } \label{importzzzstmt1b} \begin{tabular}{lcl} {\bf \verb+<+import\_stmt1\verb+>+} & ::= & import {\bf \verb+<+dotted\_as\_name~\ref{dottedzzzaszzznameb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+import\_stmt1~\ref{importzzzstmt1b}\verb+>+} \verb|,| {\bf \verb+<+dotted\_as\_name~\ref{dottedzzzaszzznameb}\verb+>+} \\ \end{tabular} \subsection{import\_stmt2 } \label{importzzzstmt2b} \begin{tabular}{lcl} {\bf \verb+<+import\_stmt2\verb+>+} & ::= & {\bf \verb+<+import\_as\_name~\ref{importzzzaszzznameb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+import\_as\_name~\ref{importzzzaszzznameb}\verb+>+} \verb|,| {\bf \verb+<+import\_stmt2~\ref{importzzzstmt2b}\verb+>+} \\ \end{tabular} \subsection{import\_as\_name } \label{importzzzaszzznameb} \begin{tabular}{lcl} {\bf \verb+<+import\_as\_name\verb+>+} & ::= & {\bf \verb+<+ident~\ref{identb}\verb+>+} NAME {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ \end{tabular} \subsection{dotted\_as\_name } \label{dottedzzzaszzznameb} \begin{tabular}{lcl} {\bf \verb+<+dotted\_as\_name\verb+>+} & ::= & {\bf \verb+<+dotted\_name~\ref{dottedzzznameb}\verb+>+} NAME {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+dotted\_name~\ref{dottedzzznameb}\verb+>+} \\ \end{tabular} \subsection{dotted\_name } \label{dottedzzznameb} \begin{tabular}{lcl} {\bf \verb+<+dotted\_name\verb+>+} & ::= & {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+ident~\ref{identb}\verb+>+} \verb|.| {\bf \verb+<+dotted\_name~\ref{dottedzzznameb}\verb+>+} \\ \end{tabular} \subsection{assert\_stmt} \label{assertzzzstmtb} \begin{tabular}{lcl} {\bf \verb+<+assert\_stmt\verb+>+} & ::= & assert {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & assert {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| {\bf \verb+<+test~\ref{testb}\verb+>+} \\ \end{tabular} \\ \path{http://www.python.org/doc/current/ref/assert.html} Assertions would be nice for simple test suites. The simple form, ``\verb|assert expression|'', is equivalent to \begin{verbatim} if __debug__: if not expression: raise AssertionError \end{verbatim} The extended form, ``\verb|assert expression1, expression2|'', is equivalent to \begin{verbatim} if __debug__: if not expression1: raise AssertionError, expression2 \end{verbatim} \subsection{compound\_stmt} \label{compoundzzzstmtb} \newlength{\tw} \settowidth{\tw}{compound\_stmt ::= } \addtolength{\tw}{2\arraycolsep} \newlength{\len} \setlength{\len}{\textwidth} \addtolength{\len}{-1\tw} \begin{tabular}{lcp{\len}} {\bf \verb+<+compound\_stmt\verb+>+} & ::= & {\bf \verb+<+if\_stmt~\ref{ifzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+while\_stmt~\ref{whilezzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+for\_stmt~\ref{forzzzstmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+funcdef~\ref{funcdefb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+classdef~\ref{classdefb}\verb+>+} \\ \end{tabular} \\ Compound statements can span multiple lines, so they may introduce a new indentation level. \subsection{if\_stmt} \label{ifzzzstmtb} \newlength{\tw} \settowidth{\tw}{if\_stmt ::= } \addtolength{\tw}{2\arraycolsep} \newlength{\len} \setlength{\len}{\textwidth} \addtolength{\len}{-1\tw} \begin{tabular}{lcp{\len}} {\bf \verb+<+if\_stmt\verb+>+} & ::= & if {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} {\bf \verb+<+elif\_list~\ref{elifzzzlistb}\verb+>+} \\ & \verb+|+ & if {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} {\bf \verb+<+elif\_list~\ref{elifzzzlistb}\verb+>+} else \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} \subsection{elif\_list} \label{elifzzzlistb} \begin{tabular}{lcl} {\bf \verb+<+elif\_list\verb+>+} & ::= & $\varepsilon$ \\ & \verb+|+ & {\bf \verb+<+elif\_list~\ref{elifzzzlistb}\verb+>+} elif {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} %the suite subsection was moved \subsection{suite} \label{suiteb} \begin{tabular}{lcl} {\bf \verb+<+suite\verb+>+} & ::= & {\bf \verb+<+simple\_stmt~\ref{simplezzzstmtb}\verb+>+} \\ & \verb+|+ & NEWLINE INDENT {\bf \verb+<+stmt\_list\_plus~\ref{stmtzzzlistzzzplusb}\verb+>+} DEDENT \\ \end{tabular} \\ The INDENT token indicates a new indentation level. Similarly, the DEDENT token indicates a return to the previous indentation level. \subsection{stmt\_list\_plus} \label{stmtzzzlistzzzplusb} \begin{tabular}{lcl} {\bf \verb+<+stmt\_list\_plus\verb+>+} & ::= & {\bf \verb+<+stmt~\ref{stmtb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+stmt\_list\_plus~\ref{stmtzzzlistzzzplusb}\verb+>+} {\bf \verb+<+stmt~\ref{stmtb}\verb+>+} \\ \end{tabular} \subsection{while\_stmt} \label{whilezzzstmtb} \newlength{\tw} \settowidth{\tw}{while\_stmt ::= } \addtolength{\tw}{2\arraycolsep} \newlength{\len} \setlength{\len}{\textwidth} \addtolength{\len}{-1\tw} \begin{tabular}{lcp{\len}} {\bf \verb+<+while\_stmt\verb+>+} & ::= & while {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} \subsection{for\_stmt} \label{forzzzstmtb} \newlength{\tw} \settowidth{\tw}{for\_stmt ::= } \addtolength{\tw}{2\arraycolsep} \newlength{\len} \setlength{\len}{\textwidth} \addtolength{\len}{-1\tw} \begin{tabular}{lcp{\len}} {\bf \verb+<+for\_stmt\verb+>+} & ::= & for {\bf \verb+<+target\_tuple\_or\_expr~\ref{targetzzztuplezzzorzzzexprb}\verb+>+} in {\bf \verb+<+tuple\_or\_test~\ref{tuplezzzorzzztestb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} \subsection{funcdef} \label{funcdefb} \urlh{http://www.python.org/doc/current/ref/function.html}{Function Definition\\ (\path{\1})} \\ \begin{tabular}{lcl} {\bf \verb+<+funcdef\verb+>+} & ::= & def {\bf \verb+<+ident~\ref{identb}\verb+>+} {\bf \verb+<+parameters~\ref{parametersb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} \subsection{parameters} \label{parametersb} \begin{tabular}{lcl} {\bf \verb+<+parameters\verb+>+} & ::= & \verb|(| \verb|)| \\ & \verb+|+ & \verb|(| {\bf \verb+<+varargslist~\ref{varargslistb}\verb+>+} \verb|)| \\ \end{tabular} \subsection{varargslist} \label{varargslistb} \begin{tabular}{lcl} {\bf \verb+<+varargslist\verb+>+} & ::= & {\bf \verb+<+fpdef~\ref{fpdefb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+fpdef~\ref{fpdefb}\verb+>+} \verb|,| {\bf \verb+<+varargslist~\ref{varargslistb}\verb+>+} \\ \end{tabular} \\ \subsection{fpdef} \label{fpdefb} \begin{tabular}{lcl} {\bf \verb+<+fpdef\verb+>+} & ::= & {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ \end{tabular} \\ A function parameter is an identifier. \subsection{classdef} \label{classdefb} \begin{tabular}{lcl} {\bf \verb+<+classdef\verb+>+} & ::= & class {\bf \verb+<+ident~\ref{identb}\verb+>+} \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ & \verb+|+ & class {\bf \verb+<+ident~\ref{identb}\verb+>+} \verb|(| {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|)| \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ & \verb+|+ & class {\bf \verb+<+ident~\ref{identb}\verb+>+} \verb|(| {\bf \verb+<+testlist~\ref{testlistb}\verb+>+} \verb|)| \verb|:| {\bf \verb+<+suite~\ref{suiteb}\verb+>+} \\ \end{tabular} \\ \urlh{http://www.python.org/doc/current/ref/class.html}{Class Definitions\\ (\path{\1})} In the CPython interpreter, ``old-style'' classes are defined by \verb+class classname(superclasses)+, and ``new-style'' classes are defined by \verb+class classname+. Such distinction is not made here; all classes are ``new-style'' classes. Adding fields dynamically will not be allowed. \section{Expressions} Expressions produce a value that can be used on the right hand side of a variable assignment (which we do have) or a conditional boolean check (\verb|if|, \verb|while|). \subsection{tuple\_or\_test} \label{tuplezzzorzzztestb} \begin{tabular}{lcl} {\bf \verb+<+tuple\_or\_test\verb+>+} & ::= & {\bf \verb+<+tuple~\ref{tupleb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \\ \end{tabular} \subsection{tuple} \label{tupleb} \begin{tabular}{lcl} {\bf \verb+<+tuple\verb+>+} & ::= & {\bf \verb+<+testlist~\ref{testlistb}\verb+>+} \\ \end{tabular} \\ A tuple has the same syntactic rules as a testlist (\ref{testlistb}), but the name ``tuple'' is more descriptive sometimes. \subsection{test} \label{testb} \urlh{http://www.python.org/doc/current/ref/Booleans.html}{Python Ref: Booleans\\ (\path{\1})} \begin{tabular}{lcl} {\bf \verb+<+test\verb+>+} & ::= & {\bf \verb+<+or\_test~\ref{orzzztestb}\verb+>+} \\ \end{tabular} \subsection{test\_list} \label{testzzzlistb} \begin{tabular}{lcl} {\bf \verb+<+test\_list\verb+>+} & ::= & $\varepsilon$ \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| {\bf \verb+<+test\_list~\ref{testzzzlistb}\verb+>+} \\ \end{tabular} The usage of {\bf test\_{}list} depends on its context, so it never clashes with {\bf testlist~\ref{testlistb}}. A {\bf test\_{}list} is either the empty string, a {\bf test~\ref{testb}}, or a comma-separated list of tests, which may or may not end with a comma. \subsection{testlist} \label{testlistb} \begin{tabular}{lcl} {\bf \verb+<+testlist\verb+>+} & ::= & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| {\bf \verb+<+testlist~\ref{testlistb}\verb+>+} \\ \end{tabular} See note for {\bf test\_{}list~\ref{testzzzlistb}}. A {\bf testlist} is never empty. \subsection{or\_test} \label{orzzztestb} \begin{tabular}{lcl} {\bf \verb+<+or\_test\verb+>+} & ::= & {\bf \verb+<+and\_test~\ref{andzzztestb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+or\_test~\ref{orzzztestb}\verb+>+} or {\bf \verb+<+and\_test~\ref{andzzztestb}\verb+>+} \\ \end{tabular} \subsection{and\_test} \label{andzzztestb} \begin{tabular}{lcl} {\bf \verb+<+and\_test\verb+>+} & ::= & {\bf \verb+<+not\_test~\ref{notzzztestb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+and\_test~\ref{andzzztestb}\verb+>+} and {\bf \verb+<+not\_test~\ref{notzzztestb}\verb+>+} \\ \end{tabular} \subsection{not\_test} \label{notzzztestb} \begin{tabular}{lcl} {\bf \verb+<+not\_test\verb+>+} & ::= & not {\bf \verb+<+not\_test~\ref{notzzztestb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+comparison~\ref{comparisonb}\verb+>+} \\ \end{tabular} \subsection{comparison} \label{comparisonb} \begin{tabular}{lcl} {\bf \verb+<+comparison\verb+>+} & ::= & {\bf \verb+<+expr~\ref{exprb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+comparison~\ref{comparisonb}\verb+>+} {\bf \verb+<+comp\_op~\ref{compzzzopb}\verb+>+} {\bf \verb+<+expr~\ref{exprb}\verb+>+} \\ \end{tabular} \subsection{comp\_op} \label{compzzzopb} \begin{tabular}{lcl} {\bf \verb+<+comp\_op\verb+>+} & ::= & \verb|<| \\ & \verb+|+ & \verb|>| \\ & \verb+|+ & \verb|==| \\ & \verb+|+ & \verb|>=| \\ & \verb+|+ & \verb|<=| \\ & \verb+|+ & \verb|<>| \\ & \verb+|+ & \verb|!=| \\ & \verb+|+ & in \\ & \verb+|+ & not in \\ & \verb+|+ & is \\ & \verb+|+ & is not \\ \end{tabular} \subsection{expr} \label{exprb} \begin{tabular}{lcl} {\bf \verb+<+expr\verb+>+} & ::= & {\bf \verb+<+xor\_expr~\ref{arithzzzexprb}\verb+>+} \\ \end{tabular} \subsection{arith\_expr} \label{arithzzzexprb} \begin{tabular}{lcl} {\bf \verb+<+arith\_expr\verb+>+} & ::= & {\bf \verb+<+term~\ref{termb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+arith\_expr~\ref{arithzzzexprb}\verb+>+} \verb|+| {\bf \verb+<+term~\ref{termb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+arith\_expr~\ref{arithzzzexprb}\verb+>+} \verb|-| {\bf \verb+<+term~\ref{termb}\verb+>+} \\ \end{tabular} \subsection{term} \label{termb} \begin{tabular}{lcl} {\bf \verb+<+term\verb+>+} & ::= & {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+term~\ref{termb}\verb+>+} \verb|*| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+term~\ref{termb}\verb+>+} \verb|/| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+term~\ref{termb}\verb+>+} \verb|%| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+term~\ref{termb}\verb+>+} \verb|//| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ \end{tabular} \subsection{factor} \label{factorb} \begin{tabular}{lcl} {\bf \verb+<+factor\verb+>+} & ::= & \verb|+| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & \verb|-| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & \verb|~| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+power~\ref{powerb}\verb+>+} \\ \end{tabular} \subsection{power} \label{powerb} \begin{tabular}{lcl} {\bf \verb+<+power\verb+>+} & ::= & {\bf \verb+<+atom~\ref{atomb}\verb+>+} {\bf \verb+<+trailer\_list~\ref{trailerzzzlistb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+atom~\ref{atomb}\verb+>+} {\bf \verb+<+trailer\_list~\ref{trailerzzzlistb}\verb+>+} \verb|**| {\bf \verb+<+factor~\ref{factorb}\verb+>+} \\ \end{tabular} \\ A trailer list is an index operation (e.g., the \verb|[7]| in \verb|x[7]|), a function argument list, or a class attribute reference. \subsection{trailer\_list} \label{trailerzzzlistb} \begin{tabular}{lcl} {\bf \verb+<+trailer\_list\verb+>+} & ::= & $\varepsilon$ \\ & \verb+|+ & {\bf \verb+<+trailer~\ref{trailerb}\verb+>+} {\bf \verb+<+trailer\_list~\ref{trailerzzzlistb}\verb+>+} \\ \end{tabular} \subsection{trailer} \label{trailerb} \begin{tabular}{lcl} {\bf \verb+<+trailer\verb+>+} & ::= & ( ) \\ & \verb+|+ & ( {\bf \verb+<+arglist~\ref{arglistb}\verb+>+} ) \\ & \verb+|+ & [ {\bf \verb+<+subscriptlist~\ref{subscriptlistb}\verb+>+} ] \\ & \verb+|+ & [ {\bf \verb+<+subscript~\ref{subscriptb}\verb+>+} ] \\ & \verb+|+ & \verb|.| {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ \end{tabular} \subsection{arglist} \label{arglistb} \begin{tabular}{lcl} {\bf \verb+<+arglist\verb+>+} & ::= & {\bf \verb+<+argument~\ref{argumentb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+argument~\ref{argumentb}\verb+>+} \verb|,| {\bf \verb+<+arglist~\ref{arglistb}\verb+>+} \\ \end{tabular} \subsection{argument} \label{argumentb} \begin{tabular}{lcl} {\bf \verb+<+argument\verb+>+} & ::= & {\bf \verb+<+test~\ref{testb}\verb+>+} \\ \end{tabular} \subsection{atom} \label{atomb} \begin{tabular}{lcl} {\bf \verb+<+atom\verb+>+} & ::= & ( {\bf \verb+<+tuple\_or\_test~\ref{tuplezzzorzzztestb}\verb+>+} ) \\ & \verb+|+ & [ {\bf \verb+<+listmaker~\ref{listmakerb}\verb+>+} ] \\ & \verb+|+ & \{ {\bf \verb+<+dictmaker~\ref{dictmakerb}\verb+>+} \} \\ & \verb+|+ & ( ) \\ & \verb+|+ & [ ] \\ & \verb+|+ & \{ \} \\ & \verb+|+ & {\bf \verb+<+ident~\ref{identb}\verb+>+} \\ & \verb+|+ & NUMBER \\ & \verb+|+ & {\bf \verb+<+string\_list\_plus~\ref{stringzzzlistzzzplusb}\verb+>+} \\ \end{tabular} \\ \begin{itemize} \item ( ) is the empty tuple \item \verb|[ ]| is the empty list \item \{ \} is the empty dictionary \item \verb|`|\ldots\verb|`| is a shortcut for \verb|repr(|\ldots\verb|)| \end{itemize} Dictionaries are necessary to use classes -- they hold the member fields and methods. The quoted \verb{repr} is a nice shortcut, similar to a Scheme apostrophe rather than the \scm{quote} form. \subsection{string\_list\_plus} \label{stringzzzlistzzzplusb} \begin{tabular}{lcl} {\bf \verb+<+string\_list\_plus\verb+>+} & ::= & STRING \\ & \verb+|+ & STRING {\bf \verb+<+string\_list\_plus~\ref{stringzzzlistzzzplusb}\verb+>+} \\ \end{tabular} \subsection{listmaker} \label{listmakerb} \begin{tabular}{lcl} {\bf \verb+<+listmaker\verb+>+} & ::= & {\bf \verb+<+testlist~\ref{testlistb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \\ \end{tabular} \subsection{subscriptlist} \label{subscriptlistb} \begin{tabular}{lcl} {\bf \verb+<+subscriptlist\verb+>+} & ::= & {\bf \verb+<+subscript~\ref{subscriptb}\verb+>+} \verb|,| \\ & \verb+|+ & {\bf \verb+<+subscript~\ref{subscriptb}\verb+>+} \verb|,| {\bf \verb+<+subscript~\ref{subscriptb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+subscript~\ref{subscriptb}\verb+>+} \verb|,| {\bf \verb+<+subscriptlist~\ref{subscriptlistb}\verb+>+} \\ \end{tabular} %the last production had to be changed from \verb|.| \verb|.| \verb|.| to \verb|...| \subsection{subscript} \label{subscriptb} \begin{tabular}{lcl} {\bf \verb+<+subscript\verb+>+} & ::= & \verb|:| \\ & \verb+|+ & \verb|:| {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \\ \end{tabular} Slices are restricted to simple slices. \subsection{exprlist} \label{exprlistb} \begin{tabular}{lcl} {\bf \verb+<+exprlist\verb+>+} & ::= & {\bf \verb+<+expr~\ref{exprb}\verb+>+} \verb|,| \\ & \verb+|+ & {\bf \verb+<+expr~\ref{exprb}\verb+>+} \verb|,| {\bf \verb+<+expr~\ref{exprb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+expr~\ref{exprb}\verb+>+} \verb|,| {\bf \verb+<+exprlist~\ref{exprlistb}\verb+>+} \\ \end{tabular} \subsection{target\_tuple\_or\_expr} \label{targetzzztuplezzzorzzzexprb} \begin{tabular}{lcl} {\bf \verb+<+target\_tuple\_or\_expr\verb+>+} & ::= & {\bf \verb+<+target\_tuple~\ref{targetzzztupleb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+expr~\ref{exprb}\verb+>+} \\ \end{tabular} \subsection{target\_tuple} \label{targetzzztupleb} \begin{tabular}{lcl} {\bf \verb+<+target\_tuple\verb+>+} & ::= & {\bf \verb+<+exprlist~\ref{exprlistb}\verb+>+} \\ \end{tabular} \subsection{dictmaker} \label{dictmakerb} \begin{tabular}{lcl} {\bf \verb+<+dictmaker\verb+>+} & ::= & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+test~\ref{testb}\verb+>+} \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| \\ & \verb+|+ & {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|:| {\bf \verb+<+test~\ref{testb}\verb+>+} \verb|,| {\bf \verb+<+dictmaker~\ref{dictmakerb}\verb+>+} \\ \end{tabular} \subsection{ident} \label{identb} \begin{tabular}{lcl} {\bf \verb+<+ident\verb+>+} & ::= & NAME \\ \end{tabular} \\ The NAME token is a valid Python identifier. A valid Python identifier is an alphanumeric sequence of characters starting with a letter. The underscore character (\_) counts as a letter. The language is case-sensitive. \section{Omissions} Given the following omissions, it should be easier to implement Python. In other words, no hash tables. \subsection{Eval Input} \verb|eval| will not be available. \subsection{Changes to simple statements} Augassign is unnecessary for a beginner language, as are the \verb|del|, \verb|raise|, \verb|exec|, and \verb|yield| statements. The \verb|print| statement should be replaced by a function. The \verb|global| statement will not be available, so it will be impossible to mutate variables in an outer scope. \subsection{Changes to compound statements} There is no need for an \verb|else| clause in the \verb|while| or \verb|for| statements. The \verb|try/except| mechanism is also struck from the language. \subsection{Function arguments} There are no default, tuple, rest-, or hash-table arguments. \subsection{Expressions} There is no \verb|lambda|, and there are no bitwise operators. For sequences, only simple slicings are allowed, and list comprehensions are not available. \end{document} |
From: Christopher A. <ra...@us...> - 2004-12-17 23:22:01
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3598 Removed Files: beginner-python-grammar.dvi beginner-python-grammar.pdf beginner-python-grammar.ps beginner-python-grammar.tex py-module Log Message: get rid of useless junk in the repository. move the grammar.tex to doc. etc. --- beginner-python-grammar.ps DELETED --- --- beginner-python-grammar.tex DELETED --- --- beginner-python-grammar.pdf DELETED --- --- py-module DELETED --- --- beginner-python-grammar.dvi DELETED --- |
From: Christopher A. <ra...@us...> - 2004-12-17 23:20:11
|
Update of /cvsroot/plt-spy/python/doc In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3399/doc Log Message: Directory /cvsroot/plt-spy/python/doc added to the repository |
From: Christopher A. <ra...@us...> - 2004-12-14 11:23:48
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv7159 Modified Files: README Log Message: be more informative in the README. Index: README =================================================================== RCS file: /cvsroot/plt-spy/python/README,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- README 14 Dec 2004 10:47:15 -0000 1.1 +++ README 14 Dec 2004 11:23:39 -0000 1.2 @@ -1,8 +1,16 @@ Get PLT 299 from http://download.plt-scheme.org/scheme/installers/ +You will probably need to hack the embed/Makefile. + * Point MZC at the correct location (the 299 mzc) + * If you're on gcc 3.x, replace the big long CWARNING_FLAGS= with + simply "CWARNING_FLAGS=" (i.e., nothing on the right) + * Get rid of any other bits that offend you + * Like, for example, the stuff that links in all the *.sos in + site-packages (PY_EXTENSIONS) and gtk/gobject. Without this + hack, however, you will not be able to import any Python C + extension modules. We will try to resolve this issue shortly. + cd embed -# Hack the Makefile to point MZC at the correct location -# Maybe hack the Makefile to get rid of any other bits that offend you mkdir compiled/native/i386-linux/ make cd .. |
From: Daniel S. <aph...@us...> - 2004-12-14 10:54:54
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv1331 Added Files: generator-oleg.ss Log Message: Adding Oleg's implementation of generators (with his permission). --- NEW FILE: generator-oleg.ss --- (module generator-oleg mzscheme ;; Implemetation of Generators by Oleg ;; http://okmij.org/ftp/Scheme/enumerators-callcc.html#Generators (provide generative) (define (generative generator) (delay (call-with-current-continuation (lambda (k-main) (define (suspend val) (printf "suspending (yielding) val: ~a~n" val) (call-with-current-continuation (lambda (k-reenter) (printf "jumping back into k-main~n") (k-main (cons val (delay (call-with-current-continuation (lambda (k-new-main) (printf "setting k-main to be this point~n") (set! k-main k-new-main) (printf "jumping back into k-reenter~n") (k-reenter #f))))))))) (printf "entering the generator~n") (generator suspend) (printf "finished with the generator~n") (k-main '()))))) #| ;;; PLTized in case we want to edit it (define (gen g) (delay (let/cc k-main (define (yield val) (let/cc k-reenter (k-main (cons val (delay (let/cc k-new-main (set! k-main k-new-main) (k-reenter #f))))))) (g yield) (k-main null)))) (define it% (class object% (init-field init-cursor) (define promise init-cursor) (define/public (next) (unless promise (raise "StopIteration")) (let ([res (with-handlers ([void (lambda (e) (set! promise #f) (raise e))]) (force promise))]) (if (pair? res) (begin (set! promise (cdr res)) (car res)) (begin (set! promise #f) (next))))) (define/public (reset) (set! promise init-cursor)) (super-new))) |# ) |
From: Daniel S. <aph...@us...> - 2004-12-14 10:51:44
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv707 Added Files: bindings-mixin.ss Log Message: abstracted out bindings-mixin into this module --- NEW FILE: bindings-mixin.ss --- (module bindings-mixin mzscheme (require (lib "class.ss") (lib "contract.ss") "compiler-target.ss") ;; for tidentifier=? (define binding-scope-base<%> (interface () get-body-statement)) (define binding-scope<%> (interface (binding-scope-base<%>) get-global-table get-bindings add-binding is-global? is-local? is-bound? binding-tid)) (define (binding-scope-base-implementation? %) (implementation? % binding-scope-base<%>)) (define (binding-scope-implementation? %) (implementation? % binding-scope<%>)) (define (binding-scope-base/default-mixin %) (class* % (binding-scope-base<%>) (define fake-body (make-object (class object% (define/public (collect-globals) null) (super-instantiate ())))) (define/public (get-body-statement) fake-body) (super-new))) (define (bindings-mixin %) ;(unless (implementation? % binding-scope<%>) ; (error 'bindings-mixin ; (format "Expected a binding-scope<%> superclass, got: ~a" %))) (class* % (binding-scope<%>) (super-new) (inherit get-body-statement) (define global-table (let ((ht (make-hash-table))) (for-each (lambda (g) (hash-table-put! ht (send g get-symbol) g)) (send (get-body-statement) collect-globals)) ht)) ;;daniel ;; looks like global-table is a hash-table of key: symbol, value: identifier% ;; bindings: (listof (is-a?/c tidentifier%)) (define bindings null) ;;daniel (define/public (get-global-table) global-table) (define/public (get-bindings) bindings) (define/public (add-binding id) (unless (hash-table-get global-table (send id get-symbol) (lambda () #f)) (set! bindings (cons id bindings)))) ; (define/public (is-global? id) ; (cond ; ((hash-table-get global-table (send id get-symbol) (lambda () #f)) #t) ; (else #f))) ;;daniel ;; modified to return the tidentifier% in case it's in the global table (define/public (is-global? id) (hash-table-get global-table (send id get-symbol) (lambda () #f))) ;;daniel (define/public (is-local? id) (ormap (lambda (b) (and (tidentifier=? id b) b)) bindings)) ;;daniel (define/public (is-bound? id) (or (is-global? id) (is-local? id))) ;;daniel ;; binding-tid: (union identifier% tidentifier%) -> (union identifier% tidentifier% false) ;; which tidentifier% or identifier% is the first-seen (binding) instance of this id? (define/public (binding-tid id) (is-bound? id)) ;(super-new) )) (provide/contract [bindings-mixin (binding-scope-base-implementation? . -> . binding-scope-implementation?)] [binding-scope-base/default-mixin (any/c . -> . binding-scope-base-implementation?)]) (provide binding-scope<%> binding-scope-base<%>) ) |
From: Christopher A. <ra...@us...> - 2004-12-14 10:47:26
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv32201 Added Files: README Log Message: an initial stab at a README --- NEW FILE: README --- Get PLT 299 from http://download.plt-scheme.org/scheme/installers/ cd embed # Hack the Makefile to point MZC at the correct location # Maybe hack the Makefile to get rid of any other bits that offend you mkdir compiled/native/i386-linux/ make cd .. mzscheme --require python.ss |
From: Daniel S. <aph...@us...> - 2004-12-14 10:41:15
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30686 Modified Files: base.ss compile-python.ss compiler-expr.ss compiler-stmt.ss parser.ss python-import.ss python.ss read-python.ss runtime-support.ss Added Files: utils.ss Log Message: Added utils.ss for some functions like "dotted-string->symbol" Index: runtime-support.ss =================================================================== RCS file: /cvsroot/plt-spy/python/runtime-support.ss,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- runtime-support.ss 23 Nov 2004 21:46:16 -0000 1.5 +++ runtime-support.ss 14 Dec 2004 10:41:02 -0000 1.6 @@ -25,7 +25,7 @@ [(_ name parent defs ...) #`(spy-apply cpy-type (make-py-symbol name) - (make-py-tuple (list parent)) + (vector->pytuple (vector parent)) (make-py-dict #,(let ([clauses (map (lambda (def) (syntax-case def (define/public) @@ -80,7 +80,7 @@ (define py-generator (spy-apply cpy-type (make-py-symbol 'generator) - (make-py-tuple (list cpy-object)) + (vector->pytuple (vector cpy-object)) (make-py-dict (let () (define promise #f) @@ -132,14 +132,22 @@ ; (require-for-syntax "compiler.ss") ;; get the compiler context (provide get-py-string ;; from C bindings get-py-number - get-py-list - get-py-tuple + pyint->integer + integer->pyint + pylist->list + pylist->vector + pytuple->list + pytuple->vector get-py-dict - make-py-list + int-vector->pylist + vector->pylist + list->pylist + vector->pytuple + list->pytuple make-py-string make-py-symbol - make-py-tuple make-py-number + (rename make-py-number number->py-number) make-py-code make-py-function make-py-dict @@ -155,8 +163,14 @@ (rename cpy-none py-none) py-number? py-list? + py-string? python-node? + python-object? + py-none? + (rename spy-callable? py-callable?) + (rename py-object%->string pyobject->string) (rename spy-getitem/obj py-get-item/obj) + (rename spy-setitem/obj py-set-item/obj) (rename spy-getattr/obj py-get-attr/obj) (rename spy-getattr/sym py-get-attr/sym) (rename spy-getattr/str py-get-attr/str) @@ -177,6 +191,7 @@ cpy-str cpy-list cpy-type + (rename cpy-type py-type) cpy-tuple cpy-dict cpy-int @@ -185,6 +200,11 @@ cpy-bool cpy-complex cpy-unicode + cpy-slice + (rename cpy-function py-function) + (rename cpy-SchemeFunction py-schemefunction) + (rename cpy-true py-true) + (rename cpy-false py-false) ;cpy-buffer ;; temporary python-method-call @@ -199,6 +219,7 @@ py-not py-raise py-compatible-exn? + bool->pybool exn:python? exn:python-value ;current-runtime-support-context @@ -242,7 +263,7 @@ (define py-number%->number get-py-number) (define py-string%->string get-py-string) - (define py-list%->list get-py-list) + (define py-list%->list pylist->list) (define py-file%->port get-py-file) (define py-is-a? spy-instanceof) (define py-number? spy-number?) @@ -337,7 +358,10 @@ (get-py-string (py-object->py-string x))) (define (bool->py-number% b) - (make-py-number (if b 1 0))) + (integer->pyint (if b 1 0))) + + (define (bool->pybool b) + (if b cpy-true cpy-false)) (define (py-object%->bool obj) (cond Index: parser.ss =================================================================== RCS file: /cvsroot/plt-spy/python/parser.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- parser.ss 23 Nov 2004 12:57:15 -0000 1.3 +++ parser.ss 14 Dec 2004 10:41:02 -0000 1.4 @@ -406,7 +406,7 @@ (start-pos $1-start-pos) (end-pos $5-end-pos)))) (parameters ((|(| |)|) null) ((|(| varargslist |)|) $2)) - (varargslist ((* ident) `((dict ,$2))) + (varargslist ((** ident) `((dict ,$2))) ((* ident) `((seq ,$2))) ((* ident |,| ** ident) `((seq ,$2) (dict ,$5))) ((fpdef |,|) `((pos ,$1))) Index: read-python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/read-python.ss,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- read-python.ss 10 Apr 2004 18:42:08 -0000 1.1.1.1 +++ read-python.ss 14 Dec 2004 10:41:02 -0000 1.2 @@ -7,7 +7,8 @@ (lib "contract.ss") (lib "class.ss")) - (provide/contract (read-python (string? . -> . (listof (is-a?/c ast-node%))))) +; (provide/contract (read-python (string? . -> . (listof (is-a?/c ast-node%))))) +(provide read-python); (provide ;read-python read-python-port) read-python-port) ; (read-python-port (input-port? (union (is-a?/c text%) string?) . -> . (listof (is-a?/c ast-node%))))) @@ -27,4 +28,4 @@ ast-l)) - ) \ No newline at end of file + ) Index: python-import.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python-import.ss,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- python-import.ss 23 Nov 2004 21:46:15 -0000 1.5 +++ python-import.ss 14 Dec 2004 10:41:02 -0000 1.6 @@ -180,7 +180,7 @@ symbols) symbols)))))) - (define python-verbose-eval? #f) + (define python-verbose-eval? #t) (define (set-python-verbose-eval?! ya?) (set! python-verbose-eval? ya?)) (provide/contract [set-python-verbose-eval?! (boolean? . -> . void?)]) --- NEW FILE: utils.ss --- (module utils mzscheme ;; Utilities not related to Spy or Python at all. (require (lib "etc.ss") (lib "list.ss")) (provide symbol-list->dotted-string ) (define (symbol-list->dotted-string sl) (foldr (lambda (a b) (if (string=? b "") a (string-append a "." b))) "" (map symbol->string sl))) ) Index: python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python.ss,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- python.ss 23 Nov 2004 21:46:16 -0000 1.5 +++ python.ss 14 Dec 2004 10:41:02 -0000 1.6 @@ -1,7 +1,7 @@ (module python mzscheme (require (lib "errortrace.ss" "errortrace")) (require (lib "class.ss") - ; (lib "list.ss") + (lib "list.ss") (lib "etc.ss") (lib "contract.ss") ; "compiler.ss" @@ -19,7 +19,7 @@ (provide in-ns in-python) (define (py-gen-result? x) (or (syntax? x) (python-node? x))) - +#| (provide/contract [python (string? . -> . (listof string?))] [py-eval/asts ((listof syntax?) . -> . (listof string?))] @@ -32,6 +32,9 @@ [ns-get (symbol? . -> . any/c)] [render-python-value ((union python-node? void?) port? procedure? . -> . void?)] [render-python-value/format ((union python-node? void?) port? procedure? . -> . void?)]) +|# + + (provide python py-eval/asts/to-strs py-eval/asts py-eval py-eval0 py-eval* py-eval/to-strs py-gen ssd ssd* python-ns ns-set! ns-get render-python-value render-python-value/format) (define ssd syntax-object->datum) @@ -87,7 +90,31 @@ (define python-ns (make-parameter pns)) (define python-ss-module-ns (current-namespace)) - + + (define (get-py-symbol pystr) + (string->symbol (get-py-string pystr))) + + (define pystring->symbol get-py-symbol) + + (define (load-builtins-into-ns ns) + (define builtin (python-load-module/dotted "__builtin__")) + (define pydict (py-get-attr/sym builtin '__dict__)) + (define pykeys (python-method-call pydict 'keys)) + (define keyvec (pylist->vector pykeys)) + (define veclen (vector-length keyvec)) + (parameterize ([current-namespace pns]) + (let loop ([idx 0]) + (unless (= idx veclen) + (let* ([pyname (vector-ref keyvec idx)] + [name (pystring->symbol pyname)]) + ;; TODO: should we use dict[name] or module.name? + (unless (eq? '__name__ name) ;; skip the special symbols + (namespace-set-variable-value! name + (py-get-attr/sym builtin name))) + (loop (add1 idx))))))) + + (load-builtins-into-ns pns) + ; (define (python-ns) ; pns) @@ -99,12 +126,30 @@ (copy-namespace-bindings pns (current-namespace) #f #f)) |# + (define (py-eval/asts/to-strs asts) + (map convert-value + (py-eval/asts asts))) + (define (py-eval/asts asts) + (eval-python asts (python-ns))) + + (define (py-eval* str) + (py-eval/asts (py-gen str))) + + (define (py-eval/to-strs str) (map convert-value - (eval-python asts (python-ns)))) + (py-eval* str))) + + (define (py-eval0 str) + (car (py-eval* str))) (define (py-eval str) - (py-eval/asts (py-gen str))) + (let ([lst (py-eval* str)]) + (if (null? lst) + lst + (car (last-pair lst))))) + + ;(require "base.ss") ;(dynamic-require "base.ss" #f #f) Index: compiler-expr.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-expr.ss,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- compiler-expr.ss 23 Nov 2004 21:46:14 -0000 1.5 +++ compiler-expr.ss 14 Dec 2004 10:41:02 -0000 1.6 @@ -1,10 +1,13 @@ (module compiler-expr mzscheme + (require (lib "errortrace.ss" "errortrace")) + (require (lib "class.ss") (lib "list.ss") (lib "etc.ss") ;build-list (lib "plt-match.ss") "compiler.ss" "compiler-target.ss" + "bindings-mixin.ss" ; "empty-context.ss") ; "primitives.ss" ; "runtime-support.ss" @@ -64,7 +67,8 @@ (let ([seq (send parms get-seq)]) (if seq (let ([seq (send seq to-scheme)]) - `([,seq (list->py-tuple% ,seq)])) + ;; TODO: can we use a vector here? + `([,seq (,(py-so 'list->pytuple) ,seq)])) empty)) ;(let ([dict (send parms get-dict)]) ; (if dict @@ -104,11 +108,11 @@ (let ([code-sxp `(,(py-so 'make-py-code) ',name ,(generate-lambda parms body-so) ,(length (send parms get-pos)) - ,(cond + ',(cond [(and seq dict) '(var-args kw-args)] [seq '(var-args)] [dict '(kw-args)] - [else null]))]) + [else '()]))]) (if (and key (not (empty? key))) `(,(py-so 'make-py-function) ,code-sxp ,(map (lambda (k) @@ -134,6 +138,9 @@ 'null `(list ,@exprs))) + (define (generate-vector exprs) + `(vector ,@exprs)) + (define parameters% (class ast-node% @@ -147,7 +154,7 @@ ;; (and (list? x) (andmap (lambda (x) (parm-tup? x)) x)))) (init parm-list) - ; (printf "def parm-list: ~a~n" parm-list) + ; (printf "def parm-list: ~a~n" parm-list) ;; pos: (listof parm-tup?) (define pos null) @@ -176,6 +183,8 @@ (set! dict (cadr param))))) parm-list) + ;(printf "parameter% seq: ~a~n" seq) + (define (parm-tup-bindings x) (cond ((is-a? x identifier%) (list x)) @@ -226,6 +235,7 @@ ; (printf "second id: ~a~n" (send (cdr (first-atom k)) get-symbol))) (send (first-atom k) to-scheme)) (reverse key))]) + ;(printf "parameters%-to-scheme~n") (->orig-so (cond [(and seq dict) (begin ;(printf "identifier binding for seq: ~a~n" (identifier-binding (send seq to-scheme))) @@ -233,8 +243,9 @@ ;;`(,(send dict to-scheme) ,@Ps ,@Ks . ,(send seq to-scheme)))] ;; using the same format as CPython now `(,@Ps ,@Ks ,(send seq to-scheme) ,(send dict to-scheme)))] - [seq ;;`(,@Ps ,@Ks . ,(send seq to-scheme))] - `(,@Ps ,@Ks ,(send seq to-scheme))] + [seq (begin ; (printf "seq~n") + `(,@Ps ,@Ks . ,(send seq to-scheme)))] + ;;`(,@Ps ,@Ks ,(send seq to-scheme))] [dict ;;`(,(send dict to-scheme) ,@Ps ,@Ks)] `(,@Ps ,@Ks ,(send dict to-scheme))] [else `(,@Ps ,@Ks)])))) @@ -297,13 +308,13 @@ (inherit ->orig-so) (define/override (to-scheme) (->orig-so - #| (if (string? value) + (if (string? value) (make-py-string value) - (make-py-number value))))|# - `(,(py-so (if (string? value) + (make-py-number value)))) + #| `(,(py-so (if (string? value) 'make-py-string 'make-py-number)) - ,value))) + ,value))) |# (super-instantiate ()))) @@ -325,8 +336,8 @@ ;;daniel (inherit ->orig-so) (define/override (to-scheme) - (->orig-so `(,(py-so 'make-py-tuple) ;;,(py-so 'list->py-tuple%) - ,(generate-list (map (lambda (e) + (->orig-so `(,(py-so 'vector->pytuple) ;;,(py-so 'list->py-tuple%) + ,(generate-vector (map (lambda (e) (send e to-scheme)) expressions))))) @@ -350,8 +361,8 @@ (inherit ->orig-so) ;;; TODO: evaluate at compile-time if it's a literal list (define/override (to-scheme) - (->orig-so `(,(py-so 'make-py-list) ;;,(py-so 'list->py-list%) - ,(generate-list (map (lambda (e) (send e to-scheme)) + (->orig-so `(,(py-so 'vector->pylist) ;;,(py-so 'list->py-list%) + ,(generate-vector (map (lambda (e) (send e to-scheme)) expressions))))) (super-instantiate ()))) @@ -376,7 +387,8 @@ (->orig-so (let ([result (gensym 'result)]) `(let ([,result null]) ,(send for to-scheme (send expr to-scheme) result) - (,(py-so 'make-py-list) ;;,(py-so 'list->py-list%) + ;; TODO: use a vector instead + (,(py-so 'list->pylist) ;;,(py-so 'list->py-list%) ,result))))) (super-instantiate ()))) @@ -427,7 +439,8 @@ ; ,body) ; (,(py-so 'py-sequence%->list) ,item))))] ; [else (error "bad target for a list comprehension")]) - (,(py-so 'get-py-list) ;;,(py-so 'py-sequence%->list) + ;; TODO: use a vector instead + (,(py-so 'pylist->list) ;;,(py-so 'py-sequence%->list) ,(send vals to-scheme)))))) @@ -576,14 +589,16 @@ (->orig-so `(,(py-so 'python-method-call) ,(send expression to-scheme) '__getitem__ - (list (,(py-so 'py-create) - ,(py-so 'py-slice%) + (,(py-so 'py-apply) + ,(py-so 'cpy-slice) ,(if lower (send lower to-scheme) + ;; TODO: (compiler:mk-py-literal 0) `(,(py-so 'number->py-number) 0)) ,(if upper (send upper to-scheme) - `(,(py-so 'number->py-number) +inf.0))))))) + ;; TODO: (compiler:mk-py-literal +inf.0) + `(,(py-so 'number->py-number) +inf.0)))))) (super-instantiate ()))) @@ -770,8 +785,11 @@ (super-instantiate ()))) + ;; 5.10 (define lambda% + (bindings-mixin + (binding-scope-base/default-mixin (class expression% ;; parms: (is-a?/c parameters%) @@ -779,16 +797,18 @@ (init-field parms body) ;; bindings: (listof (is-a?/c tidentifier%)) - (define bindings null) +; (define bindings null) (define/override (set-bindings! enclosing-scope) (send parms set-bindings! enclosing-scope) (send body set-bindings! this)) - (define/public (add-binding id) - (set! bindings (cons id bindings))) + ; (define/public (add-binding id) + ; (set! bindings (cons id bindings))) - (define/public (get-bindings) bindings) + ; (define/public (get-bindings) bindings) + + ; (define/public (get-body-statement) #f) (define/public (get-parms) parms) @@ -801,8 +821,9 @@ ; `(,(py-so 'procedure->py-function%) (lambda ,(send parms to-scheme) ; ,(send body to-scheme))))) - (define/public (is-global? b) #f) + ;; TODO: is it safe to comment this out? + ; (define/public (is-global? b) #f) - (super-instantiate ()))) + (super-instantiate ()))))) ) Index: compile-python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compile-python.ss,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- compile-python.ss 13 Jul 2004 13:01:27 -0000 1.2 +++ compile-python.ss 14 Dec 2004 10:41:02 -0000 1.3 @@ -8,13 +8,14 @@ "runtime-context.ss" "read-python.ss") - +#| (provide/contract [compile-python ((listof (is-a?/c ast-node%)) . -> . (listof syntax?))] [compile-python-ast ((is-a?/c ast-node%) . -> . syntax?)] ; [py-gen/file (string? . -> . (listof syntax?))] [py-gen/str (string? namespace? . -> . (listof syntax?))] - [py-gen/port (input-port? string? namespace? . -> . (listof syntax?))]) + [py-gen/port (input-port? string? namespace? . -> . (listof syntax?))])|# + (provide compile-python compile-python-ast py-gen/str py-gen/port) (provide py-gen/file) Index: compiler-stmt.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-stmt.ss,v retrieving revision 1.6 retrieving revision 1.7 diff -u -d -r1.6 -r1.7 --- compiler-stmt.ss 23 Nov 2004 21:46:15 -0000 1.6 +++ compiler-stmt.ss 14 Dec 2004 10:41:02 -0000 1.7 @@ -1,4 +1,6 @@ (module compiler-stmt mzscheme + (require (lib "errortrace.ss" "errortrace")) + (require (lib "class.ss") (lib "list.ss") (lib "etc.ss") ; build-list @@ -6,14 +8,15 @@ "compiler.ss" "compiler-expr.ss" "compiler-target.ss" + "bindings-mixin.ss" ; "primitives.ss" - ; "runtime-support.ss" + ;"runtime-support.ss" "runtime-context.ss" "utils.ss" ; "empty-context.ss" ) - (provide (all-defined-except bindings-mixin)) + (provide (all-defined)) (define using-return-ec? (make-parameter #f)) (define using-break-ec? (make-parameter #f)) @@ -206,8 +209,8 @@ lower) target)] [upper ((class-field-accessor tsimple-slicing% upper) target)]) - `(,(py-so 'py-create) - ,(py-so 'py-slice%) + `(,(py-so 'py-apply) + ,(py-so 'cpy-slice) ,(if lower (send lower to-scheme) `(,(py-so 'number->py-number%) 0)) @@ -995,14 +998,14 @@ (let ([parm-names (target->parm-tup target)]) (->orig-so (let* ([bdy `(let* ([index -1] [seq ,(send vals to-scheme)] - [len (,(py-so 'get-py-number) + [len (,(py-so 'pyint->integer) (,(py-so 'python-method-call) seq '__len__))]) (let ,continue-symbol () (set! index (+ 1 index)) (unless (= index len) ,(if (is-a? parm-names identifier%) `(set! ,(send parm-names to-scheme) (,(py-so 'python-index) seq index)) - `(let ([current-tuple (python-index seq index)]) + `(let ([current-tuple (,(py-so 'python-index) seq index)]) ,@(map (lambda (parm-index) `(set! ,(send (list-ref parm-names parm-index) to-scheme) @@ -1205,74 +1208,24 @@ (super-new [substatements (list body finalizer)]))) - - (define (bindings-mixin %) - (class % - (super-instantiate ()) - (inherit-field body) - (define global-table - (let ((ht (make-hash-table))) - (for-each - (lambda (g) - (hash-table-put! ht (send g get-symbol) g)) - (send body collect-globals)) - ht)) - - ;;daniel - ;; looks like global-table is a hash-table of key: symbol, value: identifier% - - ;; bindings: (listof (is-a?/c tidentifier%)) - (define bindings null) - - ;;daniel - (define/public (get-global-table) global-table) - (define/public (get-bindings) bindings) - - (define/public (add-binding id) - (unless (hash-table-get global-table (send id get-symbol) (lambda () #f)) - (set! bindings (cons id bindings)))) - -; (define/public (is-global? id) -; (cond -; ((hash-table-get global-table (send id get-symbol) (lambda () #f)) #t) -; (else #f))) - ;;daniel - ;; modified to return the tidentifier% in case it's in the global table - (define/public (is-global? id) - (hash-table-get global-table (send id get-symbol) (lambda () #f))) - - ;;daniel - (define/public (is-local? id) - (ormap (lambda (b) - (and (tidentifier=? id b) b)) - bindings)) - - ;;daniel - (define/public (is-bound? id) - (or (is-global? id) (is-local? id))) - - ;;daniel - ;; binding-tid: (union identifier% tidentifier%) -> (union identifier% tidentifier% false) - ;; which tidentifier% or identifier% is the first-seen (binding) instance of this id? - (define/public (binding-tid id) - (is-bound? id)) - - )) - ;; daniel ;; use this as the object type of the initial empty environment for a module (define module-scope% (class (bindings-mixin - (class object%;(bindings-mixin object%) - (field [body (make-object (class object% - (define/public (collect-globals) null) - (super-instantiate ())))]) + (binding-scope-base/default-mixin + (class object% + +; (define fake-body (make-object (class object% +; (define/public (collect-globals) null) +; (super-instantiate ())))) + (define/public (get-escape-continuation-symbol) (gensym 'unusable)) +; (define/public (get-body-statement) fake-body) - (super-instantiate ()))) + (super-new)))) (define/override (is-bound? id) #f) @@ -1282,7 +1235,7 @@ ;; 7.5 (define function-definition% (bindings-mixin - (class compound-statement% + (class* compound-statement% (binding-scope-base<%>) (inherit stx-err) ;; name: (is-a?/c identifier%) @@ -1290,6 +1243,8 @@ ;; body: (is-a?/c suite%) (init-field name parms body) + (define/public (get-body-statement) body) + ;;daniel -- moved (send name to-target) here to make set-bindings! work correctly (define tname (send name to-target)) @@ -1347,7 +1302,7 @@ ;; 7.6 (define class-definition% (bindings-mixin - (class statement% + (class* statement% (binding-scope-base<%>) ;; name: (is-a?/c identifier%) ;; inherit-expr: (listof (is-a?/c expression%)) ;; body: (is-a?/c suite%) @@ -1355,6 +1310,8 @@ ;; daniel -- moved send to-target here (define tname (send name to-target)) + + (define/public (get-body-statement) body) (define/override (set-bindings! enclosing-scope) (when (and enclosing-scope @@ -1374,9 +1331,9 @@ (->orig-so `(define ,class-name (,(py-so 'py-apply) ,(py-so 'cpy-type) (,(py-so 'make-py-symbol) ',class-name) - (,(py-so 'make-py-tuple) (list ,@(if (empty? inherit-list) - (list (py-so 'cpy-object)) - inherit-list))) + (,(py-so 'vector->pytuple) ,(generate-vector (if (empty? inherit-list) + (list (py-so 'cpy-object)) + inherit-list))) (,(py-so 'make-py-dict) (let () ,(foldr (lambda (next acc) (if (or (is-a? next assignment%) Index: base.ss =================================================================== RCS file: /cvsroot/plt-spy/python/base.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- base.ss 23 Nov 2004 12:57:15 -0000 1.3 +++ base.ss 14 Dec 2004 10:41:02 -0000 1.4 @@ -1,4 +1,5 @@ (module base mzscheme + (require (lib "errortrace.ss" "errortrace")) (require "runtime-support.ss" (lib "contract.ss") (lib "etc.ss") @@ -30,12 +31,29 @@ ; (define myappend (guarded my-append)) + (define instanceof (wrap (lambda (obj type) + (bool->pybool (py-is-a? obj type))) + 'instanceof)) (define callcc (wrap py-call/cc)) (define callec (wrap py-call/ec)) - (define (py-range cnt) - (make-py-list (build-list (get-py-number cnt) make-py-number))) + ;; TODO: eww, this is awful.. building a big scheme list and then translating it to a python list? +; (define (py-range cnt) +; (make-py-list (build-list (get-py-number cnt) make-py-number))) + (define py-range + (opt-lambda (x [y #f] [pystep #f]) + (let ([start (if y (get-py-number x) 0)] + [stop (if y (get-py-number y) (get-py-number x))]) + (define step (if pystep (get-py-number pystep) 1)) + (define count (ceiling (/ (- stop start) step))) + (define v (make-vector count)) + (let loop ([idx 0] + [val start]) + (unless (= idx count) + (vector-set! v idx val) + (loop (add1 idx) (+ step val)))) + (int-vector->pylist v)))) (define range (wrap py-range)) @@ -63,6 +81,9 @@ ;syntax (all-from "runtime-support.ss") + + ;; Spy addons + instanceof ;; types (rename cpy-object object) @@ -78,6 +99,7 @@ (rename cpy-bool bool) (rename cpy-complex complex) (rename cpy-unicode unicode) + (rename cpy-slice slice) ;(rename cpy-buffer buffer) ) |
From: Daniel S. <aph...@us...> - 2004-12-14 10:41:14
|
Update of /cvsroot/plt-spy/python/embed In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30686/embed Modified Files: Makefile attributes.c embed.c schemefunctionobject.c spy.h value-conversion.c Log Message: Added utils.ss for some functions like "dotted-string->symbol" Index: spy.h =================================================================== RCS file: /cvsroot/plt-spy/python/embed/spy.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- spy.h 23 Nov 2004 21:46:17 -0000 1.2 +++ spy.h 14 Dec 2004 10:41:02 -0000 1.3 @@ -1,5 +1,23 @@ -#include "escheme.h" +#ifndef _SPY_H_ +#define _SPY_H_ + #include "Python.h" +#include "escheme.h" + +////////////// general convenience + +#define DEBUG_SPY + +#ifdef DEBUG_SPY +#define PRINTF(fmt, args...) printf(fmt, ##args) +#define dprintf(fmt, args...) printf("[%s] " fmt, name, ##args) +#else +#define PRINTF(fmt, args...) do {} while(0) +#define dprintf(fmt, args...) do { } while(0) +#endif + + + // MzScheme convenience extern Scheme_Env* spy_global_env; @@ -22,6 +40,13 @@ return sapply( func, 1, args ); } +static Scheme_Object* sapply0(const char* func_name) +{ + Scheme_Object* func = slookup(func_name); + return sapply( func, 0, NULL ); +} + + #define SCM_ARG(name, type_ok, expected, which, argc, argv) \ (type_ok ? argv[which] : (scheme_wrong_type(name, expected, which, argc, argv), NULL)) @@ -35,12 +60,16 @@ #define SCM_PY_ARG_CONV(name, which, argc, argv) PY(SCM_PY_ARG(name, which, argc, argv)) -#define GUARANTEE_PY(fname, argnum) GUARANTEE_TYPE (fname, argnum, SCHEME_CPTRP, "python object") +#define SCHEME_PYOBJP(x) (SCHEME_CPTRP(x) && SCHEME_CPTR_TYPE(x) == spy_tag) + +#define GUARANTEE_PY(fname, argnum) GUARANTEE_TYPE (fname, argnum, SCHEME_PYOBJP, "python object") #define GUARANTEE_NUMBER(fname, argnum) GUARANTEE_TYPE (fname, argnum, SCHEME_NUMBERP, "number") #define GUARANTEE_LIST(fname, argnum) GUARANTEE_TYPE(fname, argnum, SCHEME_LISTP, "list") +#define GUARANTEE_VECTOR(fname, argnum) GUARANTEE_TYPE(fname, argnum, SCHEME_VECTORP, "vector") + #define SPYARG(argnum) GUARANTEE_PY(name, argnum) #define PYARG(argnum) PY(SPYARG(argnum)) @@ -53,12 +82,36 @@ Py_XDECREF(PY(spy_cptr_to_pyobj)); } +static void spy_cptr_finalize_dbg(void* spy_cptr_to_pyobj, void* data) +{ + printf("finalizing scheme object at 0x%x\n", (unsigned int) spy_cptr_to_pyobj); +} + +#ifdef DEBUG_SPY +#include <assert.h> +#endif + static Scheme_Object* SCM(PyObject* p) { + if (!p) + { +#ifdef DEBUG_SPY + fprintf(stderr, "SCM: got a null pointer."); + int* foo = NULL; + int bar = (int) *foo; + assert(0); +#else + scheme_signal_error("SCM: got a null pointer."); +#endif + } + { Scheme_Object* sobj = scheme_make_cptr(p, spy_tag); Py_XINCREF(p); scheme_register_finalizer(sobj, spy_cptr_finalize, NULL, NULL, NULL); +// scheme_register_finalizer(sobj, spy_cptr_finalize_dbg, NULL, NULL, NULL); +// scheme_dont_gc_ptr(sobj); return sobj; + } } static PyObject* PY(Scheme_Object* s) @@ -72,17 +125,7 @@ #define PyInt_FromInt(x) PyInt_FromLong((long)x) - -////////////// general convenience - -//#define DEBUG_SPY - -#ifdef DEBUG_SPY - #define PRINTF(fmt, args...) printf(fmt, ##args) - #define dprintf(fmt, args...) printf("[%s] " fmt, name, ##args) -#else - #define PRINTF(fmt, args...) - #define dprintf(fmt, args...) -#endif +#define PyObject_AsCString(obj) PyString_AsString(PyObject_Str(obj)) +#endif // _SPY_H_ Index: value-conversion.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/value-conversion.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- value-conversion.c 23 Nov 2004 21:46:17 -0000 1.2 +++ value-conversion.c 14 Dec 2004 10:41:02 -0000 1.3 @@ -1,4 +1,5 @@ #include "spy.h" +#include "schemefunctionobject.h" Scheme_Object* spy_make_py_string(const char* me, int argc, Scheme_Object* argv[]) { @@ -18,11 +19,78 @@ PyObject* str; Scheme_Object* sym = GUARANTEE_SYMBOL(name, 0); str = PyString_InternFromString(SCHEME_SYM_VAL(sym)); - assert(str != NULL); - assert(PyString_Check(str)); return SCM(str); } +// TODO: unsafe! vec els might not be integers +Scheme_Object* spy_make_pylist_from_int_vector(const char* name, int argc, Scheme_Object* argv[]) +{ + Scheme_Object* vec = GUARANTEE_VECTOR(name, 0); + int size = SCHEME_VEC_SIZE(vec); + PyObject* plist = PyList_New(size); + Scheme_Object** els = SCHEME_VEC_ELS(vec); + int i = 0; + dprintf("entry\n"); + for (; i < size; i++) + PyList_SET_ITEM(plist, i, PyInt_FromLong(SCHEME_INT_VAL(els[i]))); + return SCM(plist); +} + +// TODO: unsafe! vec els might not be pyobjs +Scheme_Object* spy_make_pylist_from_vector(const char* name, int argc, Scheme_Object* argv[]) +{ + Scheme_Object* vec = GUARANTEE_VECTOR(name, 0); + int size = SCHEME_VEC_SIZE(vec); + PyObject* plist = PyList_New(size); + Scheme_Object** els = SCHEME_VEC_ELS(vec); + int i = 0; + dprintf("entry\n"); + for (; i < size; i++) + PyList_SET_ITEM(plist, i, PY(els[i])); + return SCM(plist); +} + +// TODO: unsafe! arg might not be a pylist +Scheme_Object* spy_make_vector_from_pylist(const char* name, int argc, Scheme_Object* argv[]) +{ + PyObject* plist = PYARG(0); + int size = PyList_GET_SIZE(plist); + Scheme_Object* vec = scheme_make_vector(size, NULL); + int i = 0; + dprintf("entry\n"); + for (; i < size; i++) + SCHEME_VEC_ELS(vec)[i] = SCM(PyList_GET_ITEM(plist, i)); + return vec; +} + +// TODO: unsafe! vec els might not be pyobjs +Scheme_Object* spy_make_pytuple_from_vector(const char* name, int argc, Scheme_Object* argv[]) +{ + Scheme_Object* vec = GUARANTEE_VECTOR(name, 0); + int size = SCHEME_VEC_SIZE(vec); + PyObject* ptuple = PyTuple_New(size); + Scheme_Object** els = SCHEME_VEC_ELS(vec); + int i = 0; + dprintf("entry\n"); + for (; i < size; i++) + PyTuple_SET_ITEM(ptuple, i, PY(els[i])); + return SCM(ptuple); +} + +// TODO: unsafe! arg might not be a pytuple +Scheme_Object* spy_make_vector_from_pytuple(const char* name, int argc, Scheme_Object* argv[]) +{ + PyObject* ptuple = PYARG(0); + int size = PyTuple_GET_SIZE(ptuple); + Scheme_Object* vec = scheme_make_vector(size, NULL); + int i = 0; + dprintf("entry\n"); + for (; i < size; i++) + SCHEME_VEC_ELS(vec)[i] = SCM(PyTuple_GET_ITEM(ptuple, i)); + return vec; +} + + static PyObject* proper_list_to_py_list(Scheme_Object* slist) { int len = scheme_proper_list_length(slist); @@ -38,18 +106,20 @@ } // make-py-list: list -> PyList -Scheme_Object* spy_make_py_list(const char* name, int argc, Scheme_Object* argv[]) +Scheme_Object* spy_make_pylist_from_list(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* slist = GUARANTEE_LIST(name, 0); + dprintf("entry\n"); return SCM( proper_list_to_py_list(slist) ); } static Scheme_Object* py_list_to_scheme_list_unsafe(PyObject* plist, int pos, int len); // get-py-list: PyList -> list -Scheme_Object* spy_get_py_list(const char* name, int argc, Scheme_Object* argv[]) +Scheme_Object* spy_make_list_from_pylist(const char* name, int argc, Scheme_Object* argv[]) { PyObject* plist = PY(GUARANTEE_PY(name, 0)); + dprintf("entry\n"); if (!PyList_Check(plist)) scheme_wrong_type(name, "PyListObject", 0, argc, argv); return py_list_to_scheme_list_unsafe(plist, 0, PyList_GET_SIZE(plist)); @@ -64,9 +134,10 @@ } // make-py-tuple: list -> PyTuple -Scheme_Object* spy_make_py_tuple(const char* name, int argc, Scheme_Object* argv[]) +Scheme_Object* spy_make_pytuple_from_list(const char* name, int argc, Scheme_Object* argv[]) { - return SCM( PyList_AsTuple(PY(spy_make_py_list("make-py-list", argc, argv))) ); + dprintf("entry\n"); + return SCM( PyList_AsTuple(PY(spy_make_pylist_from_list("list->pylist", argc, argv))) ); } static Scheme_Object* py_tuple_to_scheme_list_unsafe(PyObject* ptup, int pos, int len) @@ -79,7 +150,7 @@ } // get-py-tuple: PyTuple -> list -Scheme_Object* spy_get_py_tuple(const char* name, int argc, Scheme_Object* argv[]) +Scheme_Object* spy_make_list_from_pytuple(const char* name, int argc, Scheme_Object* argv[]) { PyObject* ptuple = PY( GUARANTEE_PY(name, 0) ); dprintf("entry\n"); @@ -157,7 +228,6 @@ sapply1("display", n); seval("(newline)"); pynum = PyLong_FromString(SCHEME_STR_VAL((Scheme_Object*)sapply1("number->string", n)), NULL, 10); - assert(PyNumber_Check(pynum)); return pynum; } @@ -181,8 +251,6 @@ SCHEME_INTP(n) ? chk_mpn(PyInt_FromLong(SCHEME_INT_VAL(n))) : SCHEME_DBLP(n) ? PyFloat_FromDouble(SCHEME_DBL_VAL(n)) : make_py_number_last_resort(n); - // TODO: take out this assertion, though I guess it compiles away - assert(PyNumber_Check(pynum)); return SCM(pynum); } @@ -190,8 +258,6 @@ Scheme_Object* spy_get_py_number(const char* name, int argc, Scheme_Object* argv[]) { PyObject* n = PY(GUARANTEE_PY(name, 0)); - // TODO: does this assertion compile away? - assert(PyNumber_Check(n)); return PyInt_Check(n) ? scheme_make_integer(PyInt_AsLong(n)) : PyLong_Check(n) ? scheme_make_integer(PyLong_AsLong(n)) @@ -199,6 +265,18 @@ : sapply1("string->number", scheme_make_string(PyString_AsString(PyObject_Str(n)))); } +// integer -> PyInt +Scheme_Object* spy_make_pyint(const char* name, int argc, Scheme_Object* argv[]) +{ + return SCM(PyInt_FromLong(SCHEME_INT_VAL(GUARANTEE_INTEGER(name, 0)))); +} + +// PyInt -> integer +Scheme_Object* spy_make_int(const char* name, int argc, Scheme_Object* argv[]) +{ + return scheme_make_integer(PyInt_AsLong(PYARG(0))); +} + // make-py-file: port -> PyFile Scheme_Object* spy_make_py_file(const char* name, int argc, Scheme_Object* argv[]) { @@ -212,7 +290,6 @@ { // TODO: implement me PyObject* pfile = PY(argv[0]); - assert(PyFile_Check(pfile)); scheme_signal_error("get_py_file not implemented yet"); return NULL; } Index: attributes.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/attributes.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- attributes.c 23 Nov 2004 12:57:16 -0000 1.1 +++ attributes.c 14 Dec 2004 10:41:02 -0000 1.2 @@ -12,21 +12,23 @@ return SCM(PySequence_GetItem(seq, SCHEME_INT_VAL(idx))); } +#define COULD_NOT_FIND_ATTR(key_cstr) \ + do { \ + char ___msg_buf[1024]; \ + snprintf(___msg_buf, 1024, "Could not find attribute: %s\n", key_cstr); \ + scheme_signal_error(___msg_buf); } while(0) + static PyObject* got_attrib_obj(PyObject* attr, PyObject* obj, PyObject* key) { if (attr == NULL) - scheme_signal_error("Could not find attribute."); - assert(attr != NULL); -// assert(SPY_NODEP(attr)); + COULD_NOT_FIND_ATTR(PyString_AsString(PyObject_Str(key))); return attr; } static PyObject* got_attrib_str(PyObject* attr, PyObject* obj, char* key) { if (attr == NULL) - scheme_signal_error("Could not find attribute."); - assert(attr != NULL); -// assert(SPY_NODEP(attr)); + COULD_NOT_FIND_ATTR(key); return attr; } @@ -37,19 +39,31 @@ PyObject* item; PyObject* obj = PY(GUARANTEE_PY(name, 0)); PyObject* key = PY(GUARANTEE_PY(name, 1)); - assert(argv[0] != NULL); - assert(argv[1] != NULL); +// dprintf("looking for item %s in %s\n", PyObject_AsCString(key), PyObject_AsCString(obj)); + dprintf("looking for item %s\n", PyObject_AsCString(key)); item = PyObject_GetItem(obj, key); + if (item) + dprintf("got item: %s\n", PyObject_AsCString(item)); + else + dprintf("item is NULL!\n"); return SCM(got_attrib_obj(item, obj, key)); } +Scheme_Object* spy_setitem_obj(const char* name, int argc, Scheme_Object* argv[]) +{ + PyObject* obj = PYARG(0); + PyObject* key = PYARG(1); + PyObject* value = PYARG(2); + PyObject_SetItem(obj, key, value); + return scheme_void; +} + // PyObject PyString -> PyObject Scheme_Object* spy_getattr_obj(const char* name, int argc, Scheme_Object* argv[]) { PyObject* attr; PyObject* obj = PY(GUARANTEE_PY(name, 0)); PyObject* key = PY(GUARANTEE_PY(name, 1)); -// assert(PyString_Check(argv[1])); attr = PyObject_GetAttr(obj, key); return SCM(got_attrib_obj(attr, obj, key)); } Index: schemefunctionobject.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/schemefunctionobject.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- schemefunctionobject.c 23 Nov 2004 21:46:17 -0000 1.2 +++ schemefunctionobject.c 14 Dec 2004 10:41:02 -0000 1.3 @@ -1,5 +1,12 @@ #include "schemefunctionobject.h" + +void PySchemeFunction_Dealloc(PyObject* pyobj) +{ + scheme_gc_ptr_ok(((PySchemeFunctionObject*)pyobj)->proc); + PyObject_GC_Del(pyobj); +} + PyObject* PySchemeFunction_New(Scheme_Object* proc, const char* fname) { PySchemeFunctionObject* fnobj = PyObject_GC_New(PySchemeFunctionObject, &PySchemeFunction_Type); @@ -21,13 +28,14 @@ // save the Scheme lambda fnobj->proc = proc; + scheme_dont_gc_ptr(proc); return (PyObject*) fnobj; } #include "structmember.h" static PyMemberDef schemefunction_members[] = { //{"__ns__", T_OBJECT, offsetof(PySchemeNSObject, ns), READONLY}, - {0} + {0, 0, 0, 0, 0} }; PyObject* PySchemeFunction_Call(PyObject* self, PyObject* argsTuple, PyObject* kw) @@ -46,6 +54,7 @@ argv[i] = SCM(arg); } result = _scheme_apply(fn->proc, argc, argv); + //result = _scheme_tail_apply_no_copy(fn->proc, argc, argv); if (result == NULL) scheme_signal_error("PySchemeFunction_Call: result is NULL."); return PY(result); @@ -75,7 +84,7 @@ PySchemeFunctionObject* fn = (PySchemeFunctionObject*) obj; char fname[1024]; // TODO: eww, memory/cpu pig - snprintf(fname, 1024, "<SchemeFunction %s at 0x%x>", PyString_AsString(fn->func_name), obj); + snprintf(fname, 1024, "<SchemeFunction %s at 0x%x>", PyString_AsString(fn->func_name), (unsigned int) obj); return PyString_FromString(fname); } @@ -87,12 +96,12 @@ "SchemeFunction", /* tp_name */ sizeof(PySchemeFunctionObject), /* tp_size */ 0, /* tp_itemsize */ - 0, /* tp_dealloc */ + PySchemeFunction_Dealloc, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ - 0, /* tp_repr */ + PySchemeFunction_ToString, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ @@ -122,5 +131,6 @@ 0, /* tp_alloc */ 0, /* tp_new */ 0, /* tp_free */ + 0, 0, 0, 0, 0, 0, 0 }; Index: Makefile =================================================================== RCS file: /cvsroot/plt-spy/python/embed/Makefile,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- Makefile 23 Nov 2004 12:57:16 -0000 1.1 +++ Makefile 14 Dec 2004 10:41:02 -0000 1.2 @@ -5,11 +5,14 @@ MZC=/home/dsilva/cvs/plt-nightly/plt/bin-nogc/mzc #MZC=/tmp/plt-299.22/bin/mzc CC=$(MZC) +COPTIMIZE_FLAGS=++ccf -ftracer ++ccf -foptimize-sibling-calls +CWARNING_FLAGS=++ccf -Wchar-subscripts ++ccf -Wformat ++ccf -Wformat-nonliteral ++ccf -Wformat-security ++ccf -Wnonnull ++ccf -Wimplicit-int ++ccf -Werror-implicit-function-declaration ++ccf -Wmissing-braces ++ccf -Wparentheses ++ccf -Wsequence-point ++ccf -Wreturn-type ++ccf -Wswitch ++ccf -Wuninitialized ++ccf -W ++ccf -Wmissing-noreturn ++ccf -Wunreachable-code ++ccf -Werror #CFLAGS=++ccf -I. ++ccf -g ++ccf -finline-functions ++ccf -foptimize-sibling-calls ++ccf -mtail-call --cc -CFLAGS= ++ccf -I. ++ccf -I/usr/include/python2.3 ++ccf -g ++ccf -fPIC ++ccf -Wl,-E --cc +CFLAGS= ++ccf -I. ++ccf -I/usr/include/python2.3 $(CWARNING_FLAGS) ++ccf -g3 ++ccf -fPIC ++ccf -Wl,-E $(COPTIMIZE_FLAGS) --cc PY_EXT_DIR=/usr/lib/python2.3/site-packages -PY_EXTENSIONS=$(shell find $(PY_EXT_DIR) -name "*.so") +### hack, plus skip OpenGL bindings +PY_EXTENSIONS=$(shell find $(PY_EXT_DIR) -name "*.so" | grep -v GL) ALL_RPATHS=$(foreach e,$(PY_EXTENSIONS),$(shell echo ++ldf -Wl,-rpath ++ldf $(shell dirname $(e)))) #$(shell bash -c 'for F in $(PY_EXTENSIONS); do echo ++ldf -Wl,-rpath \$F; done') RPATHS=$(shell echo $(ALL_RPATHS) | uniq) @@ -32,13 +35,14 @@ #MZLIBDIR=/home/dsilva/cvs/plt-nightly/plt/lib/ TARGET=compiled/native/i386-linux/embed.so +#TARGET=embed.so #fake-program: fake-program.c embed.so # gcc -o fake-program -I/home/dsilva/plt/include fake-program.c cpy-bindings.so $(MZLIBDIR)/libmzscheme.a $(MZLIBDIR)/mzdyn.o $(MZLIBDIR)/libmzgc.a -lm -ldl -lpython2.3 #/home/dsilva/incoming/modded-python/libpython2.3.a # echo MakeSpy: no errors. $(TARGET): $(ALLOBJS) - $(MZC) -v --linker /usr/bin/gcc --ld $(TARGET) ++ldf -g ++ldf -fPIC ++ldf -Xlinker ++ldf -export-dynamic ++ldf -Wl,-E $(RPATHS) $(LIBS) --ldf-show --ldl-show $(ALLOBJS) #/home/dsilva/incoming/modded-python/out/lib/libpython2.3.a + $(MZC) -v --linker /usr/bin/gcc --ld $(TARGET) ++ldf -g ++ldf -fPIC ++ldf -Xlinker ++ldf -export-dynamic ++ldf -Wl,-E $(RPATHS) $(LIBS) --ldf-show --ldl-show $(ALLOBJS) #/home/dsilva/incoming/modded-python/out/lib/libpython2.3.a .c.o: $(CC) -d `dirname $@` $(CFLAGS) $< Index: embed.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/embed.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- embed.c 23 Nov 2004 21:46:16 -0000 1.2 +++ embed.c 14 Dec 2004 10:41:02 -0000 1.3 @@ -1,6 +1,10 @@ #include "spy.h" +#include "schemefunctionobject.h" Scheme_Env* spy_global_env; +Scheme_Object* spy_none; +//static Scheme_Object* spy_apply_argv[1024]; + ///////////////////////////////////////////////// @@ -20,22 +24,11 @@ { // just keep some object around in Spy's namespace; // shut down Python when Spy is gone too. + scheme_register_extension_global(&pytoken, sizeof(Scheme_Object*)); pytoken = cons(scheme_null, scheme_null); scheme_register_finalizer(pytoken, spy_Py_Finalize, NULL, NULL, NULL); } -Scheme_Object* scheme_initialize(Scheme_Env* ns) -{ - spy_tag = sym("spy-object"); - PRINTF("[scheme_initialize] Initializing libpython...\n"); - Py_Initialize(); - PRINTF("[scheme_initialize] Initialized libpython.\n"); -// spy_register_pytoken(ns); - - if(1) { PyObject* warn = PyExc_Warning; } - - return scheme_reload(ns); -} /* @@ -60,17 +53,48 @@ return PYARG(0) == PYARG(1) ? scheme_true : scheme_false; } +Scheme_Object* spy_callable_huh(const char* name, int argc, Scheme_Object* argv[]) +{ + return PyCallable_Check(PYARG(0)) ? scheme_true : scheme_false; +} + + + Scheme_Object* spy_apply(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* scheme_pyfn = GUARANTEE_PY(name, 0); PyObject* fnobj = PY(scheme_pyfn); - PyObject* args = PyTuple_New(argc - 1); - PyObject* result, *objstr; - int i; - // TODO: maybe use this optimization - /* if (fnobj->ob_type == &PySchemeFunction_Type) - result = _scheme_apply(fnobj->proc, argc - 1, argv + 1); - */ + dprintf("entry\n"); + + if (fnobj->ob_type == &PySchemeFunction_Type) + { + Scheme_Object* argv_static[4]; + Scheme_Object** argvptr + = --argc < 5 ? + argv_static : + (Scheme_Object**) scheme_malloc(argc*sizeof(Scheme_Object*)); + int i = 0; + dprintf("fnobj is a scheme fn, new argc is %d\n", argc); + dprintf("proc address is 0x%x\n", (unsigned int) (((PySchemeFunctionObject*)fnobj)->proc)); + dprintf("argv is at 0x%x\n", (unsigned int) argv); + memcpy(argvptr, argv + 1, sizeof(Scheme_Object*) * argc); +/* for ( ; i < argcc; i++ ) + argvptr[i] = SPYARG(i + 1); */ + dprintf("applying...\n"); + return _scheme_tail_apply_no_copy(((PySchemeFunctionObject*)fnobj)->proc, argc, argvptr); + } + else + if (!PyCallable_Check(fnobj)) + { + scheme_signal_error("py-apply: object is not callable"); + return NULL; + } + else + { + PyObject* args = PyTuple_New(argc - 1); + PyObject* result, *objstr; + int i; + dprintf("argc is %d\n", argc); dprintf("arg 0: %s\n", PyString_AsString(PyObject_Str(fnobj))); @@ -80,19 +104,15 @@ if ( arg == NULL ) scheme_signal_error("spy_apply got a NULL argument."); dprintf("arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); - // TODO: change this to PyTuple_SET_ITEM for speed - PyTuple_SetItem(args, i - 1, arg); + PyTuple_SET_ITEM(args, i - 1, arg); } - dprintf("calling object at address 0x%x\n", fnobj); -// objstr = PyObject_Str(fnobj); -// dprintf("objstr is at 0x%x\n", objstr); -// dprintf("obj: %s\n", PyString_AsString(objstr)); - dprintf("callabe? %s\n", (PyCallable_Check(fnobj) ? "yes" : "no")); + dprintf("calling object at address 0x%x\n", (unsigned int) fnobj); result = fnobj->ob_type->tp_call ? fnobj->ob_type->tp_call(fnobj, args, NULL) : PyObject_CallObject(fnobj, args); - dprintf("result address: 0x%x\n", result); - return SCM( result ); + dprintf("result address: 0x%x\n", (unsigned int) result); + return result == NULL ? spy_none : SCM(result); + } } static const PyTypeObject* spy_builtin_types[] = { @@ -108,6 +128,9 @@ &PyComplex_Type, &PyUnicode_Type, &PyDict_Type, + &PySlice_Type, + &PyFunction_Type, + &PySchemeFunction_Type, }; static void spy_load_builtin_types(Scheme_Env* env, const PyTypeObject* spy_builtin_types[], unsigned long size) @@ -118,12 +141,21 @@ for (i = 0; i < N; i++) { char name[1024]; - type = spy_builtin_types[i]; + type = (PyTypeObject*) spy_builtin_types[i]; snprintf(name, 1024, "cpy-%s", type->tp_name); - scheme_add_global(name, SCM(type), env); + scheme_add_global(name, SCM((PyObject*) type), env); } } +Scheme_Object* spy_python_object_huh(const char* name, int argc, Scheme_Object* argv[]) +{ + return SCHEME_PYOBJP(argv[0]) ? scheme_true : scheme_false; +} + +Scheme_Object* spy_repr(const char* name, int argc, Scheme_Object* argv[]) +{ + return SCM(PyObject_Repr(PYARG(0))); +} #define SPY_PROTO(id) Scheme_Object* id(const char* name, int argc, Scheme_Object* argv[]) @@ -132,10 +164,17 @@ SPY_PROTO( spy_make_py_symbol ); SPY_PROTO( spy_make_py_number ); SPY_PROTO( spy_get_py_number ); -SPY_PROTO( spy_make_py_list ); -SPY_PROTO( spy_get_py_list ); -SPY_PROTO( spy_make_py_tuple ); -SPY_PROTO( spy_get_py_tuple ); +SPY_PROTO( spy_make_pyint ); +SPY_PROTO( spy_make_int ); +SPY_PROTO( spy_make_pylist_from_list ); +SPY_PROTO( spy_make_pylist_from_vector ); +SPY_PROTO( spy_make_pylist_from_int_vector ); +SPY_PROTO( spy_make_list_from_pylist ); +SPY_PROTO( spy_make_vector_from_pylist ); +SPY_PROTO( spy_make_pytuple_from_list ); +SPY_PROTO( spy_make_pytuple_from_vector ); +SPY_PROTO( spy_make_list_from_pytuple ); +SPY_PROTO( spy_make_vector_from_pytuple ); SPY_PROTO( spy_make_py_dict ); SPY_PROTO( spy_get_py_dict ); SPY_PROTO( spy_make_py_file ); @@ -151,6 +190,7 @@ SPY_PROTO( spy_len_py ); SPY_PROTO( spy_len_scm ); SPY_PROTO( spy_getitem_obj ); +SPY_PROTO( spy_setitem_obj ); SPY_PROTO( spy_getattr_obj ); SPY_PROTO( spy_getattr_sym ); SPY_PROTO( spy_getattr_str ); @@ -167,27 +207,40 @@ SPY_PROTO( spy_index ); SPY_PROTO( spy_import ); SPY_PROTO( spy_is_huh ); +SPY_PROTO( spy_python_object_huh ); +SPY_PROTO( spy_repr ); +SPY_PROTO( spy_callable_huh ); + + +typedef Scheme_Object* (Scheme_Closed_CStr_Prim)(const char* name, int argc, Scheme_Object *argv[]); struct scm_prim { - char *name; - Scheme_Closed_Prim *prim; - int mina; - int maxa; + const char *name; + Scheme_Closed_CStr_Prim *prim; + int mina; + int maxa; }; static const struct scm_prim scm_prim[] = { - { "make-py-string", spy_make_py_string, 1, 1 }, + { "make-py-string", &spy_make_py_string, 1, 1 }, { "get-py-string", spy_get_py_string, 1, 1 }, { "make-py-symbol", spy_make_py_symbol, 1, 1 }, { "make-py-number", spy_make_py_number, 1, 1 }, + { "integer->pyint", spy_make_pyint, 1, 1 }, + { "pyint->integer", spy_make_int, 1, 1 }, { "get-py-number", spy_get_py_number, 1, 1 }, - { "make-py-list", spy_make_py_list, 1, 1 }, - { "get-py-list", spy_get_py_list, 1, 1 }, - { "make-py-tuple", spy_make_py_tuple, 1, 1 }, - { "get-py-tuple", spy_get_py_tuple, 1, 1 }, + { "int-vector->pylist", spy_make_pylist_from_int_vector, 1, 1 }, + { "vector->pylist", spy_make_pylist_from_vector, 1, 1 }, + { "list->pylist", spy_make_pylist_from_list, 1, 1 }, + { "pylist->list", spy_make_list_from_pylist, 1, 1 }, + { "pylist->vector", spy_make_vector_from_pylist, 1, 1 }, + { "vector->pytuple", spy_make_pytuple_from_vector, 1, 1 }, + { "list->pytuple", spy_make_pytuple_from_list, 1, 1 }, + { "pytuple->vector", spy_make_vector_from_pytuple, 1, 1 }, + { "pytuple->list", spy_make_list_from_pytuple, 1, 1 }, { "make-py-dict", spy_make_py_dict, 1, 1 }, { "get-py-dict", spy_get_py_dict, 1, 1 }, { "make-py-file", spy_make_py_file, 1, 1 }, @@ -203,6 +256,7 @@ { "spy-len/scm", spy_len_scm, 1, 1 }, { "spy-len/py", spy_len_py, 1, 1 }, { "spy-getitem/obj", spy_getitem_obj, 2, 2 }, + { "spy-setitem/obj", spy_setitem_obj, 3, 3 }, { "spy-getattr/obj", spy_getattr_obj, 2, 2 }, { "spy-getattr/sym", spy_getattr_sym, 2, 2 }, { "spy-getattr/str", spy_getattr_str, 2, 2 }, @@ -219,6 +273,9 @@ { "spy-index", spy_index, 2, 2 }, { "spy-import", spy_import, 1, 1 }, { "py-is?", spy_is_huh, 2, 2 }, + { "python-object?", spy_python_object_huh, 1, 1 }, + { "spy-repr", spy_repr, 1, 1 }, + { "spy-callable?", spy_callable_huh, 1, 1 }, }; // from Scott Owens's sgl/gl-prims.h @@ -232,8 +289,8 @@ for (i = 0; i < N; i++) { - prim = scheme_make_closed_prim_w_arity(scm_prim[i].prim, - scm_prim[i].name, + prim = scheme_make_closed_prim_w_arity((Scheme_Closed_Prim*) scm_prim[i].prim, + (void*) scm_prim[i].name, scm_prim[i].name, scm_prim[i].mina, scm_prim[i].maxa); @@ -241,18 +298,41 @@ } } - -Scheme_Object* scheme_reload(Scheme_Env* enclosing_ns) +Scheme_Object* scheme_initialize(Scheme_Env* enclosing_ns) { + scheme_register_extension_global(&spy_tag, sizeof(Scheme_Object*)); +// scheme_register_extension_global(&spy_apply_argv, sizeof(spy_apply_argv)); + spy_tag = sym("spy-object"); + PRINTF("[scheme_initialize] Initializing libpython...\n"); + Py_Initialize(); + PRINTF("[scheme_initialize] Initialized libpython.\n"); + PyType_Ready(&PySchemeFunction_Type); +// spy_register_pytoken(ns); + +// if(1) { PyObject* warn = PyExc_Warning; } + { Scheme_Env* ns = scheme_primitive_module(scheme_module_name(), enclosing_ns); - spy_global_env = ns; + scheme_register_extension_global(&spy_global_env, sizeof(Scheme_Env*)); + scheme_register_extension_global(&spy_none, sizeof(spy_none)); + spy_global_env = enclosing_ns; + spy_none = SCM(Py_None); scheme_add_global("__pytoken__", pytoken, ns); - scheme_add_global("cpy-none", SCM(Py_None), ns); + scheme_add_global("cpy-none", spy_none, ns); + scheme_add_global("cpy-true", SCM(Py_True), ns); + scheme_add_global("cpy-false", SCM(Py_False), ns); scheme_load_prim(ns, scm_prim, sizeof(scm_prim)); spy_load_builtin_types(ns, spy_builtin_types, sizeof(spy_builtin_types)); scheme_finish_primitive_module(ns); + } + + return scheme_reload(enclosing_ns); +} + + +Scheme_Object* scheme_reload(Scheme_Env* enclosing_ns) +{ return scheme_void; } |
From: Daniel S. <aph...@us...> - 2004-11-23 21:46:54
|
Update of /cvsroot/plt-spy/python/embed In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3836/embed Modified Files: embed.c schemefunctionobject.c schemefunctionobject.h spy.h value-conversion.c Log Message: fixed PySchemeFunction_Call: uses _scheme_apply instead of scheme_apply, now we can jump into continuations PySchemeFunction_New: now storing the function name new fn: PySchemeFunction_ToString try-except%: grab the value from exn:python runtime-support.ss: new: py-compatible-exn? make-py-code now returns `(,scheme-proc ,proc-name-symbol) return% and suite%: don't generate a call to the return continuation if it's the last statement Index: schemefunctionobject.h =================================================================== RCS file: /cvsroot/plt-spy/python/embed/schemefunctionobject.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- schemefunctionobject.h 23 Nov 2004 12:57:16 -0000 1.1 +++ schemefunctionobject.h 23 Nov 2004 21:46:17 -0000 1.2 @@ -23,4 +23,4 @@ PyAPI_DATA(PyTypeObject) PySchemeFunction_Type; -PyObject* PySchemeFunction_New(Scheme_Object* proc); +PyObject* PySchemeFunction_New(Scheme_Object* proc, const char* name); Index: schemefunctionobject.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/schemefunctionobject.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- schemefunctionobject.c 23 Nov 2004 12:57:16 -0000 1.1 +++ schemefunctionobject.c 23 Nov 2004 21:46:17 -0000 1.2 @@ -1,6 +1,6 @@ #include "schemefunctionobject.h" -PyObject* PySchemeFunction_New(Scheme_Object* proc) +PyObject* PySchemeFunction_New(Scheme_Object* proc, const char* fname) { PySchemeFunctionObject* fnobj = PyObject_GC_New(PySchemeFunctionObject, &PySchemeFunction_Type); if (fnobj == NULL) @@ -14,7 +14,7 @@ fnobj->func_defaults = NULL; fnobj->func_closure = NULL; fnobj->func_doc = NULL; - fnobj->func_name = NULL; + fnobj->func_name = PyString_FromString(fname); fnobj->func_dict = NULL; fnobj->func_weakreflist = NULL; fnobj->func_module = NULL; @@ -35,16 +35,20 @@ PySchemeFunctionObject* fn = (PySchemeFunctionObject*) self; // TODO: fix this static array, it's a security hole Scheme_Object* argv[1024]; + Scheme_Object* result; int i, argc; argc = PyTuple_GET_SIZE(argsTuple); - printf("[PySchemeFunction_Call] args tuple length is %d\n", argc); + PRINTF("[PySchemeFunction_Call] args tuple length is %d\n", argc); for ( i = 0; i < argc; i++ ) { PyObject* arg = PyTuple_GET_ITEM(argsTuple, i); - printf("[PySchemeFunction_Call] arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); + PRINTF("[PySchemeFunction_Call] arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); argv[i] = SCM(arg); } - return PY( scheme_apply(fn->proc, argc, argv) ); + result = _scheme_apply(fn->proc, argc, argv); + if (result == NULL) + scheme_signal_error("PySchemeFunction_Call: result is NULL."); + return PY(result); } /* Bind a function to an object */ @@ -66,6 +70,15 @@ return meth; } +PyObject* PySchemeFunction_ToString(PyObject* obj) +{ + PySchemeFunctionObject* fn = (PySchemeFunctionObject*) obj; + char fname[1024]; + // TODO: eww, memory/cpu pig + snprintf(fname, 1024, "<SchemeFunction %s at 0x%x>", PyString_AsString(fn->func_name), obj); + return PyString_FromString(fname); +} + PyTypeObject PySchemeFunction_Type = { @@ -85,7 +98,7 @@ 0, /* tp_as_mapping */ 0, /* tp_hash */ PySchemeFunction_Call, /* tp_call */ - 0, /* tp_str */ + PySchemeFunction_ToString, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Index: embed.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/embed.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- embed.c 23 Nov 2004 12:57:16 -0000 1.1 +++ embed.c 23 Nov 2004 21:46:16 -0000 1.2 @@ -54,6 +54,11 @@ +Scheme_Object* spy_is_huh(const char* name, int argc, Scheme_Object* argv[]) +{ + // TODO: I think this is wrong :) "1000 is 1000" will fail + return PYARG(0) == PYARG(1) ? scheme_true : scheme_false; +} Scheme_Object* spy_apply(const char* name, int argc, Scheme_Object* argv[]) { @@ -62,11 +67,18 @@ PyObject* args = PyTuple_New(argc - 1); PyObject* result, *objstr; int i; + // TODO: maybe use this optimization + /* if (fnobj->ob_type == &PySchemeFunction_Type) + result = _scheme_apply(fnobj->proc, argc - 1, argv + 1); + */ + dprintf("argc is %d\n", argc); dprintf("arg 0: %s\n", PyString_AsString(PyObject_Str(fnobj))); for ( i = 1; i < argc; i++ ) { PyObject* arg = PYARG(i); + if ( arg == NULL ) + scheme_signal_error("spy_apply got a NULL argument."); dprintf("arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); // TODO: change this to PyTuple_SET_ITEM for speed PyTuple_SetItem(args, i - 1, arg); @@ -154,6 +166,7 @@ SPY_PROTO( spy_pyobj_to_pystr ); SPY_PROTO( spy_index ); SPY_PROTO( spy_import ); +SPY_PROTO( spy_is_huh ); struct scm_prim { @@ -205,6 +218,7 @@ { "py-object->py-string", spy_pyobj_to_pystr, 1, 1 }, { "spy-index", spy_index, 2, 2 }, { "spy-import", spy_import, 1, 1 }, + { "py-is?", spy_is_huh, 2, 2 }, }; // from Scott Owens's sgl/gl-prims.h @@ -233,7 +247,7 @@ Scheme_Env* ns = scheme_primitive_module(scheme_module_name(), enclosing_ns); spy_global_env = ns; scheme_add_global("__pytoken__", pytoken, ns); - scheme_add_global("cpy-none", Py_None, ns); + scheme_add_global("cpy-none", SCM(Py_None), ns); scheme_load_prim(ns, scm_prim, sizeof(scm_prim)); spy_load_builtin_types(ns, spy_builtin_types, sizeof(spy_builtin_types)); Index: spy.h =================================================================== RCS file: /cvsroot/plt-spy/python/embed/spy.h,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- spy.h 23 Nov 2004 12:57:16 -0000 1.1 +++ spy.h 23 Nov 2004 21:46:17 -0000 1.2 @@ -75,7 +75,7 @@ ////////////// general convenience -#define DEBUG_SPY +//#define DEBUG_SPY #ifdef DEBUG_SPY #define PRINTF(fmt, args...) printf(fmt, ##args) Index: value-conversion.c =================================================================== RCS file: /cvsroot/plt-spy/python/embed/value-conversion.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- value-conversion.c 23 Nov 2004 12:57:16 -0000 1.1 +++ value-conversion.c 23 Nov 2004 21:46:17 -0000 1.2 @@ -142,8 +142,10 @@ Scheme_Object* spy_make_py_function(const char* name, int argc, Scheme_Object* argv[]) { - Scheme_Object* proc = GUARANTEE_PROCEDURE(name, 0); - PyObject* fnobj = PySchemeFunction_New(proc); + Scheme_Object* proc_and_name = GUARANTEE_PAIR(name, 0); + Scheme_Object* proc = SCHEME_CAR(proc_and_name); + Scheme_Object* fname = SCHEME_CDR(proc_and_name); + PyObject* fnobj = PySchemeFunction_New(proc, SCHEME_SYM_VAL(fname)); return SCM(fnobj); } |
From: Daniel S. <aph...@us...> - 2004-11-23 21:46:53
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3836 Modified Files: compiler-expr.ss compiler-stmt.ss python-import.ss python.ss runtime-support.ss Log Message: fixed PySchemeFunction_Call: uses _scheme_apply instead of scheme_apply, now we can jump into continuations PySchemeFunction_New: now storing the function name new fn: PySchemeFunction_ToString try-except%: grab the value from exn:python runtime-support.ss: new: py-compatible-exn? make-py-code now returns `(,scheme-proc ,proc-name-symbol) return% and suite%: don't generate a call to the return continuation if it's the last statement Index: runtime-support.ss =================================================================== RCS file: /cvsroot/plt-spy/python/runtime-support.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- runtime-support.ss 23 Nov 2004 12:57:15 -0000 1.4 +++ runtime-support.ss 23 Nov 2004 21:46:16 -0000 1.5 @@ -11,6 +11,11 @@ "generator-oleg.ss" (lib "contract.ss") ) + + ;; symbol procedure number dunno -> procecure + (define (make-py-code name proc arity x) + (cons proc name)) + #| (require-for-syntax (lib "list.ss")) @@ -81,14 +86,14 @@ (define promise #f) (define __init__ (make-py-function - ;(make-py-code '__init__ + (make-py-code '__init__ (lambda (self init-cursor) ;; shouldn't init-cursor be a PyObj? (unless (promise? init-cursor) (error 'py-generator.__init__ (format "Expected a delayed expression, got ~v" init-cursor))) (set! promise init-cursor)) - ; 2 - ; null) + 2 + null) )) (define (next/scheme) (unless promise @@ -106,11 +111,11 @@ (next/scheme))))) (define next (make-py-function - ; (make-py-code 'next + (make-py-code 'next (lambda (self) (next/scheme)) - ; 1 - ; null) + 1 + null) )) (list (cons (make-py-symbol '__init__) __init__) @@ -193,6 +198,7 @@ py-if py-not py-raise + py-compatible-exn? exn:python? exn:python-value ;current-runtime-support-context @@ -204,8 +210,22 @@ (define-struct (exn:python exn) (value)) - (define (py-raise py-obj) - (raise (make-exn:python py-obj (current-continuation-marks)))) + ;; TODO: fixme, should be something like exn_type(parm, traceback) + (define (py-raise exn_type parm traceback) + (raise (make-exn:python (string->immutable-string (format "Spy user exception: type: ~a parm: ~a traceback: ~a" + (and exn_type + (py-object%->string exn_type)) + (and parm + (py-object%->string parm)) + (and traceback + (py-object%->string traceback)))) + (current-continuation-marks) + exn_type))) + + ;; warning: thrown-obj is NOT an exn:python object, it's a python object + (define (py-compatible-exn? expected-type thrown-obj) + (or (py-is? expected-type thrown-obj) + (py-is-a? thrown-obj expected-type))) (define py-add/safe spy-add) #| (define (py-add/safe x y) @@ -230,7 +250,9 @@ (define py-list? spy-list?) (define python-get-type-name spy-get-type-name) - (define (py-none? x) (eqv? x cpy-none)) + (define (py-none? x) + ;(eqv? x cpy-none)) + (py-is? x cpy-none)) #| TODO: uncomment this @@ -247,8 +269,8 @@ |# ;; symbol procedure number dunno -> procecure - (define (make-py-code name proc arity x) - proc) +; (define (make-py-code name proc arity x) +; (cons proc name)) (define (py-call/cc py-fn) @@ -322,7 +344,11 @@ [(py-number? obj) (not (zero? (get-py-number obj)))] [(py-list? obj) (not (zero? (spy-len/scm obj)))] [(py-none? obj) #f] - [else (error 'py-object%->bool "Can't handle this object yet: ~a" (py-object%->string obj))])) + [else (begin + ;; TODO: add more cond clauses + ;(printf "py-object%->bool WARNING: Can't handle this object yet: ~a" + ; (py-object%->string obj)) + #t)])) ;; ==: X X -> bool (define(== a b) Index: compiler-stmt.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-stmt.ss,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- compiler-stmt.ss 23 Nov 2004 12:57:15 -0000 1.5 +++ compiler-stmt.ss 23 Nov 2004 21:46:15 -0000 1.6 @@ -14,6 +14,10 @@ ) (provide (all-defined-except bindings-mixin)) + + (define using-return-ec? (make-parameter #f)) + (define using-break-ec? (make-parameter #f)) + (define using-continue-ec? (make-parameter #f)) (define statement% (class ast-node% @@ -49,6 +53,12 @@ (define/public (yields?) #f) + ;; return-is-last?: -> bool + ;; determine whether the last substatement of a compound is a return, + ;; so that suite% can omit the default py-none return at the end of a function + (define/public (return-is-last?) + #f) + (super-instantiate ()))) (define compound-statement% @@ -60,7 +70,10 @@ (send s yields?)) substatements)) - (printf "new compound statement: ~a~n" this) + (define/override (return-is-last?) + (send (first (last-pair substatements)) return-is-last?)) + + ;(printf "new compound statement: ~a~n" this) (super-new))) @@ -116,8 +129,10 @@ ;;daniel (define scope #f) - (define (top?) (or (is-a? scope module-scope%) - (is-a? scope class-definition%))) + (define (top?) + ;(printf "ASSIGNMENT SCOPE::: ~a~n" scope) + (or (is-a? scope module-scope%) + (is-a? scope class-definition%))) ;;daniel, temporary for debugging (define/public (get-targs) targets) @@ -152,8 +167,8 @@ (->orig-so (cond [(is-a? target tidentifier%) (let ([target-so (send target to-scheme)]) - (when (eq? 'sys (send target get-symbol)) - (printf "ASSIGN SYS: def? ~a binding? ~a set? ~a bound? ~a scope: ~a module-scope? ~a~n" (def-targ? target) (binding-targ? target) (set-targ? target) (send scope is-bound? target) scope (is-a? scope module-scope%))) + ;(when (eq? 'sys (send target get-symbol)) + ; (printf "ASSIGN SYS: def? ~a binding? ~a set? ~a bound? ~a scope: ~a module-scope? ~a~n" (def-targ? target) (binding-targ? target) (set-targ? target) (send scope is-bound? target) scope (is-a? scope module-scope%))) (if (def-targ? target) `(define ,target-so ,rhs) `(set! ,target-so ,rhs)))] @@ -212,11 +227,11 @@ ;;daniel (inherit ->orig-so) (define/override (to-scheme) - (printf "ASSIGNMENT: BEGIN COMPILING~n") + ;(printf "ASSIGNMENT: BEGIN COMPILING~n") (let* ([rhs (datum->syntax-object (current-toplevel-context) (gensym 'rhs) #f #f)] [body (map (lambda (t) (let ([so (assignment-so t rhs)]) - (printf "ASSIGNMENT TARGET: ~v~n" (syntax-object->datum so)) + ;(printf "ASSIGNMENT TARGET: ~v~n" (syntax-object->datum so)) so)) targets)]) (->orig-so (if (top?) @@ -434,15 +449,20 @@ (set! tail-position? #t)) (define/override (needs-escape-continuation? ec) - ;(printf "return~n") + ;(printf "RETURN: tail position? ~a~n" tail-position?) + ;(printf "RETURN: return symbol: ~a~n" (send scope get-return-symbol)) + ;(printf "RETURN: ec: ~a~n" ec) (and (not tail-position?) (eq? ec (send scope get-return-symbol)))) + (define/override (return-is-last?) #t) + ;;daniel (inherit ->orig-so ->lex-so) (define/override (to-scheme); py-return) ;(->orig-so `(,py-return ,(send expression to-scheme)))) - (->orig-so (if tail-position? + (->orig-so (if (and tail-position? + (not (using-return-ec?))) (send expression to-scheme) `(,(send scope get-return-symbol) ,(if expression (send expression to-scheme) @@ -752,23 +772,41 @@ (ormap (lambda (statement) (send statement needs-escape-continuation? ec)) statements)) + + ;; compound-statement% already implements this method, + ;; but we already have a ref to the last statement, + ;; so this is faster + (define/override (return-is-last?) + (send last-statement return-is-last?)) ;;daniel (inherit ->orig-so) (define/override to-scheme (opt-lambda ([escape-continuation-symbol #f] [lambda-suite? #f] [def-suite? #f]) - (let* ([return-is-last? (is-a? last-statement return%)] - [bodies (let ([statements (sub-stmt-map (lambda (s) (send s to-scheme)))] + (let* (;[return-is-last? (is-a? last-statement return%)] + [using-ec? (and escape-continuation-symbol + (needs-escape-continuation? escape-continuation-symbol))] + ;; TODO: ugh, these parameters are ugly + [bodies (let ([statements (parameterize ([using-return-ec? (or (and using-ec? + def-suite?) + (using-return-ec?))] + ;; TODO: implement these two + [using-break-ec? #f] + [using-continue-ec? #f]) + (sub-stmt-map (lambda (s) (send s to-scheme))))] [insert-void-return? (and def-suite? - (not return-is-last?))]) + (not (return-is-last?)))]) + ;; TODO: analyze flow for suites that always raise exceptions (if insert-void-return? ; functions that have no return must return None (append statements `(,(py-so 'py-none))) statements))] [bodies-with-let (if (or lambda-suite? def-suite?) (generate-function-bindings (send scope get-parms) bodies scope) bodies)]) - (->orig-so (if (and escape-continuation-symbol - (needs-escape-continuation? escape-continuation-symbol)) + ;(printf "SUITE: escape symbol: ~a~n" escape-continuation-symbol) + ;(printf "SUITE: needs escape? ~a~n" (needs-escape-continuation? escape-continuation-symbol)) + ;(printf "SUITE: is the last statement a return%? ~a~n" return-is-last?) + (->orig-so (if using-ec? `(call-with-escape-continuation (lambda (,escape-continuation-symbol) ,@bodies-with-let)) @@ -1014,6 +1052,8 @@ (list body else) (list body))]))) + (require (lib "pretty.ss")) + ;; 7.4 (define try-except% (class compound-statement% @@ -1056,30 +1096,41 @@ (define/override (collect-globals) (apply append (sub-stmt-map (lambda (s) (send s collect-globals))))) - - (define exn (gensym 'exn)) + + (define/override (needs-escape-continuation? ec) + ;(printf "TRY-EXCEPT: body is:~n") + ;(pretty-print (syntax-object->datum (send body to-scheme))) + + ;;;; erm... sub-stmt-ormap? :P + (let/ec k + (sub-stmt-map (lambda (s) + (when (send s needs-escape-continuation? ec) + (k #t)))) + #f)) + + (define scm-exn (gensym 'scm-exn)) (inherit ->orig-so) (define/override (to-scheme) (define error?-id (gensym 'error?)) (let ([handled `(with-handlers ,(map (match-lambda [(list e t s) - `[(lambda (,exn) - (and (,(py-so 'exn:python?) ,exn) + `[(lambda (,scm-exn) + (and (,(py-so 'exn:python?) ,scm-exn) ,(if e `(,(py-so 'py-compatible-exn?) ,(send e to-scheme) - ,exn) + (,(py-so 'exn:python-value) ,scm-exn)) #t))) ,(let* ([handler-body (send s to-scheme)] [lambda-body (if t `(,(generate-lambda (target->parameters t) handler-body) - (,(py-so 'exn:python-value) ,exn)) + (,(py-so 'exn:python-value) ,scm-exn)) handler-body)]) - `(lambda (,exn) + `(lambda (,scm-exn) ,(if else `(begin (set! ,error?-id #t) ,lambda-body) @@ -1247,12 +1298,20 @@ (define/public (get-return-symbol) return-symbol) (define/public (get-escape-continuation-symbol) return-symbol) + (define scope #f) + (define/override (set-bindings! es) + (set! scope es) (when es (unless (send es is-local? tname) (send es add-binding tname))) (send parms set-bindings! es) (send body set-bindings! this)) + + (define (top?) + ;(printf "FUNCTION DEF SCOPE::: ~a~n" scope) + (or (is-a? scope module-scope%) + (is-a? scope class-definition%))) (define/override (check-break/cont enclosing-loop) (send body check-break/cont #f)) @@ -1271,7 +1330,8 @@ (define/override (to-scheme) (->orig-so (let ([proc-name (send name to-scheme)] [body-so (send body to-scheme return-symbol #t #t)]) - `(define ,proc-name + `(,(if (top?) 'define 'set!) + ,proc-name ,(generate-py-lambda proc-name parms (if (ormap (lambda (s) (send s yields?)) Index: compiler-expr.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-expr.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- compiler-expr.ss 23 Nov 2004 12:57:15 -0000 1.4 +++ compiler-expr.ss 23 Nov 2004 21:46:14 -0000 1.5 @@ -427,7 +427,7 @@ ; ,body) ; (,(py-so 'py-sequence%->list) ,item))))] ; [else (error "bad target for a list comprehension")]) - (get-py-list ;;,(py-so 'py-sequence%->list) + (,(py-so 'get-py-list) ;;,(py-so 'py-sequence%->list) ,(send vals to-scheme)))))) Index: python-import.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python-import.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- python-import.ss 23 Nov 2004 12:57:15 -0000 1.4 +++ python-import.ss 23 Nov 2004 21:46:15 -0000 1.5 @@ -5,6 +5,7 @@ (lib "file.ss") (lib "etc.ss") (lib "string.ss") + (lib "contract.ss") "compile-python.ss" "utils.ss" ) @@ -179,13 +180,19 @@ symbols) symbols)))))) + (define python-verbose-eval? #f) + (define (set-python-verbose-eval?! ya?) + (set! python-verbose-eval? ya?)) + (provide/contract [set-python-verbose-eval?! (boolean? . -> . void?)]) + ; eval-python: (listof syntax-object) namespace -> (listof python-node) (define (eval-python s-obj-list namespace) (filter (lambda (result) (not (void? result))) (parameterize ([current-namespace namespace]) (map (lambda (so) - (printf "[eval-python] ~v~n" (syntax-object->datum so)) + (when python-verbose-eval? + (printf "[eval-python] ~v~n" (syntax-object->datum so))) (eval so)) s-obj-list)))) Index: python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- python.ss 23 Nov 2004 12:57:15 -0000 1.4 +++ python.ss 23 Nov 2004 21:46:16 -0000 1.5 @@ -20,7 +20,6 @@ (define (py-gen-result? x) (or (syntax? x) (python-node? x))) - (provide/contract [python (string? . -> . (listof string?))] [py-eval/asts ((listof syntax?) . -> . (listof string?))] @@ -37,6 +36,13 @@ (define ssd syntax-object->datum) (define (ssd* lst) (map ssd lst)) + + (require (lib "pretty.ss")) + (define (py-disp str) + (pretty-print (ssd* (py-gen str)))) + (define (py-disp/file fname) + (pretty-print (ssd* (py-gen/file fname)))) + (provide py-disp py-disp/file) (define (convert-value value) ;(namespace-require '(lib "primitives.ss" "python")) |
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3708 Modified Files: base.ss c-bindings.ss compiler-expr.ss compiler-stmt.ss compiler.ss parser.ss python-import.ss python.ss runtime-support.ss Log Message: Ditching the c directory soon. Hacking CPython has sort of failed. The new embed directory is an embedding of CPython. Spy will slowly implement parts of the runtime system and subclass parts of CPython (like PyFunction_Type) to override CPython instead of distributing a hacked libpython binary. Index: runtime-support.ss =================================================================== RCS file: /cvsroot/plt-spy/python/runtime-support.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- runtime-support.ss 3 Sep 2004 15:51:14 -0000 1.3 +++ runtime-support.ss 23 Nov 2004 12:57:15 -0000 1.4 @@ -1,14 +1,128 @@ (module runtime-support mzscheme + (require (lib "errortrace.ss" "errortrace")) (require (lib "list.ss") (lib "etc.ss") ;"primitives.ss" "python-import.ss" - "c-bindings.ss" + ;"c-bindings.ss" + "embed/embed.ss" "python-node.ss" "runtime-context.ss" + "generator-oleg.ss" (lib "contract.ss") ) + +#| + (require-for-syntax (lib "list.ss")) + (define-syntax (python-class stx) + (syntax-case stx (define/public) + [(_ name parent defs ...) + #`(spy-apply cpy-type + (make-py-symbol name) + (make-py-tuple (list parent)) + (make-py-dict + #,(let ([clauses (map (lambda (def) + (syntax-case def (define/public) + [(define/public (id args ...) bodies ...) + (cons #`(define id + (make-py-function (make-py-code 'id + (lambda (args ...) + bodies ...) + #,(length (syntax->list (syntax (args ...)))) + null))) + (syntax-e (syntax id)))] + [else def])) + (syntax->list (syntax (defs ...))))]) + #`(let () + #,@(map (lambda (clause) + (if (pair? clause) + (car clause) + clause)) + clauses) + #,(syntax-case (datum->syntax-object stx + (map cdr (filter pair? clauses))) () + [(ids ...) #`(list (cons (make-py-symbol 'ids) + ids) ...)])))))])) + + (define py-generator + (python-class 'generator cpy-object + (define promise #f) + + (define/public (__init__ self init-cursor) + (unless (promise? init-cursor) + (error 'py-generator.__init__ + (format "Expected a delayed expression, got ~v" init-cursor))) + (set! promise init-cursor)) + + (define (next/scheme) + (unless promise + (raise "StopIteration")) + (let ([res (with-handlers ([void (lambda (e) + (set! promise #f) + (raise e))]) + (force promise))]) + (if (pair? res) + (begin (set! promise (cdr res)) + (car res)) + (begin (set! promise #f) + (next/scheme))))) + + (define/public (next self) + (next/scheme)))) +|# + + (define py-generator + (spy-apply cpy-type + (make-py-symbol 'generator) + (make-py-tuple (list cpy-object)) + (make-py-dict + (let () + (define promise #f) + (define __init__ + (make-py-function + ;(make-py-code '__init__ + (lambda (self init-cursor) ;; shouldn't init-cursor be a PyObj? + (unless (promise? init-cursor) + (error 'py-generator.__init__ + (format "Expected a delayed expression, got ~v" init-cursor))) + (set! promise init-cursor)) + ; 2 + ; null) + )) + (define (next/scheme) + (unless promise + (raise "StopIteration")) + (printf "forcing....~n") + (let ([res (with-handlers ([void (lambda (e) + (set! promise #f) + (raise e))]) + (force promise))]) + (printf "forced~n") + (if (pair? res) + (begin (set! promise (cdr res)) + (car res)) + (begin (set! promise #f) + (next/scheme))))) + (define next + (make-py-function + ; (make-py-code 'next + (lambda (self) + (next/scheme)) + ; 1 + ; null) + )) + (list (cons (make-py-symbol '__init__) + __init__) + (cons (make-py-symbol 'next) + next)))))) + + + (define (make-py-generator/lambda fun) + (collect-garbage) + (printf "spy-apply is: ~v~n" spy-apply) + (spy-apply py-generator + (generative fun))) ; (require-for-syntax "compiler.ss") ;; get the compiler context (provide get-py-string ;; from C bindings @@ -24,32 +138,35 @@ make-py-code make-py-function make-py-dict + make-py-generator/lambda py-object->py-string namespace->py-module% python-load-module python-load-module/dotted python-import-from-module - (rename spy-cpython-apply py-apply) - (rename spy-cpython-index python-index) - py-is-a? ;(rename spy-cpython-instanceof py-is-a?) + (rename spy-apply py-apply) + (rename spy-index python-index) + py-is-a? ;(rename spy-instanceof py-is-a?) (rename cpy-none py-none) py-number? py-list? python-node? - (rename spy-cpython-getitem/obj py-get-item/obj) - (rename spy-cpython-getattr/obj py-get-attr/obj) - (rename spy-cpython-getattr/sym py-get-attr/sym) - (rename spy-cpython-getattr/str py-get-attr/str) - (rename spy-cpython-setattr/obj py-set-attr/obj) - (rename spy-cpython-setattr/sym py-set-attr/sym) - (rename spy-cpython-setattr/str py-set-attr/str) + (rename spy-getitem/obj py-get-item/obj) + (rename spy-getattr/obj py-get-attr/obj) + (rename spy-getattr/sym py-get-attr/sym) + (rename spy-getattr/str py-get-attr/str) + (rename spy-setattr/obj py-set-attr/obj) + (rename spy-setattr/sym py-set-attr/sym) + (rename spy-setattr/str py-set-attr/str) py-add/safe - (rename spy-cpython-add py-add) - (rename spy-cpython-sub py-sub) - (rename spy-cpython-mul py-mul) - (rename spy-cpython-div py-div) - (rename spy-cpython-mod py-mod) + (rename spy-add py-add) + (rename spy-sub py-sub) + (rename spy-mul py-mul) + (rename spy-div py-div) + (rename spy-mod py-mod) + py-bit-or py-call/cc + py-call/ec ;; types cpy-object cpy-str @@ -57,6 +174,13 @@ cpy-type cpy-tuple cpy-dict + cpy-int + cpy-long + cpy-float + cpy-bool + cpy-complex + cpy-unicode + ;cpy-buffer ;; temporary python-method-call ;;;;; pure Scheme @@ -68,6 +192,9 @@ py-print/file py-if py-not + py-raise + exn:python? + exn:python-value ;current-runtime-support-context ;current-toplevel-context ) @@ -75,47 +202,77 @@ ;;;;;;;;;; Python Runtime Support by Daniel ;;;;;;;;; - (define py-add/safe spy-cpython-add) + (define-struct (exn:python exn) (value)) + + (define (py-raise py-obj) + (raise (make-exn:python py-obj (current-continuation-marks)))) + + (define py-add/safe spy-add) #| (define (py-add/safe x y) ;(if (and (py-number? x) (py-number? y)) - (spy-cpython-add x y)) + (spy-add x y)) ;(python-method-call x '__add__ y) |# + (define py-bit-or spy-bit-or) + ;; temporary... delete me (define (python-method-call obj meth-name . args) - (apply spy-cpython-apply (spy-cpython-getattr/sym obj meth-name) args)) + (apply spy-apply (spy-getattr/sym obj meth-name) args)) (define py-number%->number get-py-number) (define py-string%->string get-py-string) (define py-list%->list get-py-list) (define py-file%->port get-py-file) - (define py-is-a? spy-cpython-instanceof) - (define py-number? spy-cpython-number?) - (define py-string? spy-cpython-string?) - (define py-list? spy-cpython-list?) - (define python-get-type-name spy-cpython-get-type-name) + (define py-is-a? spy-instanceof) + (define py-number? spy-number?) + (define py-string? spy-string?) + (define py-list? spy-list?) + (define python-get-type-name spy-get-type-name) (define (py-none? x) (eqv? x cpy-none)) + #| TODO: uncomment this + (define (my-append lst item) - (spy-cpython-apply (spy-cpython-getattr/sym lst 'append) item)) + (spy-apply (spy-getattr/sym lst 'append) item)) (provide/contract [my-append ((and/c py-list? (lambda (lst) - (< (spy-cpython-len/scm lst) + (< (spy-len/scm lst) 5))) python-node? . -> . py-none?)]) +|# + + ;; symbol procedure number dunno -> procecure + (define (make-py-code name proc arity x) + proc) + (define (py-call/cc py-fn) (let/cc k - (spy-cpython-apply py-fn (make-py-function (make-py-code 'kontinuation + (spy-apply py-fn (make-py-function (make-py-code 'kontinuation k 1 null))))) + + (define (py-call/ec py-fn) + (let/ec k + (spy-apply py-fn (make-py-function (make-py-code 'kontinuation + k + 1 + null))))) + (define (make-py-module . args) + (error 'make-py-module "sorry, not implemented yet")) + + (define (spy-load-module-dotted import-string) + (spy-import import-string)) + + (define (spy-initialize . args) + (printf "spy-initialize called with args: ~a~n" args)) (define namespace->py-module% make-py-module) @@ -127,7 +284,7 @@ (unless (string? dotted-string) (error 'python-load-module/dotted (format "Expected a dotted string, got ~a" dotted-string))) - (spy-cpython-load-module-dotted dotted-string)) + (spy-load-module-dotted dotted-string)) (require "compile-python.ss") @@ -141,7 +298,7 @@ (eval-python (py-gen/file (module-path->string path)) ns) (namespace->py-module% ns subname path)))) - (define *initialized?* #f) + (define *initialized?* #t) (define (python-initialize) (unless *initialized?* @@ -163,7 +320,7 @@ (define (py-object%->bool obj) (cond [(py-number? obj) (not (zero? (get-py-number obj)))] - [(py-list? obj) (not (zero? (spy-cpython-len/scm obj)))] + [(py-list? obj) (not (zero? (spy-len/scm obj)))] [(py-none? obj) #f] [else (error 'py-object%->bool "Can't handle this object yet: ~a" (py-object%->string obj))])) Index: parser.ss =================================================================== RCS file: /cvsroot/plt-spy/python/parser.ss,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- parser.ss 13 Jul 2004 13:01:27 -0000 1.2 +++ parser.ss 23 Nov 2004 12:57:15 -0000 1.3 @@ -1,4 +1,6 @@ -#cs(module parser mzscheme +(module parser mzscheme + (require (lib "errortrace.ss" "errortrace")) + (require (lib "lex.ss" "parser-tools") (lib "yacc.ss" "parser-tools") (lib "readerr.ss" "syntax") @@ -9,13 +11,14 @@ "compiler-expr.ss" "compiler-stmt.ss" "compiler-target.ss") - + (require (prefix : (lib "lex-plt-v200.ss" "parser-tools"))) + ; (provide build-ast) (provide/contract (build-ast (input-port? . -> . (listof (is-a?/c ast-node%)))) (build-ast-from-file (string? . -> . (listof (is-a?/c ast-node%))))) (provide build-ast-from-port) -; (build-ast-from-port (input-port? (union (is-a?/c text%) string?) . -> . (listof (is-a?/c ast-node%))))) +; (build-ast-from-port (input-port? (:: (is-a?/c text%) string?) . -> . (listof (is-a?/c ast-node%))))) (define (build-ast ip) (port-count-lines! ip) @@ -43,46 +46,48 @@ (file-path name) (build-ast port)) + (define build-position-token make-position-token) + (define-lex-abbrevs - (blank (: #\space #\tab #\page)) - (comment (@ #\# (* (^ #\newline)))) - (letter (: (- #\A #\Z) (- #\a #\z))) - (digit (- #\0 #\9)) - (decimal-integer (: "0" - (@ (- "1" "9") (* digit)))) - (oct-integer (@ "0" (+ (- "0" "7")))) - (hex-integer (@ "0" (: #\x "X") (+ (: digit (- "A" "F") (- "a" "f"))))) - (float (: point-float exponent-float)) - (point-float (: (@ (? int-part) fraction) - (@ int-part "."))) - (exponent-float (@ (: int-part point-float) exponent)) - (int-part (+ digit)) - (fraction (@ "." (+ digit))) - (exponent (@ (: #\e "E") (? (: #\+ #\-)) (+ digit))) - (string-literal (@ string-prefix (: short-string1 + (py-blank (:: #\space #\tab #\page)) + (comment (:@ #\# (:* (:^ #\newline)))) + (letter (:: (:- #\A #\Z) (:- #\a #\z))) + (digit (:- #\0 #\9)) + (decimal-integer (:: "0" + (:@ (:- "1" "9") (:* digit)))) + (oct-integer (:@ "0" (:+ (:- "0" "7")))) + (hex-integer (:@ "0" (:: #\x "X") (:+ (:: digit (:- "A" "F") (:- "a" "f"))))) + (float (:: point-float exponent-float)) + (point-float (:: (:@ (:? int-part) fraction) + (:@ int-part "."))) + (exponent-float (:@ (:: int-part point-float) exponent)) + (int-part (:+ digit)) + (fraction (:@ "." (:+ digit))) + (exponent (:@ (:: #\e "E") (:? (:: #\+ #\-)) (:+ digit))) + (string-literal (:@ string-prefix (:: short-string1 short-string2 long-string1 long-string2))) - (string-prefix (@ (? (: #\u #\U)) (? (: #\r #\R)))) - (short-string1 (@ "'" (* short-string-item1) "'")) - (short-string2 (@ #\" (* short-string-item2) #\")) - (long-string1 (@ "'''" (* (: long-string-item1 - (@ #\' long-string-item1) - (@ "''" long-string-item1))) + (string-prefix (:@ (:? (:: #\u #\U)) (:? (:: #\r #\R)))) + (short-string1 (:@ "'" (:* short-string-item1) "'")) + (short-string2 (:@ #\" (:* short-string-item2) #\")) + (long-string1 (:@ "'''" (:* (:: long-string-item1 + (:@ #\' long-string-item1) + (:@ "''" long-string-item1))) "'''")) - (long-string2 (@ "\"\"\"" (* (: long-string-item2 - (@ #\" long-string-item2) - (@ "\"\"" long-string-item2))) + (long-string2 (:@ "\"\"\"" (:* (:: long-string-item2 + (:@ #\" long-string-item2) + (:@ "\"\"" long-string-item2))) "\"\"\"")) - (short-string-item1 (: short-string-char1 escape-seq)) - (short-string-item2 (: short-string-char2 escape-seq)) - (long-string-item1 (: long-string-char1 escape-seq)) - (long-string-item2 (: long-string-char2 escape-seq)) - (short-string-char1 (^ #\\ #\newline #\')) - (short-string-char2 (^ #\\ #\newline #\")) - (long-string-char1 (^ #\\ #\')) - (long-string-char2 (^ #\\ #\")) - (escape-seq (@ #\\ (- #\000 #\377)))) + (short-string-item1 (:: short-string-char1 escape-seq)) + (short-string-item2 (:: short-string-char2 escape-seq)) + (long-string-item1 (:: long-string-char1 escape-seq)) + (long-string-item2 (:: long-string-char2 escape-seq)) + (short-string-char1 (:^ #\\ #\newline #\')) + (short-string-char2 (:^ #\\ #\newline #\")) + (long-string-char1 (:^ #\\ #\')) + (long-string-char2 (:^ #\\ #\")) + (escape-seq (:@ #\\ (:- #\000 #\377)))) (define (translate-string str sp ep) (let-values (((raw? unicode? quote-length) @@ -179,7 +184,7 @@ (lexer-src-pos ;; 2.1.5, 2.1.6 - ((@ #\newline (* (@ (* blank) (? comment) #\newline))) + ((:@ #\newline (:* (:@ (:* py-blank) (:? comment) #\newline))) (cond ((and (= 0 p-depth) (= 0 sq-depth) (= 0 c-depth)) (set! line-start #t) @@ -190,12 +195,12 @@ ;; 2.1.3 (comment (return-without-pos (lex-token input-port))) ;; 2.1.4 - ((@ #\\ #\newline) (return-without-pos (lex-token input-port))) + ((:@ #\\ #\newline) (return-without-pos (lex-token input-port))) ;; 2.1.8 - ((+ blank) (return-without-pos (lex-token input-port))) + ((:+ py-blank) (return-without-pos (lex-token input-port))) ;;2.3.1 - ((: "and" "del" "for" "is" "raise" + ((:: "and" "del" "for" "is" "raise" "assert" "elif" "from" "lambda" "return" "break" "else" "global" "not" "try" "class" "except" "if" "or" "while" @@ -203,7 +208,7 @@ "def" "finally" "in" "print") (string->symbol lexeme)) ;; 2.3 - ((@ (: letter "_") (* (: letter digit "_"))) + ((:@ (:: letter "_") (:* (:: letter digit "_"))) (token-NAME lexeme)) @@ -217,13 +222,13 @@ (token-NUMBER (string->number lexeme 8))) (hex-integer (token-NUMBER (string->number (substring lexeme 2 (string-length lexeme)) 16))) - ((@ decimal-integer (: #\l "L")) + ((:@ decimal-integer (:: #\l "L")) (token-NUMBER (string->number (substring lexeme 0 (sub1 (string-length lexeme)))))) - ((@ oct-integer (: #\l "L")) + ((:@ oct-integer (:: #\l "L")) (token-NUMBER (string->number (substring lexeme 0 (sub1 (string-length lexeme))) 8))) - ((@ hex-integer (: #\l "L")) + ((:@ hex-integer (:: #\l "L")) (token-NUMBER (string->number (substring lexeme 2 (sub1 (string-length lexeme))) 16))) ;; 2.4.5 @@ -231,11 +236,11 @@ (token-NUMBER (string->number lexeme))) ;; 2.4.6 - ((@ (: float int-part) (: #\j "J")) + ((:@ (:: float int-part) (:: #\j "J")) (token-NUMBER (* +i (string->number (substring lexeme 0 (sub1 (string-length lexeme))))))) ;; 2.5 - ((: #\+ #\- #\* "**" #\/ "//" #\% + ((:: #\+ #\- #\* "**" #\/ "//" #\% "<<" ">>" #\& #\| #\^ #\~ #\< #\> "<=" ">=" "==" "!=" "<>") (if (string=? "|" lexeme) @@ -250,7 +255,7 @@ ("]" (CBRACKET)) ("{" (OBRACE)) ("}" (CBRACE)) - ((: #\, #\; #\. #\` + ((:: #\, #\; #\. #\` #\: #\= "+=" "-=" "*=" "/=" "//=" "%=" "&=" "^=" ">>=" "<<=" "**=") (string->symbol lexeme)) @@ -291,7 +296,8 @@ ;; There are still DEDENTS to do ((> num-dedents 0) (set! num-dedents (sub1 num-dedents)) - (list 'DEDENT last-pos last-pos)) +; (printf "PARSER: there are still DEDENTS to do~n") + (build-position-token 'DEDENT last-pos last-pos)) ;; We are at the start of a line (line-start (let ((indent-num (lex-whitespace ip))) @@ -299,16 +305,19 @@ ;; Same indentation level as last line, find the first token on the line ((= indent-num (car indent-stack)) (set! line-start #f) +; (printf "PARSER: same indentation level as last line~n") (get-token ip)) ;; indented from last line ((> indent-num (car indent-stack)) (set! line-start #f) (set! indent-stack (cons indent-num indent-stack)) - (list 'INDENT last-pos last-pos)) +; (printf "PARSER: indented from last line~n") + (build-position-token 'INDENT last-pos last-pos)) (else (set! line-start #f) +; (printf "PARSER: else~n") (set! num-dedents (sub1 (count-dedent indent-num indent-stack))) - (list 'DEDENT last-pos last-pos))))) + (build-position-token 'DEDENT last-pos last-pos))))) (else (lex-token ip)))))) get-token)) @@ -392,11 +401,12 @@ (eval_input ((tuple_or_test) $1) ((eval_input NEWLINE) $1)) (funcdef ((def ident parameters : suite) - (make-object function-definition% - $2 (make-object parameters% $3 $3-start-pos $3-end-pos) $5 $1-start-pos $5-end-pos))) + (instantiate function-definition% + ($2 (make-object parameters% $3 $3-start-pos $3-end-pos) $5) + (start-pos $1-start-pos) (end-pos $5-end-pos)))) (parameters ((|(| |)|) null) ((|(| varargslist |)|) $2)) - (varargslist ((** ident) `((dict ,$2))) + (varargslist ((* ident) `((dict ,$2))) ((* ident) `((seq ,$2))) ((* ident |,| ** ident) `((seq ,$2) (dict ,$5))) ((fpdef |,|) `((pos ,$1))) @@ -424,11 +434,12 @@ ((flow_stmt) $1) ((import_stmt) $1) ((global_stmt) - (make-object global% (reverse $1) $1-start-pos $1-end-pos)) + (instantiate global% ((reverse $1)) (start-pos $1-start-pos) (end-pos $1-end-pos))) ((exec_stmt) $1) ((assert_stmt) $1)) (expr_stmt ((test augassign tuple_or_test) - (make-object aug-assignment% $1 $2 $3 $1-start-pos $3-end-pos)) + (instantiate aug-assignment% ($1 $2 $3) + (start-pos $1-start-pos) (end-pos $3-end-pos))) ((testlist_list_plus) (if (null? (cdr $1)) (make-object expr-stmt% (car $1) $1-start-pos $1-end-pos) @@ -475,7 +486,7 @@ ((return) (make-object return% #f $1-start-pos $1-end-pos))) (yield_stmt ((yield tuple_or_test) - (make-object yield% $2 $1-start-pos $2-end-pos))) + (instantiate yield% ($2) (start-pos $1-start-pos) (end-pos $2-end-pos)))) (raise_stmt ((raise) (make-object raise% #f #f #f $1-start-pos $1-end-pos)) ((raise test) @@ -527,25 +538,30 @@ ((funcdef) $1) ((classdef) $1)) (if_stmt ((if test : suite elif_list) - (make-object if% (cons (list $2 $4) (reverse $5)) #f $1-start-pos $5-end-pos)) + (instantiate if% ((cons (list $2 $4) (reverse $5)) #f) + (start-pos $1-start-pos) (end-pos $5-end-pos))) ((if test : suite elif_list else : suite) - (make-object if% (cons (list $2 $4) (reverse $5)) $8 $1-start-pos $8-end-pos))) + (instantiate if% ((cons (list $2 $4) (reverse $5)) $8) + (start-pos $1-start-pos) (end-pos $8-end-pos)))) (elif_list (() null) ((elif_list elif test : suite) (cons (list $3 $5) $1))) (while_stmt ((while test : suite) - (make-object while% $2 $4 #f $1-start-pos $4-end-pos)) + (instantiate while% ($2 $4 #f) + (start-pos $1-start-pos) + (end-pos $4-end-pos))) ((while test : suite else : suite) - (make-object while% $2 $4 $7 $1-start-pos $7-end-pos))) + (instantiate while% ($2 $4 $7) + (start-pos $1-start-pos) (end-pos $7-end-pos)))) (for_stmt ((for target_tuple_or_expr in tuple_or_test : suite) - (make-object for% $2 $4 $6 #f $1-start-pos $6-end-pos)) + (instantiate for% ($2 $4 $6 #f) (start-pos $1-start-pos) (end-pos $6-end-pos))) ((for target_tuple_or_expr in tuple_or_test : suite else : suite) - (make-object for% $2 $4 $6 $9 $1-start-pos $9-end-pos))) + (instantiate for% ($2 $4 $6 $9) (start-pos $1-start-pos) (end-pos $9-end-pos)))) (try_stmt ((try : suite except_clause_list_plus) - (make-object try-except% $3 (reverse $4) #f $1-start-pos $4-end-pos)) + (instantiate try-except% ($3 (reverse $4) #f) (start-pos $1-start-pos) (end-pos $4-end-pos))) ((try : suite except_clause_list_plus else : suite) - (make-object try-except% $3 (reverse $4) $7 $1-start-pos $7-end-pos)) + (instantiate try-except% ($3 (reverse $4) $7) (start-pos $1-start-pos) (end-pos $7-end-pos))) ((try : suite finally : suite) - (make-object try-finally% $3 $6 $1-start-pos $6-end-pos))) + (instantiate try-finally% ($3 $6) (start-pos $1-start-pos) (end-pos $6-end-pos)))) (except_clause_list_plus ((except_clause : suite) (list (append $1 (list $3)))) ((except_clause_list_plus except_clause : suite) (cons (append $2 (list $4)) $1))) @@ -553,9 +569,9 @@ ((except test) (list $2 #f)) ((except test |,| test) (list $2 $4))) (suite ((simple_stmt) - (make-object suite% $1 $1-start-pos $1-end-pos)) + (instantiate suite% ($1) (start-pos $1-start-pos) (end-pos $1-end-pos))) ((NEWLINE INDENT stmt_list_plus DEDENT) - (make-object suite% (reverse $3) $1-start-pos $4-end-pos))) + (instantiate suite% ((reverse $3)) (start-pos $1-start-pos) (end-pos $4-end-pos)))) (stmt_list_plus ((stmt) $1) ((stmt_list_plus stmt) (append $2 $1))) (test ((or_test) $1) @@ -701,7 +717,7 @@ (make-object class-definition% $2 (list $4) $7 $1-start-pos $7-end-pos)) ((class ident |(| testlist |)| : suite) (make-object class-definition% $2 $4 $7 $1-start-pos $7-end-pos))) - (arglist ((** test) (list `(dict ,$2))) + (arglist ((* test) (list `(dict ,$2))) ((* test) (list `(seq ,$2))) ((* test |,| ** test) (list `(seq ,$2) `(dict ,$5))) ((argument) (list $1)) @@ -715,14 +731,16 @@ (let ((l (if (null? (cdr $4)) (car $4) (make-object tuple% $4 $4-start-pos $4-end-pos)))) - (make-object list-for% $2 l #f $1-start-pos $4-end-pos))) + (instantiate list-for% ($2 l #f) (start-pos $1-start-pos) (end-pos $4-end-pos)))) ((for target_tuple_or_expr in testlist_safe list_iter) (let ((l (if (null? (cdr $4)) (car $4) (make-object tuple% $4 $4-start-pos $4-end-pos)))) - (make-object list-for% $2 l $5 $1-start-pos $5-end-pos)))) + (instantiate list-for% ($2 l $5) (start-pos $1-start-pos) (end-pos $5-end-pos))))) (list_if ((if test) - (make-object list-if% $2 #f $1-start-pos $2-end-pos)) + (instantiate list-if% ($2 #f) + (start-pos $1-start-pos) (end-pos $2-end-pos))) ((if test list_iter) - (make-object list-if% $2 $3 $1-start-pos $3-end-pos))))))) + (instantiate list-if% ($2 $3) + (start-pos $1-start-pos) (end-pos $3-end-pos)))))))) Index: compiler-expr.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-expr.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- compiler-expr.ss 3 Sep 2004 15:51:14 -0000 1.3 +++ compiler-expr.ss 23 Nov 2004 12:57:15 -0000 1.4 @@ -9,6 +9,7 @@ ; "primitives.ss" ; "runtime-support.ss" "runtime-context.ss" + "embed/embed.ss" ) (provide (all-defined)) @@ -73,7 +74,7 @@ ; empty)) (if scope (map (lambda (b) - `[,(send b to-scheme) (void)]) + `[,(send b to-scheme) void]) (send scope get-bindings)) empty))] ;[body-src (syntax-e body-so)] @@ -282,7 +283,7 @@ (super-instantiate ()))) - (require "c-bindings.ss") +; (require "c-bindings.ss") ;; 5.2.2 (define literal% (class expression% @@ -296,13 +297,13 @@ (inherit ->orig-so) (define/override (to-scheme) (->orig-so - (if (string? value) + #| (if (string? value) (make-py-string value) - (make-py-number value)))) -; `(,(py-so (if (string? value) -; 'make-py-string -; 'make-py-number)) - ; ,value))) + (make-py-number value))))|# + `(,(py-so (if (string? value) + 'make-py-string + 'make-py-number)) + ,value))) (super-instantiate ()))) @@ -324,7 +325,7 @@ ;;daniel (inherit ->orig-so) (define/override (to-scheme) - (->orig-so `(make-py-tuple ;;,(py-so 'list->py-tuple%) + (->orig-so `(,(py-so 'make-py-tuple) ;;,(py-so 'list->py-tuple%) ,(generate-list (map (lambda (e) (send e to-scheme)) expressions))))) @@ -347,6 +348,7 @@ ;;daniel (inherit ->orig-so) + ;;; TODO: evaluate at compile-time if it's a literal list (define/override (to-scheme) (->orig-so `(,(py-so 'make-py-list) ;;,(py-so 'list->py-list%) ,(generate-list (map (lambda (e) (send e to-scheme)) @@ -695,6 +697,7 @@ [(*) 'py-mul] [(/) 'py-div] [(%) 'py-mod] + [(\|) 'py-bit-or] [else (raise (format "binary% op unsupported: ~a" op))])) ,lhs-so ,rhs-so)))])) Index: python-import.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python-import.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- python-import.ss 3 Sep 2004 15:51:14 -0000 1.3 +++ python-import.ss 23 Nov 2004 12:57:15 -0000 1.4 @@ -1,4 +1,6 @@ (module python-import mzscheme + (require (lib "errortrace.ss" "errortrace")) + (require (lib "list.ss") (lib "file.ss") (lib "etc.ss") @@ -156,7 +158,7 @@ ; (printf "namespace symbol: ~a~n" symbol) ; (unless (namespace-variable-value symbol #t ; (lambda () #f)) - (with-handlers ([exn:syntax? (lambda (exn) #f)]) ;ignore defined macros + (with-handlers ([exn:fail:syntax? (lambda (exn) #f)]) ;ignore defined macros (namespace-set-variable-value! (let ([symbol (or (and bindings (cadr (assq symbol bindings))) @@ -182,7 +184,9 @@ (filter (lambda (result) (not (void? result))) (parameterize ([current-namespace namespace]) - (map eval + (map (lambda (so) + (printf "[eval-python] ~v~n" (syntax-object->datum so)) + (eval so)) s-obj-list)))) ; make-python-namespace: [bool] -> namespace @@ -227,10 +231,17 @@ (define base-path #f) (define (init-python-import) + (define base-spec `(file ,(path->string (build-path (this-expression-source-directory) + "base.ss"))));'(lib "base.ss" "python") (parameterize ([current-namespace outer-namespace]) - (dynamic-require '(lib "base.ss" "python") #f) - (set! base-path ((current-module-name-resolver) '(lib "base.ss" "python") #f #f)))) + (dynamic-require base-spec + #f) + (printf "base-path before: ~a~n" base-path) + (set! base-path ((current-module-name-resolver) base-spec #f #f)) + (printf "base-path after: ~a~n" base-path))) + (init-python-import) + (provide init-python-import) @@ -244,7 +255,7 @@ [path2 ((current-module-name-resolver) spec #f #f)] [caller-namespace (current-namespace)]) (parameterize ([current-namespace dest-namespace]) - (with-handlers ([exn:application:mismatch? + (with-handlers ([exn:fail:contract? (lambda (e) (printf "FAILURE. spec: ~a path: ~a exn: ~a~n" spec path2 e))]) ; (namespace-attach-module cache path) @@ -263,7 +274,7 @@ [path (lookup-cached-mzscheme-module spec)] [caller-namespace (current-namespace)]) (parameterize ([current-namespace dest-namespace]) - (with-handlers ([exn:application:mismatch? (lambda (e) + (with-handlers ([exn:fail:contract? (lambda (e) (printf "FAILURE. spec: ~a path: ~a exn: ~a~n" spec path e))]) (namespace-attach-module cache path) (namespace-transformer-require spec) @@ -341,7 +352,7 @@ module)) (*loaded-modules*))) - (define current-python-library-paths (make-parameter (list (build-path (collection-path "python") + (define current-python-library-paths (make-parameter (list (build-path (this-expression-source-directory);(collection-path "python") "libs")))) (define (list-head list items) Index: python.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- python.ss 3 Sep 2004 15:51:14 -0000 1.3 +++ python.ss 23 Nov 2004 12:57:15 -0000 1.4 @@ -1,4 +1,5 @@ (module python mzscheme + (require (lib "errortrace.ss" "errortrace")) (require (lib "class.ss") ; (lib "list.ss") (lib "etc.ss") @@ -11,7 +12,7 @@ "python-import.ss" ;"base.ss" "runtime-support.ss" - "c-bindings.ss" + ; "c-bindings.ss" ;"get-base.ss" ) @@ -28,8 +29,8 @@ [ssd (syntax? . -> . (union pair? symbol? number? string?))] [ssd* ((listof py-gen-result?) . -> . (listof (union python-node? pair? symbol? number? string?)))] [python-ns parameter?] - [ns-set! (symbol? any? . -> . void?)] - [ns-get (symbol? . -> . any?)] + [ns-set! (symbol? any/c . -> . void?)] + [ns-get (symbol? . -> . any/c)] [render-python-value ((union python-node? void?) port? procedure? . -> . void?)] [render-python-value/format ((union python-node? void?) port? procedure? . -> . void?)]) @@ -59,9 +60,21 @@ (port-write to-render) (display to-render port))))) +#| (require (lib "pconvert.ss")) + (install-converting-printer) + (define hook current-print-convert-hook) + (define old-hook (hook)) + (define (new-hook value basic-convert sub-convert) + (if (python-node? value) + (format "<python-node: ~a>" (convert-value value)) + (old-hook value basic-convert sub-convert))) + (hook?new-hook) + |# + +; (current-directory "/home/dsilva/incoming/Python-2.3.4/Lib") ; (python-initialize) - (define pns (make-namespace)) - ;(make-python-namespace)) + (define pns ;(make-namespace)) + (make-python-namespace)) ;(set-python-namespace-name! pns '__main__) ; (set-pyns! pns) @@ -116,6 +129,5 @@ (define ns-get namespace-variable-value) - (current-directory "/home/dsilva/incoming/Python-2.3.4/Lib") ) Index: c-bindings.ss =================================================================== RCS file: /cvsroot/plt-spy/python/c-bindings.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- c-bindings.ss 3 Sep 2004 15:51:14 -0000 1.4 +++ c-bindings.ss 23 Nov 2004 12:57:15 -0000 1.5 @@ -50,7 +50,8 @@ ; (parameterize ([current-namespace gns]) ;(namespace-require (here-path "builtin-types-uninitialized.ss")) ; (namespace-require (here-path "python-node.ss")) - (namespace-require '(lib "python-node.ss" "python")) + ;(namespace-require '(lib "python-node.ss" "python")) + (namespace-require `(file ,(build-path sdir "python-node.ss"))) ;(namespace-set-variable-value! 'python-ns python-ns) (namespace-set-variable-value! 'spy-add-cpython-type spy-add-cpython-type) (define spy-initialize/c (load-extension path)) @@ -85,6 +86,13 @@ cpy-type cpy-tuple cpy-dict + cpy-int + cpy-long + cpy-float + cpy-bool + cpy-complex + cpy-unicode + cpy-buffer make-py-list make-py-string make-py-symbol Index: compiler-stmt.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler-stmt.ss,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- compiler-stmt.ss 3 Sep 2004 15:51:14 -0000 1.4 +++ compiler-stmt.ss 23 Nov 2004 12:57:15 -0000 1.5 @@ -22,7 +22,7 @@ ;; statement, or #f if there is none. Check that all break ;; and continue statements are inside some loop and annotate the ;; enclosing-loop that it contains a break or continue. - (define/public (check-break/cont enclosing-loop) (void)) + (define/public (check-break/cont enclosing-loop) void) ;; collect-globals: -> (listof symbol?) ;; Return the list of all variables in "global" statements in the @@ -36,7 +36,7 @@ ;; do nothing for most statements (define/public (set-tail-position!) - (void)) + void) ;; needs-escape-continuation?: symbol -> bool ;; determine whether this is a non-tail return/break/continue statement @@ -44,7 +44,25 @@ (define/public (needs-escape-continuation? ec) #f) + ;; yields?: -> bool + ;; determine whether this statement might yield + (define/public (yields?) + #f) + (super-instantiate ()))) + + (define compound-statement% + (class statement% + (init-field substatements) + + (define/override (yields?) + (ormap (lambda (s) + (send s yields?)) + substatements)) + + (printf "new compound statement: ~a~n" this) + + (super-new))) ;; 6.1 (define expr-stmt% @@ -134,6 +152,8 @@ (->orig-so (cond [(is-a? target tidentifier%) (let ([target-so (send target to-scheme)]) + (when (eq? 'sys (send target get-symbol)) + (printf "ASSIGN SYS: def? ~a binding? ~a set? ~a bound? ~a scope: ~a module-scope? ~a~n" (def-targ? target) (binding-targ? target) (set-targ? target) (send scope is-bound? target) scope (is-a? scope module-scope%))) (if (def-targ? target) `(define ,target-so ,rhs) `(set! ,target-so ,rhs)))] @@ -192,9 +212,12 @@ ;;daniel (inherit ->orig-so) (define/override (to-scheme) + (printf "ASSIGNMENT: BEGIN COMPILING~n") (let* ([rhs (datum->syntax-object (current-toplevel-context) (gensym 'rhs) #f #f)] [body (map (lambda (t) - (assignment-so t rhs)) + (let ([so (assignment-so t rhs)]) + (printf "ASSIGNMENT TARGET: ~v~n" (syntax-object->datum so)) + so)) targets)]) (->orig-so (if (top?) `(begin (define ,rhs ,(send expression to-scheme)) @@ -321,6 +344,7 @@ (super-instantiate ()))) ;; 6.5 + ;;; TODO: handle local vars, subscripts (define del% (class statement% ;; targ-exp: (is-a?/c expression%) @@ -328,13 +352,22 @@ ;; target: (is-a?/c target%) (define target (send targ-exp to-target)) + (define the-targ-exp targ-exp) (define/override (set-bindings! enclosing-scope) (send target set-bindings! enclosing-scope)) (inherit ->orig-so stx-err) (define/override (to-scheme) + (printf "del targ-exp: ~v, target: ~v~n" the-targ-exp target) + (del-target-to-scheme target)) + + (define (del-target-to-scheme target) + (printf "del-target-to-scheme: ~v~n" target) (->orig-so (cond - [(is-a? target tidentifier%) `(namespace-set-variable-value! ,(send target to-scheme) 'undefined)] + [(is-a? target tidentifier%) `(namespace-set-variable-value! ',(send target to-scheme) 'undefined)] + [(is-a? target ttuple%) + `(begin ,@(map del-target-to-scheme + (send target get-sub-targets)))] [else (stx-err "del statement not fully implemented yet.")]))) (super-instantiate ()))) @@ -426,6 +459,12 @@ (define/override (set-bindings! enclosing-scope) (send expression set-bindings! enclosing-scope)) + (define/override (yields?) #t) + + (inherit ->orig-so) + (define/override (to-scheme) + (->orig-so `(yield ,(send expression to-scheme)))) + (super-instantiate ()))) ;; 6.9 @@ -470,7 +509,7 @@ (inherit ->orig-so) (define/override (to-scheme) - (->orig-so `(,(send loop get-break-symbol) (void)))) + (->orig-so `(,(send loop get-break-symbol) void))) (super-instantiate ()))) @@ -637,7 +676,7 @@ ;; 7 (define suite% - (class statement% + (class compound-statement% ;; statements: (listof (is-a?/c statement%)) (init-field statements) @@ -738,11 +777,11 @@ `(begin ,@bodies-with-let))))))) - (super-instantiate ()))) + (super-new (substatements statements)))) ;; 7.1 (define if% - (class statement% + (class compound-statement% ;; test-bodies: (listof (list/p (is-a?/c expression%) (is-a?/c suite%))) ;; else: (or/f (is-a?/c suite%) false?) (init-field test-bodies else) @@ -791,7 +830,7 @@ (match test-bodies ['() (if else (send else to-scheme) - (void))] + void)] [(list (list test then) rest-bodies ...) `(,(py-so 'py-if) ,(send test to-scheme) ,(send then to-scheme) @@ -802,11 +841,11 @@ ; [else ,(send else to-scheme)]) ; `(cond ,@s-test-bodies))))) - (super-instantiate ()))) + (super-new [substatements (sub-stmt-map identity)]))) ;; 7.2 (define while% - (class statement% + (class compound-statement% ;; test: (is-a?/c expression%) ;; body: (is-a?/c suite%) @@ -860,11 +899,13 @@ ;) - (super-instantiate ()))) + (super-new [substatements (if else + (list body else) + (list body))]))) ;; 7.3 (define for% - (class statement% + (class compound-statement% ;; targ-exp: (is-a?/c expression) (init targ-exp) @@ -969,11 +1010,13 @@ ; ,(if else ; (send else to-scheme))))) - (super-instantiate ()))) + (super-new [substatements (if else + (list body else) + (list body))]))) ;; 7.4 (define try-except% - (class statement% + (class compound-statement% (inherit stx-err) ;; body: (is-a?/c suite%) (init-field body) @@ -1017,6 +1060,7 @@ (define exn (gensym 'exn)) (inherit ->orig-so) (define/override (to-scheme) + (define error?-id (gensym 'error?)) (let ([handled `(with-handlers ,(map (match-lambda [(list e t s) @@ -1028,25 +1072,33 @@ to-scheme) ,exn) #t))) - ,(let ([handler-body (send s to-scheme)]) - `(lambda (,exn) - ,(if t + ,(let* ([handler-body (send s to-scheme)] + [lambda-body + (if t `(,(generate-lambda (target->parameters t) handler-body) (,(py-so 'exn:python-value) ,exn)) - handler-body)))]]) + handler-body)]) + `(lambda (,exn) + ,(if else + `(begin (set! ,error?-id #t) + ,lambda-body) + lambda-body) + ,(py-so 'py-none)))]]) exceptions) - ;; return the result in a list to signal successful try block - (list ,(send body to-scheme)))]) + ,(send body to-scheme) + ,(py-so 'py-none))]) (->orig-so (if else - `(if (list? ,handled) - ,(send else to-scheme)) + `(let () + (define ,error?-id #f) + ,handled + (when ,error?-id ,(send else to-scheme))) handled)))) - (super-instantiate ()) + (super-new [substatements (sub-stmt-map identity)]) (let loop ((e exceptions)) (cond - ((null? e) (void)) + ((null? e) void) ((not (null? (cdr e))) (if (not (caar e)) (stx-err "default except clause must be last")) @@ -1086,6 +1138,8 @@ (let ([,finally ,(send finalizer to-scheme)]) (if (,(py-so 'exn:python?) ,value) (raise ,value) + ;; TODO: check this "void?" part + ;; probably should check PyNone (,ec (if (void? ,finally) ; void unless finalizer returned something ,value ,finally)))))]) @@ -1098,7 +1152,7 @@ ; (raise ,handled) ; ,finally)))))) - (super-instantiate ()))) + (super-new [substatements (list body finalizer)]))) (define (bindings-mixin %) @@ -1158,18 +1212,26 @@ ;; daniel ;; use this as the object type of the initial empty environment for a module (define module-scope% + (class (bindings-mixin - (class object% + (class object%;(bindings-mixin object%) (field [body (make-object (class object% (define/public (collect-globals) null) (super-instantiate ())))]) (define/public (get-escape-continuation-symbol) (gensym 'unusable)) - (super-instantiate ())))) + + + (super-instantiate ()))) + + (define/override (is-bound? id) #f) + + (super-new))) + ;; 7.5 (define function-definition% (bindings-mixin - (class statement% + (class compound-statement% (inherit stx-err) ;; name: (is-a?/c identifier%) @@ -1195,6 +1257,9 @@ (define/override (check-break/cont enclosing-loop) (send body check-break/cont #f)) + ;; inner functions can't turn outer fns into generators + (define/override (yields?) #f) + (define/public (get-parms) parms) (send body set-tail-position!) @@ -1202,42 +1267,22 @@ ;;daniel (inherit ->orig-so ->lex-so) + (inherit-field substatements) (define/override (to-scheme) - (->orig-so (let ([proc-name (send name to-scheme)]) - `(define ,proc-name ;(send name get-symbol) - ;namespace-set-variable-value! #cs',proc-name + (->orig-so (let ([proc-name (send name to-scheme)] + [body-so (send body to-scheme return-symbol #t #t)]) + `(define ,proc-name ,(generate-py-lambda proc-name parms - (send body to-scheme return-symbol #t #t)))) - ; (current-toplevel-context) + (if (ormap (lambda (s) (send s yields?)) + substatements) + `(,(py-so 'make-py-generator/lambda) + (lambda (yield) + ,body-so)) + body-so)))) )) -; (,(py-so 'procedure->py-function%) -; ;; this is where the outer "let" for default values should -; ;; be placed; I'll do that after I finish the rest of the def/call code. -; ;; it's bad design anyway... -; (opt-lambda ,(send parms to-scheme) -; (let ,(append (normalize-assoc-list -; (flatten1 -; (map (lambda (tuple) -; (unpack tuple -; (send (first-atom tuple) to-scheme))) -; (filter list? pos)))) -; (map (lambda (b) -; `[,(send b to-scheme) (void)]) -; (send this get-bindings))) -; ,(send body to-scheme return-symbol #t))) -; ',proc-name -; (list ,@(map (lambda (p) -; `',(send (first-atom p) to-scheme)) -; pos)) -; (list ,@(map (lambda (k) -; `(cons ',(send (car k) to-scheme) -; ,(send (cdr k) to-scheme))) -; key)) -; ,(and seq (send seq to-scheme)) -; ,(and dict (send dict to-scheme))))))) - (super-instantiate ())))) + (super-new [substatements (list body)])))) ;; 7.6 (define class-definition% Index: compiler.ss =================================================================== RCS file: /cvsroot/plt-spy/python/compiler.ss,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- compiler.ss 3 Sep 2004 15:51:14 -0000 1.3 +++ compiler.ss 23 Nov 2004 12:57:15 -0000 1.4 @@ -2,6 +2,7 @@ (require (lib "class.ss") ; (lib "list.ss") (lib "etc.ss") + (lib "plt-match.ss") (lib "lex.ss" "parser-tools") (lib "readerr.ss" "syntax")) @@ -91,12 +92,10 @@ ;; first-atom: sxp -> atom ;; if sxp is a list, find the first item in it (or its inner lists) that isn't a list (define (first-atom s) - (cond - [(and (list? s) - (not (null? s))) - (first-atom (car s))] - [(pair? s) (car s)] - [else s])) + (match s + [(list-rest item items) + (first-atom item)] + [else s])) ;; flatten: list -> list ;; flattens a nested list @@ -129,4 +128,4 @@ [else (cons (car l) (normalize-assoc-list (cdr l)))])) - ) \ No newline at end of file + ) Index: base.ss =================================================================== RCS file: /cvsroot/plt-spy/python/base.ss,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- base.ss 13 Jul 2004 13:01:27 -0000 1.2 +++ base.ss 23 Nov 2004 12:57:15 -0000 1.3 @@ -26,24 +26,30 @@ fn (procedure-arity fn) null)))) - - (define myappend (guarded my-append)) +;; TODO: uncomment + +; (define myappend (guarded my-append)) (define callcc (wrap py-call/cc)) + (define callec (wrap py-call/ec)) (define (py-range cnt) (make-py-list (build-list (get-py-number cnt) make-py-number))) (define range (wrap py-range)) + + (provide #%top #%app #%datum #%module-begin - myappend + ;; TODO: uncomment the next three lines + ;;myappend callcc + callec range ;#%provide @@ -65,6 +71,14 @@ (rename cpy-type type) (rename cpy-tuple tuple) (rename cpy-dict dict) + (rename py-none |None|) + (rename cpy-int int) + (rename cpy-long long) + (rename cpy-float float) + (rename cpy-bool bool) + (rename cpy-complex complex) + (rename cpy-unicode unicode) + ;(rename cpy-buffer buffer) ) |
From: Daniel S. <aph...@us...> - 2004-11-23 12:57:32
|
Update of /cvsroot/plt-spy/python/c In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3708/c Modified Files: Makefile Python.h fake-program.c methodobject.c pscm.c spy-modsupport.c spy-moduleobject.c typeobject.c Log Message: Ditching the c directory soon. Hacking CPython has sort of failed. The new embed directory is an embedding of CPython. Spy will slowly implement parts of the runtime system and subclass parts of CPython (like PyFunction_Type) to override CPython instead of distributing a hacked libpython binary. Index: pscm.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/pscm.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- pscm.c 4 Sep 2004 16:36:39 -0000 1.5 +++ pscm.c 23 Nov 2004 12:57:16 -0000 1.6 @@ -12,6 +12,10 @@ __inline__ PyObject* smethod0(PyObject* obj, const char* method_name); +#define scheme_make_string scheme_make_utf8_string +#define scheme_make_sized_string(s, l, x) scheme_make_sized_utf8_string(s, l) +#define SCHEME_STR_VAL(str) SCHEME_BYTE_STR_VAL(scheme_char_string_to_byte_string(str)) +#define SCHEME_STRINGP SCHEME_CHAR_STRINGP #define Scheme_Struct_Type void @@ -48,10 +52,12 @@ extern PyTypeObject PyMemberDescr_Type; extern PyTypeObject PyGetSetDescr_Type; extern PyTypeObject proxytype; +#ifdef SPY_EXPOSE_HIDDEN_TYPES extern PyTypeObject wrappertype; extern PyTypeObject PyNone_Type; extern PyTypeObject PyNotImplemented_Type; extern PyTypeObject PyEllipsis_Type; +#endif // these were not static but not in headers extern PyTypeObject PyTupleIter_Type; @@ -60,7 +66,9 @@ extern PyTypeObject PyListIter_Type; // extra +#ifdef SPY_USE_SCHEMENS_TYPE extern PyTypeObject PySchemeNS_Type; +#endif // in place of interp->builtins PyObject* curr_builtins = NULL; @@ -133,11 +141,12 @@ //extern PyMethodDef string_methods[]; // spy-specific -void spy_init_PyString(); void _Py_ReadyTypes(); +#ifdef SPY_MODIFIED_LIBPYTHON +void spy_init_PyString(); void spy_init_ClassObject(); PyObject* SpyModule_New(char* name, PyObject* ns); - +#endif static const PyMethodDef empty_method_table[] = { {NULL} /* Sentinel */ @@ -231,11 +240,13 @@ return SCM(PyObject_Str(PY(argv[0]))); } +/* void spy_init_cpython_type(PyTypeObject* tobj) { PRINTF ("spy_init_cpython_type: entry\n"); SPY_INIT_OBJ((PyObject*) tobj, &PyType_Type); } +*/ // PyObject PyTypeObject -> bool @@ -394,13 +405,7 @@ SPY_INIT_GLOBAL_T(PyInstance_Type); SPY_INIT_GLOBAL_T(PyMethod_Type); SPY_INIT_GLOBAL_T(PyCObject_Type); - SPY_INIT_GLOBAL_T(PyMethodDescr_Type); - SPY_INIT_GLOBAL_T(PyClassMethodDescr_Type); - SPY_INIT_GLOBAL_T(PyMemberDescr_Type); - SPY_INIT_GLOBAL_T(PyGetSetDescr_Type); SPY_INIT_GLOBAL_T(PyWrapperDescr_Type); - SPY_INIT_GLOBAL_T(proxytype); - SPY_INIT_GLOBAL_T(wrappertype); SPY_INIT_GLOBAL_T(PyProperty_Type); SPY_INIT_GLOBAL_T(PyDict_Type); SPY_INIT_GLOBAL_T(PyDictIter_Type); @@ -412,6 +417,7 @@ SPY_INIT_GLOBAL_T(PyClassMethod_Type); SPY_INIT_GLOBAL_T(PyStaticMethod_Type); SPY_INIT_GLOBAL_T(PyInt_Type); + SPY_INIT_GLOBAL_T(PyComplex_Type); SPY_INIT_GLOBAL_T(PySeqIter_Type); SPY_INIT_GLOBAL_T(PyCallIter_Type); SPY_INIT_GLOBAL_T(PyList_Type); @@ -420,23 +426,52 @@ SPY_INIT_GLOBAL_T(PyCFunction_Type); SPY_INIT_GLOBAL_T(PyBool_Type); SPY_INIT_GLOBAL_T(PyModule_Type); - SPY_INIT_GLOBAL_T(PyEllipsis_Type); SPY_INIT_GLOBAL_T(PySlice_Type); SPY_INIT_GLOBAL_T(PyCode_Type); SPY_INIT_GLOBAL_T(PyBaseString_Type); SPY_INIT_GLOBAL_T(PyString_Type); + SPY_INIT_GLOBAL_T(PyUnicode_Type); SPY_INIT_GLOBAL_T(PyTuple_Type); SPY_INIT_GLOBAL_T(PyTupleIter_Type); SPY_INIT_GLOBAL_T(PyType_Type); SPY_INIT_GLOBAL_T(PyBaseObject_Type); SPY_INIT_GLOBAL_T(PySuper_Type); + SPY_INIT_GLOBAL_T(_PyWeakref_RefType); + // more + SPY_INIT_GLOBAL_T(PyRange_Type); + SPY_INIT_GLOBAL_T(PyTraceBack_Type); +#ifdef SPY_EXPOSE_HIDDEN_TYPES + SPY_INIT_GLOBAL_T(wrappertype); + SPY_INIT_GLOBAL_T(PyEllipsis_Type); + SPY_INIT_GLOBAL_T(PyGetSetDescr_Type); + SPY_INIT_GLOBAL_T(proxytype); + SPY_INIT_GLOBAL_T(PyMethodDescr_Type); + SPY_INIT_GLOBAL_T(PyClassMethodDescr_Type); + SPY_INIT_GLOBAL_T(PyMemberDescr_Type); + SPY_INIT_GLOBAL_T(PyNone_Type); +#endif +// SPY_INIT_GLOBAL_T(CdPlayertype); +// SPY_INIT_GLOBAL_T(CdParsertype); +// SPY_INIT_GLOBAL_T(PyCursesWindow_Type); +// SPY_INIT_GLOBAL_T(PyCursesPanel_Type); +// SPY_INIT_GLOBAL_T(PyST_Type); +// SPY_INIT_GLOBAL_T(Capturetype); +// SPY_INIT_GLOBAL_T(Svtype); // hmm... +#ifdef SPY_USE_SCHEMENS_TYPE SPY_INIT_GLOBAL_T(PySchemeNS_Type); +#endif + + SPY_INIT_GLOBAL(*Py_None); + SPY_INIT_GLOBAL(*Py_Ellipsis); + SPY_INIT_GLOBAL(*Py_NotImplemented); + scheme_register_extension_global( &_Py_TrueStruct, sizeof(PyIntObject)); + scheme_register_extension_global( &_Py_ZeroStruct, sizeof(PyIntObject)); } #define INITTYPE(tobj/*,name*/) do {/*PRINTF("INITTYPE: %s\n", sname );*/ spy_init_cpython_type(&tobj); SPY_INIT_GLOBAL_T_impl(&tobj, sizeof(PyTypeObject));} while(0) - +/* SPY_INIT_GLOBAL already registers globals void spy_cpython_init_types() { INITTYPE(PyCell_Type); @@ -460,6 +495,7 @@ INITTYPE(PyClassMethod_Type); INITTYPE(PyStaticMethod_Type); INITTYPE(PyInt_Type); + INITTYPE(PyComplex_Type); INITTYPE(PySeqIter_Type); INITTYPE(PyCallIter_Type); INITTYPE(PyList_Type); @@ -473,6 +509,7 @@ INITTYPE(PyCode_Type); INITTYPE(PyBaseString_Type); INITTYPE(PyString_Type); + INITTYPE(PyUnicode_Type); INITTYPE(PyTuple_Type); INITTYPE(PyTupleIter_Type); INITTYPE(PyType_Type); @@ -484,6 +521,7 @@ // hmm... INITTYPE(PySchemeNS_Type); } +*/ // PyCallable [PyObject ...] -> PyObject Scheme_Object* spy_cpython_apply(int argc, Scheme_Object* argv[]) @@ -561,6 +599,65 @@ return SCM(PyImport_Import(PyString_FromString(SCHEME_STR_VAL(argv[0])))); } +void spy_cpython_provide_type(PyTypeObject* tobj) +{ +// scheme_register_extension_global((void*) tobj, sizeof(PyTypeObject)); + assert(SCHEME_STRUCTP(SCM(tobj))); + //PyType_Ready(tobj); + sapply2("spy-add-cpython-type", tobj->tp_name ? sym(tobj->tp_name) : sym("(null)"), SCM(tobj)); +} + +void spy_cpython_provide_types() +{ + #define PROVIDE_TYPE(type_val, name) spy_cpython_provide_type(&type_val) + /////// types visible to Scheme + PROVIDE_TYPE(PyBaseObject_Type, "pobj"); + PROVIDE_TYPE(PyType_Type, "ptype"); + PROVIDE_TYPE(PyInt_Type, "pint"); + PROVIDE_TYPE(PyComplex_Type, "pcomplex"); + PROVIDE_TYPE(PyLong_Type, "plong"); + PROVIDE_TYPE(PyFloat_Type, "pfloat"); + PROVIDE_TYPE(PyDict_Type, "pdict"); + PROVIDE_TYPE(PyString_Type, "pstr"); + PROVIDE_TYPE(PyUnicode_Type, "punicode"); + PROVIDE_TYPE(PyList_Type, "plist"); + PROVIDE_TYPE(PyCObject_Type, "pcobj"); + PROVIDE_TYPE(PyTuple_Type, "ptuple"); + PROVIDE_TYPE(PySlice_Type, "pslice"); + PROVIDE_TYPE(PyFunction_Type, "pfunc"); + PROVIDE_TYPE(PyCode_Type, "pcode"); + PROVIDE_TYPE(PyFile_Type, "pfile"); + PROVIDE_TYPE(PyBuffer_Type, "pbuffer"); + PROVIDE_TYPE(PyEnum_Type, "penum"); + PROVIDE_TYPE(PyClassMethod_Type, "pclassmethod"); + PROVIDE_TYPE(PyStaticMethod_Type, "pstaticmethod"); + PROVIDE_TYPE(PySeqIter_Type, "pseqiter"); + PROVIDE_TYPE(PyCallIter_Type, "pcalliter"); + PROVIDE_TYPE(PyCFunction_Type, "pcfunction"); + PROVIDE_TYPE(PyModule_Type, "pmodule"); + PROVIDE_TYPE(PySuper_Type, "psuper"); + PROVIDE_TYPE(PyProperty_Type, "pproperty"); + PROVIDE_TYPE(PyWrapperDescr_Type, "pwrapperdescr"); + PROVIDE_TYPE(PyCell_Type, "pcell"); + PROVIDE_TYPE(PyBool_Type, "pbool"); + PROVIDE_TYPE(PyClass_Type, "pclass"); + PROVIDE_TYPE(PyInstance_Type, "pinstance"); + PROVIDE_TYPE(PyMethod_Type, "pmethod"); +#ifdef SPY_EXPOSE_HIDDEN_TYPES + PROVIDE_TYPE(PyMethodDescr_Type, "pmethod_descriptor"); + PROVIDE_TYPE(PyClassMethodDescr_Type, "pclassmethod_descriptor"); + PROVIDE_TYPE(PyMemberDescr_Type, "pmember_descriptor"); + PROVIDE_TYPE(PyGetSetDescr_Type, "pgetset_descriptor"); + PROVIDE_TYPE(proxytype, "pdictproxy"); + PROVIDE_TYPE(wrappertype, "pmethod-wrapper"); + PROVIDE_TYPE(PyNone_Type, "pNoneType"); + PROVIDE_TYPE(PyNotImplemented_Type, "pNotImplementedType"); + PROVIDE_TYPE(PyEllipsis_Type, "pEllipsisType"); +#endif + PROVIDE_TYPE(PyTupleIter_Type, "ptupleiterator"); + PROVIDE_TYPE(_PyWeakref_RefType, "pweakref"); +} + Scheme_Object* scheme_initialize(Scheme_Env* env) { // PyObject* m; @@ -579,24 +676,25 @@ PRINTF("scheme_initialize: set SPY_GLOBALS_SCHEME_STRUCT and SPY_GLOBALS_PYTHON_NODE\n"); spy_cpython_register_globals(); - spy_cpython_init_types(); + /// init types is done at the PROVIDE_TYPE macro + //spy_cpython_init_types(); + - SPY_INIT_GLOBAL_T(PyNone_Type); - SPY_INIT_GLOBAL(*Py_None); - SPY_INIT_GLOBAL(*Py_Ellipsis); - SPY_INIT_GLOBAL(*Py_NotImplemented); - scheme_register_extension_global( &_Py_TrueStruct, sizeof(PyIntObject)); - scheme_register_extension_global( &_Py_ZeroStruct, sizeof(PyIntObject)); +#ifdef SPY_MODIFIED_LIBPYTHON +spy_init_PyString(); +#endif + printf("pscm.c: scheme_initialize: finished adding global types\n"); -spy_init_PyString(); _Py_ReadyTypes(); if ( !_PyInt_Init() ) PRINTF("WARNING: Spy could not initialize the PyInt module.\n"); else {PRINTF("scheme_initialize: initialized the PyInt module\n");} +#ifdef SPY_MODIFIED_LIBPYTHON spy_init_ClassObject(); +#endif _PyExc_Init(); EMPTY_TUPLE = SPY_INIT_OBJ(PyTuple_New(0), &PyTuple_Type); @@ -619,12 +717,6 @@ return scheme_reload(env); } -void spy_cpython_add_type_from_stack(PyTypeObject* tobj) -{ - scheme_register_extension_global((void*) tobj, sizeof(PyTypeObject)); - assert(SCHEME_STRUCTP((Scheme_Object*)tobj)); - sapply2("spy-add-cpython-type", tobj->tp_name ? sym(tobj->tp_name) : sym("(null)"), SCM(tobj)); -} Scheme_Object* python_import_submodule_for_c = NULL; Scheme_Object* spy_initialize(int argc, Scheme_Object* argv[]); @@ -635,49 +727,7 @@ printf("RELOAD\n"); - #define ADD_TYPE_FROM_STACK(type_val, name) spy_cpython_add_type_from_stack(&type_val) - - ADD_TYPE_FROM_STACK(PyBaseObject_Type, "pobj"); - ADD_TYPE_FROM_STACK(PyType_Type, "ptype"); - ADD_TYPE_FROM_STACK(PyInt_Type, "pint"); - ADD_TYPE_FROM_STACK(PyLong_Type, "plong"); - ADD_TYPE_FROM_STACK(PyFloat_Type, "pfloat"); - ADD_TYPE_FROM_STACK(PyDict_Type, "pdict"); - ADD_TYPE_FROM_STACK(PyString_Type, "pstr"); - ADD_TYPE_FROM_STACK(PyList_Type, "plist"); - ADD_TYPE_FROM_STACK(PyCObject_Type, "pcobj"); - ADD_TYPE_FROM_STACK(PyTuple_Type, "ptuple"); - ADD_TYPE_FROM_STACK(PySlice_Type, "pslice"); - ADD_TYPE_FROM_STACK(PyFunction_Type, "pfunc"); - ADD_TYPE_FROM_STACK(PyCode_Type, "pcode"); - ADD_TYPE_FROM_STACK(PyFile_Type, "pfile"); - ADD_TYPE_FROM_STACK(PyBuffer_Type, "pbuffer"); - ADD_TYPE_FROM_STACK(PyEnum_Type, "penum"); - ADD_TYPE_FROM_STACK(PyClassMethod_Type, "pclassmethod"); - ADD_TYPE_FROM_STACK(PyStaticMethod_Type, "pstaticmethod"); - ADD_TYPE_FROM_STACK(PySeqIter_Type, "pseqiter"); - ADD_TYPE_FROM_STACK(PyCallIter_Type, "pcalliter"); - ADD_TYPE_FROM_STACK(PyCFunction_Type, "pcfunction"); - ADD_TYPE_FROM_STACK(PyModule_Type, "pmodule"); - ADD_TYPE_FROM_STACK(PySuper_Type, "psuper"); - ADD_TYPE_FROM_STACK(PyProperty_Type, "pproperty"); - ADD_TYPE_FROM_STACK(PyWrapperDescr_Type, "pwrapperdescr"); - ADD_TYPE_FROM_STACK(PyCell_Type, "pcell"); - ADD_TYPE_FROM_STACK(PyBool_Type, "pbool"); - ADD_TYPE_FROM_STACK(PyClass_Type, "pclass"); - ADD_TYPE_FROM_STACK(PyInstance_Type, "pinstance"); - ADD_TYPE_FROM_STACK(PyMethod_Type, "pmethod"); - ADD_TYPE_FROM_STACK(PyMethodDescr_Type, "pmethod_descriptor"); - ADD_TYPE_FROM_STACK(PyClassMethodDescr_Type, "pclassmethod_descriptor"); - ADD_TYPE_FROM_STACK(PyMemberDescr_Type, "pmember_descriptor"); - ADD_TYPE_FROM_STACK(PyGetSetDescr_Type, "pgetset_descriptor"); - ADD_TYPE_FROM_STACK(proxytype, "pdictproxy"); - ADD_TYPE_FROM_STACK(wrappertype, "pmethod-wrapper"); - ADD_TYPE_FROM_STACK(PyNone_Type, "pNoneType"); - ADD_TYPE_FROM_STACK(PyNotImplemented_Type, "pNotImplementedType"); - ADD_TYPE_FROM_STACK(PyEllipsis_Type, "pEllipsisType"); - ADD_TYPE_FROM_STACK(PyTupleIter_Type, "ptupleiterator"); - ADD_TYPE_FROM_STACK(_PyWeakref_RefType, "pweakref"); + spy_cpython_provide_types(); #define ADDFN_NAMES(cname, sname, min_args, max_args) \ @@ -846,6 +896,7 @@ int is_spy_initialized = 0; + Scheme_Object* spy_initialize(int argc, Scheme_Object* argv[]) { // Scheme_Env* pns = spy_global_env; @@ -857,7 +908,7 @@ python_import_submodule_for_c = argv[0]; Py_NoSiteFlag = 1; // don't "import site" - /* Py_Initialize imports types.py, and we don't compile yield% yet + //Py_Initialize imports types.py, and we don't compile yield% yet Py_Initialize(); { // add the current directory to sys.path @@ -874,7 +925,7 @@ PyObject* dx = PyImport_Import(PyString_FromString("define_x")); assert(dx); } - */ + return scheme_void; } @@ -1318,9 +1369,13 @@ assert(SCHEME_SYMBOLP(name)); assert(SCHEME_STRINGP(path)); { +#ifdef SPY_MODIFIED_LIBPYTHON PyObject* m = SpyModule_New(SCHEME_SYM_VAL(name), PySchemeNS_New(ns)); PyObject_SetAttrString(m, "__file__", PyString_FromString(SCHEME_STR_VAL(path))); return SCM(m); +#else + return NULL; +#endif } } @@ -2164,7 +2219,7 @@ int amt_to_copy; void *new_buffer; PRINTF("PyMem_Realloc\n"); - printf("PyMem_Realloc: realloc %x to %d\n", o, new_size); + printf("PyMem_Realloc: realloc ptr at 0x%x to %d\n", o, new_size); if ( new_size == 0 ) { //PRINTF("PyMem_Realloc: size = 0, returning free()\n"); @@ -2175,12 +2230,12 @@ if ( !o ) { PRINTF("PyMem_Realloc: orig is null, returning malloc(%d)\n", new_size); - return scheme_malloc_eternal(new_size);//PyMem_MALLOC(new_size); + return /*scheme_malloc_eternal(new_size);*/PyMem_MALLOC(new_size); } amt_to_copy = new_size; // how can I get the old size? should be min(new_size, old_size) PRINTF("PyMem_Realloc: allocating %d bytes\n", new_size); // new_buffer = PyMem_Malloc(new_size ? new_size : sizeof(PyVarObject)); - new_buffer = scheme_malloc_eternal(new_size);//PyMem_Malloc(new_size); + new_buffer = /*scheme_malloc_eternal(new_size);*/PyMem_Malloc(new_size); PRINTF("PyMem_Realloc: allocated new object\n"); PRINTF("PyMem_Realloc: copying %d bytes\n", amt_to_copy); if ( amt_to_copy ) Index: typeobject.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/typeobject.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- typeobject.c 3 Sep 2004 15:51:15 -0000 1.3 +++ typeobject.c 23 Nov 2004 12:57:16 -0000 1.4 @@ -1968,8 +1968,12 @@ } assert(dict && PyDict_Check(dict)); res = PyDict_GetItem(dict, name); - if (res != NULL) + if (res != NULL) { + printf("_PyType_Lookup: name is %s, result is 0x%x\n", PyString_AsString(name), res); + assert(res->ob_type != NULL); + printf("_PyType_Lookup: type at 0x%x is named %s\n", res->ob_type, res->ob_type->tp_name); return res; + } } return NULL; } @@ -3100,7 +3104,7 @@ type->tp_flags |= Py_TPFLAGS_READYING; // spliced in by daniel -SPY_INIT_SCHEME_HEADER(type); +//SPY_INIT_SCHEME_HEADER(type); #ifdef Py_TRACE_REFS /* PyType_Ready is the closest thing we have to a choke point @@ -3270,6 +3274,9 @@ i = PyList_GET_SIZE(list); while (--i >= 0) { ref = PyList_GET_ITEM(list, i); +// printf("REF: addr: 0x%x\n", ref); +// printf("REF: type addr: 0x%x\n", ref->ob_type); +// printf("REF: type name: %s\n", ref->ob_type->tp_name); assert(PyWeakref_CheckRef(ref)); if (PyWeakref_GET_OBJECT(ref) == Py_None) return PyList_SetItem(list, i, new); @@ -5594,7 +5601,7 @@ new->type = su->type; new->obj = obj; new->obj_type = obj_type; - SPY_INIT_OBJ(new, new->ob_type); +// SPY_INIT_OBJ(new, new->ob_type); return (PyObject *)new; } } Index: Makefile =================================================================== RCS file: /cvsroot/plt-spy/python/c/Makefile,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- Makefile 4 Sep 2004 16:36:38 -0000 1.5 +++ Makefile 23 Nov 2004 12:57:15 -0000 1.6 @@ -3,26 +3,32 @@ MZC=mzc CC=$(MZC) #CFLAGS=++ccf -I. ++ccf -g ++ccf -finline-functions ++ccf -foptimize-sibling-calls ++ccf -mtail-call --cc -CFLAGS=++ccf -I. ++ccf -I./Include ++ccf -g ++ccf -fPIC --ccf-show --cc +CFLAGS= ++ccf -I. ++ccf -I./Include ++ccf -g ++ccf -fPIC --cc -OBJECTS=pscm.o listobject.o stringobject.o intobject.o longobject.o floatobject.o cobject.o dictobject.o tupleobject.o object.o typeobject.o classobject.o pystate.o descrobject.o spy-modsupport.o abstract.o funcobject.o methodobject.o weakrefobject.o iterobject.o sliceobject.o structmember.o cellobject.o spy-codeobject.o fileobject.o codecs.o spy-exceptions.o spy-moduleobject.o boolobject.o spy-schemensobject.o Objects/structseq.o Objects/bufferobject.o Objects/enumobject.o Objects/rangeobject.o +OBJECTS=pscm.o #Objects/complexobject.o listobject.o stringobject.o intobject.o longobject.o floatobject.o cobject.o dictobject.o tupleobject.o object.o typeobject.o classobject.o pystate.o descrobject.o spy-modsupport.o abstract.o funcobject.o methodobject.o weakrefobject.o iterobject.o sliceobject.o structmember.o cellobject.o spy-codeobject.o fileobject.o codecs.o spy-exceptions.o spy-moduleobject.o boolobject.o spy-schemensobject.o Objects/structseq.o Objects/bufferobject.o Objects/enumobject.o Objects/rangeobject.o Objects/unicodeobject.o Objects/unicodectype.o -MODOBJS= Modules/threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o Modules/config.o Modules/spy-gcmodule.o Modules/getpath.o Modules/getbuildinfo.o +#MODOBJS= Modules/threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o Modules/config.o Modules/spy-gcmodule.o Modules/getpath.o Modules/getbuildinfo.o -CPYOBJS= Python/thread.o Python/spymod-ceval.o Python/spymod-pythonrun.o Python/spymod-import.o Python/spymod-marshal.o Python/spymod-errors.o Python/getmtime.o Python/modsupport.o Python/spymod-importdl.o Python/dynload_shlib.o Python/spymod-sysmodule.o Python/mysnprintf.o Python/getcopyright.o Python/frozen.o Python/traceback.o Python/getversion.o Python/getplatform.o Python/getcompiler.o Python/spymod-bltinmodule.o Python/getargs.o +#CPYOBJS= Python/thread.o Python/spymod-ceval.o Python/spymod-pythonrun.o Python/spymod-import.o Python/spymod-marshal.o Python/spymod-errors.o Python/getmtime.o Python/modsupport.o Python/spymod-importdl.o Python/dynload_shlib.o Python/spymod-sysmodule.o Python/mysnprintf.o Python/getcopyright.o Python/frozen.o Python/traceback.o Python/getversion.o Python/getplatform.o Python/getcompiler.o Python/spymod-bltinmodule.o Python/getargs.o -PARSEROBJS= Parser/myreadline.o +#PARSEROBJS= Parser/myreadline.o ALLOBJS=$(OBJECTS) $(MODOBJS) $(CPYOBJS) $(PARSEROBJS) LIBS=++ldf -lpthread ++ldf -lutil +MZLIBDIR=/home/dsilva/cvs/plt-nightly/plt/lib/ + fake-program: fake-program.c cpy-bindings.so - gcc -o fake-program -I/home/dsilva/plt/include fake-program.c cpy-bindings.so ~/plt/lib/libmzscheme.a ~/plt/lib/mzdyn.o ~/plt/lib/libmzgc.a -lm -ldl + gcc -o fake-program -I/home/dsilva/plt/include fake-program.c cpy-bindings.so $(MZLIBDIR)/libmzscheme.a $(MZLIBDIR)/mzdyn.o $(MZLIBDIR)/libmzgc.a -lm -ldl -lpython2.3 #/home/dsilva/incoming/modded-python/libpython2.3.a echo MakeSpy: no errors. cpy-bindings.so: $(ALLOBJS) - $(MZC) --linker /usr/bin/gcc --ld cpy-bindings.so ++ldf -fPIC $(LIBS) --ldf-show --ldl-show $(ALLOBJS) + $(MZC) -v --linker /usr/bin/gcc --ld cpy-bindings.so ++ldf -g ++ldf -fPIC $(LIBS) --ldf-show --ldl-show $(ALLOBJS) .c.o: $(CC) -d `dirname $@` $(CFLAGS) $< + +clean: + rm -f $(ALLOBJS) + Index: Python.h =================================================================== RCS file: /cvsroot/plt-spy/python/c/Python.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- Python.h 4 Sep 2004 16:36:39 -0000 1.5 +++ Python.h 23 Nov 2004 12:57:16 -0000 1.6 @@ -18,9 +18,11 @@ #include "escheme.h" +/* #ifdef Py_USING_UNICODE #undef Py_USING_UNICODE #endif +*/ //#define PYTHON_API_VERSION 25 @@ -177,7 +179,10 @@ return obj; } -#define SPY_INIT_GLOBAL_T(global) SPY_INIT_GLOBAL_T_impl(&global, sizeof(global)) +#define SPY_INIT_GLOBAL_T(global) do { \ + extern PyTypeObject global; \ + SPY_INIT_GLOBAL_T_impl(&global, sizeof(PyTypeObject)); \ + } while(0) #if 0 // what if obj is some expression?? #define SPY_INIT_OBJ(obj, type) \ @@ -211,9 +216,9 @@ //#define PyObject_TypeCheck(obj, type) ((obj) && sapply2("py-is-a?", obj, type)) - -#define SPY_CODE_GET_LAMBDA(code_obj) (((PyCodeObject*)(code_obj))->co_code) -#define SPY_CODE_SET_LAMBDA(code_obj, lam) (((PyCodeObject*)(code_obj))->co_code = (lam)) +// HACK! +#define SPY_CODE_GET_LAMBDA(code_obj) ((Scheme_Object*)(((PyCodeObject*)(code_obj))->co_code)) +#define SPY_CODE_SET_LAMBDA(code_obj, lam) (((PyCodeObject*)(code_obj))->co_code = (PyObject*) (lam)) //#define Py_GCC_ATTRIBUTE(stuff) @@ -464,7 +469,7 @@ */ // no unicode -#define PyUnicode_Check(obj) 0 +//#define PyUnicode_Check(obj) 0 // this should be in boolobject.h /* Don't use these directly */ @@ -532,7 +537,7 @@ //typedef unsigned int Py_uintptr_t; //typedef int Py_intptr_t; -#define WITHOUT_COMPLEX +// //#define WITHOUT_COMPLEX //#define SIZEOF_VOID_P sizeof(void*) #define SIZEOF_VOID_P 4 @@ -545,6 +550,7 @@ static __inline__ void* PyMem_Malloc(size_t count) { void* ptr = scheme_malloc_eternal(count); assert(ptr != NULL); + /* if (count >= sizeof(Scheme_Object)) { return (void*) SPY_INIT_SCHEME_HEADER((Scheme_Object*)ptr); @@ -552,7 +558,8 @@ else { return ptr; - } + }*/ + return ptr; } //#define PyMem_Malloc(count) (scheme_malloc(count)) #define PyMem_MALLOC(count) PyMem_Malloc(count) @@ -615,6 +622,7 @@ #include <pyport.h> #include <object.h> +#include <unicodeobject.h> PyObject* spy_ext_new_instance(PyTypeObject* type); // called by PyObject_INIT PyObject* spy_init_obj(PyObject* obj, PyTypeObject* py_type); @@ -637,6 +645,7 @@ #include <longobject.h> #include <floatobject.h> #include <intobject.h> +#include <complexobject.h> #include <boolobject.h> #include <sliceobject.h> @@ -665,11 +674,15 @@ #include <frameobject.h> +#include <codecs.h> + // hmm, not sure if this should be here #include <import.h> #include <modsupport.h> #include <patchlevel.h> #include <traceback.h> +#include <sysmodule.h> +#include <eval.h> #include "spy-schemensobject.h" Index: fake-program.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/fake-program.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- fake-program.c 10 Apr 2004 18:42:14 -0000 1.1.1.1 +++ fake-program.c 23 Nov 2004 12:57:16 -0000 1.2 @@ -1,8 +1,9 @@ +#include <stdio.h> int main() { printf("calling scheme_initialize\n"); - scheme_initialize(0); + scheme_initialize(NULL); printf("called scheme_initialize\n"); return 0; } Index: spy-moduleobject.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/spy-moduleobject.c,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- spy-moduleobject.c 4 Sep 2004 16:36:39 -0000 1.3 +++ spy-moduleobject.c 23 Nov 2004 12:57:16 -0000 1.4 @@ -26,10 +26,17 @@ PyObject* SpyModule_New(char* name, PyObject* ns); PyObject* PyModule_New(char *name); +static Scheme_Object* new_scheme_ns() +{ + Scheme_Object* argv[1]; + argv[0] = sym("empty"); + return scheme_make_namespace(1, argv); +} + PyObject * PyModule_New(char *name) { - Scheme_Object* o = scheme_make_namespace(0, 0); + Scheme_Object* o = new_scheme_ns(); assert(SCHEME_NAMESPACEP(o)); /// HACK! HACK! HACK!! this shouldn't be necessary scheme_dont_gc_ptr(o); @@ -122,7 +129,7 @@ } d = ((PyModuleObject *)m)->md_ns; if (d == NULL || - (nameobj = PyObject_GetItemString(d, "__name__")) == NULL || + (nameobj = PyObject_GetAttrString(d, "__name__")) == NULL || /* d = ((PyModuleObject *)m)->md_dict; if (d == NULL || (nameobj = PyDict_GetItemString(d, "__name__")) == NULL ||*/ @@ -222,10 +229,10 @@ return -1; dict = m->md_ns; if (dict == NULL) { - Scheme_Object* ns = scheme_make_namespace(0, 0); + Scheme_Object* ns = new_scheme_ns(); assert(SCHEME_NAMESPACEP(ns)); - dict = PySchemeNS_New(ns); + dict = (PyObject*) PySchemeNS_New((Scheme_Env*) ns); if (dict == NULL) return -1; m->md_ns = dict; @@ -316,7 +323,7 @@ } if (!PyString_Check(name)) return 0; - if (PyObject_GetItemString(globals, name) == NULL && + if (PyObject_GetAttrString(globals, PyString_AsString(name)) == NULL && /*if (PyDict_GetItem(globals, name) == NULL && */ PyDict_GetItem(builtin_names, name) != NULL) { return 1; Index: spy-modsupport.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/spy-modsupport.c,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- spy-modsupport.c 9 Aug 2004 17:52:22 -0000 1.2 +++ spy-modsupport.c 23 Nov 2004 12:57:16 -0000 1.3 @@ -8,6 +8,21 @@ typedef double va_double; #endif +static PyObject* do_mkvalue(char **p_format, va_list *p_va); +static PyObject* do_mktuple(char **p_format, va_list *p_va, int endchar, int n); +static PyObject* do_mklist(char **p_format, va_list *p_va, int endchar, int n); + +#ifdef Py_USING_UNICODE +static int +_ustrlen(Py_UNICODE *u) +{ + int i = 0; + Py_UNICODE *v = u; + while (*v != 0) { i++; v++; } + return i; +} +#endif + static PyObject * do_mkdict(char **p_format, va_list *p_va, int endchar, int n) @@ -52,6 +67,7 @@ } +static PyObject* do_mklist(char **p_format, va_list *p_va, int endchar, int n) { PyObject *v; @@ -79,7 +95,7 @@ return v; } - +static PyObject* do_mkvalue(char **p_format, va_list *p_va) { for (;;) { @@ -239,7 +255,7 @@ - +static PyObject* do_mktuple(char **p_format, va_list *p_va, int endchar, int n) { PyObject *v; Index: methodobject.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/methodobject.c,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- methodobject.c 10 Apr 2004 18:42:14 -0000 1.1.1.1 +++ methodobject.c 23 Nov 2004 12:57:16 -0000 1.2 @@ -13,7 +13,7 @@ op = free_list; if (op != NULL) { free_list = (PyCFunctionObject *)(op->m_self); - PyObject_INIT(op, &PyCFunction_Type); + PyObject_INIT((PyObject*) op, &PyCFunction_Type); } else { op = PyObject_GC_New(PyCFunctionObject, &PyCFunction_Type); |
From: Daniel S. <aph...@us...> - 2004-11-23 12:57:31
|
Update of /cvsroot/plt-spy/python/c/Python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3708/c/Python Modified Files: spymod-bltinmodule.c spymod-ceval.c Log Message: Ditching the c directory soon. Hacking CPython has sort of failed. The new embed directory is an embedding of CPython. Spy will slowly implement parts of the runtime system and subclass parts of CPython (like PyFunction_Type) to override CPython instead of distributing a hacked libpython binary. Index: spymod-bltinmodule.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/Python/spymod-bltinmodule.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- spymod-bltinmodule.c 9 Aug 2004 17:52:26 -0000 1.1 +++ spymod-bltinmodule.c 23 Nov 2004 12:57:16 -0000 1.2 @@ -1543,6 +1543,7 @@ For example, range(4) returns [0, 1, 2, 3]. The end point is omitted!\n\ These are exactly the valid indices for a list of 4 elements."); +#include <sysmodule.h> static PyObject * builtin_raw_input(PyObject *self, PyObject *args) Index: spymod-ceval.c =================================================================== RCS file: /cvsroot/plt-spy/python/c/Python/spymod-ceval.c,v retrieving revision 1.1 retrieving revision 1.2 diff -u -d -r1.1 -r1.2 --- spymod-ceval.c 9 Aug 2004 17:52:26 -0000 1.1 +++ spymod-ceval.c 23 Nov 2004 12:57:16 -0000 1.2 @@ -32,7 +32,9 @@ typedef PyObject *(*callproc)(PyObject *, PyObject *, PyObject *); /* Forward declarations */ +#ifndef SPY static PyObject *eval_frame(PyFrameObject *); +#endif static PyObject *call_function(PyObject ***, int); static PyObject *fast_function(PyObject *, PyObject ***, int, int, int); static PyObject *do_call(PyObject *, PyObject ***, int, int); @@ -193,7 +195,8 @@ gen_iternext(genobject *gen) { #ifdef SPY - // TODO: implement me + // TODO: is this ok? + scheme_signal_error("spymod-eval.c: gen_iternext: Should never reach this!"); return NULL; #else PyThreadState *tstate = PyThreadState_GET(); @@ -553,6 +556,7 @@ /* Interpreter main loop */ +#ifndef SPY // Spy doesn't need this static PyObject * eval_frame(PyFrameObject *f) { @@ -665,6 +669,8 @@ /* Local variable macros */ +#ifndef SPY + #define GETLOCAL(i) (fastlocals[i]) /* The SETLOCAL() macro must not DECREF the local variable in-place and @@ -677,6 +683,8 @@ GETLOCAL(i) = value; \ Py_XDECREF(tmp); } while (0) +#endif // SPY + /* Start of code */ if (f == NULL) @@ -2441,6 +2449,7 @@ return retval; } +#endif // no SPY (eval_frame not needed for Spy) PyObject * PyEval_EvalCodeEx(PyCodeObject *co, PyObject *globals, PyObject *locals, @@ -2450,7 +2459,7 @@ #ifdef SPY Scheme_Object** scm_argv = NULL; int scm_argc; -#define SETLOCAL(idx, val) (scm_argv[idx] = val) +#define SETLOCAL(idx, val) (scm_argv[idx] = SCM(val)) #define GETLOCAL(idx) scm_argv[idx] #define INIT_SCM_ARGV(len) \ if (!scm_argv) \ @@ -2717,7 +2726,7 @@ fail: return NULL; // kaboom? succeed: - return scheme_apply(SPY_CODE_GET_LAMBDA(co), scm_argc, scm_argv); + return PY(scheme_apply(SPY_CODE_GET_LAMBDA(co), scm_argc, scm_argv)); #endif } @@ -3555,6 +3564,7 @@ PCALL(PCALL_FUNCTION); PCALL(PCALL_FAST_FUNCTION); +#ifndef SPY // we don't use PyFrameObject, so we can't do this if (argdefs == NULL && co->co_argcount == n && nk==0 && co->co_flags == (CO_OPTIMIZED | CO_NEWLOCALS | CO_NOFREE)) { PyFrameObject *f; @@ -3587,6 +3597,7 @@ --tstate->recursion_depth; return retval; } +#endif // no SPY if (argdefs != NULL) { d = &PyTuple_GET_ITEM(argdefs, 0); nd = ((PyTupleObject *)argdefs)->ob_size; |
From: Daniel S. <aph...@us...> - 2004-11-23 12:57:30
|
Update of /cvsroot/plt-spy/python/embed In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3708/embed Added Files: Makefile attributes.c embed.c import.c primitive-functions.c schemefunctionobject.c schemefunctionobject.h spy.h type-utils.c value-conversion.c Log Message: Ditching the c directory soon. Hacking CPython has sort of failed. The new embed directory is an embedding of CPython. Spy will slowly implement parts of the runtime system and subclass parts of CPython (like PyFunction_Type) to override CPython instead of distributing a hacked libpython binary. --- NEW FILE: schemefunctionobject.h --- #include "spy.h" typedef struct { PyObject_HEAD PyObject *func_code; PyObject *func_globals; PyObject *func_defaults; PyObject *func_closure; PyObject *func_doc; PyObject *func_name; PyObject *func_dict; PyObject *func_weakreflist; PyObject *func_module; Scheme_Object* proc; } PySchemeFunctionObject; /* typedef struct { PyObject_HEAD Scheme_Object* proc; } PySchemeFunctionObject; */ PyAPI_DATA(PyTypeObject) PySchemeFunction_Type; PyObject* PySchemeFunction_New(Scheme_Object* proc); --- NEW FILE: import.c --- #include "spy.h" Scheme_Object* spy_import(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* modname = GUARANTEE_CHAR_STRING(name, 0); //PyObject* mod = PyImport_Import(PyString_FromString(SCHEME_STR_VAL(modname))); PyObject* mod = PyImport_ImportModule(SCHEME_STR_VAL(modname)); if (mod == NULL) { if (PyErr_Occurred() != NULL) PyErr_Print(); else dprintf("imported module is null but no error occurred?\n"); } dprintf("imported module: %s\n", PyString_AsString(PyObject_Str(mod))); return SCM(mod); } --- NEW FILE: embed.c --- #include "spy.h" Scheme_Env* spy_global_env; ///////////////////////////////////////////////// static void spy_Py_Finalize(void* pytoken, void* data) { Py_Finalize(); } // just keep some object around in Spy's namespace; // shut down Python when Spy is gone too. Scheme_Object* pytoken = NULL; Scheme_Object* spy_tag; static void spy_register_pytoken(Scheme_Env* ns) { // just keep some object around in Spy's namespace; // shut down Python when Spy is gone too. pytoken = cons(scheme_null, scheme_null); scheme_register_finalizer(pytoken, spy_Py_Finalize, NULL, NULL, NULL); } Scheme_Object* scheme_initialize(Scheme_Env* ns) { spy_tag = sym("spy-object"); PRINTF("[scheme_initialize] Initializing libpython...\n"); Py_Initialize(); PRINTF("[scheme_initialize] Initialized libpython.\n"); // spy_register_pytoken(ns); if(1) { PyObject* warn = PyExc_Warning; } return scheme_reload(ns); } /* struct SpyClosure { void* proc; PyObject* callHandler(PyObject* unused, PyObject* argsTuple) { Scheme_Object* argv[] = (Scheme_Object*[]) &PyTuple_GET_ITEM(argsTuple, 0); Scheme_Object* result = scheme_apply(proc, argc, argv); return PY(result); } }; */ Scheme_Object* spy_apply(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* scheme_pyfn = GUARANTEE_PY(name, 0); PyObject* fnobj = PY(scheme_pyfn); PyObject* args = PyTuple_New(argc - 1); PyObject* result, *objstr; int i; dprintf("argc is %d\n", argc); dprintf("arg 0: %s\n", PyString_AsString(PyObject_Str(fnobj))); for ( i = 1; i < argc; i++ ) { PyObject* arg = PYARG(i); dprintf("arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); // TODO: change this to PyTuple_SET_ITEM for speed PyTuple_SetItem(args, i - 1, arg); } dprintf("calling object at address 0x%x\n", fnobj); // objstr = PyObject_Str(fnobj); // dprintf("objstr is at 0x%x\n", objstr); // dprintf("obj: %s\n", PyString_AsString(objstr)); dprintf("callabe? %s\n", (PyCallable_Check(fnobj) ? "yes" : "no")); result = fnobj->ob_type->tp_call ? fnobj->ob_type->tp_call(fnobj, args, NULL) : PyObject_CallObject(fnobj, args); dprintf("result address: 0x%x\n", result); return SCM( result ); } static const PyTypeObject* spy_builtin_types[] = { &PyType_Type, &PyBaseObject_Type, &PyList_Type, &PyString_Type, &PyTuple_Type, &PyLong_Type, &PyInt_Type, &PyFloat_Type, &PyBool_Type, &PyComplex_Type, &PyUnicode_Type, &PyDict_Type, }; static void spy_load_builtin_types(Scheme_Env* env, const PyTypeObject* spy_builtin_types[], unsigned long size) { PyTypeObject* type; int i, N = size / sizeof (PyTypeObject*); for (i = 0; i < N; i++) { char name[1024]; type = spy_builtin_types[i]; snprintf(name, 1024, "cpy-%s", type->tp_name); scheme_add_global(name, SCM(type), env); } } #define SPY_PROTO(id) Scheme_Object* id(const char* name, int argc, Scheme_Object* argv[]) SPY_PROTO( spy_make_py_string ); SPY_PROTO( spy_get_py_string ); SPY_PROTO( spy_make_py_symbol ); SPY_PROTO( spy_make_py_number ); SPY_PROTO( spy_get_py_number ); SPY_PROTO( spy_make_py_list ); SPY_PROTO( spy_get_py_list ); SPY_PROTO( spy_make_py_tuple ); SPY_PROTO( spy_get_py_tuple ); SPY_PROTO( spy_make_py_dict ); SPY_PROTO( spy_get_py_dict ); SPY_PROTO( spy_make_py_file ); SPY_PROTO( spy_get_py_file ); SPY_PROTO( spy_make_py_function ); SPY_PROTO( spy_apply ); SPY_PROTO( spy_add ); SPY_PROTO( spy_sub ); SPY_PROTO( spy_mul ); SPY_PROTO( spy_div ); SPY_PROTO( spy_mod ); SPY_PROTO( spy_bit_or ); SPY_PROTO( spy_len_py ); SPY_PROTO( spy_len_scm ); SPY_PROTO( spy_getitem_obj ); SPY_PROTO( spy_getattr_obj ); SPY_PROTO( spy_getattr_sym ); SPY_PROTO( spy_getattr_str ); SPY_PROTO( spy_setattr_obj ); SPY_PROTO( spy_setattr_sym ); SPY_PROTO( spy_setattr_str ); SPY_PROTO( spy_instanceof ); SPY_PROTO( spy_get_type_name ); SPY_PROTO( spy_isnumber ); SPY_PROTO( spy_isstring ); SPY_PROTO( spy_islist ); SPY_PROTO( spy_istuple ); SPY_PROTO( spy_pyobj_to_pystr ); SPY_PROTO( spy_index ); SPY_PROTO( spy_import ); struct scm_prim { char *name; Scheme_Closed_Prim *prim; int mina; int maxa; }; static const struct scm_prim scm_prim[] = { { "make-py-string", spy_make_py_string, 1, 1 }, { "get-py-string", spy_get_py_string, 1, 1 }, { "make-py-symbol", spy_make_py_symbol, 1, 1 }, { "make-py-number", spy_make_py_number, 1, 1 }, { "get-py-number", spy_get_py_number, 1, 1 }, { "make-py-list", spy_make_py_list, 1, 1 }, { "get-py-list", spy_get_py_list, 1, 1 }, { "make-py-tuple", spy_make_py_tuple, 1, 1 }, { "get-py-tuple", spy_get_py_tuple, 1, 1 }, { "make-py-dict", spy_make_py_dict, 1, 1 }, { "get-py-dict", spy_get_py_dict, 1, 1 }, { "make-py-file", spy_make_py_file, 1, 1 }, { "get-py-file", spy_get_py_file, 1, 1 }, { "make-py-function", spy_make_py_function, 1, 1 }, { "spy-apply", spy_apply, 1, 99 }, { "spy-add", spy_add, 2, 2 }, { "spy-sub", spy_sub, 2, 2 }, { "spy-mul", spy_mul, 2, 2 }, { "spy-div", spy_div, 2, 2 }, { "spy-mod", spy_mod, 2, 2 }, { "spy-bit-or", spy_bit_or, 2, 2 }, { "spy-len/scm", spy_len_scm, 1, 1 }, { "spy-len/py", spy_len_py, 1, 1 }, { "spy-getitem/obj", spy_getitem_obj, 2, 2 }, { "spy-getattr/obj", spy_getattr_obj, 2, 2 }, { "spy-getattr/sym", spy_getattr_sym, 2, 2 }, { "spy-getattr/str", spy_getattr_str, 2, 2 }, { "spy-setattr/obj", spy_setattr_obj, 3, 3 }, { "spy-setattr/sym", spy_setattr_sym, 3, 3 }, { "spy-setattr/str", spy_setattr_str, 3, 3 }, { "spy-instanceof", spy_instanceof, 2, 2 }, { "spy-get-type-name", spy_get_type_name, 1, 1 }, { "spy-number?", spy_isnumber, 1, 1 }, { "spy-string?", spy_isstring, 1, 1 }, { "spy-list?", spy_islist, 1, 1 }, { "spy-tuple?", spy_istuple, 1, 1 }, { "py-object->py-string", spy_pyobj_to_pystr, 1, 1 }, { "spy-index", spy_index, 2, 2 }, { "spy-import", spy_import, 1, 1 }, }; // from Scott Owens's sgl/gl-prims.h static void scheme_load_prim(Scheme_Env *env, const struct scm_prim *scm_prim, unsigned long s) { Scheme_Object *prim; int i, N = s / sizeof (struct scm_prim); /* printf("%d procedures\n", N - 45); */ for (i = 0; i < N; i++) { prim = scheme_make_closed_prim_w_arity(scm_prim[i].prim, scm_prim[i].name, scm_prim[i].name, scm_prim[i].mina, scm_prim[i].maxa); scheme_add_global(scm_prim[i].name, prim, env); } } Scheme_Object* scheme_reload(Scheme_Env* enclosing_ns) { Scheme_Env* ns = scheme_primitive_module(scheme_module_name(), enclosing_ns); spy_global_env = ns; scheme_add_global("__pytoken__", pytoken, ns); scheme_add_global("cpy-none", Py_None, ns); scheme_load_prim(ns, scm_prim, sizeof(scm_prim)); spy_load_builtin_types(ns, spy_builtin_types, sizeof(spy_builtin_types)); scheme_finish_primitive_module(ns); return scheme_void; } Scheme_Object* scheme_module_name() { return sym("embed"); } --- NEW FILE: spy.h --- #include "escheme.h" #include "Python.h" // MzScheme convenience extern Scheme_Env* spy_global_env; #define seval(str) scheme_eval_string((str), spy_global_env) #define slookup(str) scheme_eval(sym(str), spy_global_env) #define sym(str) scheme_intern_exact_symbol(str, strlen(str)) #define sapply scheme_apply #define cons scheme_make_pair #define scheme_make_string scheme_make_utf8_string #define scheme_make_sized_string(s, l, x) scheme_make_sized_utf8_string(s, l) #define SCHEME_STR_VAL(str) SCHEME_BYTE_STR_VAL(scheme_char_string_to_byte_string(str)) #define SCHEME_STRINGP SCHEME_CHAR_STRINGP static Scheme_Object* sapply1(const char* func_name, Scheme_Object* arg) { Scheme_Object* args[1] = { arg }; Scheme_Object* func = slookup(func_name); return sapply( func, 1, args ); } #define SCM_ARG(name, type_ok, expected, which, argc, argv) \ (type_ok ? argv[which] : (scheme_wrong_type(name, expected, which, argc, argv), NULL)) #define SCM_STR_ARG(name, which, argc, argv) SCM_ARG(name, SCHEME_STRINGP(argv[which]), "string", which, argc, argv) //#define SCM_STR_ARG(name, which, argc, argv) GUARANTEE_CHAR_STRING(name, 0) //#define SCM_PROC_ARG(name, which, argc, argv) SCM_ARG(name, SCHEME_PROCP(argv[which]), "procedure", which, argc, argv) #define SCM_PY_ARG(name, which, argc, argv) SCM_ARG(name, SCHEME_CPTRP(argv[which]), "python object", which, argc, argv) #define SCM_PY_ARG_CONV(name, which, argc, argv) PY(SCM_PY_ARG(name, which, argc, argv)) #define GUARANTEE_PY(fname, argnum) GUARANTEE_TYPE (fname, argnum, SCHEME_CPTRP, "python object") #define GUARANTEE_NUMBER(fname, argnum) GUARANTEE_TYPE (fname, argnum, SCHEME_NUMBERP, "number") #define GUARANTEE_LIST(fname, argnum) GUARANTEE_TYPE(fname, argnum, SCHEME_LISTP, "list") #define SPYARG(argnum) GUARANTEE_PY(name, argnum) #define PYARG(argnum) PY(SPYARG(argnum)) extern Scheme_Object* spy_tag; static PyObject* PY(Scheme_Object* s); static void spy_cptr_finalize(void* spy_cptr_to_pyobj, void* data) { Py_XDECREF(PY(spy_cptr_to_pyobj)); } static Scheme_Object* SCM(PyObject* p) { Scheme_Object* sobj = scheme_make_cptr(p, spy_tag); Py_XINCREF(p); scheme_register_finalizer(sobj, spy_cptr_finalize, NULL, NULL, NULL); return sobj; } static PyObject* PY(Scheme_Object* s) { return (PyObject*) SCHEME_CPTR_VAL(s); } /////////////////////////// /// Python convenience #define PyInt_FromInt(x) PyInt_FromLong((long)x) ////////////// general convenience #define DEBUG_SPY #ifdef DEBUG_SPY #define PRINTF(fmt, args...) printf(fmt, ##args) #define dprintf(fmt, args...) printf("[%s] " fmt, name, ##args) #else #define PRINTF(fmt, args...) #define dprintf(fmt, args...) #endif --- NEW FILE: value-conversion.c --- #include "spy.h" Scheme_Object* spy_make_py_string(const char* me, int argc, Scheme_Object* argv[]) { Scheme_Object* sstr = SCM_STR_ARG(me, 0, argc, argv); PyObject* pstr = PyString_FromString(SCHEME_STR_VAL(sstr)); return SCM(pstr); } Scheme_Object* spy_get_py_string(const char* name, int argc, Scheme_Object* argv[]) { return scheme_make_string(PyString_AsString(SCM_PY_ARG_CONV(name, 0, argc, argv))); } // make-py-symbol: symbol -> PyString[interned] Scheme_Object* spy_make_py_symbol(const char* name, int argc, Scheme_Object* argv[]) { PyObject* str; Scheme_Object* sym = GUARANTEE_SYMBOL(name, 0); str = PyString_InternFromString(SCHEME_SYM_VAL(sym)); assert(str != NULL); assert(PyString_Check(str)); return SCM(str); } static PyObject* proper_list_to_py_list(Scheme_Object* slist) { int len = scheme_proper_list_length(slist); PyObject* plist = PyList_New(len); int i = 0; while ( slist != scheme_null ) { PyList_SET_ITEM(plist, i, PY( SCHEME_CAR(slist)) ); i++; slist = SCHEME_CDR(slist); } return plist; } // make-py-list: list -> PyList Scheme_Object* spy_make_py_list(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* slist = GUARANTEE_LIST(name, 0); return SCM( proper_list_to_py_list(slist) ); } static Scheme_Object* py_list_to_scheme_list_unsafe(PyObject* plist, int pos, int len); // get-py-list: PyList -> list Scheme_Object* spy_get_py_list(const char* name, int argc, Scheme_Object* argv[]) { PyObject* plist = PY(GUARANTEE_PY(name, 0)); if (!PyList_Check(plist)) scheme_wrong_type(name, "PyListObject", 0, argc, argv); return py_list_to_scheme_list_unsafe(plist, 0, PyList_GET_SIZE(plist)); } static Scheme_Object* py_list_to_scheme_list_unsafe(PyObject* plist, int pos, int len) { return pos == len ? scheme_null : scheme_make_pair(SCM(PyList_GET_ITEM(plist, pos)), py_list_to_scheme_list_unsafe(plist, pos + 1, len)); } // make-py-tuple: list -> PyTuple Scheme_Object* spy_make_py_tuple(const char* name, int argc, Scheme_Object* argv[]) { return SCM( PyList_AsTuple(PY(spy_make_py_list("make-py-list", argc, argv))) ); } static Scheme_Object* py_tuple_to_scheme_list_unsafe(PyObject* ptup, int pos, int len) { // TODO: translate recursion into loop return pos == len ? scheme_null : scheme_make_pair(SCM(PyTuple_GET_ITEM(ptup, pos)), py_tuple_to_scheme_list_unsafe(ptup, pos + 1, len)); } // get-py-tuple: PyTuple -> list Scheme_Object* spy_get_py_tuple(const char* name, int argc, Scheme_Object* argv[]) { PyObject* ptuple = PY( GUARANTEE_PY(name, 0) ); dprintf("entry\n"); if (!PyTuple_Check(ptuple)) scheme_wrong_type(name, "PyTupleObject", 0, argc, argv); return py_tuple_to_scheme_list_unsafe(ptuple, 0, PyTuple_GET_SIZE(ptuple)); } // make-py-dict: assoc-list -> PyDict Scheme_Object* spy_make_py_dict(const char *name, int argc, Scheme_Object* argv[]) { Scheme_Object* al = GUARANTEE_LIST(name, 0); int len; int i = 0; PyObject* dict; // assert(sapply1("list?", al) != scheme_false); //len = scheme_proper_list_length(al); dict = PyDict_New(); while ( al != scheme_null ) { Scheme_Object* pair = SCHEME_CAR(al); PyObject* key = PY(SCHEME_CAR(pair)); PyObject* value = PY(SCHEME_CDR(pair)); //PRINTF("make_py_dict: key is %s\n", PyString_AsString(key)); //PRINTF("make_py_dict: value is %d\n", PyInt_AsLong(value)); PyDict_SetItem(dict, key, value); i++; al = SCHEME_CDR(al); } return SCM(dict); } static Scheme_Object* py_dict_to_scheme_assoc_list_unsafe_entry(PyObject* dict, PyObject* keys, int pos) { PyObject* key = PyList_GET_ITEM(keys, pos); //PRINTF("py_dict_to_scheme_assoc_list_unsafe_entry: pos = %d\n", pos); //PRINTF("py_dict_to_scheme_assoc_list_unsafe_entry: key = %s\n", PyString_AsString(key)); return scheme_make_pair(SCM(key), SCM(PyDict_GetItem(dict, key))); } static Scheme_Object* py_dict_to_scheme_assoc_list_unsafe(PyObject* dict, PyObject* keys, int pos, int len) { return pos == len ? scheme_null : scheme_make_pair(py_dict_to_scheme_assoc_list_unsafe_entry(dict, keys, pos), py_dict_to_scheme_assoc_list_unsafe(dict, keys, pos + 1, len)); } // get-py-dict: PyDict -> assoc-list Scheme_Object* spy_get_py_dict(const char *name, int argc, Scheme_Object* argv[]) { PyObject* dict = PY(GUARANTEE_PY(name, 0)); PyObject* keys; // assert(PyDict_Check(dict)); keys = PyDict_Keys(dict); //assert(PyList_Check(keys)); return py_dict_to_scheme_assoc_list_unsafe(dict, keys, 0, PyList_GET_SIZE(keys)); } Scheme_Object* spy_make_py_function(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* proc = GUARANTEE_PROCEDURE(name, 0); PyObject* fnobj = PySchemeFunction_New(proc); return SCM(fnobj); } PyObject* make_py_number_last_resort(Scheme_Object* n) { PyObject* pynum; PRINTF("make_py_number_last_resort: entry\n"); sapply1("display", scheme_make_string("make_py_number_last_resort: ")); sapply1("display", n); seval("(newline)"); pynum = PyLong_FromString(SCHEME_STR_VAL((Scheme_Object*)sapply1("number->string", n)), NULL, 10); assert(PyNumber_Check(pynum)); return pynum; } #ifdef DEBUG PyObject* chk_mpn(PyObject* n) { PRINTF("chk_mpn: entry\n"); PRINTF("chk_mpn: n is at %x\n", n); return n; } #else #define chk_mpn(n) n #endif // make-py-number: number -> (U PyInt PyLong PyFloat) Scheme_Object* spy_make_py_number(const char* name, int argc, Scheme_Object* argv[]) { Scheme_Object* n = GUARANTEE_NUMBER(name, 0); PyObject* pynum; pynum = SCHEME_INTP(n) ? chk_mpn(PyInt_FromLong(SCHEME_INT_VAL(n))) : SCHEME_DBLP(n) ? PyFloat_FromDouble(SCHEME_DBL_VAL(n)) : make_py_number_last_resort(n); // TODO: take out this assertion, though I guess it compiles away assert(PyNumber_Check(pynum)); return SCM(pynum); } // get-py-number: (U PyInt PyLong PyFloat) -> number Scheme_Object* spy_get_py_number(const char* name, int argc, Scheme_Object* argv[]) { PyObject* n = PY(GUARANTEE_PY(name, 0)); // TODO: does this assertion compile away? assert(PyNumber_Check(n)); return PyInt_Check(n) ? scheme_make_integer(PyInt_AsLong(n)) : PyLong_Check(n) ? scheme_make_integer(PyLong_AsLong(n)) : PyFloat_Check(n) ? scheme_make_double(PyFloat_AsDouble(n)) : sapply1("string->number", scheme_make_string(PyString_AsString(PyObject_Str(n)))); } // make-py-file: port -> PyFile Scheme_Object* spy_make_py_file(const char* name, int argc, Scheme_Object* argv[]) { //TODO: implement me scheme_signal_error("make_py_file not implemented yet"); return NULL; } // get-py-file: PyFile -> port Scheme_Object* spy_get_py_file(const char* name, int argc, Scheme_Object* argv[]) { // TODO: implement me PyObject* pfile = PY(argv[0]); assert(PyFile_Check(pfile)); scheme_signal_error("get_py_file not implemented yet"); return NULL; } --- NEW FILE: attributes.c --- #include "spy.h" // TODO: remove all these assertions // PySequence integer -> PyObject // seq[idx] ===> (python-index seq idx) Scheme_Object* spy_index(const char* name, int argc, Scheme_Object* argv[]) { PyObject* seq = PYARG(0); Scheme_Object* idx = GUARANTEE_INTEGER(name, 1); return SCM(PySequence_GetItem(seq, SCHEME_INT_VAL(idx))); } static PyObject* got_attrib_obj(PyObject* attr, PyObject* obj, PyObject* key) { if (attr == NULL) scheme_signal_error("Could not find attribute."); assert(attr != NULL); // assert(SPY_NODEP(attr)); return attr; } static PyObject* got_attrib_str(PyObject* attr, PyObject* obj, char* key) { if (attr == NULL) scheme_signal_error("Could not find attribute."); assert(attr != NULL); // assert(SPY_NODEP(attr)); return attr; } // PyObject PyObject -> PyObject Scheme_Object* spy_getitem_obj(const char* name, int argc, Scheme_Object* argv[]) { PyObject* item; PyObject* obj = PY(GUARANTEE_PY(name, 0)); PyObject* key = PY(GUARANTEE_PY(name, 1)); assert(argv[0] != NULL); assert(argv[1] != NULL); item = PyObject_GetItem(obj, key); return SCM(got_attrib_obj(item, obj, key)); } // PyObject PyString -> PyObject Scheme_Object* spy_getattr_obj(const char* name, int argc, Scheme_Object* argv[]) { PyObject* attr; PyObject* obj = PY(GUARANTEE_PY(name, 0)); PyObject* key = PY(GUARANTEE_PY(name, 1)); // assert(PyString_Check(argv[1])); attr = PyObject_GetAttr(obj, key); return SCM(got_attrib_obj(attr, obj, key)); } // PyObject symbol -> PyObject Scheme_Object* spy_getattr_sym(const char* name, int argc, Scheme_Object* argv[]) { PyObject* attr; PyObject* obj = PY(GUARANTEE_PY(name, 0)); char* str = SCHEME_SYM_VAL(GUARANTEE_SYMBOL(name, 1)); dprintf("looking up symbol: %s\n", str); attr = PyObject_GetAttrString(obj, str); return SCM(got_attrib_str(attr, obj, str)); } // PyObject string -> PyObject Scheme_Object* spy_getattr_str(const char* name, int argc, Scheme_Object* argv[]) { char* str; PyObject* obj = PY(GUARANTEE_PY(name, 0)); str = SCHEME_STR_VAL(GUARANTEE_CHAR_STRING(name, 0)); return SCM(got_attrib_str( PyObject_GetAttrString(obj, str), obj, str)); } // PyObject PyObject PyObject -> PyObject Scheme_Object* spy_setattr_obj(const char* name, int argc, Scheme_Object* argv[]) { // assert(PyString_Check(PY(argv[1]))); if ( PyObject_SetAttr(PY(GUARANTEE_PY(name, 0)), PY(GUARANTEE_PY(name, 1)), PY(GUARANTEE_PY(name, 2))) ) scheme_signal_error("spy_setattr_obj failed."); return SCM(Py_None); } // PyObject symbol PyObject -> PyObject Scheme_Object* spy_setattr_sym(const char* name, int argc, Scheme_Object* argv[]) { if ( PyObject_SetAttrString(PY(GUARANTEE_PY(name, 0)), SCHEME_SYM_VAL(GUARANTEE_SYMBOL(name, 1)), PY(GUARANTEE_PY(name, 2))) ) scheme_signal_error("spy_setattr_sym failed."); return SCM(Py_None); } // PyObject string PyObject -> PyObject Scheme_Object* spy_setattr_str(const char* name, int argc, Scheme_Object* argv[]) { if ( PyObject_SetAttrString(PY(GUARANTEE_PY(name, 0)), SCHEME_STR_VAL(GUARANTEE_CHAR_STRING(name, 1)), PY(GUARANTEE_PY(name, 2))) ) scheme_signal_error("spy_setattr_str failed."); return SCM(Py_None); } --- NEW FILE: schemefunctionobject.c --- #include "schemefunctionobject.h" PyObject* PySchemeFunction_New(Scheme_Object* proc) { PySchemeFunctionObject* fnobj = PyObject_GC_New(PySchemeFunctionObject, &PySchemeFunction_Type); if (fnobj == NULL) { fprintf(stderr, "out of memory in PySchemeFunction_New!\n"); exit(1); } // we don't care about these fields fnobj->func_code = NULL; fnobj->func_globals = NULL; fnobj->func_defaults = NULL; fnobj->func_closure = NULL; fnobj->func_doc = NULL; fnobj->func_name = NULL; fnobj->func_dict = NULL; fnobj->func_weakreflist = NULL; fnobj->func_module = NULL; // save the Scheme lambda fnobj->proc = proc; return (PyObject*) fnobj; } #include "structmember.h" static PyMemberDef schemefunction_members[] = { //{"__ns__", T_OBJECT, offsetof(PySchemeNSObject, ns), READONLY}, {0} }; PyObject* PySchemeFunction_Call(PyObject* self, PyObject* argsTuple, PyObject* kw) { PySchemeFunctionObject* fn = (PySchemeFunctionObject*) self; // TODO: fix this static array, it's a security hole Scheme_Object* argv[1024]; int i, argc; argc = PyTuple_GET_SIZE(argsTuple); printf("[PySchemeFunction_Call] args tuple length is %d\n", argc); for ( i = 0; i < argc; i++ ) { PyObject* arg = PyTuple_GET_ITEM(argsTuple, i); printf("[PySchemeFunction_Call] arg%2d: %s\n", i, PyString_AsString(PyObject_Str(arg))); argv[i] = SCM(arg); } return PY( scheme_apply(fn->proc, argc, argv) ); } /* Bind a function to an object */ static PyObject * schemefunc_descr_get(PyObject *func, PyObject *obj, PyObject *type) { PyObject* meth; PRINTF("[schemefunc_descr_get] func: %s\n", PyString_AsString(PyObject_Str(func))); PRINTF("[schemefunc_descr_get] obj: %s\n", PyString_AsString(PyObject_Str(obj))); PRINTF("[schemefunc_descr_get] type: %s\n", PyString_AsString(PyObject_Str(type))); if (obj == Py_None) { //printf("[schemefunc_descr_get] obj is Py_None\n"); obj = NULL; } meth = PyMethod_New(func, obj, type); //printf("[schemefunc_descr_get] method is at address 0x%x\n", meth); PRINTF("[schemefunc_descr_get] method object is %s\n", PyString_AsString(PyObject_Str(meth))); return meth; } PyTypeObject PySchemeFunction_Type = { PyObject_HEAD_INIT(&PyType_Type) 0, /* ob_size */ "SchemeFunction", /* tp_name */ sizeof(PySchemeFunctionObject), /* tp_size */ 0, /* tp_itemsize */ 0, /* tp_dealloc */ 0, /* tp_print */ 0, /* tp_getattr */ 0, /* tp_setattr */ 0, /* tp_compare */ 0, /* tp_repr */ 0, /* tp_as_number */ 0, /* tp_as_sequence */ 0, /* tp_as_mapping */ 0, /* tp_hash */ PySchemeFunction_Call, /* tp_call */ 0, /* tp_str */ PyObject_GenericGetAttr, /* tp_getattro */ PyObject_GenericSetAttr, /* tp_setattro */ 0, /* tp_as_buffer */ Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 0, /* tp_doc */ 0, /* tp_traverse */ 0, /* tp_clear */ 0, /* tp_richcompare */ 0, /* tp_weaklistoffset */ 0, /* tp_iter */ 0, /* tp_iternext */ 0, /* tp_methods */ schemefunction_members, /* tp_members */ 0, /* tp_getset */ &PyFunction_Type, /* tp_base */ 0, /* tp_dict */ schemefunc_descr_get, /* tp_descr_get */ 0, /* tp_descr_set */ 0/*offsetof(PyModuleObject, md_dict)*/, /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ 0, /* tp_new */ 0, /* tp_free */ }; --- NEW FILE: Makefile --- #MZC=/home/dsilva/plt/bin/mzc #MZC=mzc #MZC=/home/dsilva/plt-299/bin/mzc MZC=/home/dsilva/cvs/plt-nightly/plt/bin-nogc/mzc #MZC=/tmp/plt-299.22/bin/mzc CC=$(MZC) #CFLAGS=++ccf -I. ++ccf -g ++ccf -finline-functions ++ccf -foptimize-sibling-calls ++ccf -mtail-call --cc CFLAGS= ++ccf -I. ++ccf -I/usr/include/python2.3 ++ccf -g ++ccf -fPIC ++ccf -Wl,-E --cc PY_EXT_DIR=/usr/lib/python2.3/site-packages PY_EXTENSIONS=$(shell find $(PY_EXT_DIR) -name "*.so") ALL_RPATHS=$(foreach e,$(PY_EXTENSIONS),$(shell echo ++ldf -Wl,-rpath ++ldf $(shell dirname $(e)))) #$(shell bash -c 'for F in $(PY_EXTENSIONS); do echo ++ldf -Wl,-rpath \$F; done') RPATHS=$(shell echo $(ALL_RPATHS) | uniq) #OBJECTS=pscm.o #Objects/complexobject.o listobject.o stringobject.o intobject.o longobject.o floatobject.o cobject.o dictobject.o tupleobject.o object.o typeobject.o classobject.o pystate.o descrobject.o spy-modsupport.o abstract.o funcobject.o methodobject.o weakrefobject.o iterobject.o sliceobject.o structmember.o cellobject.o spy-codeobject.o fileobject.o codecs.o spy-exceptions.o spy-moduleobject.o boolobject.o spy-schemensobject.o Objects/structseq.o Objects/bufferobject.o Objects/enumobject.o Objects/rangeobject.o Objects/unicodeobject.o Objects/unicodectype.o #MODOBJS= Modules/threadmodule.o Modules/signalmodule.o Modules/posixmodule.o Modules/errnomodule.o Modules/_sre.o Modules/_codecsmodule.o Modules/zipimport.o Modules/symtablemodule.o Modules/xxsubtype.o Modules/config.o Modules/spy-gcmodule.o Modules/getpath.o Modules/getbuildinfo.o #CPYOBJS= Python/thread.o Python/spymod-ceval.o Python/spymod-pythonrun.o Python/spymod-import.o Python/spymod-marshal.o Python/spymod-errors.o Python/getmtime.o Python/modsupport.o Python/spymod-importdl.o Python/dynload_shlib.o Python/spymod-sysmodule.o Python/mysnprintf.o Python/getcopyright.o Python/frozen.o Python/traceback.o Python/getversion.o Python/getplatform.o Python/getcompiler.o Python/spymod-bltinmodule.o Python/getargs.o #PARSEROBJS= Parser/myreadline.o NEWOBJS=embed.o schemefunctionobject.o value-conversion.o primitive-functions.o attributes.o type-utils.o import.o ALLOBJS=$(NEWOBJS) #$(OBJECTS) $(MODOBJS) $(CPYOBJS) $(PARSEROBJS) LIBS=++ldf -lpthread ++ldf -lutil ++ldf -lpython2.3 ++ldf -lgtk-x11-2.0 /usr/lib/python2.3/site-packages/gtk-2.0/gobject.so $(PY_EXTENSIONS) #MZLIBDIR=/home/dsilva/cvs/plt-nightly/plt/lib/ TARGET=compiled/native/i386-linux/embed.so #fake-program: fake-program.c embed.so # gcc -o fake-program -I/home/dsilva/plt/include fake-program.c cpy-bindings.so $(MZLIBDIR)/libmzscheme.a $(MZLIBDIR)/mzdyn.o $(MZLIBDIR)/libmzgc.a -lm -ldl -lpython2.3 #/home/dsilva/incoming/modded-python/libpython2.3.a # echo MakeSpy: no errors. $(TARGET): $(ALLOBJS) $(MZC) -v --linker /usr/bin/gcc --ld $(TARGET) ++ldf -g ++ldf -fPIC ++ldf -Xlinker ++ldf -export-dynamic ++ldf -Wl,-E $(RPATHS) $(LIBS) --ldf-show --ldl-show $(ALLOBJS) #/home/dsilva/incoming/modded-python/out/lib/libpython2.3.a .c.o: $(CC) -d `dirname $@` $(CFLAGS) $< clean: rm -f $(ALLOBJS) --- NEW FILE: type-utils.c --- #include "spy.h" // PyObject PyTypeObject -> bool Scheme_Object* spy_instanceof(const char* name, int argc, Scheme_Object* argv[]) { return PyObject_TypeCheck(PYARG(0), (PyTypeObject*) PYARG(1)) ? scheme_true : scheme_false; } // PyObject -> bool #define DEF_CHECKER(stype, ptype) \ Scheme_Object* spy_is##stype (const char* name, int argc, Scheme_Object* argv[]) \ { \ return Py##ptype##_Check((PyObject*) PYARG(0)) ? scheme_true : scheme_false; \ } DEF_CHECKER(number, Number) DEF_CHECKER(string, String) DEF_CHECKER(list, List) DEF_CHECKER(tuple, Tuple) #define PY_TYPE_GET_NAME(type_obj) ((PyTypeObject*) type_obj)->tp_name // PyTypeObject -> string Scheme_Object* spy_get_type_name(const char* name, int argc, Scheme_Object* argv[]) { return scheme_make_string(PY_TYPE_GET_NAME(PYARG(0))); } --- NEW FILE: primitive-functions.c --- #include "spy.h" #define WRAP_BIN_FN(myname, cpy_name) \ Scheme_Object* spy_##myname (const char* name, int argc, Scheme_Object* argv[]) \ {\ Scheme_Object* a = GUARANTEE_PY(name, 0); \ Scheme_Object* b = GUARANTEE_PY(name, 1); \ return SCM(cpy_name(PY(a), PY(b)));\ } // PyNumber PyNumber -> PyNumber #define WRAP_BIN_NUM_FN(myname, cpy_num_name) WRAP_BIN_FN(myname, PyNumber_##cpy_num_name) WRAP_BIN_NUM_FN(add, Add) WRAP_BIN_NUM_FN(sub, Subtract) WRAP_BIN_NUM_FN(mul, Multiply) WRAP_BIN_NUM_FN(div, Divide) WRAP_BIN_NUM_FN(mod, Remainder) WRAP_BIN_NUM_FN(bit_or, Or) Scheme_Object* spy_len_scm(const char* name, int argc, Scheme_Object* argv[]) { return scheme_make_integer(PyObject_Size(PY(GUARANTEE_PY(name, 0)))); } Scheme_Object* spy_len_py(const char* name, int argc, Scheme_Object* argv[]) { return SCM(PyInt_FromInt(PyObject_Size(PY(GUARANTEE_PY(name, 0))))); } // PyObject -> PyString Scheme_Object* spy_pyobj_to_pystr(const char* name, int argc, Scheme_Object* argv[]) { return SCM(PyObject_Str(PYARG(0))); } |
From: Daniel S. <aph...@us...> - 2004-11-23 12:53:51
|
Update of /cvsroot/plt-spy/python/embed In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3481/embed Log Message: Directory /cvsroot/plt-spy/python/embed added to the repository |
From: Daniel S. <aph...@us...> - 2004-09-04 16:37:02
|
Update of /cvsroot/plt-spy/python In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv4181 Modified Files: python-node.ss Log Message: removed 'is_mutable' field from PyObject_HEAD macro Index: python-node.ss =================================================================== RCS file: /cvsroot/plt-spy/python/python-node.ss,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- python-node.ss 10 Apr 2004 18:42:08 -0000 1.1.1.1 +++ python-node.ss 4 Sep 2004 16:36:38 -0000 1.2 @@ -1,16 +1,9 @@ (module python-node mzscheme (provide (all-defined)) - ;; python-node is a: - ;; (make-python-node python-node #|(hash-table-of dict-member)|# bool) - - ;; a dict-member is one of: - ;; symbol python-node - ;; gensym scheme-value - - -; (define-struct python-node (type dict mutable?) (make-inspector)) - (define-struct python-node (type mutable?)) + ;; python-node is a: + ;; (make-python-node python-node) + (define-struct python-node (type)) ;; these are hidden keys in built-in data types to hold their actual scheme values @@ -38,7 +31,7 @@ |# (define (python-new-object type) - (make-python-node type #|(make-hash-table)|# #t)) + (make-python-node type)) ) |