Commit bb09b855 authored by Stefan Behnel's avatar Stefan Behnel

test and fix for unicode.encode() transformation

parent 5b76c0c5
...@@ -788,7 +788,7 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform): ...@@ -788,7 +788,7 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform):
PyUnicode_AsEncodedString_func_type = PyrexTypes.CFuncType( PyUnicode_AsEncodedString_func_type = PyrexTypes.CFuncType(
Builtin.bytes_type, [ Builtin.bytes_type, [
PyrexTypes.CFuncTypeArg("obj", PyrexTypes.py_object_type, None), PyrexTypes.CFuncTypeArg("obj", Builtin.unicode_type, None),
PyrexTypes.CFuncTypeArg("encoding", PyrexTypes.c_char_ptr_type, None), PyrexTypes.CFuncTypeArg("encoding", PyrexTypes.c_char_ptr_type, None),
PyrexTypes.CFuncTypeArg("errors", PyrexTypes.c_char_ptr_type, None), PyrexTypes.CFuncTypeArg("errors", PyrexTypes.c_char_ptr_type, None),
], ],
...@@ -796,7 +796,7 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform): ...@@ -796,7 +796,7 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform):
PyUnicode_AsXyzString_func_type = PyrexTypes.CFuncType( PyUnicode_AsXyzString_func_type = PyrexTypes.CFuncType(
Builtin.bytes_type, [ Builtin.bytes_type, [
PyrexTypes.CFuncTypeArg("obj", PyrexTypes.py_object_type, None), PyrexTypes.CFuncTypeArg("obj", Builtin.unicode_type, None),
], ],
exception_value = "NULL") exception_value = "NULL")
...@@ -880,11 +880,11 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform): ...@@ -880,11 +880,11 @@ class OptimizeBuiltinCalls(Visitor.VisitorTransform):
self.PyUnicode_AsXyzString_func_type, self.PyUnicode_AsXyzString_func_type,
'encode', is_unbound_method, [string_node]) 'encode', is_unbound_method, [string_node])
if len(args) == 2:
return self._substitute_method_call( return self._substitute_method_call(
node, "PyUnicode_AsEncodedString", node, "PyUnicode_AsEncodedString",
self.PyUnicode_AsEncodedString_func_type, self.PyUnicode_AsEncodedString_func_type,
'encode', is_unbound_method, [string_node, encoding_node]) 'encode', is_unbound_method,
[string_node, encoding_node, null_node])
return self._substitute_method_call( return self._substitute_method_call(
node, "PyUnicode_AsEncodedString", node, "PyUnicode_AsEncodedString",
......
# -*- coding: utf-8 -*-
__doc__ = u"""
>>> len(u)
15
>>> default == 'abcdefg'.encode()
True
>>> isinstance(utf8, _bytes)
True
>>> utf8 == u.encode('UTF-8')
True
>>> isinstance(utf8_strict, _bytes)
True
>>> utf8_strict == u.encode('UTF-8', 'strict')
True
>>> isinstance(ascii_replace, _bytes)
True
>>> ascii_replace == u.encode('ASCII', 'replace')
True
>>> isinstance(cp850_strict, _bytes)
True
>>> cp850_strict == u.encode('cp850', 'strict')
True
>>> isinstance(latin1, _bytes)
True
>>> latin1 == u.encode('latin-1')
True
>>> isinstance(latin1_constant, _bytes)
True
>>> latin1_constant == latin1
True
"""
_bytes = bytes
cdef unicode text = u'abcäöüöéèâÁÀABC'
u = text
default = u'abcdefg'.encode()
utf8 = text.encode(u'UTF-8')
utf8_strict = text.encode(u'UTF-8', u'strict')
ascii_replace = text.encode(u'ASCII', u'replace')
cp850_strict = text.encode(u'cp850', u'strict')
latin1 = text.encode(u'latin-1')
latin1_constant = u'abcäöüöéèâÁÀABC'.encode('latin1')
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment