[pypy-commit] pypy default: cpyext: Add PyUnicode_*Latin1 functions
amauryfa
noreply at buildbot.pypy.org
Sun Nov 6 20:59:24 CET 2011
Author: Amaury Forgeot d'Arc <amauryfa at gmail.com>
Branch:
Changeset: r48846:051fd7d46101
Date: 2011-10-27 01:06 +0200
http://bitbucket.org/pypy/pypy/changeset/051fd7d46101/
Log: cpyext: Add PyUnicode_*Latin1 functions
diff --git a/pypy/module/cpyext/test/test_unicodeobject.py b/pypy/module/cpyext/test/test_unicodeobject.py
--- a/pypy/module/cpyext/test/test_unicodeobject.py
+++ b/pypy/module/cpyext/test/test_unicodeobject.py
@@ -385,6 +385,24 @@
data, len(u), lltype.nullptr(rffi.CCHARP.TO))
rffi.free_wcharp(data)
+ def test_latin1(self, space, api):
+ s = 'abcdefg'
+ data = rffi.str2charp(s)
+ w_u = api.PyUnicode_DecodeLatin1(data, len(s), lltype.nullptr(rffi.CCHARP.TO))
+ assert space.eq_w(w_u, space.wrap(u"abcdefg"))
+ rffi.free_charp(data)
+
+ uni = u'abcdefg'
+ data = rffi.unicode2wcharp(uni)
+ w_s = api.PyUnicode_EncodeLatin1(data, len(uni), lltype.nullptr(rffi.CCHARP.TO))
+ assert space.eq_w(space.wrap("abcdefg"), w_s)
+ rffi.free_wcharp(data)
+
+ ustr = "abcdef"
+ w_ustr = space.wrap(ustr.decode("ascii"))
+ result = api.PyUnicode_AsLatin1String(w_ustr)
+ assert space.eq_w(space.wrap(ustr), result)
+
def test_format(self, space, api):
w_format = space.wrap(u'hi %s')
w_args = space.wrap((u'test',))
diff --git a/pypy/module/cpyext/unicodeobject.py b/pypy/module/cpyext/unicodeobject.py
--- a/pypy/module/cpyext/unicodeobject.py
+++ b/pypy/module/cpyext/unicodeobject.py
@@ -498,16 +498,16 @@
"""Encode a Unicode object using ASCII and return the result as Python string
object. Error handling is "strict". Return NULL if an exception was raised
by the codec."""
- return space.call_method(w_unicode, 'encode', space.wrap('ascii')) #space.w_None for errors?
+ return space.call_method(w_unicode, 'encode', space.wrap('ascii'))
- at cpython_api([rffi.CCHARP, Py_ssize_t, rffi.CCHARP], PyObject)
+ at cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
def PyUnicode_DecodeASCII(space, s, size, errors):
"""Create a Unicode object by decoding size bytes of the ASCII encoded string
s. Return NULL if an exception was raised by the codec."""
w_s = space.wrap(rffi.charpsize2str(s, size))
return space.call_method(w_s, 'decode', space.wrap('ascii'))
- at cpython_api([rffi.CWCHARP, Py_ssize_t, rffi.CCHARP], PyObject)
+ at cpython_api([CONST_WSTRING, Py_ssize_t, CONST_STRING], PyObject)
def PyUnicode_EncodeASCII(space, s, size, errors):
"""Encode the Py_UNICODE buffer of the given size using ASCII and return a
Python string object. Return NULL if an exception was raised by the codec.
@@ -516,6 +516,33 @@
w_s = space.wrap(rffi.wcharpsize2unicode(s, size))
return space.call_method(w_s, 'encode', space.wrap('ascii'))
+ at cpython_api([PyObject], PyObject)
+def PyUnicode_AsLatin1String(space, w_unicode):
+ """Encode a Unicode object using Latin-1 and return the result as Python string
+ object. Error handling is "strict". Return NULL if an exception was raised
+ by the codec."""
+ return space.call_method(w_unicode, 'encode', space.wrap('latin-1'))
+
+ at cpython_api([CONST_STRING, Py_ssize_t, CONST_STRING], PyObject)
+def PyUnicode_DecodeLatin1(space, s, size, errors):
+ """Create a Unicode object by decoding size bytes of the Latin-1 encoded string
+ s. Return NULL if an exception was raised by the codec.
+
+ This function used an int type for size. This might require
+ changes in your code for properly supporting 64-bit systems."""
+ w_s = space.wrap(rffi.charpsize2str(s, size))
+ return space.call_method(w_s, 'decode', space.wrap('latin-1'))
+
+ at cpython_api([CONST_WSTRING, Py_ssize_t, CONST_STRING], PyObject)
+def PyUnicode_EncodeLatin1(space, s, size, errors):
+ """Encode the Py_UNICODE buffer of the given size using Latin-1 and return
+ a Python string object. Return NULL if an exception was raised by the codec.
+
+ This function used an int type for size. This might require
+ changes in your code for properly supporting 64-bit systems."""
+ w_s = space.wrap(rffi.wcharpsize2unicode(s, size))
+ return space.call_method(w_s, 'encode', space.wrap('latin-1'))
+
if sys.platform == 'win32':
@cpython_api([CONST_WSTRING, Py_ssize_t, CONST_STRING], PyObject)
def PyUnicode_EncodeMBCS(space, wchar_p, length, errors):
More information about the pypy-commit
mailing list