Skip to content

gh-92536: Remove PyUnicode_READY() calls #105210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 0 additions & 12 deletions Modules/_csv.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,6 @@ _set_char_or_none(const char *name, Py_UCS4 *target, PyObject *src, Py_UCS4 dflt
name);
return -1;
}
/* PyUnicode_READY() is called in PyUnicode_GetLength() */
*target = PyUnicode_READ_CHAR(src, 0);
}
}
Expand Down Expand Up @@ -296,7 +295,6 @@ _set_char(const char *name, Py_UCS4 *target, PyObject *src, Py_UCS4 dflt)
name);
return -1;
}
/* PyUnicode_READY() is called in PyUnicode_GetLength() */
*target = PyUnicode_READ_CHAR(src, 0);
}
return 0;
Expand All @@ -316,8 +314,6 @@ _set_str(const char *name, PyObject **target, PyObject *src, const char *dflt)
return -1;
}
else {
if (PyUnicode_READY(src) == -1)
return -1;
Py_XSETREF(*target, Py_NewRef(src));
}
}
Expand Down Expand Up @@ -904,10 +900,6 @@ Reader_iternext(ReaderObj *self)
Py_DECREF(lineobj);
return NULL;
}
if (PyUnicode_READY(lineobj) == -1) {
Py_DECREF(lineobj);
return NULL;
}
++self->line_num;
kind = PyUnicode_KIND(lineobj);
data = PyUnicode_DATA(lineobj);
Expand Down Expand Up @@ -1185,8 +1177,6 @@ join_append(WriterObj *self, PyObject *field, int quoted)
Py_ssize_t rec_len;

if (field != NULL) {
if (PyUnicode_READY(field) == -1)
return 0;
field_kind = PyUnicode_KIND(field);
field_data = PyUnicode_DATA(field);
field_len = PyUnicode_GET_LENGTH(field);
Expand Down Expand Up @@ -1515,8 +1505,6 @@ csv_register_dialect(PyObject *module, PyObject *args, PyObject *kwargs)
"dialect name must be a string");
return NULL;
}
if (PyUnicode_READY(name_obj) == -1)
return NULL;
dialect = _call_dialect(module_state, dialect_obj, kwargs);
if (dialect == NULL)
return NULL;
Expand Down
9 changes: 0 additions & 9 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2901,9 +2901,6 @@ date_new(PyTypeObject *type, PyObject *args, PyObject *kw)
}
}
else if (PyUnicode_Check(state)) {
if (PyUnicode_READY(state)) {
return NULL;
}
if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATE_DATASIZE &&
MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2)))
{
Expand Down Expand Up @@ -4234,9 +4231,6 @@ time_new(PyTypeObject *type, PyObject *args, PyObject *kw)
}
}
else if (PyUnicode_Check(state)) {
if (PyUnicode_READY(state)) {
return NULL;
}
if (PyUnicode_GET_LENGTH(state) == _PyDateTime_TIME_DATASIZE &&
(0x7F & PyUnicode_READ_CHAR(state, 0)) < 24)
{
Expand Down Expand Up @@ -4909,9 +4903,6 @@ datetime_new(PyTypeObject *type, PyObject *args, PyObject *kw)
}
}
else if (PyUnicode_Check(state)) {
if (PyUnicode_READY(state)) {
return NULL;
}
if (PyUnicode_GET_LENGTH(state) == _PyDateTime_DATETIME_DATASIZE &&
MONTH_IS_SANE(PyUnicode_READ_CHAR(state, 2) & 0x7F))
{
Expand Down
4 changes: 0 additions & 4 deletions Modules/_decimal/_decimal.c
Original file line number Diff line number Diff line change
Expand Up @@ -1934,10 +1934,6 @@ numeric_as_ascii(PyObject *u, int strip_ws, int ignore_underscores)
Py_ssize_t j, len;
int d;

if (PyUnicode_READY(u) == -1) {
return NULL;
}

kind = PyUnicode_KIND(u);
data = PyUnicode_DATA(u);
len = PyUnicode_GET_LENGTH(u);
Expand Down
2 changes: 0 additions & 2 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -1448,8 +1448,6 @@ _elementtree_Element_iter_impl(ElementObject *self, PyTypeObject *cls,
/*[clinic end generated code: output=bff29dc5d4566c68 input=f6944c48d3f84c58]*/
{
if (PyUnicode_Check(tag)) {
if (PyUnicode_READY(tag) < 0)
return NULL;
if (PyUnicode_GET_LENGTH(tag) == 1 && PyUnicode_READ_CHAR(tag, 0) == '*')
tag = Py_None;
}
Expand Down
3 changes: 0 additions & 3 deletions Modules/_hashopenssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1988,9 +1988,6 @@ _hashlib_compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)

/* ASCII unicode string */
if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
return NULL;
}
if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
PyErr_SetString(PyExc_TypeError,
"comparing strings with non-ASCII characters is "
Expand Down
6 changes: 0 additions & 6 deletions Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,6 @@ write_str(stringio *self, PyObject *obj)
return -1;

assert(PyUnicode_Check(decoded));
if (PyUnicode_READY(decoded)) {
Py_DECREF(decoded);
return -1;
}
len = PyUnicode_GET_LENGTH(decoded);
assert(len >= 0);

Expand Down Expand Up @@ -542,8 +538,6 @@ _io_StringIO_write(stringio *self, PyObject *obj)
Py_TYPE(obj)->tp_name);
return NULL;
}
if (PyUnicode_READY(obj))
return NULL;
CHECK_CLOSED(self);
size = PyUnicode_GET_LENGTH(obj);

Expand Down
13 changes: 1 addition & 12 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,6 @@ check_decoded(PyObject *decoded)
Py_DECREF(decoded);
return -1;
}
if (PyUnicode_READY(decoded) < 0) {
Py_DECREF(decoded);
return -1;
}
return 0;
}

