diff options
author | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-08-27 20:56:59 +0200 |
---|---|---|
committer | Stefan Israelsson Tampe <stefan.itampe@gmail.com> | 2018-08-27 20:56:59 +0200 |
commit | 44b33505e59219864881bf2c3bfe1fb445d4b91c (patch) | |
tree | aeefa85312fb468b0065dd30246ef63117519881 | |
parent | 34ca3d2231a96e484c9b7114dc73c82f5ea4db60 (diff) |
hashlib works okay
-rw-r--r-- | modules/language/python/compile.scm | 26 | ||||
-rw-r--r-- | modules/language/python/eval.scm | 86 | ||||
-rw-r--r-- | modules/language/python/module.scm | 62 | ||||
-rw-r--r-- | modules/language/python/module/_sha224.scm | 2 | ||||
-rw-r--r-- | modules/language/python/module/hashlib.py | 55 |
5 files changed, 191 insertions, 40 deletions
diff --git a/modules/language/python/compile.scm b/modules/language/python/compile.scm index 39597bc..0f964ff 100644 --- a/modules/language/python/compile.scm +++ b/modules/language/python/compile.scm @@ -77,13 +77,22 @@ (define-syntax-rule (use p l a ...) - (catch #t - (lambda () - (if (not p) (reload-module (resolve-module l))) - (use-modules a ...)) - (lambda x - (warn "failed to load " x) - (raise (ImportError '(a ...)))))) + (begin + (eval-when (compile) + (catch #t + (lambda () + (if (not p) (reload-module (resolve-module l))) + (use-modules a ...)) + (lambda x + #f))) + (eval-when (eval load) + (catch #t + (lambda () + (if (not p) (reload-module (resolve-module l))) + (use-modules a ...)) + (lambda x + (raise (ImportError ((@ (guile) format) + #f "failed to import ~a ~a" l x)))))))) (define level (make-fluid 0)) @@ -263,7 +272,7 @@ ((#:global . _) vs) - + ((#:import (#:name ((ids ...) . as) ...) ...) (let lp ((ids ids) (as as) (vs vs)) (if (pair? ids) @@ -355,6 +364,7 @@ (ids (car ids))) (union vs (list (exp '() (if as as (car ids))))))) vs))) + ((x . y) (defs y (defs x vs))) (_ vs))) diff --git a/modules/language/python/eval.scm b/modules/language/python/eval.scm index a54ea64..1cdb459 100644 --- a/modules/language/python/eval.scm +++ b/modules/language/python/eval.scm @@ -1,6 +1,12 @@ (define-module (language python eval) #:use-module (parser stis-parser lang python3-parser) #:use-module (language python exceptions) + #:use-module (language python module) + #:use-module (language python try) + #:use-module (language python list) + #:use-module (language python for) + #:use-module (language python dict) + #:use-module (oop pf-objects) #:use-module ((ice-9 local-eval) #:select ((the-environment . locals))) #:re-export (locals) #:replace (eval) @@ -12,22 +18,82 @@ (define-syntax-rule (L x) (@@ (ice-9 local-eval) x)) -(define MM (list 'error)) -(define (M mod) - (let ((l '())) - (module-for-each - (lambda (k v) - (if (not (eq? (variable-ref v) MM)) - (set! l (cons (symbol->string k) l)))) - mod) - l)) - (define-syntax globals (lambda (x) (syntax-case x () ((g) #'(M ((L env-module) (locals g))))))) +(define-syntax-rule (call- self item a ...) + (let ((class (ref self '_module))) + ((rawref class item) class a ...))) + +(define-syntax-rule (apply- self item a ...) + (let ((class (ref self '_module))) + (apply (rawref class item) class a ...))) + +(define-syntax-rule (ref- self item) + (let ((class (ref self '_module))) + (rawref class item))) + + +(define-python-class GlobalModuleWrap (dict) + (define __init__ + (lambda (self module) + (set self '_module module))) + + (define __getitem__ + (lambda (self key) + (if (string? key) (set! key (string->symbol key))) + (call- self '__global_getitem__ key))) + + (define get + (lambda (self key . es) + (if (string? key) (set! key (string->symbol key))) + (apply- self '__global_get__ key es))) + + (define __setitem__ + (lambda (self key val) + (if (string? key) (set! key (string->symbol key))) + (call- self '__global_setitem__ key val))) + + (define __iter__ + (lambda (self) + (call- self '__global_iter__))) + + (define values + (lambda (self) + (for ((k v : (__iter__ self))) ((l '())) + (cons v l) + #:final l))) + + (define keys + (lambda (self) + (for ((k v : (__iter__ self))) ((l '())) + (cons k l) + #:final l))) + + (define items __iter__) + + (define __repr__ + (lambda (self) + (format #f "globals(~a)" (ref- self '__name__))))) + + + +(define MM (list 'error)) +(define (M mod) + (set! mod (module-name mod)) + (if (and (> (length mod) 3) + (eq? (car mod) 'language) + (eq? (cadr mod) 'python) + (eq? (caddr mod) 'module)) + (set! mod (Module (reverse mod) + (reverse (cdddr mod)))) + (set! mod (Module (reverse mod) (reverse mod)))) + + (GlobalModuleWrap mod)) + (define* (local-eval x locals globals) "Evaluate the expression @var{x} within the local environment @var{local} and diff --git a/modules/language/python/module.scm b/modules/language/python/module.scm index 5cefeba..adc87fd 100644 --- a/modules/language/python/module.scm +++ b/modules/language/python/module.scm @@ -39,6 +39,11 @@ (rawref self '_module) (rawref self '_export)))) +(define (globals self) + (aif it (rawref self '_export) + it + (rawref self '_module))) + (define-python-class Module () (define _modules (make-hash-table)) (define __setprivate__ @@ -88,7 +93,7 @@ '(language python module))) (__init__ self (reverse '(language python module)) (cdddr l) '()) - (__init__ self '() l '())) + (__init__ self '() (reverse l) '())) (__init__ self (append '(language python module) @@ -172,6 +177,47 @@ (lambda x (fail))) (fail)))))) + (define __global_setitem__ + (lambda (self k v) + (let ((k (_k k)) + (fail (lambda () (raise KeyError "setattr in Module" k)))) + (aif m (rawref self '_module) + (catch #t + (lambda () + (if (module-defined? m k) + (module-set! m k v) + (begin + (module-define! m k v) + (module-export! m (list k))))) + (lambda x (fail))) + (fail))))) + + (define __global_getitem__ + (lambda (self k) + (let ((k (_k k)) + (fail (lambda () (raise KeyError "global setattr in Module" k)))) + (aif m (rawref self '_export) + (catch #t + (lambda () + (if (module-defined? m k) + (module-ref m k) + (fail))) + (lambda x (fail))) + (fail))))) + + (define __global_get__ + (lambda (self k . es) + (let ((k (_k k)) + (fail (lambda () (raise KeyError "global setattr in Module" k)))) + (aif m (rawref self '_export) + (catch #t + (lambda () + (if (module-defined? m k) + (module-ref m k) + (if (pair? es) (car es) #f))) + (lambda x (fail))) + (fail))))) + (define __delattr__ (lambda (self k) (define (fail) (raise KeyError "delattr in Module")) @@ -209,7 +255,19 @@ (module-for-each add m) (module-for-each add (rawref self '_export)) l))) - + + (define __global_iter__ + (lambda (self) + (let* ((m (globals self)) + (l '()) + (add (lambda (k v) + (let ((k (symbol->string k))) + (if (and (not (in "-" k)) (variable-bound? v)) + (set! l (cons (list k (variable-ref v)) + l))))))) + (module-for-each add m) + l))) + (define __repr__ diff --git a/modules/language/python/module/_sha224.scm b/modules/language/python/module/_sha224.scm index 16f1618..2b032a0 100644 --- a/modules/language/python/module/_sha224.scm +++ b/modules/language/python/module/_sha224.scm @@ -1,7 +1,7 @@ (define-module (language python module _sha224) #:use-module (language python checksum) #:use-module (oop pf-objects) - #:export (sha224) + #:export (sha224)) (define-python-class sha224 (Summer) (define name "sha224") diff --git a/modules/language/python/module/hashlib.py b/modules/language/python/module/hashlib.py index 343f116..dd6892a 100644 --- a/modules/language/python/module/hashlib.py +++ b/modules/language/python/module/hashlib.py @@ -84,27 +84,42 @@ def __get_builtin_constructor(name): elif name in ('MD5', 'md5'): import _md5 cache['MD5'] = cache['md5'] = _md5.md5 - elif name in ('SHA256', 'sha256', 'SHA224', 'sha224'): + elif name in ('SHA256', 'sha256'): import _sha256 - cache['SHA224'] = cache['sha224'] = _sha256.sha224 cache['SHA256'] = cache['sha256'] = _sha256.sha256 - elif name in ('SHA512', 'sha512', 'SHA384', 'sha384'): + elif name in ('SHA224', 'sha224'): + import _sha224 + cache['SHA224'] = cache['sha224'] = _sha224.sha224 + elif name in ('SHA512', 'sha512'): import _sha512 - cache['SHA384'] = cache['sha384'] = _sha512.sha384 cache['SHA512'] = cache['sha512'] = _sha512.sha512 - elif name in ('blake2b', 'blake2s'): - import _blake2 - cache['blake2b'] = _blake2.blake2b - cache['blake2s'] = _blake2.blake2s - elif name in {'sha3_224', 'sha3_256', 'sha3_384', 'sha3_512', - 'shake_128', 'shake_256'}: - import _sha3 - cache['sha3_224'] = _sha3.sha3_224 - cache['sha3_256'] = _sha3.sha3_256 - cache['sha3_384'] = _sha3.sha3_384 - cache['sha3_512'] = _sha3.sha3_512 - cache['shake_128'] = _sha3.shake_128 - cache['shake_256'] = _sha3.shake_256 + elif name in ('SHA384', 'sha384'): + import _sha384 + cache['SHA384'] = cache['sha384'] = _sha384.sha384 + elif name in ('blake2b',): + import _blake2b + cache['blake2b'] = _blake2b.blake2b + elif name in ('blake2s',): + import _blake2s + cache['blake2s'] = _blake2s.blake2s + elif name in ('sha3_224',): + import _sha3_224 + cache['sha3_224'] = _sha3_224.sha3_224 + elif name in ('sha3_256',): + import _sha3_256 + cache['sha3_256'] = _sha3_256.sha3_256 + elif name in ('sha3_384',): + import _sha3_384 + cache['sha3_384'] = _sha3_384.sha3_384 + elif name in ('sha3_512',): + import _sha3_512 + cache['sha3_512'] = _sha3_512.sha3_512 + elif name in ('shake_128',): + import _shake_128 + cache['shake_128'] = _shake_128.shake_128 + elif name in ('shake_256',): + import _shake_256 + cache['shake_256'] = _shake_256.shake_256 except ImportError: pass # no extension module, this hash is unsupported. @@ -169,7 +184,8 @@ except ImportError: try: # OpenSSL's PKCS5_PBKDF2_HMAC requires OpenSSL 1.0+ with HMAC and SHA - from _hashlib import pbkdf2_hmac + from _hashlib import pbkdf2_hmac as _hmac + pbkdf2_hmac = _hmac except ImportError: _trans_5C = bytes((x ^ 0x5C) for x in range(256)) _trans_36 = bytes((x ^ 0x36) for x in range(256)) @@ -233,7 +249,8 @@ except ImportError: try: # OpenSSL's scrypt requires OpenSSL 1.1+ - from _hashlib import scrypt + from _hashlib import scrypt as _scrypt + scrypt = _scrypt except ImportError: pass |