unicode_escape.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """ Python 'unicode-escape' Codec
  2. Written by Marc-Andre Lemburg (mal@lemburg.com).
  3. (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  4. """
  5. import codecs
  6. ### Codec APIs
  7. class Codec(codecs.Codec):
  8. # Note: Binding these as C functions will result in the class not
  9. # converting them to methods. This is intended.
  10. encode = codecs.unicode_escape_encode
  11. decode = codecs.unicode_escape_decode
  12. class IncrementalEncoder(codecs.IncrementalEncoder):
  13. def encode(self, input, final=False):
  14. return codecs.unicode_escape_encode(input, self.errors)[0]
  15. class IncrementalDecoder(codecs.BufferedIncrementalDecoder):
  16. def _buffer_decode(self, input, errors, final):
  17. return codecs.unicode_escape_decode(input, errors, final)
  18. class StreamWriter(Codec,codecs.StreamWriter):
  19. pass
  20. class StreamReader(Codec,codecs.StreamReader):
  21. def decode(self, input, errors='strict'):
  22. return codecs.unicode_escape_decode(input, errors, False)
  23. ### encodings module API
  24. def getregentry():
  25. return codecs.CodecInfo(
  26. name='unicode-escape',
  27. encode=Codec.encode,
  28. decode=Codec.decode,
  29. incrementalencoder=IncrementalEncoder,
  30. incrementaldecoder=IncrementalDecoder,
  31. streamwriter=StreamWriter,
  32. streamreader=StreamReader,
  33. )