[pypy-svn] r73872 - pypy/trunk/pypy/module/_codecs
benjamin at codespeak.net
benjamin at codespeak.net
Sun Apr 18 06:54:35 CEST 2010
Author: benjamin
Date: Sun Apr 18 06:54:33 2010
New Revision: 73872
Modified:
pypy/trunk/pypy/module/_codecs/__init__.py
pypy/trunk/pypy/module/_codecs/app_codecs.py
pypy/trunk/pypy/module/_codecs/interp_codecs.py
Log:
move some app things around to get _codecs to initialize properly
Modified: pypy/trunk/pypy/module/_codecs/__init__.py
==============================================================================
--- pypy/trunk/pypy/module/_codecs/__init__.py (original)
+++ pypy/trunk/pypy/module/_codecs/__init__.py Sun Apr 18 06:54:33 2010
@@ -1,5 +1,6 @@
from pypy.interpreter.mixedmodule import MixedModule
from pypy.rlib import runicode
+from pypy.module._codecs import interp_codecs
class Module(MixedModule):
appleveldefs = {
@@ -16,7 +17,6 @@
'unicode_internal_encode' : 'app_codecs.unicode_internal_encode',
'utf_7_decode' : 'app_codecs.utf_7_decode',
'utf_7_encode' : 'app_codecs.utf_7_encode',
- '_register_existing_errors': 'app_codecs._register_existing_errors',
'charmap_build' : 'app_codecs.charmap_build'
}
interpleveldefs = {
@@ -57,9 +57,5 @@
MixedModule.__init__(self, space, *args)
- def setup_after_space_initialization(self):
- "NOT_RPYTHON"
- self.space.appexec([], """():
- import _codecs
- _codecs._register_existing_errors()
- """)
+ def startup(self, space):
+ interp_codecs.register_builtin_error_handlers(space)
Modified: pypy/trunk/pypy/module/_codecs/app_codecs.py
==============================================================================
--- pypy/trunk/pypy/module/_codecs/app_codecs.py (original)
+++ pypy/trunk/pypy/module/_codecs/app_codecs.py Sun Apr 18 06:54:33 2010
@@ -214,79 +214,6 @@
res = ''.join(res)
return res, len(res)
-def check_exception(exc):
- try:
- delta = exc.end - exc.start
- if delta < 0 or not isinstance(exc.object, (unicode, str)):
- raise TypeError("wrong exception")
- except AttributeError:
- raise TypeError("wrong exception")
-
-def strict_errors(exc):
- if isinstance(exc, Exception):
- raise exc
- else:
- raise TypeError("codec must pass exception instance")
-
-def ignore_errors(exc):
- check_exception(exc)
- if isinstance(exc, UnicodeEncodeError):
- return u'', exc.end
- elif isinstance(exc, (UnicodeDecodeError, UnicodeTranslateError)):
- return u'', exc.end
- else:
- raise TypeError("don't know how to handle %.400s in error callback"%exc)
-
-Py_UNICODE_REPLACEMENT_CHARACTER = u"\ufffd"
-
-def replace_errors(exc):
- check_exception(exc)
- if isinstance(exc, UnicodeEncodeError):
- return u'?'*(exc.end-exc.start), exc.end
- elif isinstance(exc, (UnicodeTranslateError, UnicodeDecodeError)):
- return Py_UNICODE_REPLACEMENT_CHARACTER*(exc.end-exc.start), exc.end
- else:
- raise TypeError("don't know how to handle %.400s in error callback"%exc)
-
-def xmlcharrefreplace_errors(exc):
- if isinstance(exc, UnicodeEncodeError):
- res = []
- for ch in exc.object[exc.start:exc.end]:
- res += '&#'
- res += str(ord(ch))
- res += ';'
- return u''.join(res), exc.end
- else:
- raise TypeError("don't know how to handle %.400s in error callback"%type(exc))
-
-def backslashreplace_errors(exc):
- if isinstance(exc, UnicodeEncodeError):
- p = []
- for c in exc.object[exc.start:exc.end]:
- p += '\\'
- oc = ord(c)
- if (oc >= 0x00010000):
- p += 'U'
- p += "%.8x" % ord(c)
- elif (oc >= 0x100):
- p += 'u'
- p += "%.4x" % ord(c)
- else:
- p += 'x'
- p += "%.2x" % ord(c)
- return u''.join(p), exc.end
- else:
- raise TypeError("don't know how to handle %.400s in error callback"%type(exc))
-
-
-def _register_existing_errors():
- import _codecs
- _codecs.register_error("strict", strict_errors)
- _codecs.register_error("ignore", ignore_errors)
- _codecs.register_error("replace", replace_errors)
- _codecs.register_error("xmlcharrefreplace", xmlcharrefreplace_errors)
- _codecs.register_error("backslashreplace", backslashreplace_errors)
-
# ----------------------------------------------------------------------
##import sys
Modified: pypy/trunk/pypy/module/_codecs/interp_codecs.py
==============================================================================
--- pypy/trunk/pypy/module/_codecs/interp_codecs.py (original)
+++ pypy/trunk/pypy/module/_codecs/interp_codecs.py Sun Apr 18 06:54:33 2010
@@ -1,5 +1,5 @@
from pypy.interpreter.error import OperationError, operationerrfmt
-from pypy.interpreter.gateway import ObjSpace, NoneNotWrapped
+from pypy.interpreter.gateway import ObjSpace, NoneNotWrapped, applevel
from pypy.interpreter.baseobjspace import W_Root
from pypy.rlib.rstring import StringBuilder, UnicodeBuilder
@@ -107,7 +107,80 @@
space.w_LookupError,
"unknown encoding: %s", encoding)
lookup_codec.unwrap_spec = [ObjSpace, str]
-
+
+app_errors = applevel("""
+def check_exception(exc):
+ try:
+ delta = exc.end - exc.start
+ if delta < 0 or not isinstance(exc.object, (unicode, str)):
+ raise TypeError("wrong exception")
+ except AttributeError:
+ raise TypeError("wrong exception")
+
+def strict_errors(exc):
+ if isinstance(exc, Exception):
+ raise exc
+ else:
+ raise TypeError("codec must pass exception instance")
+
+def ignore_errors(exc):
+ check_exception(exc)
+ if isinstance(exc, UnicodeEncodeError):
+ return u'', exc.end
+ elif isinstance(exc, (UnicodeDecodeError, UnicodeTranslateError)):
+ return u'', exc.end
+ else:
+ raise TypeError("don't know how to handle %.400s in error callback"%exc)
+
+Py_UNICODE_REPLACEMENT_CHARACTER = u"\ufffd"
+
+def replace_errors(exc):
+ check_exception(exc)
+ if isinstance(exc, UnicodeEncodeError):
+ return u'?'*(exc.end-exc.start), exc.end
+ elif isinstance(exc, (UnicodeTranslateError, UnicodeDecodeError)):
+ return Py_UNICODE_REPLACEMENT_CHARACTER*(exc.end-exc.start), exc.end
+ else:
+ raise TypeError("don't know how to handle %.400s in error callback"%exc)
+
+def xmlcharrefreplace_errors(exc):
+ if isinstance(exc, UnicodeEncodeError):
+ res = []
+ for ch in exc.object[exc.start:exc.end]:
+ res += '&#'
+ res += str(ord(ch))
+ res += ';'
+ return u''.join(res), exc.end
+ else:
+ raise TypeError("don't know how to handle %.400s in error callback"%type(exc))
+
+def backslashreplace_errors(exc):
+ if isinstance(exc, UnicodeEncodeError):
+ p = []
+ for c in exc.object[exc.start:exc.end]:
+ p += '\\\\'
+ oc = ord(c)
+ if (oc >= 0x00010000):
+ p += 'U'
+ p += "%.8x" % ord(c)
+ elif (oc >= 0x100):
+ p += 'u'
+ p += "%.4x" % ord(c)
+ else:
+ p += 'x'
+ p += "%.2x" % ord(c)
+ return u''.join(p), exc.end
+ else:
+ raise TypeError("don't know how to handle %.400s in error callback"%type(exc))
+""")
+
+def register_builtin_error_handlers(space):
+ state = space.fromcache(CodecState)
+ for error in ("strict", "ignore", "replace", "xmlcharrefreplace",
+ "backslashreplace"):
+ name = error + "_errors"
+ state.codec_error_registry[error] = app_errors.wget(space, name)
+
def lookup_error(space, errors):
"""lookup_error(errors) -> handler
More information about the Pypy-commit
mailing list