summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFred Drake <fdrake@acm.org>2000-05-03 15:17:02 (GMT)
committerFred Drake <fdrake@acm.org>2000-05-03 15:17:02 (GMT)
commit25871c001f55941fec83e7ce9832e5f45c69a8cf (patch)
tree82d9108087ec195c76712a9e1fa87de0223c7970
parentae08853fb0c77067baa2b8bdc28f4e74df4f9f68 (diff)
downloadcpython-25871c001f55941fec83e7ce9832e5f45c69a8cf.zip
cpython-25871c001f55941fec83e7ce9832e5f45c69a8cf.tar.gz
cpython-25871c001f55941fec83e7ce9832e5f45c69a8cf.tar.bz2
Brian Hooper <brian_takashi@hotmail.com>:
Added 'u' and 'u#' tags for PyArg_ParseTuple - these turn a PyUnicodeObject argument into a Py_UNICODE * buffer, or a Py_UNICODE * buffer plus a length with the '#'. Also added an analog to 'U' for Py_BuildValue.
-rw-r--r--Doc/ext/ext.tex28
-rw-r--r--Python/getargs.c32
2 files changed, 60 insertions, 0 deletions
diff --git a/Doc/ext/ext.tex b/Doc/ext/ext.tex
index cf5b1ef..e4a42a8 100644
--- a/Doc/ext/ext.tex
+++ b/Doc/ext/ext.tex
@@ -691,6 +691,17 @@ case the C pointer is set to \NULL{}.
\item[\samp{z\#} (string or \code{None}) {[char *, int]}]
This is to \samp{s\#} as \samp{z} is to \samp{s}.
+\item[\samp{u} (Unicode string) {[Py_UNICODE *]}]
+Convert a Python Unicode object to a C pointer to a null-terminated
+buffer of Unicode (UCS-2) data. As with \samp{s}, there is no need
+to provide storage for the Unicode data buffer; a pointer to the
+existing Unicode data is stored into the Py_UNICODE pointer variable whose
+address you pass.
+
+\item[\samp{u\#} (Unicode string) {[Py_UNICODE *, int]}]
+This variant on \samp{u} stores into two C variables, the first one
+a pointer to a Unicode data buffer, the second one its length.
+
\item[\samp{b} (integer) {[char]}]
Convert a Python integer to a tiny int, stored in a C \ctype{char}.
@@ -751,6 +762,11 @@ Like \samp{O} but requires that the Python object is a string object.
Raises \exception{TypeError} if the object is not a string object.
The C variable may also be declared as \ctype{PyObject *}.
+\item[\samp{U} (Unicode string) {[PyUnicodeObject *]}]
+Like \samp{O} but requires that the Python object is a Unicode object.
+Raises \exception{TypeError} if the object is not a Unicode object.
+The C variable may also be declared as \ctype{PyObject *}.
+
\item[\samp{t\#} (read-only character buffer) {[char *, int]}]
Like \samp{s\#}, but accepts any object which implements the read-only
buffer interface. The \ctype{char *} variable is set to point to the
@@ -1016,6 +1032,15 @@ Convert a Unicode (UCS-2) data buffer and its length to a Python
Unicode object. If the Unicode buffer pointer is \NULL, the length
is ignored and \code{None} is returned.
+\item[\samp{u} (Unicode string) {[Py_UNICODE *]}]
+Convert a null-terminated buffer of Unicode (UCS-2) data to a Python Unicode
+object. If the Unicode buffer pointer is \NULL{}, \code{None} is returned.
+
+\item[\samp{u\#} (Unicode string) {[Py_UNICODE *, int]}]
+Convert a Unicode (UCS-2) data buffer and its length to a Python Unicode
+object. If the Unicode buffer pointer is \NULL{}, the length is ignored and
+\code{None} is returned.
+
\item[\samp{i} (integer) {[int]}]
Convert a plain C \ctype{int} to a Python integer object.
@@ -1050,6 +1075,9 @@ exception. If no exception has been raised yet,
\item[\samp{S} (object) {[PyObject *]}]
Same as \samp{O}.
+\item[\samp{U} (object) {[PyObject *]}]
+Same as \samp{O}.
+
\item[\samp{N} (object) {[PyObject *]}]
Same as \samp{O}, except it doesn't increment the reference count on
the object. Useful when the object is created by a call to an object
diff --git a/Python/getargs.c b/Python/getargs.c
index 1e88a88..0579aff 100644
--- a/Python/getargs.c
+++ b/Python/getargs.c
@@ -777,6 +777,38 @@ convertsimple1(arg, p_format, p_va)
break;
}
+ case 'u': /* raw unicode buffer (Py_UNICODE *) */
+ {
+ if (*format == '#') { /* any buffer-like object */
+ void **p = (void **)va_arg(*p_va, char **);
+ PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
+ int *q = va_arg(*p_va, int *);
+ int count;
+
+ if ( pb == NULL ||
+ pb->bf_getreadbuffer == NULL ||
+ pb->bf_getsegcount == NULL )
+ return "read-only buffer";
+ if ( (*pb->bf_getsegcount)(arg, NULL) != 1 )
+ return "single-segment read-only buffer";
+ if ( (count =
+ (*pb->bf_getreadbuffer)(arg, 0, p)) < 0 )
+ return "(unspecified)";
+ /* buffer interface returns bytes, we want
+ length in characters */
+ *q = count/(sizeof(Py_UNICODE));
+ format++;
+ } else {
+ Py_UNICODE **p = va_arg(*p_va, Py_UNICODE **);
+
+ if (PyUnicode_Check(arg))
+ *p = PyUnicode_AS_UNICODE(arg);
+ else
+ return "unicode";
+ }
+ break;
+ }
+
case 'S': /* string object */
{
PyObject **p = va_arg(*p_va, PyObject **);