diff options
Diffstat (limited to 'modules/language/python/bytes.scm')
-rw-r--r-- | modules/language/python/bytes.scm | 48 |
1 files changed, 43 insertions, 5 deletions
diff --git a/modules/language/python/bytes.scm b/modules/language/python/bytes.scm index daf7adc..4ba7d2b 100644 --- a/modules/language/python/bytes.scm +++ b/modules/language/python/bytes.scm @@ -2,10 +2,12 @@ #:use-module (oop goops) #:use-module (oop pf-objects) #:use-module (ice-9 match) + #:use-module (ice-9 iconv) #:use-module (rnrs bytevectors) #:use-module (system foreign) #:use-module (language python string) #:use-module (language python for) + #:use-module (language python def) #:use-module (language python try) #:use-module (language python exceptions) #:use-module (language python list) @@ -596,12 +598,46 @@ (lp (+ i 1) (+ j 1))))) (bytes s))) -(define-py** decode (py-decode bytes o n . l) - (let lp ((i 0) (r '())) - (if (< i n) - (lp (+ i 1) (cons (b-ref o i) r)) - (utf8->string (list->b (reverse r)))))) +(define-python-class UnicodeDecodeError (Exception)) +(define-py** decode (py-decode bytes o n . l) + (apply + (lam ((= encoding "UTF-8") (= errors "strict")) + (set! errors (py-lower (scm-str errors))) + (set! errors (cond + ((equal? errors "strict") + 'error) + ((equal? errors "replace") + 'substitute) + ((equal? errors "ignore") + (warn + (string-append + "not possible to use ignore " + "encodong error strategy " + "using replace in stead")) + 'substitute) + (else + (warn + "not a correct encodong error strategy") + 'error))) + (set! encoding (py-upper (scm-str encoding))) + + (let lp ((i 0) (r '())) + (if (< i n) + (lp (+ i 1) (cons (b-ref o i) r)) + (catch #t + (lambda () + (bytevector->string + (list->b (reverse r)) + encoding + errors)) + (lambda x + (raise (UnicodeDecodeError + (+ + "failed to decode " + encoding)))))))) + l)) + ;;;py-encode (define-py* endswith (py-endswith bytes o n suff . l) @@ -1330,3 +1366,5 @@ zfill))))) (pylist-sort! l) l)) + +(set! (@@ (language python string) bytes) bytes) |