| 1 | from rpython.rtyper.lltypesystem import rffi |
| 2 | from pypy.module.cpyext.api import cpython_api, PyObject, CONST_STRING |
| 3 | from pypy.module._codecs import interp_codecs |
| 4 | |
| 5 | @cpython_api([CONST_STRING, CONST_STRING], PyObject) |
| 6 | def PyCodec_IncrementalEncoder(space, encoding, errors): |
| 7 | w_codec = interp_codecs.lookup_codec(space, rffi.charp2str(encoding)) |
| 8 | if errors: |
| 9 | w_errors = space.newtext(rffi.charp2str(errors)) |
| 10 | return space.call_method(w_codec, "incrementalencoder", w_errors) |
| 11 | else: |
| 12 | return space.call_method(w_codec, "incrementalencoder") |
| 13 | |
| 14 | @cpython_api([CONST_STRING, CONST_STRING], PyObject) |
| 15 | def PyCodec_IncrementalDecoder(space, encoding, errors): |
| 16 | w_codec = interp_codecs.lookup_codec(space, rffi.charp2str(encoding)) |
| 17 | if errors: |
| 18 | w_errors = space.newtext(rffi.charp2str(errors)) |
| 19 | return space.call_method(w_codec, "incrementaldecoder", w_errors) |
| 20 | else: |
| 21 | return space.call_method(w_codec, "incrementaldecoder") |
| 22 | |
| 23 | @cpython_api([CONST_STRING], PyObject) |
| 24 | def PyCodec_Encoder(space, encoding): |
| 25 | w_codec = interp_codecs.lookup_codec(space, rffi.charp2str(encoding)) |
| 26 | return space.getitem(w_codec, space.newint(0)) |
| 27 | |
| 28 | @cpython_api([CONST_STRING], PyObject) |
| 29 | def PyCodec_Decoder(space, encoding): |
| 30 | w_codec = interp_codecs.lookup_codec(space, rffi.charp2str(encoding)) |
| 31 | return space.getitem(w_codec, space.newint(1)) |
| 32 | |
| 33 | @cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject) |
| 34 | def PyCodec_Encode(space, w_object, encoding, errors): |
| 35 | """Generic codec based encoding API. |
| 36 | |
| 37 | object is passed through the encoder function found for the given |
| 38 | encoding using the error handling method defined by errors. errors may |
| 39 | be NULL to use the default method defined for the codec. Raises a |
| 40 | LookupError if no encoder can be found.""" |
| 41 | w_encoding = space.newtext(rffi.charp2str(encoding)) |
| 42 | if errors: |
| 43 | w_errors = space.newtext(rffi.charp2str(errors)) |
| 44 | return space.call_method(w_object, "encode", w_encoding, w_errors) |
| 45 | else: |
| 46 | return space.call_method(w_object, "encode", w_encoding) |
| 47 | |
| 48 | |
| 49 | @cpython_api([PyObject, CONST_STRING, CONST_STRING], PyObject) |
| 50 | def PyCodec_Decode(space, w_object, encoding, errors): |
| 51 | """Generic codec based decoding API. |
| 52 | |
| 53 | object is passed through the decoder function found for the given |
| 54 | encoding using the error handling method defined by errors. errors may |
| 55 | be NULL to use the default method defined for the codec. Raises a |
| 56 | LookupError if no encoder can be found.""" |
| 57 | w_encoding = space.newtext(rffi.charp2str(encoding)) |
| 58 | if errors: |
| 59 | w_errors = space.newtext(rffi.charp2str(errors)) |
| 60 | return space.call_method(w_object, "decode", w_encoding, w_errors) |
| 61 | else: |
| 62 | return space.call_method(w_object, "decode", w_encoding) |
| 63 | |
| 64 |