From f7c63792f00ec584799fbc207656cdc6003b1040 Mon Sep 17 00:00:00 2001 From: "Ralf W. Grosse-Kunstleve" Date: Wed, 21 Feb 2001 10:00:27 +0000 Subject: [PATCH] map [plain] char '\0' <-> "" (Python string of length 0) [SVN r9306] --- src/conversions.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/conversions.cpp b/src/conversions.cpp index 66951f2f..f14330bd 100644 --- a/src/conversions.cpp +++ b/src/conversions.cpp @@ -162,21 +162,20 @@ unsigned short from_python(PyObject* p, boost::python::type type PyObject* to_python(char c) { - return PyString_FromStringAndSize(&c, 1); + if (c == '\0') return PyString_FromString(""); + return PyString_FromStringAndSize(&c, 1); } char from_python(PyObject* p, boost::python::type) { - if (! PyString_Check(p)) { - PyErr_SetString(PyExc_TypeError, "expected string with exactly one character"); + int l = -1; + if (PyString_Check(p)) l = PyString_Size(p); + if (l < 0 || l > 1) { + PyErr_SetString(PyExc_TypeError, "expected string of length 0 or 1"); throw boost::python::argument_error(); } - const char* s = PyString_AsString(p); - if (! s || s[0] == '\0' || s[1] != '\0') { - PyErr_SetString(PyExc_ValueError, "expected string with exactly one character"); - throw boost::python::argument_error(); - } - return s[0]; + if (l == 0) return '\0'; + return PyString_AsString(p)[0]; } PyObject* to_python(unsigned char i)