Expand Down Expand Up @@ -1611,9 +1607,6 @@ _io_TextIOWrapper_write_impl(textio *self, PyObject *text)
int haslf = 0;
int needflush = 0, text_needflush = 0;

if (PyUnicode_READY(text) == -1)
return NULL;

CHECK_ATTACHED(self);
CHECK_CLOSED(self);

Expand Down Expand Up @@ -1972,8 +1965,6 @@ _io_TextIOWrapper_read_impl(textio *self, Py_ssize_t n)
result = textiowrapper_get_decoded_chars(self, n);
if (result == NULL)
goto fail;
if (PyUnicode_READY(result) == -1)
goto fail;
remaining -= PyUnicode_GET_LENGTH(result);

/* Keep reading chunks until we have n characters to return */
Expand Down Expand Up @@ -2185,8 +2176,6 @@ _textiowrapper_readline(textio *self, Py_ssize_t limit)
Py_CLEAR(remaining);
if (line == NULL)
goto error;
if (PyUnicode_READY(line) == -1)
goto error;
}

ptr = PyUnicode_DATA(line);
Expand Down Expand Up @@ -3106,7 +3095,7 @@ textiowrapper_iternext(textio *self)
}
}

if (line == NULL || PyUnicode_READY(line) == -1)
if (line == NULL)
return NULL;

