8000 Add private _PyUnicode_AsUTF8() function by serhiy-storchaka · Pull Request #111957 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Add private _PyUnicode_AsUTF8() 8000 function #111957

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
Nov 10, 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
4 changes: 4 additions & 0 deletions Include/internal/pycore_unicodeobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,10 @@ struct _Py_unicode_state {
extern void _PyUnicode_InternInPlace(PyInterpreterState *interp, PyObject **p);
extern void _PyUnicode_ClearInterned(PyInterpreterState *interp);

// Like PyUnicode_AsUTF8(), but check for embedded null characters.
// Export for '_sqlite3' shared extension.
PyAPI_FUNC(const char *) _PyUnicode_AsUTF8NoNUL(PyObject *);


#ifdef __cplusplus
}
Expand Down
7 changes: 1 addition & 6 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1020,15 +1020,10 @@ io_check_errors(PyObject *errors)
return 0;
}

Py_ssize_t name_length;
const char *name = PyUnicode_AsUTF8AndSize(errors, &name_length);
const char *name = _PyUnicode_AsUTF8NoNUL(errors);
if (name == NULL) {
return -1;
}
if (strlen(name) != (size_t)name_length) {
PyErr_SetString(PyExc_ValueError, "embedded null character in errors");
return -1;
}
PyObject *handler = PyCodec_LookupError(name);
if (handler != NULL) {
Py_DECREF(handler);
Expand Down
8 changes: 1 addition & 7 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,10 @@ isolation_level_converter(PyObject *str_or_none, const char **result)
*result = NULL;
}
else if (PyUnicode_Check(str_or_none)) {
Py_ssize_t sz;
const char *str = PyUnicode_AsUTF8AndSize(str_or_none, &sz);
const char *str = _PyUnicode_AsUTF8NoNUL(str_or_none);
if (str == NULL) {
return 0;
}
if (strlen(str) != (size_t)sz) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return 0;
}

const char *level = get_isolation_level(str);
if (level == NULL) {
return 0;
Expand Down
12 changes: 12 additions & 0 deletions Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -3847,6 +3847,18 @@ PyUnicode_AsUTF8(PyObject *unicode)
return PyUnicode_AsUTF8AndSize(unicode, NULL);
}

const char *
_PyUnicode_AsUTF8NoNUL(PyObject *unicode)
{
Py_ssize_t size;
const char *s = PyUnicode_AsUTF8AndSize(unicode, &size);
if (s && strlen(s) != (size_t)size) {
PyErr_SetString(PyExc_ValueError, "embedded null character");
return NULL;
}
return s;
}

/*
PyUnicode_GetSize() has been deprecated since Python 3.3
because it returned length of Py_UNICODE.
Expand Down
0