mirror of
https://github.com/python/cpython.git
synced 2025-07-23 11:15:24 +00:00
Issue #22215: Now ValueError is raised instead of TypeError when str or bytes
argument contains not permitted null character or byte.
This commit is contained in:
parent
4a4b679515
commit
d8a1447c99
14 changed files with 41 additions and 39 deletions
|
@ -745,8 +745,8 @@ source_as_string(PyObject *cmd, char *funcname, char *what, PyCompilerFlags *cf)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
if (strlen(str) != (size_t)size) {
|
||||
PyErr_SetString(PyExc_TypeError,
|
||||
if (strlen(str) != (size_t)size) {
|
||||
PyErr_SetString(PyExc_ValueError,
|
||||
"source code string cannot contain null bytes");
|
||||
return NULL;
|
||||
}
|
||||
|
|
|
@ -872,10 +872,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
STORE_SIZE(count);
|
||||
format++;
|
||||
} else {
|
||||
if (strlen(*p) != (size_t)count)
|
||||
return converterr(
|
||||
"bytes without null bytes",
|
||||
arg, msgbuf, bufsize);
|
||||
if (strlen(*p) != (size_t)count) {
|
||||
PyErr_SetString(PyExc_ValueError, "embedded null byte");
|
||||
RETURN_ERR_OCCURRED;
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -948,16 +948,15 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
if (sarg == NULL)
|
||||
return converterr(CONV_UNICODE,
|
||||
arg, msgbuf, bufsize);
|
||||
if (strlen(sarg) != (size_t)len) {
|
||||
PyErr_SetString(PyExc_ValueError, "embedded null character");
|
||||
RETURN_ERR_OCCURRED;
|
||||
}
|
||||
*p = sarg;
|
||||
}
|
||||
else
|
||||
return converterr(c == 'z' ? "str or None" : "str",
|
||||
arg, msgbuf, bufsize);
|
||||
if (*p != NULL && sarg != NULL && (Py_ssize_t) strlen(*p) != len)
|
||||
return converterr(
|
||||
c == 'z' ? "str without null characters or None"
|
||||
: "str without null characters",
|
||||
arg, msgbuf, bufsize);
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
@ -994,10 +993,10 @@ convertsimple(PyObject *arg, const char **p_format, va_list *p_va, int flags,
|
|||
*p = PyUnicode_AsUnicodeAndSize(arg, &len);
|
||||
if (*p == NULL)
|
||||
RETURN_ERR_OCCURRED;
|
||||
if (Py_UNICODE_strlen(*p) != (size_t)len)
|
||||
return converterr(
|
||||
"str without null characters or None",
|
||||
arg, msgbuf, bufsize);
|
||||
if (Py_UNICODE_strlen(*p) != (size_t)len) {
|
||||
PyErr_SetString(PyExc_ValueError, "embedded null character");
|
||||
RETURN_ERR_OCCURRED;
|
||||
}
|
||||
} else
|
||||
return converterr(c == 'Z' ? "str or None" : "str",
|
||||
arg, msgbuf, bufsize);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue