diff options
author | Marc-André Lemburg <mal@egenix.com> | 2000-09-19 21:04:18 (GMT) |
---|---|---|
committer | Marc-André Lemburg <mal@egenix.com> | 2000-09-19 21:04:18 (GMT) |
commit | d1ba443206b535f41154f10b9d56d4fc76a1a9d8 (patch) | |
tree | ae56be2777275ca19f347e1154f756ddde4d4b22 /Python | |
parent | f8d071332a485ede280675a55e3319e136826dd0 (diff) | |
download | cpython-d1ba443206b535f41154f10b9d56d4fc76a1a9d8.zip cpython-d1ba443206b535f41154f10b9d56d4fc76a1a9d8.tar.gz cpython-d1ba443206b535f41154f10b9d56d4fc76a1a9d8.tar.bz2 |
This patch adds a new Python C API called PyString_AsStringAndSize()
which implements the automatic conversion from Unicode to a string
object using the default encoding.
The new API is then put to use to have eval() and exec accept
Unicode objects as code parameter. This closes bugs #110924
and #113890.
As side-effect, the traditional C APIs PyString_Size() and
PyString_AsString() will also accept Unicode objects as
parameters.
Diffstat (limited to 'Python')
-rw-r--r-- | Python/bltinmodule.c | 9 | ||||
-rw-r--r-- | Python/ceval.c | 10 |
2 files changed, 7 insertions, 12 deletions
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index 3eac8d5..88656ca 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -748,17 +748,14 @@ builtin_eval(PyObject *self, PyObject *args) } if (PyCode_Check(cmd)) return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); - if (!PyString_Check(cmd)) { + if (!PyString_Check(cmd) && + !PyUnicode_Check(cmd)) { PyErr_SetString(PyExc_TypeError, "eval() argument 1 must be string or code object"); return NULL; } - str = PyString_AsString(cmd); - if (strlen(str) != (size_t)PyString_Size(cmd)) { - PyErr_SetString(PyExc_ValueError, - "embedded '\\0' in string arg"); + if (PyString_AsStringAndSize(cmd, &str, NULL)) return NULL; - } while (*str == ' ' || *str == '\t') str++; return PyRun_String(str, Py_eval_input, globals, locals); diff --git a/Python/ceval.c b/Python/ceval.c index 09ae132..491a73b 100644 --- a/Python/ceval.c +++ b/Python/ceval.c @@ -3042,6 +3042,7 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, else if (locals == Py_None) locals = globals; if (!PyString_Check(prog) && + !PyUnicode_Check(prog) && !PyCode_Check(prog) && !PyFile_Check(prog)) { PyErr_SetString(PyExc_TypeError, @@ -3064,13 +3065,10 @@ exec_statement(PyFrameObject *f, PyObject *prog, PyObject *globals, v = PyRun_File(fp, name, Py_file_input, globals, locals); } else { - char *s = PyString_AsString(prog); - if (strlen(s) != (size_t)PyString_Size(prog)) { - PyErr_SetString(PyExc_ValueError, - "embedded '\\0' in exec string"); + char *str; + if (PyString_AsStringAndSize(prog, &str, NULL)) return -1; - } - v = PyRun_String(s, Py_file_input, globals, locals); + v = PyRun_String(str, Py_file_input, globals, locals); } if (plain) PyFrame_LocalsToFast(f, 0); |