summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2016-04-13 12:37:23 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2016-04-13 12:37:23 (GMT)
commit21a663ea2829b6808dd6981904c393332d271f8e (patch)
treede7302e8c98b9b075236c6e52f006835d01562e2
parent131b8f8eee3498d5d334bde9671825bdfe0cf222 (diff)
downloadcpython-21a663ea2829b6808dd6981904c393332d271f8e.zip
cpython-21a663ea2829b6808dd6981904c393332d271f8e.tar.gz
cpython-21a663ea2829b6808dd6981904c393332d271f8e.tar.bz2
Issue #26057: Got rid of nonneeded use of PyUnicode_FromObject().
-rw-r--r--Modules/_codecsmodule.c231
-rw-r--r--Modules/socketmodule.c44
-rw-r--r--Objects/stringlib/find.h13
-rw-r--r--Objects/unicodeobject.c562
-rw-r--r--Python/bltinmodule.c5
-rw-r--r--Python/getargs.c26
6 files changed, 240 insertions, 641 deletions
diff --git a/Modules/_codecsmodule.c b/Modules/_codecsmodule.c
index 7575773..1951da9 100644
--- a/Modules/_codecsmodule.c
+++ b/Modules/_codecsmodule.c
@@ -20,10 +20,6 @@
<encoding>_decode(char_buffer_obj[,errors='strict']) ->
(Unicode object, bytes consumed)
- <encoding>_encode() interfaces also accept non-Unicode object as
- input. The objects are then converted to Unicode using
- PyUnicode_FromObject() prior to applying the conversion.
-
These <encoding>s are available: utf_8, unicode_escape,
raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit),
mbcs (on win32).
@@ -718,7 +714,7 @@ _codecs_unicode_internal_encode_impl(PyModuleDef *module, PyObject *obj,
/*[clinic input]
_codecs.utf_7_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -728,22 +724,13 @@ _codecs_utf_7_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=a7accc496a32b759 input=fd91a78f103b0421]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_8_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -753,17 +740,8 @@ _codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ec831d80e7aedede input=2c22d40532f071f3]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsUTF8String(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/* This version provides access to the byteorder parameter of the
@@ -775,7 +753,7 @@ _codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str,
/*[clinic input]
_codecs.utf_16_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
@@ -786,22 +764,13 @@ _codecs_utf_16_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, int byteorder)
/*[clinic end generated code: output=93ac58e960a9ee4d input=3935a489b2d5385e]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_16_le_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -811,22 +780,13 @@ _codecs_utf_16_le_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=422bedb8da34fb66 input=bc27df05d1d20dfe]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_16_be_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -836,17 +796,8 @@ _codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=3aa7ee9502acdd77 input=5a69d4112763462b]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
+ PyUnicode_GET_LENGTH(str));
}
/* This version provides access to the byteorder parameter of the
@@ -858,7 +809,7 @@ _codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str,
/*[clinic input]
_codecs.utf_32_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
byteorder: int = 0
/
@@ -869,22 +820,13 @@ _codecs_utf_32_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, int byteorder)
/*[clinic end generated code: output=3e7d5a003b02baed input=434a1efa492b8d58]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_32_le_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -894,22 +836,13 @@ _codecs_utf_32_le_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=5dda641cd33dbfc2 input=dfa2d7dc78b99422]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.utf_32_be_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -919,22 +852,13 @@ _codecs_utf_32_be_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ccca8b44d91a7c7a input=4595617b18169002]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.unicode_escape_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -944,22 +868,13 @@ _codecs_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=389f23d2b8f8d80b input=8273506f14076912]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.raw_unicode_escape_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -969,22 +884,13 @@ _codecs_raw_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=fec4e39d6ec37a62 input=181755d5dfacef3c]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.latin_1_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -994,22 +900,13 @@ _codecs_latin_1_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=ecf00eb8e48c889c input=f03f6dcf1d84bee4]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsLatin1String(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.ascii_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -1019,22 +916,13 @@ _codecs_ascii_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=a9d18fc6b6b91cfb input=d87e25a10a593fee]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_AsASCIIString(str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.charmap_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
mapping: object = NULL
/
@@ -1045,20 +933,11 @@ _codecs_charmap_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors, PyObject *mapping)
/*[clinic end generated code: output=14ca42b83853c643 input=85f4172661e8dad9]*/
{
- PyObject *v;
-
if (mapping == Py_None)
mapping = NULL;
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
@@ -1078,7 +957,7 @@ _codecs_charmap_build_impl(PyModuleDef *module, PyObject *map)
/*[clinic input]
_codecs.mbcs_encode
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -1088,23 +967,14 @@ _codecs_mbcs_encode_impl(PyModuleDef *module, PyObject *str,
const char *errors)
/*[clinic end generated code: output=d1a013bc68798bd7 input=65c09ee1e4203263]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
+ PyUnicode_GET_LENGTH(str));
}
/*[clinic input]
_codecs.code_page_encode
code_page: int
- str: object
+ str: unicode
errors: str(accept={str, NoneType}) = NULL
/
[clinic start generated code]*/
@@ -1114,19 +984,8 @@ _codecs_code_page_encode_impl(PyModuleDef *module, int code_page,
PyObject *str, const char *errors)
/*[clinic end generated code: output=3b406618dbfbce25 input=c8562ec460c2e309]*/
{
- PyObject *v;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL || PyUnicode_READY(str) < 0) {
- Py_XDECREF(str);
- return NULL;
- }
- v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
- str,
- errors),
- PyUnicode_GET_LENGTH(str));
- Py_DECREF(str);
- return v;
+ return codec_tuple(PyUnicode_EncodeCodePage(code_page, str, errors),
+ PyUnicode_GET_LENGTH(str));
}
#endif /* HAVE_MBCS */
diff --git a/Modules/socketmodule.c b/Modules/socketmodule.c
index bcff004..ec35fb9 100644
--- a/Modules/socketmodule.c
+++ b/Modules/socketmodule.c
@@ -1401,7 +1401,7 @@ static int
idna_converter(PyObject *obj, struct maybe_idna *data)
{
size_t len;
- PyObject *obj2, *obj3;
+ PyObject *obj2;
if (obj == NULL) {
idna_cleanup(data);
return 1;
@@ -1416,31 +1416,27 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
data->buf = PyByteArray_AsString(obj);
len = PyByteArray_Size(obj);
}
- else if (PyUnicode_Check(obj) && PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
- data->buf = PyUnicode_DATA(obj);
- len = PyUnicode_GET_LENGTH(obj);
- }
- else {
- obj2 = PyUnicode_FromObject(obj);
- if (!obj2) {
- PyErr_Format(PyExc_TypeError, "string or unicode text buffer expected, not %s",
- obj->ob_type->tp_name);
- return 0;
+ else if (PyUnicode_Check(obj)) {
+ if (PyUnicode_READY(obj) == 0 && PyUnicode_IS_COMPACT_ASCII(obj)) {
+ data->buf = PyUnicode_DATA(obj);
+ len = PyUnicode_GET_LENGTH(obj);
}
- obj3 = PyUnicode_AsEncodedString(obj2, "idna", NULL);
- Py_DECREF(obj2);
- if (!obj3) {
- PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
- return 0;
- }
- if (!PyBytes_Check(obj3)) {
- Py_DECREF(obj3);
- PyErr_SetString(PyExc_TypeError, "encoding of hostname failed to return bytes");
- return 0;
+ else {
+ obj2 = PyUnicode_AsEncodedString(obj, "idna", NULL);
+ if (!obj2) {
+ PyErr_SetString(PyExc_TypeError, "encoding of hostname failed");
+ return 0;
+ }
+ assert(PyBytes_Check(obj2));
+ data->obj = obj2;
+ data->buf = PyBytes_AS_STRING(obj2);
+ len = PyBytes_GET_SIZE(obj2);
}
- data->obj = obj3;
- data->buf = PyBytes_AS_STRING(obj3);
- len = PyBytes_GET_SIZE(obj3);
+ }
+ else {
+ PyErr_Format(PyExc_TypeError, "str, bytes or bytearray expected, not %s",
+ obj->ob_type->tp_name);
+ return 0;
}
if (strlen(data->buf) != len) {
Py_CLEAR(data->obj);
diff --git a/Objects/stringlib/find.h b/Objects/stringlib/find.h
index 14815f6..a7065fc 100644
--- a/Objects/stringlib/find.h
+++ b/Objects/stringlib/find.h
@@ -123,11 +123,6 @@ STRINGLIB(parse_args_finds)(const char * function_name, PyObject *args,
/*
Wraps stringlib_parse_args_finds() and additionally ensures that the
first argument is a unicode object.
-
-Note that we receive a pointer to the pointer of the substring object,
-so when we create that object in this function we don't DECREF it,
-because it continues living in the caller functions (those functions,
-after finishing using the substring, must DECREF it).
*/
Py_LOCAL_INLINE(int)
@@ -135,14 +130,10 @@ STRINGLIB(parse_args_finds_unicode)(const char * function_name, PyObject *args,
PyObject **substring,
Py_ssize_t *start, Py_ssize_t *end)
{
- PyObject *tmp_substring;
-
- if(STRINGLIB(parse_args_finds)(function_name, args, &tmp_substring,
+ if(STRINGLIB(parse_args_finds)(function_name, args, substring,
start, end)) {
- tmp_substring = PyUnicode_FromObject(tmp_substring);
- if (!tmp_substring)
+ if (ensure_unicode(*substring) < 0)
return 0;
- *substring = tmp_substring;
return 1;
}
return 0;
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index 8dc2a38..d3bb13a 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -751,6 +751,18 @@ make_bloom_mask(int kind, void* ptr, Py_ssize_t len)
#undef BLOOM_UPDATE
}
+static int
+ensure_unicode(PyObject *obj)
+{
+ if (!PyUnicode_Check(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "must be str, not %.100s",
+ Py_TYPE(obj)->tp_name);
+ return -1;
+ }
+ return PyUnicode_READY(obj);
+}
+
/* Compilation of templated routines */
#include "stringlib/asciilib.h"
@@ -3066,7 +3078,7 @@ PyUnicode_FromEncodedObject(PyObject *obj,
/* Retrieve a bytes buffer view through the PEP 3118 buffer interface */
if (PyObject_GetBuffer(obj, &buffer, PyBUF_SIMPLE) < 0) {
PyErr_Format(PyExc_TypeError,
- "coercing to str: need a bytes-like object, %.80s found",
+ "decoding to str: need a bytes-like object, %.80s found",
Py_TYPE(obj)->tp_name);
return NULL;
}
@@ -3787,19 +3799,17 @@ PyUnicode_FSConverter(PyObject* arg, void* addr)
output = arg;
Py_INCREF(output);
}
- else {
- arg = PyUnicode_FromObject(arg);
- if (!arg)
- return 0;
+ else if (PyUnicode_Check(arg)) {
output = PyUnicode_EncodeFSDefault(arg);
- Py_DECREF(arg);
if (!output)
return 0;
- if (!PyBytes_Check(output)) {
- Py_DECREF(output);
- PyErr_SetString(PyExc_TypeError, "encoder failed to return bytes");
- return 0;
- }
+ assert(PyBytes_Check(output));
+ }
+ else {
+ PyErr_Format(PyExc_TypeError,
+ "must be str or bytes, not %.100s",
+ Py_TYPE(arg)->tp_name);
+ return 0;
}
size = PyBytes_GET_SIZE(output);
data = PyBytes_AS_STRING(output);
@@ -3871,7 +3881,7 @@ PyUnicode_AsUTF8AndSize(PyObject *unicode, Py_ssize_t *psize)
if (PyUnicode_UTF8(unicode) == NULL) {
assert(!PyUnicode_IS_COMPACT_ASCII(unicode));
- bytes = _PyUnicode_AsUTF8String(unicode, "strict");
+ bytes = _PyUnicode_AsUTF8String(unicode, NULL);
if (bytes == NULL)
return NULL;
_PyUnicode_UTF8(unicode) = PyObject_MALLOC(PyBytes_GET_SIZE(bytes) + 1);
@@ -8860,10 +8870,8 @@ _PyUnicode_TranslateCharmap(PyObject *input,
kind = PyUnicode_KIND(input);
size = PyUnicode_GET_LENGTH(input);
- if (size == 0) {
- Py_INCREF(input);
- return input;
- }
+ if (size == 0)
+ return PyUnicode_FromObject(input);
/* allocate enough for a simple 1:1 translation without
replacements, if we need more, we'll resize */
@@ -8974,14 +8982,9 @@ PyUnicode_Translate(PyObject *str,
PyObject *mapping,
const char *errors)
{
- PyObject *result;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL)
+ if (ensure_unicode(str) < 0)
return NULL;
- result = _PyUnicode_TranslateCharmap(str, mapping, errors);
- Py_DECREF(str);
- return result;
+ return _PyUnicode_TranslateCharmap(str, mapping, errors);
}
static Py_UCS4
@@ -9163,9 +9166,10 @@ PyUnicode_EncodeDecimal(Py_UNICODE *s,
}
static Py_ssize_t
-any_find_slice(int direction, PyObject* s1, PyObject* s2,
+any_find_slice(PyObject* s1, PyObject* s2,
Py_ssize_t start,
- Py_ssize_t end)
+ Py_ssize_t end,
+ int direction)
{
int kind1, kind2;
void *buf1, *buf2;
@@ -9334,54 +9338,35 @@ PyUnicode_Count(PyObject *str,
Py_ssize_t end)
{
Py_ssize_t result;
- PyObject* str_obj;
- PyObject* sub_obj;
int kind1, kind2;
void *buf1 = NULL, *buf2 = NULL;
Py_ssize_t len1, len2;
- str_obj = PyUnicode_FromObject(str);
- if (!str_obj)
- return -1;
- sub_obj = PyUnicode_FromObject(substr);
- if (!sub_obj) {
- Py_DECREF(str_obj);
+ if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
return -1;
- }
- if (PyUnicode_READY(sub_obj) == -1 || PyUnicode_READY(str_obj) == -1) {
- Py_DECREF(sub_obj);
- Py_DECREF(str_obj);
- return -1;
- }
- kind1 = PyUnicode_KIND(str_obj);
- kind2 = PyUnicode_KIND(sub_obj);
- if (kind1 < kind2) {
- Py_DECREF(sub_obj);
- Py_DECREF(str_obj);
+ kind1 = PyUnicode_KIND(str);
+ kind2 = PyUnicode_KIND(substr);
+ if (kind1 < kind2)
return 0;
- }
- len1 = PyUnicode_GET_LENGTH(str_obj);
- len2 = PyUnicode_GET_LENGTH(sub_obj);
+ len1 = PyUnicode_GET_LENGTH(str);
+ len2 = PyUnicode_GET_LENGTH(substr);
ADJUST_INDICES(start, end, len1);
- if (end - start < len2) {
- Py_DECREF(sub_obj);
- Py_DECREF(str_obj);
+ if (end - start < len2)
return 0;
- }
- buf1 = PyUnicode_DATA(str_obj);
- buf2 = PyUnicode_DATA(sub_obj);
+ buf1 = PyUnicode_DATA(str);
+ buf2 = PyUnicode_DATA(substr);
if (kind2 != kind1) {
- buf2 = _PyUnicode_AsKind(sub_obj, kind1);
+ buf2 = _PyUnicode_AsKind(substr, kind1);
if (!buf2)
goto onError;
}
switch (kind1) {
case PyUnicode_1BYTE_KIND:
- if (PyUnicode_IS_ASCII(str_obj) && PyUnicode_IS_ASCII(sub_obj))
+ if (PyUnicode_IS_ASCII(str) && PyUnicode_IS_ASCII(substr))
result = asciilib_count(
((Py_UCS1*)buf1) + start, end - start,
buf2, len2, PY_SSIZE_T_MAX
@@ -9408,16 +9393,11 @@ PyUnicode_Count(PyObject *str,
assert(0); result = 0;
}
- Py_DECREF(sub_obj);
- Py_DECREF(str_obj);
-
if (kind2 != kind1)
PyMem_Free(buf2);
return result;
onError:
- Py_DECREF(sub_obj);
- Py_DECREF(str_obj);
if (kind2 != kind1 && buf2)
PyMem_Free(buf2);
return -1;
@@ -9425,35 +9405,15 @@ PyUnicode_Count(PyObject *str,
Py_ssize_t
PyUnicode_Find(PyObject *str,
- PyObject *sub,
+ PyObject *substr,
Py_ssize_t start,
Py_ssize_t end,
int direction)
{
- Py_ssize_t result;
-
- str = PyUnicode_FromObject(str);
- if (!str)
- return -2;
- sub = PyUnicode_FromObject(sub);
- if (!sub) {
- Py_DECREF(str);
+ if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
return -2;
- }
- if (PyUnicode_READY(sub) == -1 || PyUnicode_READY(str) == -1) {
- Py_DECREF(sub);
- Py_DECREF(str);
- return -2;
- }
- result = any_find_slice(direction,
- str, sub, start, end
- );
-
- Py_DECREF(str);
- Py_DECREF(sub);
-
- return result;
+ return any_find_slice(str, substr, start, end, direction);
}
Py_ssize_t
@@ -9556,22 +9516,10 @@ PyUnicode_Tailmatch(PyObject *str,
Py_ssize_t end,
int direction)
{
- Py_ssize_t result;
-
- str = PyUnicode_FromObject(str);
- if (str == NULL)
+ if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0)
return -1;
- substr = PyUnicode_FromObject(substr);
- if (substr == NULL) {
- Py_DECREF(str);
- return -1;
- }
- result = tailmatch(str, substr,
- start, end, direction);
- Py_DECREF(str);
- Py_DECREF(substr);
- return result;
+ return tailmatch(str, substr, start, end, direction);
}
/* Apply fixfct filter to the Unicode object self and return a
@@ -10177,13 +10125,8 @@ PyUnicode_Splitlines(PyObject *string, int keepends)
{
PyObject *list;
- string = PyUnicode_FromObject(string);
- if (string == NULL)
+ if (ensure_unicode(string) < 0)
return NULL;
- if (PyUnicode_READY(string) == -1) {
- Py_DECREF(string);
- return NULL;
- }
switch (PyUnicode_KIND(string)) {
case PyUnicode_1BYTE_KIND:
@@ -10210,7 +10153,6 @@ PyUnicode_Splitlines(PyObject *string, int keepends)
assert(0);
list = 0;
}
- Py_DECREF(string);
return list;
}
@@ -10771,28 +10713,27 @@ unicode_casefold(PyObject *self)
}
-/* Argument converter. Coerces to a single unicode character */
+/* Argument converter. Accepts a single Unicode character. */
static int
convert_uc(PyObject *obj, void *addr)
{
Py_UCS4 *fillcharloc = (Py_UCS4 *)addr;
- PyObject *uniobj;
- uniobj = PyUnicode_FromObject(obj);
- if (uniobj == NULL) {
- PyErr_SetString(PyExc_TypeError,
- "The fill character cannot be converted to Unicode");
+ if (!PyUnicode_Check(obj)) {
+ PyErr_Format(PyExc_TypeError,
+ "The fill character must be a unicode character, "
+ "not %.100s", Py_TYPE(obj)->tp_name);
return 0;
}
- if (PyUnicode_GET_LENGTH(uniobj) != 1) {
+ if (PyUnicode_READY(obj) < 0)
+ return 0;
+ if (PyUnicode_GET_LENGTH(obj) != 1) {
PyErr_SetString(PyExc_TypeError,
"The fill character must be exactly one character long");
- Py_DECREF(uniobj);
return 0;
}
- *fillcharloc = PyUnicode_READ_CHAR(uniobj, 0);
- Py_DECREF(uniobj);
+ *fillcharloc = PyUnicode_READ_CHAR(obj, 0);
return 1;
}
@@ -11114,59 +11055,43 @@ _PyUnicode_EQ(PyObject *aa, PyObject *bb)
}
int
-PyUnicode_Contains(PyObject *container, PyObject *element)
+PyUnicode_Contains(PyObject *str, PyObject *substr)
{
- PyObject *str, *sub;
int kind1, kind2;
void *buf1, *buf2;
Py_ssize_t len1, len2;
int result;
- /* Coerce the two arguments */
- sub = PyUnicode_FromObject(element);
- if (!sub) {
+ if (!PyUnicode_Check(substr)) {
PyErr_Format(PyExc_TypeError,
- "'in <string>' requires string as left operand, not %s",
- element->ob_type->tp_name);
+ "'in <string>' requires string as left operand, not %.100s",
+ Py_TYPE(substr)->tp_name);
return -1;
}
-
- str = PyUnicode_FromObject(container);
- if (!str) {
- Py_DECREF(sub);
+ if (PyUnicode_READY(substr) == -1)
+ return -1;
+ if (ensure_unicode(str) < 0)
return -1;
- }
kind1 = PyUnicode_KIND(str);
- kind2 = PyUnicode_KIND(sub);
- if (kind1 < kind2) {
- Py_DECREF(sub);
- Py_DECREF(str);
+ kind2 = PyUnicode_KIND(substr);
+ if (kind1 < kind2)
return 0;
- }
len1 = PyUnicode_GET_LENGTH(str);
- len2 = PyUnicode_GET_LENGTH(sub);
- if (len1 < len2) {
- Py_DECREF(sub);
- Py_DECREF(str);
+ len2 = PyUnicode_GET_LENGTH(substr);
+ if (len1 < len2)
return 0;
- }
buf1 = PyUnicode_DATA(str);
- buf2 = PyUnicode_DATA(sub);
+ buf2 = PyUnicode_DATA(substr);
if (len2 == 1) {
Py_UCS4 ch = PyUnicode_READ(kind2, buf2, 0);
result = findchar((const char *)buf1, kind1, len1, ch, 1) != -1;
- Py_DECREF(sub);
- Py_DECREF(str);
return result;
}
if (kind2 != kind1) {
- buf2 = _PyUnicode_AsKind(sub, kind1);
- if (!buf2) {
- Py_DECREF(sub);
- Py_DECREF(str);
+ buf2 = _PyUnicode_AsKind(substr, kind1);
+ if (!buf2)
return -1;
- }
}
switch (kind1) {
@@ -11184,9 +11109,6 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
assert(0);
}
- Py_DECREF(str);
- Py_DECREF(sub);
-
if (kind2 != kind1)
PyMem_Free(buf2);
@@ -11198,56 +11120,40 @@ PyUnicode_Contains(PyObject *container, PyObject *element)
PyObject *
PyUnicode_Concat(PyObject *left, PyObject *right)
{
- PyObject *u = NULL, *v = NULL, *w;
+ PyObject *result;
Py_UCS4 maxchar, maxchar2;
- Py_ssize_t u_len, v_len, new_len;
+ Py_ssize_t left_len, right_len, new_len;
- /* Coerce the two arguments */
- u = PyUnicode_FromObject(left);
- if (u == NULL)
- goto onError;
- v = PyUnicode_FromObject(right);
- if (v == NULL)
- goto onError;
+ if (ensure_unicode(left) < 0 || ensure_unicode(right) < 0)
+ return NULL;
/* Shortcuts */
- if (v == unicode_empty) {
- Py_DECREF(v);
- return u;
- }
- if (u == unicode_empty) {
- Py_DECREF(u);
- return v;
- }
+ if (left == unicode_empty)
+ return PyUnicode_FromObject(right);
+ if (right == unicode_empty)
+ return PyUnicode_FromObject(left);
- u_len = PyUnicode_GET_LENGTH(u);
- v_len = PyUnicode_GET_LENGTH(v);
- if (u_len > PY_SSIZE_T_MAX - v_len) {
+ left_len = PyUnicode_GET_LENGTH(left);
+ right_len = PyUnicode_GET_LENGTH(right);
+ if (left_len > PY_SSIZE_T_MAX - right_len) {
PyErr_SetString(PyExc_OverflowError,
"strings are too large to concat");
- goto onError;
+ return NULL;
}
- new_len = u_len + v_len;
+ new_len = left_len + right_len;
- maxchar = PyUnicode_MAX_CHAR_VALUE(u);
- maxchar2 = PyUnicode_MAX_CHAR_VALUE(v);
+ maxchar = PyUnicode_MAX_CHAR_VALUE(left);
+ maxchar2 = PyUnicode_MAX_CHAR_VALUE(right);
maxchar = Py_MAX(maxchar, maxchar2);
/* Concat the two Unicode strings */
- w = PyUnicode_New(new_len, maxchar);
- if (w == NULL)
- goto onError;
- _PyUnicode_FastCopyCharacters(w, 0, u, 0, u_len);
- _PyUnicode_FastCopyCharacters(w, u_len, v, 0, v_len);
- Py_DECREF(u);
- Py_DECREF(v);
- assert(_PyUnicode_CheckConsistency(w, 1));
- return w;
-
- onError:
- Py_XDECREF(u);
- Py_XDECREF(v);
- return NULL;
+ result = PyUnicode_New(new_len, maxchar);
+ if (result == NULL)
+ return NULL;
+ _PyUnicode_FastCopyCharacters(result, 0, left, 0, left_len);
+ _PyUnicode_FastCopyCharacters(result, left_len, right, 0, right_len);
+ assert(_PyUnicode_CheckConsistency(result, 1));
+ return result;
}
void
@@ -11362,25 +11268,21 @@ unicode_count(PyObject *self, PyObject *args)
kind1 = PyUnicode_KIND(self);
kind2 = PyUnicode_KIND(substring);
- if (kind1 < kind2) {
- Py_DECREF(substring);
+ if (kind1 < kind2)
return PyLong_FromLong(0);
- }
+
len1 = PyUnicode_GET_LENGTH(self);
len2 = PyUnicode_GET_LENGTH(substring);
ADJUST_INDICES(start, end, len1);
- if (end - start < len2) {
- Py_DECREF(substring);
+ if (end - start < len2)
return PyLong_FromLong(0);
- }
+
buf1 = PyUnicode_DATA(self);
buf2 = PyUnicode_DATA(substring);
if (kind2 != kind1) {
buf2 = _PyUnicode_AsKind(substring, kind1);
- if (!buf2) {
- Py_DECREF(substring);
+ if (!buf2)
return NULL;
- }
}
switch (kind1) {
case PyUnicode_1BYTE_KIND:
@@ -11410,8 +11312,6 @@ unicode_count(PyObject *self, PyObject *args)
if (kind2 != kind1)
PyMem_Free(buf2);
- Py_DECREF(substring);
-
return result;
}
@@ -11549,18 +11449,10 @@ unicode_find(PyObject *self, PyObject *args)
&start, &end))
return NULL;
- if (PyUnicode_READY(self) == -1) {
- Py_DECREF(substring);
- return NULL;
- }
- if (PyUnicode_READY(substring) == -1) {
- Py_DECREF(substring);
+ if (PyUnicode_READY(self) == -1)
return NULL;
- }
-
- result = any_find_slice(1, self, substring, start, end);
- Py_DECREF(substring);
+ result = any_find_slice(self, substring, start, end, 1);
if (result == -2)
return NULL;
@@ -11637,18 +11529,10 @@ unicode_index(PyObject *self, PyObject *args)
&start, &end))
return NULL;
- if (PyUnicode_READY(self) == -1) {
- Py_DECREF(substring);
- return NULL;
- }
- if (PyUnicode_READY(substring) == -1) {
- Py_DECREF(substring);
+ if (PyUnicode_READY(self) == -1)
return NULL;
- }
- result = any_find_slice(1, self, substring, start, end);
-
- Py_DECREF(substring);
+ result = any_find_slice(self, substring, start, end, 1);
if (result == -2)
return NULL;
@@ -12457,40 +12341,15 @@ unicode_repeat(PyObject *str, Py_ssize_t len)
}
PyObject *
-PyUnicode_Replace(PyObject *obj,
- PyObject *subobj,
- PyObject *replobj,
+PyUnicode_Replace(PyObject *str,
+ PyObject *substr,
+ PyObject *replstr,
Py_ssize_t maxcount)
{
- PyObject *self;
- PyObject *str1;
- PyObject *str2;
- PyObject *result;
-
- self = PyUnicode_FromObject(obj);
- if (self == NULL)
- return NULL;
- str1 = PyUnicode_FromObject(subobj);
- if (str1 == NULL) {
- Py_DECREF(self);
- return NULL;
- }
- str2 = PyUnicode_FromObject(replobj);
- if (str2 == NULL) {
- Py_DECREF(self);
- Py_DECREF(str1);
+ if (ensure_unicode(str) < 0 || ensure_unicode(substr) < 0 ||
+ ensure_unicode(replstr) < 0)
return NULL;
- }
- if (PyUnicode_READY(self) == -1 ||
- PyUnicode_READY(str1) == -1 ||
- PyUnicode_READY(str2) == -1)
- result = NULL;
- else
- result = replace(self, str1, str2, maxcount);
- Py_DECREF(self);
- Py_DECREF(str1);
- Py_DECREF(str2);
- return result;
+ return replace(str, substr, replstr, maxcount);
}
PyDoc_STRVAR(replace__doc__,
@@ -12506,28 +12365,12 @@ unicode_replace(PyObject *self, PyObject *args)
PyObject *str1;
PyObject *str2;
Py_ssize_t maxcount = -1;
- PyObject *result;
- if (!PyArg_ParseTuple(args, "OO|n:replace", &str1, &str2, &maxcount))
+ if (!PyArg_ParseTuple(args, "UU|n:replace", &str1, &str2, &maxcount))
return NULL;
if (PyUnicode_READY(self) == -1)
return NULL;
- str1 = PyUnicode_FromObject(str1);
- if (str1 == NULL)
- return NULL;
- str2 = PyUnicode_FromObject(str2);
- if (str2 == NULL) {
- Py_DECREF(str1);
- return NULL;
- }
- if (PyUnicode_READY(str1) == -1 || PyUnicode_READY(str2) == -1)
- result = NULL;
- else
- result = replace(self, str1, str2, maxcount);
-
- Py_DECREF(str1);
- Py_DECREF(str2);
- return result;
+ return replace(self, str1, str2, maxcount);
}
static PyObject *
@@ -12716,18 +12559,10 @@ unicode_rfind(PyObject *self, PyObject *args)
&start, &end))
return NULL;
- if (PyUnicode_READY(self) == -1) {
- Py_DECREF(substring);
- return NULL;
- }
- if (PyUnicode_READY(substring) == -1) {
- Py_DECREF(substring);
+ if (PyUnicode_READY(self) == -1)
return NULL;
- }
-
- result = any_find_slice(-1, self, substring, start, end);
- Py_DECREF(substring);
+ result = any_find_slice(self, substring, start, end, -1);
if (result == -2)
return NULL;
@@ -12753,18 +12588,10 @@ unicode_rindex(PyObject *self, PyObject *args)
&start, &end))
return NULL;
- if (PyUnicode_READY(self) == -1) {
- Py_DECREF(substring);
- return NULL;
- }
- if (PyUnicode_READY(substring) == -1) {
- Py_DECREF(substring);
+ if (PyUnicode_READY(self) == -1)
return NULL;
- }
-
- result = any_find_slice(-1, self, substring, start, end);
- Py_DECREF(substring);
+ result = any_find_slice(self, substring, start, end, -1);
if (result == -2)
return NULL;
@@ -12804,24 +12631,10 @@ unicode_rjust(PyObject *self, PyObject *args)
PyObject *
PyUnicode_Split(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
{
- PyObject *result;
-
- s = PyUnicode_FromObject(s);
- if (s == NULL)
+ if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0))
return NULL;
- if (sep != NULL) {
- sep = PyUnicode_FromObject(sep);
- if (sep == NULL) {
- Py_DECREF(s);
- return NULL;
- }
- }
- result = split(s, sep, maxsplit);
-
- Py_DECREF(s);
- Py_XDECREF(sep);
- return result;
+ return split(s, sep, maxsplit);
}
PyDoc_STRVAR(split__doc__,
@@ -12846,35 +12659,26 @@ unicode_split(PyObject *self, PyObject *args, PyObject *kwds)
if (substring == Py_None)
return split(self, NULL, maxcount);
- else if (PyUnicode_Check(substring))
+
+ if (PyUnicode_Check(substring))
return split(self, substring, maxcount);
- else
- return PyUnicode_Split(self, substring, maxcount);
+
+ PyErr_Format(PyExc_TypeError,
+ "must be str or None, not %.100s",
+ Py_TYPE(substring)->tp_name);
+ return NULL;
}
PyObject *
-PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
+PyUnicode_Partition(PyObject *str_obj, PyObject *sep_obj)
{
- PyObject* str_obj;
- PyObject* sep_obj;
PyObject* out;
int kind1, kind2;
void *buf1, *buf2;
Py_ssize_t len1, len2;
- str_obj = PyUnicode_FromObject(str_in);
- if (!str_obj)
- return NULL;
- sep_obj = PyUnicode_FromObject(sep_in);
- if (!sep_obj) {
- Py_DECREF(str_obj);
+ if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0)
return NULL;
- }
- if (PyUnicode_READY(sep_obj) == -1 || PyUnicode_READY(str_obj) == -1) {
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
- return NULL;
- }
kind1 = PyUnicode_KIND(str_obj);
kind2 = PyUnicode_KIND(sep_obj);
@@ -12888,8 +12692,6 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
out = PyTuple_Pack(3, str_obj, unicode_empty, unicode_empty);
Py_DECREF(unicode_empty);
}
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
return out;
}
buf1 = PyUnicode_DATA(str_obj);
@@ -12897,7 +12699,7 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
if (kind2 != kind1) {
buf2 = _PyUnicode_AsKind(sep_obj, kind1);
if (!buf2)
- goto onError;
+ return NULL;
}
switch (kind1) {
@@ -12918,39 +12720,23 @@ PyUnicode_Partition(PyObject *str_in, PyObject *sep_in)
out = 0;
}
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
if (kind2 != kind1)
PyMem_Free(buf2);
return out;
- onError:
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
- if (kind2 != kind1 && buf2)
- PyMem_Free(buf2);
- return NULL;
}
PyObject *
-PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
+PyUnicode_RPartition(PyObject *str_obj, PyObject *sep_obj)
{
- PyObject* str_obj;
- PyObject* sep_obj;
PyObject* out;
int kind1, kind2;
void *buf1, *buf2;
Py_ssize_t len1, len2;
- str_obj = PyUnicode_FromObject(str_in);
- if (!str_obj)
+ if (ensure_unicode(str_obj) < 0 || ensure_unicode(sep_obj) < 0)
return NULL;
- sep_obj = PyUnicode_FromObject(sep_in);
- if (!sep_obj) {
- Py_DECREF(str_obj);
- return NULL;
- }
kind1 = PyUnicode_KIND(str_obj);
kind2 = PyUnicode_KIND(sep_obj);
@@ -12964,8 +12750,6 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
out = PyTuple_Pack(3, unicode_empty, unicode_empty, str_obj);
Py_DECREF(unicode_empty);
}
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
return out;
}
buf1 = PyUnicode_DATA(str_obj);
@@ -12973,7 +12757,7 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
if (kind2 != kind1) {
buf2 = _PyUnicode_AsKind(sep_obj, kind1);
if (!buf2)
- goto onError;
+ return NULL;
}
switch (kind1) {
@@ -12994,18 +12778,10 @@ PyUnicode_RPartition(PyObject *str_in, PyObject *sep_in)
out = 0;
}
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
if (kind2 != kind1)
PyMem_Free(buf2);
return out;
- onError:
- Py_DECREF(sep_obj);
- Py_DECREF(str_obj);
- if (kind2 != kind1 && buf2)
- PyMem_Free(buf2);
- return NULL;
}
PyDoc_STRVAR(partition__doc__,
@@ -13037,24 +12813,10 @@ unicode_rpartition(PyObject *self, PyObject *separator)
PyObject *
PyUnicode_RSplit(PyObject *s, PyObject *sep, Py_ssize_t maxsplit)
{
- PyObject *result;
-
- s = PyUnicode_FromObject(s);
- if (s == NULL)
+ if (ensure_unicode(s) < 0 || (sep != NULL && ensure_unicode(sep) < 0))
return NULL;
- if (sep != NULL) {
- sep = PyUnicode_FromObject(sep);
- if (sep == NULL) {
- Py_DECREF(s);
- return NULL;
- }
- }
- result = rsplit(s, sep, maxsplit);
-
- Py_DECREF(s);
- Py_XDECREF(sep);
- return result;
+ return rsplit(s, sep, maxsplit);
}
PyDoc_STRVAR(rsplit__doc__,
@@ -13079,10 +12841,14 @@ unicode_rsplit(PyObject *self, PyObject *args, PyObject *kwds)
if (substring == Py_None)
return rsplit(self, NULL, maxcount);
- else if (PyUnicode_Check(substring))
+
+ if (PyUnicode_Check(substring))
return rsplit(self, substring, maxcount);
- else
- return PyUnicode_RSplit(self, substring, maxcount);
+
+ PyErr_Format(PyExc_TypeError,
+ "must be str or None, not %.100s",
+ Py_TYPE(substring)->tp_name);
+ return NULL;
}
PyDoc_STRVAR(splitlines__doc__,
@@ -13363,11 +13129,15 @@ unicode_startswith(PyObject *self,
if (PyTuple_Check(subobj)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
- substring = PyUnicode_FromObject(PyTuple_GET_ITEM(subobj, i));
- if (substring == NULL)
+ substring = PyTuple_GET_ITEM(subobj, i);
+ if (!PyUnicode_Check(substring)) {
+ PyErr_Format(PyExc_TypeError,
+ "tuple for startswith must only contain str, "
+ "not %.100s",
+ Py_TYPE(substring)->tp_name);
return NULL;
+ }
result = tailmatch(self, substring, start, end, -1);
- Py_DECREF(substring);
if (result == -1)
return NULL;
if (result) {
@@ -13377,15 +13147,13 @@ unicode_startswith(PyObject *self,
/* nothing matched */
Py_RETURN_FALSE;
}
- substring = PyUnicode_FromObject(subobj);
- if (substring == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError, "startswith first arg must be str or "
- "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
+ if (!PyUnicode_Check(subobj)) {
+ PyErr_Format(PyExc_TypeError,
+ "startswith first arg must be str or "
+ "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
return NULL;
}
- result = tailmatch(self, substring, start, end, -1);
- Py_DECREF(substring);
+ result = tailmatch(self, subobj, start, end, -1);
if (result == -1)
return NULL;
return PyBool_FromLong(result);
@@ -13415,12 +13183,15 @@ unicode_endswith(PyObject *self,
if (PyTuple_Check(subobj)) {
Py_ssize_t i;
for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) {
- substring = PyUnicode_FromObject(
- PyTuple_GET_ITEM(subobj, i));
- if (substring == NULL)
+ substring = PyTuple_GET_ITEM(subobj, i);
+ if (!PyUnicode_Check(substring)) {
+ PyErr_Format(PyExc_TypeError,
+ "tuple for endswith must only contain str, "
+ "not %.100s",
+ Py_TYPE(substring)->tp_name);
return NULL;
+ }
result = tailmatch(self, substring, start, end, +1);
- Py_DECREF(substring);
if (result == -1)
return NULL;
if (result) {
@@ -13429,15 +13200,13 @@ unicode_endswith(PyObject *self,
}
Py_RETURN_FALSE;
}
- substring = PyUnicode_FromObject(subobj);
- if (substring == NULL) {
- if (PyErr_ExceptionMatches(PyExc_TypeError))
- PyErr_Format(PyExc_TypeError, "endswith first arg must be str or "
- "a tuple of str, not %s", Py_TYPE(subobj)->tp_name);
+ if (!PyUnicode_Check(subobj)) {
+ PyErr_Format(PyExc_TypeError,
+ "endswith first arg must be str or "
+ "a tuple of str, not %.100s", Py_TYPE(subobj)->tp_name);
return NULL;
}
- result = tailmatch(self, substring, start, end, +1);
- Py_DECREF(substring);
+ result = tailmatch(self, subobj, start, end, +1);
if (result == -1)
return NULL;
return PyBool_FromLong(result);
@@ -14907,13 +14676,10 @@ PyUnicode_Format(PyObject *format, PyObject *args)
return NULL;
}
- ctx.fmtstr = PyUnicode_FromObject(format);
- if (ctx.fmtstr == NULL)
+ if (ensure_unicode(format) < 0)
return NULL;
- if (PyUnicode_READY(ctx.fmtstr) == -1) {
- Py_DECREF(ctx.fmtstr);
- return NULL;
- }
+
+ ctx.fmtstr = format;
ctx.fmtdata = PyUnicode_DATA(ctx.fmtstr);
ctx.fmtkind = PyUnicode_KIND(ctx.fmtstr);
ctx.fmtcnt = PyUnicode_GET_LENGTH(ctx.fmtstr);
@@ -14973,11 +14739,9 @@ PyUnicode_Format(PyObject *format, PyObject *args)
if (ctx.args_owned) {
Py_DECREF(ctx.args);
}
- Py_DECREF(ctx.fmtstr);
return _PyUnicodeWriter_Finish(&ctx.writer);
onError:
- Py_DECREF(ctx.fmtstr);
_PyUnicodeWriter_Dealloc(&ctx.writer);
if (ctx.args_owned) {
Py_DECREF(ctx.args);
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 31d9e0e..29fcffe 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1931,9 +1931,8 @@ builtin_input_impl(PyModuleDef *module, PyObject *prompt)
Py_CLEAR(stringpo);
if (po == NULL)
goto _readline_errors;
- promptstr = PyBytes_AsString(po);
- if (promptstr == NULL)
- goto _readline_errors;
+ assert(PyBytes_Check(po));
+ promptstr = PyBytes_AS_STRING(po);
}
else {
po = NULL;
diff --git a/Python/getargs.c b/Python/getargs.c
index 05ec27b..9858bd5 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -1056,35 +1056,25 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
return converterr("(AsCharBuffer failed)",
arg, msgbuf, bufsize);
}
- else {
- PyObject *u;
-
- /* Convert object to Unicode */
- u = PyUnicode_FromObject(arg);
- if (u == NULL)
- return converterr(
- "string or unicode or text buffer",
- arg, msgbuf, bufsize);
-
+ else if (PyUnicode_Check(arg)) {
/* Encode object; use default error handling */
- s = PyUnicode_AsEncodedString(u,
+ s = PyUnicode_AsEncodedString(arg,
encoding,
NULL);
- Py_DECREF(u);
if (s == NULL)
return converterr("(encoding failed)",
arg, msgbuf, bufsize);
- if (!PyBytes_Check(s)) {
- Py_DECREF(s);
- return converterr(
- "(encoder failed to return bytes)",
- arg, msgbuf, bufsize);
- }
+ assert(PyBytes_Check(s));
size = PyBytes_GET_SIZE(s);
ptr = PyBytes_AS_STRING(s);
if (ptr == NULL)
ptr = "";
}
+ else {
+ return converterr(
+ recode_strings ? "str" : "str, bytes or bytearray",
+ arg, msgbuf, bufsize);
+ }
/* Write output; output is guaranteed to be 0-terminated */
if (*format == '#') {