Index: CPythonLib/encodings/bridge_to_java.py =================================================================== --- CPythonLib/encodings/bridge_to_java.py (revision 0) +++ CPythonLib/encodings/bridge_to_java.py (revision 0) @@ -0,0 +1,48 @@ +""" +shift_jis.py: Python Unicode Codec for Shift JIS + +Written by NISHIO Hirokazu (Cybozu Labs Inc.) + +""" + +import codecs + +def create_codec(encoding_name): + def _chr(n): + "change from -128..127 to 0..255 and chr it" + return chr((n + 256) % 256) + + class Codec(codecs.Codec): + def encode(self, data, errors='strict'): + from java.lang import String + pys = "".join(map(_chr, String(data).getBytes(encoding_name))) + return (pys, len(data)) + + def decode(self, data, errors='strict'): + from java.lang import String + javas = String(data, encoding_name) + pys = "".join(javas.toCharArray()) + return (pys, len(data)) + + return Codec + +def getregentry(encoding_name): + "shortcut to make 'getregentry' function" + Codec = create_codec(encoding_name) + class StreamReader(Codec, codecs.StreamReader): + pass + + class StreamWriter(Codec, codecs.StreamWriter): + pass + + def getregentry(): + return ( + Codec().encode, + Codec().decode, + StreamReader, + StreamWriter, + ) + + return getregentry + + Index: CPythonLib/encodings/euc_jp.py =================================================================== --- CPythonLib/encodings/euc_jp.py (revision 0) +++ CPythonLib/encodings/euc_jp.py (revision 0) @@ -0,0 +1,16 @@ +""" +euc_jp.py: Python Unicode Codec for euc_jp + +Written by + NISHIO Hirokazu + +>>> u = u'\u3053\u3093\u306b\u3061\u306f' +>>> s = u.encode("euc_jp") +>>> assert s == '\xa4\xb3\xa4\xf3\xa4\xcb\xa4\xc1\xa4\xcf' +>>> u2 = s.decode("euc_jp") +>>> assert u2 == u +""" + +import bridge_to_java + +getregentry = bridge_to_java.getregentry("euc_jp") Index: CPythonLib/encodings/iso2022_jp.py =================================================================== --- CPythonLib/encodings/iso2022_jp.py (revision 0) +++ CPythonLib/encodings/iso2022_jp.py (revision 0) @@ -0,0 +1,16 @@ +""" +iso2022_jp.py: Python Unicode Codec for iso2022_jp + +Written by + NISHIO Hirokazu + +>>> u = u'\u3053\u3093\u306b\u3061\u306f' +>>> s = u.encode("iso2022_jp") +>>> assert s == '\x1b$B$3$s$K$A$O\x1b(B' +>>> u2 = s.decode("iso2022_jp") +>>> assert u2 == u +""" + +import bridge_to_java + +getregentry = bridge_to_java.getregentry("iso2022jp") Index: CPythonLib/encodings/ms932.py =================================================================== --- CPythonLib/encodings/ms932.py (revision 0) +++ CPythonLib/encodings/ms932.py (revision 0) @@ -0,0 +1,16 @@ +""" +ms932.py: Python Unicode Codec for ms932 + +Written by + NISHIO Hirokazu + +>>> u = u'\u3053\u3093\u306b\u3061\u306f' +>>> s = u.encode("ms932") +>>> assert s == '\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd' +>>> u2 = s.decode("ms932") +>>> assert u2 == u +""" + +import bridge_to_java + +getregentry = bridge_to_java.getregentry("ms932") Index: CPythonLib/encodings/shift_jis.py =================================================================== --- CPythonLib/encodings/shift_jis.py (revision 0) +++ CPythonLib/encodings/shift_jis.py (revision 0) @@ -0,0 +1,16 @@ +""" +shift_jis.py: Python Unicode Codec for shift_jis + +Written by + NISHIO Hirokazu + +>>> u = u'\u3053\u3093\u306b\u3061\u306f' +>>> s = u.encode("shift_jis") +>>> assert s == '\x82\xb1\x82\xf1\x82\xc9\x82\xbf\x82\xcd' +>>> u2 = s.decode("shift_jis") +>>> assert u2 == u +""" + +from encodings import bridge_to_java + +getregentry = bridge_to_java.getregentry("shift_jis") Index: CPythonLib/test/test_japanese_codecs.py =================================================================== --- CPythonLib/test/test_japanese_codecs.py (revision 0) +++ CPythonLib/test/test_japanese_codecs.py (revision 0) @@ -0,0 +1,18 @@ +""" +test_japanese_codecs.py: test following modules + * encoding/shift_jis.py + * encoding/euc_jp.py + * encoding/iso2022_jp.py + * encoding/ms932.py + +Written by + NISHIO Hirokazu +""" + +import doctest +import encodings + +doctest.testmod(encodings.shift_jis) +doctest.testmod(encodings.euc_jp) +doctest.testmod(encodings.iso2022_jp) +doctest.testmod(encodings.ms932)