From 25871c001f55941fec83e7ce9832e5f45c69a8cf Mon Sep 17 00:00:00 2001
From: Fred Drake <fdrake@acm.org>
Date: Wed, 3 May 2000 15:17:02 +0000
Subject: 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.
---
 Doc/ext/ext.tex  | 28 ++++++++++++++++++++++++++++
 Python/getargs.c | 32 ++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+)

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 **);
-- 
cgit v0.12