if (PyUnicode_GET_LENGTH(line) == 0) {
Expand Down
21 changes: 0 additions & 21 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,6 @@ ascii_escape_unicode(PyObject *pystr)
Py_UCS1 *output;
int kind;

if (PyUnicode_READY(pystr) == -1)
return NULL;

input_chars = PyUnicode_GET_LENGTH(pystr);
input = PyUnicode_DATA(pystr);
kind = PyUnicode_KIND(pystr);
Expand Down Expand Up @@ -218,9 +215,6 @@ escape_unicode(PyObject *pystr)
int kind;
Py_UCS4 maxchar;

if (PyUnicode_READY(pystr) == -1)
return NULL;

maxchar = PyUnicode_MAX_CHAR_VALUE(pystr);
input_chars = PyUnicode_GET_LENGTH(pystr);
input = PyUnicode_DATA(pystr);
Expand Down Expand Up @@ -377,9 +371,6 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
const void *buf;
int kind;

if (PyUnicode_READY(pystr) == -1)
return 0;

_PyUnicodeWriter writer;
_PyUnicodeWriter_Init(&writer);
writer.overallocate = 1;
Expand Down Expand Up @@ -675,9 +666,6 @@ _parse_object_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ss
int has_pairs_hook = (s->object_pairs_hook != Py_None);
Py_ssize_t next_idx;

if (PyUnicode_READY(pystr) == -1)
return NULL;

str = PyUnicode_DATA(pystr);
kind = PyUnicode_KIND(pystr);
end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
Expand Down Expand Up @@ -801,9 +789,6 @@ _parse_array_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssi
PyObject *rval;
Py_ssize_t next_idx;

if (PyUnicode_READY(pystr) == -1)
return NULL;

rval = PyList_New(0);
if (rval == NULL)
return NULL;
Expand Down Expand Up @@ -906,9 +891,6 @@ _match_number_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t start, Py_
PyObject *numstr = NULL;
PyObject *custom_func;

if (PyUnicode_READY(pystr) == -1)
return NULL;

str = PyUnicode_DATA(pystr);
kind = PyUnicode_KIND(pystr);
end_idx = PyUnicode_GET_LENGTH(pystr) - 1;
Expand Down Expand Up @@ -1018,9 +1000,6 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
int kind;
Py_ssize_t length;

if (PyUnicode_READY(pystr) == -1)
return NULL;

str = PyUnicode_DATA(pystr);
kind = PyUnicode_KIND(pystr);
length = PyUnicode_GET_LENGTH(pystr);
Expand Down
7 changes: 0 additions & 7 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -823,9 +823,6 @@ _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)

/* ASCII unicode string */
if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
return NULL;
}
if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
PyErr_SetString(PyExc_TypeError,
"comparing strings with non-ASCII characters is "
Expand Down Expand Up @@ -1234,10 +1231,6 @@ attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
Py_DECREF(attr);
return NULL;
}
if (PyUnicode_READY(item)) {
Py_DECREF(attr);
return NULL;
}
Py_ssize_t item_len = PyUnicode_GET_LENGTH(item);
int kind = PyUnicode_KIND(item);
const void *data = PyUnicode_DATA(item);
Expand Down
6 changes: 0 additions & 6 deletions Modules/_pickle.c
Original file line number Diff line number Diff line change
Expand Up @@ -2602,9 +2602,6 @@ raw_unicode_escape(PyObject *obj)
int kind;
_PyBytesWriter writer;

if (PyUnicode_READY(obj))
return NULL;

_PyBytesWriter_Init(&writer);

size = PyUnicode_GET_LENGTH(obj);
Expand Down Expand Up @@ -2674,9 +2671,6 @@ write_unicode_binary(PicklerObject *self, PyObject *obj)
Py_ssize_t size;
const char *data;

if (PyUnicode_READY(obj))
return -1;

