mirror of
https://github.com/python/cpython.git
synced 2025-11-02 03:01:58 +00:00
Use AsCharBuffer to get a C string out of a Python string.
This commit is contained in:
parent
83857e3bf3
commit
ccf4f0f68d
1 changed files with 16 additions and 29 deletions
|
|
@ -355,44 +355,31 @@ check_bom(int get_char(struct tok_state *),
|
||||||
static char *
|
static char *
|
||||||
fp_readl(char *s, int size, struct tok_state *tok)
|
fp_readl(char *s, int size, struct tok_state *tok)
|
||||||
{
|
{
|
||||||
PyObject* utf8 = NULL;
|
PyObject* bufobj = tok->decoding_buffer;
|
||||||
PyObject* buf = tok->decoding_buffer;
|
const char *buf;
|
||||||
char *str;
|
Py_ssize_t buflen;
|
||||||
Py_ssize_t utf8len;
|
|
||||||
|
|
||||||
/* Ask for one less byte so we can terminate it */
|
/* Ask for one less byte so we can terminate it */
|
||||||
assert(size > 0);
|
assert(size > 0);
|
||||||
size--;
|
size--;
|
||||||
|
|
||||||
if (buf == NULL) {
|
if (bufobj == NULL) {
|
||||||
buf = PyObject_CallObject(tok->decoding_readline, NULL);
|
bufobj = PyObject_CallObject(tok->decoding_readline, NULL);
|
||||||
if (buf == NULL)
|
if (bufobj == NULL)
|
||||||
return error_ret(tok);
|
|
||||||
} else {
|
|
||||||
tok->decoding_buffer = NULL;
|
|
||||||
if (PyString_CheckExact(buf))
|
|
||||||
utf8 = buf;
|
|
||||||
}
|
|
||||||
if (utf8 == NULL) {
|
|
||||||
utf8 = PyUnicode_AsUTF8String(buf);
|
|
||||||
Py_DECREF(buf);
|
|
||||||
if (utf8 == NULL)
|
|
||||||
return error_ret(tok);
|
return error_ret(tok);
|
||||||
}
|
}
|
||||||
str = PyString_AsString(utf8);
|
if (PyObject_AsCharBuffer(bufobj, &buf, &buflen) < 0)
|
||||||
utf8len = PyString_GET_SIZE(utf8);
|
return error_ret(tok);
|
||||||
if (utf8len > size) {
|
if (buflen > size) {
|
||||||
tok->decoding_buffer = PyString_FromStringAndSize(str+size, utf8len-size);
|
tok->decoding_buffer = PyBytes_FromStringAndSize(buf+size,
|
||||||
if (tok->decoding_buffer == NULL) {
|
buflen-size);
|
||||||
Py_DECREF(utf8);
|
if (tok->decoding_buffer == NULL)
|
||||||
return error_ret(tok);
|
return error_ret(tok);
|
||||||
}
|
buflen = size;
|
||||||
utf8len = size;
|
|
||||||
}
|
}
|
||||||
memcpy(s, str, utf8len);
|
memcpy(s, buf, buflen);
|
||||||
s[utf8len] = '\0';
|
s[buflen] = '\0';
|
||||||
Py_DECREF(utf8);
|
if (buflen == 0) return NULL; /* EOF */
|
||||||
if (utf8len == 0) return NULL; /* EOF */
|
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue