Commit 1fa8e88f authored by Kevin Modzelewski's avatar Kevin Modzelewski

Allow "".decode()

parent b51c4bcf
......@@ -206,3 +206,65 @@ string_splitlines(PyStringObject *self, PyObject *args)
keepends
);
}
PyObject *PyString_AsDecodedObject(PyObject *str,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyString_Check(str)) {
PyErr_BadArgument();
goto onError;
}
if (encoding == NULL) {
#ifdef Py_USING_UNICODE
encoding = PyUnicode_GetDefaultEncoding();
#else
PyErr_SetString(PyExc_ValueError, "no encoding specified");
goto onError;
#endif
}
/* Decode via the codec registry */
v = PyCodec_Decode(str, encoding, errors);
if (v == NULL)
goto onError;
return v;
onError:
return NULL;
}
PyObject *PyString_AsEncodedObject(PyObject *str,
const char *encoding,
const char *errors)
{
PyObject *v;
if (!PyString_Check(str)) {
PyErr_BadArgument();
goto onError;
}
if (encoding == NULL) {
#ifdef Py_USING_UNICODE
encoding = PyUnicode_GetDefaultEncoding();
#else
PyErr_SetString(PyExc_ValueError, "no encoding specified");
goto onError;
#endif
}
/* Encode via the codec registry */
v = PyCodec_Encode(str, encoding, errors);
if (v == NULL)
goto onError;
return v;
onError:
return NULL;
}
......@@ -2194,8 +2194,8 @@ Box* strDecode(BoxedString* self, Box* encoding, Box* error) {
if (error_str && !isSubclass(error_str->cls, str_cls))
raiseExcHelper(TypeError, "decode() argument 2 must be string, not '%s'", getTypeName(error_str));
Box* result
= PyCodec_Decode(self, encoding_str ? encoding_str->data() : NULL, error_str ? error_str->data() : NULL);
Box* result = PyString_AsDecodedObject(self, encoding_str ? encoding_str->data() : NULL,
error_str ? error_str->data() : NULL);
checkAndThrowCAPIException();
return result;
}
......@@ -2219,7 +2219,7 @@ Box* strEncode(BoxedString* self, Box* encoding, Box* error) {
if (error_str && !isSubclass(error_str->cls, str_cls))
raiseExcHelper(TypeError, "encode() argument 2 must be string, not '%s'", getTypeName(error_str));
Box* result = PyCodec_Encode(self, encoding_str ? encoding_str->data() : PyUnicode_GetDefaultEncoding(),
Box* result = PyString_AsEncodedObject(self, encoding_str ? encoding_str->data() : PyUnicode_GetDefaultEncoding(),
error_str ? error_str->data() : NULL);
checkAndThrowCAPIException();
return result;
......
......@@ -7,3 +7,7 @@ test("hello world", "hex")
test("hello world", "base64")
test("\r\n\\", "string-escape")
"".encode()
"".decode()
u"".encode()
u"".decode()
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