data = PyUnicode_AsUTF8AndSize(obj, &size);
if (data == NULL) {
/* Issue #8383: for strings with lone surrogates, fallback on the
Expand Down
2 changes: 0 additions & 2 deletions Modules/_sre/sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,6 @@ getstring(PyObject* string, Py_ssize_t* p_length,
/* Unicode objects do not support the buffer API. So, get the data
directly instead. */
if (PyUnicode_Check(string)) {
if (PyUnicode_READY(string) == -1)
return NULL;
*p_length = PyUnicode_GET_LENGTH(string);
*p_charsize = PyUnicode_KIND(string);
*p_isbytes = 0;
Expand Down
3 changes: 0 additions & 3 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -962,9 +962,6 @@ AsObj(PyObject *value)
}

if (PyUnicode_Check(value)) {
if (PyUnicode_READY(value) == -1)
return NULL;

Py_ssize_t size = PyUnicode_GET_LENGTH(value);
if (size == 0) {
return Tcl_NewStringObj("", 0);
Expand Down
2 changes: 0 additions & 2 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,6 @@ ascii_buffer_converter(PyObject *arg, Py_buffer *buf)
return 1;
}
if (PyUnicode_Check(arg)) {
if (PyUnicode_READY(arg) < 0)
return 0;
if (!PyUnicode_IS_ASCII(arg)) {
PyErr_SetString(PyExc_ValueError,
"string argument should contain only ASCII characters");
Expand Down
8 changes: 0 additions & 8 deletions Modules/cjkcodecs/multibytecodec.c
Original file line number Diff line number Diff line change
Expand Up @@ -490,8 +490,6 @@ multibytecodec_encode(const MultibyteCodec *codec,
int kind;
const void *data;

if (PyUnicode_READY(text) < 0)
return NULL;
datalen = PyUnicode_GET_LENGTH(text);

if (datalen == 0 && !(flags & MBENC_RESET))
Expand Down Expand Up @@ -603,10 +601,6 @@ _multibytecodec_MultibyteCodec_encode_impl(MultibyteCodecObject *self,
}
}

if (PyUnicode_READY(input) < 0) {
Py_XDECREF(ucvt);
return NULL;
}
datalen = PyUnicode_GET_LENGTH(input);

errorcb = internal_error_callback(errors);
Expand Down Expand Up @@ -809,8 +803,6 @@ encoder_encode_stateful(MultibyteStatefulEncoderContext *ctx,

inbuf = Py_NewRef(unistr);
}
if (PyUnicode_READY(inbuf) < 0)
goto errorexit;
inpos = 0;
datalen = PyUnicode_GET_LENGTH(inbuf);

Expand Down
2 changes: 1 addition & 1 deletion Modules/pyexpat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1109,7 +1109,7 @@ PyUnknownEncodingHandler(void *encodingHandlerData,
return XML_STATUS_ERROR;

u = PyUnicode_Decode((const char*) template_buffer, 256, name, "replace");
if (u == NULL || PyUnicode_READY(u)) {
if (u == NULL) {
Py_XDECREF(u);
return XML_STATUS_ERROR;
}
Expand Down
3 changes: 0 additions & 3 deletions Modules/socketmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1717,9 +1717,6 @@ idna_converter(PyObject *obj, struct maybe_idna *data)
len = PyByteArray_Size(obj);
}
else if (PyUnicode_Check(obj)) {
if (PyUnicode_READY(obj) == -1) {
return 0;
}
if (PyUnicode_IS_COMPACT_ASCII(obj)) {
data->buf = PyUnicode_DATA(obj);
len = PyUnicode_GET_LENGTH(obj);
Expand Down
4 changes: 0 additions & 4 deletions Modules/unicodedata.c
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,6 @@ unicodedata_UCD_is_normalized_impl(PyObject *self, PyObject *form,
PyObject *input)
/*[clinic end generated code: output=11e5a3694e723ca5 input=a544f14cea79e508]*/
{
if (PyUnicode_READY(input) == -1) {
return NULL;
}

if (PyUnicode_GET_LENGTH(input) == 0) {
/* special case empty input strings. */
Py_RETURN_TRUE;
Expand Down
2 changes: 0 additions & 2 deletions Objects/bytesobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -2367,8 +2367,6 @@ _PyBytes_FromHex(PyObject *string, int use_bytearray)
writer.use_bytearray = use_bytearray;

assert(PyUnicode_Check(string));
if (PyUnicode_READY(string))
return NULL;
hexlen = PyUnicode_GET_LENGTH(string);

if (!PyUnicode_IS_ASCII(string)) {
Expand Down
Loading