Skip to content

bpo-37348 : add _PyUnicode_FROM_ASCII #14264

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

Closed
wants to merge 4 commits into from
Closed
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
3 changes: 3 additions & 0 deletions Include/cpython/unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,9 @@ PyAPI_FUNC(PyObject*) _PyUnicode_FromASCII(
const char *buffer,
Py_ssize_t size);

/* Convenient wrapper for _PyUnicode_FromASCII */
#define _PyUnicode_FROM_ASCII(s) _PyUnicode_FromASCII((s), strlen(s))

/* Compute the maximum character of the substring unicode[start:end].
Return 127 for an empty string. */
PyAPI_FUNC(Py_UCS4) _PyUnicode_FindMaxChar (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
New ``_PyUnicode_FROM_ASCII`` macro is added. It wraps
``_PyUnicode_FromASCII`` with ``strlen``.
2 changes: 1 addition & 1 deletion Modules/_asynciomodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2390,7 +2390,7 @@ TaskObj_finalize(TaskObj *task)
goto finally;
}

message = PyUnicode_FromString("Task was destroyed but it is pending!");
message = _PyUnicode_FROM_ASCII("Task was destroyed but it is pending!");
if (message == NULL) {
goto finally;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_blake2/blake2b_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ static PyMethodDef py_blake2b_methods[] = {
static PyObject *
py_blake2b_get_name(BLAKE2bObject *self, void *closure)
{
return PyUnicode_FromString("blake2b");
return _PyUnicode_FROM_ASCII("blake2b");
}


Expand Down
2 changes: 1 addition & 1 deletion Modules/_blake2/blake2s_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ static PyMethodDef py_blake2s_methods[] = {
static PyObject *
py_blake2s_get_name(BLAKE2sObject *self, void *closure)
{
return PyUnicode_FromString("blake2s");
return _PyUnicode_FROM_ASCII("blake2s");
}


Expand Down
6 changes: 3 additions & 3 deletions Modules/_collectionsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1358,7 +1358,7 @@ deque_repr(PyObject *deque)
if (i != 0) {
if (i < 0)
return NULL;
return PyUnicode_FromString("[...]");
return _PyUnicode_FROM_ASCII("[...]");
}

aslist = PySequence_List(deque);
Expand Down Expand Up @@ -2097,7 +2097,7 @@ defdict_repr(defdictobject *dd)
if (baserepr == NULL)
return NULL;
if (dd->default_factory == NULL)
defrepr = PyUnicode_FromString("None");
defrepr = _PyUnicode_FROM_ASCII("None");
else
{
int status = Py_ReprEnter(dd->default_factory);
Expand All @@ -2106,7 +2106,7 @@ defdict_repr(defdictobject *dd)
Py_DECREF(baserepr);
return NULL;
}
defrepr = PyUnicode_FromString("...");
defrepr = _PyUnicode_FROM_ASCII("...");
}
else
defrepr = PyObject_Repr(dd->default_factory);
Expand Down
6 changes: 3 additions & 3 deletions Modules/_ctypes/callproc.c
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,7 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
cls_str = PyObject_Str(tp);
if (cls_str) {
PyUnicode_AppendAndDel(&s, cls_str);
PyUnicode_AppendAndDel(&s, PyUnicode_FromString(": "));
PyUnicode_AppendAndDel(&s, _PyUnicode_FROM_ASCII(": "));
if (s == NULL)
goto error;
} else
Expand All @@ -962,7 +962,7 @@ void _ctypes_extend_error(PyObject *exc_class, const char *fmt, ...)
PyUnicode_AppendAndDel(&s, msg_str);
else {
PyErr_Clear();
PyUnicode_AppendAndDel(&s, PyUnicode_FromString("???"));
PyUnicode_AppendAndDel(&s, _PyUnicode_FROM_ASCII("???"));
}
if (s == NULL)
goto error;
Expand Down Expand Up @@ -1250,7 +1250,7 @@ static PyObject *format_error(PyObject *self, PyObject *args)
result = PyUnicode_FromWideChar(lpMsgBuf, wcslen(lpMsgBuf));
LocalFree(lpMsgBuf);
} else {
result = PyUnicode_FromString("<no description>");
result = _PyUnicode_FROM_ASCII("<no description>");
}
return result;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2537,7 +2537,7 @@ delta_bool(PyDateTime_Delta *self)
static PyObject *
delta_repr(PyDateTime_Delta *self)
{
PyObject *args = PyUnicode_FromString("");
PyObject *args = _PyUnicode_FROM_ASCII("");

if (args == NULL) {
return NULL;
Expand Down Expand Up @@ -2571,7 +2571,7 @@ delta_repr(PyDateTime_Delta *self)
}

if (PyUnicode_GET_LENGTH(args) == 0) {
Py_SETREF(args, PyUnicode_FromString("0"));
Py_SETREF(args, _PyUnicode_FROM_ASCII("0"));
if (args == NULL) {
return NULL;
}
Expand Down Expand Up @@ -3806,7 +3806,7 @@ timezone_str(PyDateTime_TimeZone *self)
(GET_TD_DAYS(self->offset) == 0 &&
GET_TD_SECONDS(self->offset) == 0 &&
GET_TD_MICROSECONDS(self->offset) == 0))
return PyUnicode_FromString("UTC");
return _PyUnicode_FROM_ASCII("UTC");
/* Offset is normalized, so it is negative if days < 0 */
if (GET_TD_DAYS(self->offset) < 0) {
sign = '-';
Expand Down
4 changes: 2 additions & 2 deletions Modules/_elementtree.c
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ element_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
static PyObject*
get_attrib_from_keywords(PyObject *kwds)
{
PyObject *attrib_str = PyUnicode_FromString("attrib");
PyObject *attrib_str = _PyUnicode_FROM_ASCII("attrib");
if (attrib_str == NULL) {
return NULL;
}
Expand Down Expand Up @@ -4073,7 +4073,7 @@ _elementtree_XMLParser__setevents_impl(XMLParserObject *self,

if (events_to_report == Py_None) {
/* default is "end" only */
target->end_event_obj = PyUnicode_FromString("end");
target->end_event_obj = _PyUnicode_FROM_ASCII("end");
Py_RETURN_NONE;
}

Expand Down
4 changes: 2 additions & 2 deletions Modules/_functoolsmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -276,10 +276,10 @@ partial_repr(partialobject *pto)
if (status != 0) {
if (status < 0)
return NULL;
return PyUnicode_FromString("...");
return _PyUnicode_FROM_ASCII("...");
}

arglist = PyUnicode_FromString("");
arglist = _PyUnicode_FROM_ASCII("");
if (arglist == NULL)
goto done;
/* Pack positional arguments */
Expand Down
10 changes: 5 additions & 5 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -611,11 +611,11 @@ incrementalnewlinedecoder_newlines_get(nldecoder_object *self, void *context)
{
switch (self->seennl) {
case SEEN_CR:
return PyUnicode_FromString("\r");
return _PyUnicode_FROM_ASCII("\r");
case SEEN_LF:
return PyUnicode_FromString("\n");
return _PyUnicode_FROM_ASCII("\n");
case SEEN_CRLF:
return PyUnicode_FromString("\r\n");
return _PyUnicode_FROM_ASCII("\r\n");
case SEEN_CR | SEEN_LF:
return Py_BuildValue("ss", "\r", "\n");
case SEEN_CR | SEEN_CRLF:
Expand Down Expand Up @@ -1125,7 +1125,7 @@ _io_TextIOWrapper___init___impl(textio *self, PyObject *buffer,
*/
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
PyErr_Clear();
self->encoding = PyUnicode_FromString("ascii");
self->encoding = _PyUnicode_FROM_ASCII("ascii");
}
else
goto error;
Expand Down Expand Up @@ -2847,7 +2847,7 @@ textiowrapper_repr(textio *self)

CHECK_INITIALIZED(self);

res = PyUnicode_FromString("<_io.TextIOWrapper");
res = _PyUnicode_FROM_ASCII("<_io.TextIOWrapper");
if (res == NULL)
return NULL;

Expand Down
6 changes: 3 additions & 3 deletions Modules/_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1419,13 +1419,13 @@ encoder_encode_float(PyEncoderObject *s, PyObject *obj)
return NULL;
}
if (i > 0) {
return PyUnicode_FromString("Infinity");
return _PyUnicode_FROM_ASCII("Infinity");
}
else if (i < 0) {
return PyUnicode_FromString("-Infinity");
return _PyUnicode_FROM_ASCII("-Infinity");
}
else {
return PyUnicode_FromString("NaN");
return _PyUnicode_FROM_ASCII("NaN");
}
}
return PyFloat_Type.tp_repr(obj);
Expand Down
4 changes: 2 additions & 2 deletions Modules/_operator.c
Original file line number Diff line number Diff line change
Expand Up @@ -1345,7 +1345,7 @@ dotjoinattr(PyObject *attr, PyObject **attrsep)
{
if (PyTuple_CheckExact(attr)) {
if (*attrsep == NULL) {
*attrsep = PyUnicode_FromString(".");
*attrsep = _PyUnicode_FROM_ASCII(".");
if (*attrsep == NULL)
return NULL;
}
Expand Down Expand Up @@ -1627,7 +1627,7 @@ methodcaller_repr(methodcallerobject *mc)
}
}

sep = PyUnicode_FromString(", ");
sep = _PyUnicode_FROM_ASCII(", ");
if (sep == NULL)
goto done;

Expand Down
20 changes: 10 additions & 10 deletions Modules/_sha3/sha3module.c
Original file line number Diff line number Diff line change
Expand Up @@ -414,27 +414,27 @@ SHA3_get_name(SHA3object *self, void *closure)
{
PyTypeObject *type = Py_TYPE(self);
if (type == &SHA3_224type) {
return PyUnicode_FromString("sha3_224");
return _PyUnicode_FROM_ASCII("sha3_224");
} else if (type == &SHA3_256type) {
return PyUnicode_FromString("sha3_256");
return _PyUnicode_FROM_ASCII("sha3_256");
} else if (type == &SHA3_384type) {
return PyUnicode_FromString("sha3_384");
return _PyUnicode_FROM_ASCII("sha3_384");
} else if (type == &SHA3_512type) {
return PyUnicode_FromString("sha3_512");
return _PyUnicode_FROM_ASCII("sha3_512");
#ifdef PY_WITH_KECCAK
} else if (type == &Keccak_224type) {
return PyUnicode_FromString("keccak_224");
return _PyUnicode_FROM_ASCII("keccak_224");
} else if (type == &Keccak_256type) {
return PyUnicode_FromString("keccak_256");
return _PyUnicode_FROM_ASCII("keccak_256");
} else if (type == &Keccak_384type) {
return PyUnicode_FromString("keccak_384");
return _PyUnicode_FROM_ASCII("keccak_384");
} else if (type == &Keccak_512type) {
return PyUnicode_FromString("keccak_512");
return _PyUnicode_FROM_ASCII("keccak_512");
#endif
} else if (type == &SHAKE128type) {
return PyUnicode_FromString("shake_128");
return _PyUnicode_FROM_ASCII("shake_128");
} else if (type == &SHAKE256type) {
return PyUnicode_FromString("shake_256");
return _PyUnicode_FROM_ASCII("shake_256");
} else {
PyErr_BadInternalCall();
return NULL;
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int pysqlite_connection_init(pysqlite_Connection* self, PyObject* args, PyObject
}

if (!isolation_level) {
isolation_level = PyUnicode_FromString("");
isolation_level = _PyUnicode_FROM_ASCII("");
if (!isolation_level) {
return -1;
}
Expand Down
2 changes: 1 addition & 1 deletion Modules/_sre.c
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ pattern_repr(PatternObject *obj)

if (PyList_Size(flag_items) > 0) {
PyObject *flags_result;
PyObject *sep = PyUnicode_FromString("|");
PyObject *sep = _PyUnicode_FROM_ASCII("|");
if (!sep)
goto done;
flags_result = PyUnicode_Join(sep, flag_items);
Expand Down
12 changes: 6 additions & 6 deletions Modules/_ssl.c
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ _get_peer_alt_names (X509 *certificate) {
goto fail;
}

v = PyUnicode_FromString("DirName");
v = _PyUnicode_FROM_ASCII("DirName");
if (v == NULL) {
Py_DECREF(t);
goto fail;
Expand All @@ -1335,15 +1335,15 @@ _get_peer_alt_names (X509 *certificate) {
goto fail;
switch (gntype) {
case GEN_EMAIL:
v = PyUnicode_FromString("email");
v = _PyUnicode_FROM_ASCII("email");
as = name->d.rfc822Name;
break;
case GEN_DNS:
v = PyUnicode_FromString("DNS");
v = _PyUnicode_FROM_ASCII("DNS");
as = name->d.dNSName;
break;
case GEN_URI:
v = PyUnicode_FromString("URI");
v = _PyUnicode_FROM_ASCII("URI");
as = name->d.uniformResourceIdentifier;
break;
}
Expand All @@ -1366,7 +1366,7 @@ _get_peer_alt_names (X509 *certificate) {
if (t == NULL)
goto fail;

v = PyUnicode_FromString("Registered ID");
v = _PyUnicode_FROM_ASCII("Registered ID");
if (v == NULL) {
Py_DECREF(t);
goto fail;
Expand All @@ -1379,7 +1379,7 @@ _get_peer_alt_names (X509 *certificate) {
_setSSLError(NULL, 0, __FILE__, __LINE__);
goto fail;
} else if (len >= (int)sizeof(buf)) {
v = PyUnicode_FromString("<INVALID>");
v = _PyUnicode_FROM_ASCII("<INVALID>");
} else {
v = PyUnicode_FromStringAndSize(buf, len);
}
Expand Down
8 changes: 4 additions & 4 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1719,7 +1719,7 @@ test_Z_code(PyObject *self, PyObject *Py_UNUSED(ignored))
if (tuple == NULL)
return NULL;

obj = PyUnicode_FromString("test");
obj = _PyUnicode_FROM_ASCII("test");
PyTuple_SET_ITEM(tuple, 0, obj);
Py_INCREF(Py_None);
PyTuple_SET_ITEM(tuple, 1, Py_None);
Expand Down Expand Up @@ -2304,7 +2304,7 @@ datetime_check_tzinfo(PyObject *self, PyObject *args) {
static PyObject *
make_timezones_capi(PyObject *self, PyObject *args) {
PyObject *offset = PyDelta_FromDSU(0, -18000, 0);
PyObject *name = PyUnicode_FromString("EST");
PyObject *name = _PyUnicode_FROM_ASCII("EST");

PyObject *est_zone_capi = PyDateTimeAPI->TimeZone_FromTimeZone(offset, name);
PyObject *est_zone_macro = PyTimeZone_FromOffsetAndName(offset, name);
Expand All @@ -2325,7 +2325,7 @@ make_timezones_capi(PyObject *self, PyObject *args) {
static PyObject *
get_timezones_offset_zero(PyObject *self, PyObject *args) {
PyObject *offset = PyDelta_FromDSU(0, 0, 0);
PyObject *name = PyUnicode_FromString("");
PyObject *name = _PyUnicode_FROM_ASCII("");

// These two should return the UTC singleton
PyObject *utc_singleton_0 = PyTimeZone_FromOffset(offset);
Expand Down Expand Up @@ -5013,7 +5013,7 @@ decode_locale_ex(PyObject *self, PyObject *args)
static PyObject *
negative_refcount(PyObject *self, PyObject *Py_UNUSED(args))
{
PyObject *obj = PyUnicode_FromString("negative_refcount");
PyObject *obj = _PyUnicode_FROM_ASCII("negative_refcount");
if (obj == NULL) {
return NULL;
}
Expand Down
6 changes: 3 additions & 3 deletions Modules/_tkinter.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ _get_tcl_lib_path()
}

/* Check expected location for an installed Python first */
tcl_library_path = PyUnicode_FromString("\\tcl\\tcl" TCL_VERSION);
tcl_library_path = _PyUnicode_FROM_ASCII("\\tcl\\tcl" TCL_VERSION);
if (tcl_library_path == NULL) {
return NULL;
}
Expand Down Expand Up @@ -431,7 +431,7 @@ Split(const char *list)
}

if (argc == 0)
v = PyUnicode_FromString("");
v = _PyUnicode_FROM_ASCII("");
else if (argc == 1)
v = unicodeFromTclString(argv[0]);
else if ((v = PyTuple_New(argc)) != NULL) {
Expand Down Expand Up @@ -2278,7 +2278,7 @@ _tkinter_tkapp_split(TkappObject *self, PyObject *arg)
return FromObj((PyObject*)self, value);
}
if (objc == 0)
return PyUnicode_FromString("");
return _PyUnicode_FROM_ASCII("");
if (objc == 1)
return FromObj((PyObject*)self, objv[0]);
if (!(v = PyTuple_New(objc)))
Expand Down
2 changes: 1 addition & 1 deletion Modules/_tracemalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ tracemalloc_init(void)
return -1;
}

unknown_filename = PyUnicode_FromString("<unknown>");
unknown_filename = _PyUnicode_FROM_ASCII("<unknown>");
if (unknown_filename == NULL)
return -1;
PyUnicode_InternInPlace(&unknown_filename);
Expand Down
Loading