diff Python/getargs.c @ 41611:c48fe4dc34a9

Change UnicodeDecodeError objects so that the 'object' attribute is a bytes object. Add 'y' and 'y#' format specifiers that work like 's' and 's#' but only accept bytes objects.
author Walter Dörwald <walter@livinglogic.de>
date Fri, 04 May 2007 19:28:21 +0000
parents cfb3e0021b3b
children c6568550b1c6
line wrap: on
line diff
--- a/Python/getargs.c	Fri May 04 17:44:47 2007 +0000
+++ b/Python/getargs.c	Fri May 04 19:28:21 2007 +0000
@@ -819,6 +819,32 @@
 		break;
 	}
 
+	case 'y': {/* bytes */
+		if (*format == '#') {
+			void **p = (void **)va_arg(*p_va, char **);
+			FETCH_SIZE;
+			
+			if (PyBytes_Check(arg)) {
+				*p = PyBytes_AS_STRING(arg);
+				STORE_SIZE(PyBytes_GET_SIZE(arg));
+			}
+			else
+				return converterr("bytes", arg, msgbuf, bufsize);
+			format++;
+		} else {
+			char **p = va_arg(*p_va, char **);
+			
+			if (PyBytes_Check(arg))
+				*p = PyBytes_AS_STRING(arg);
+			else
+				return converterr("bytes", arg, msgbuf, bufsize);
+			if ((Py_ssize_t)strlen(*p) != PyBytes_Size(arg))
+				return converterr("bytes without null bytes",
+						  arg, msgbuf, bufsize);
+		}
+		break;
+	}
+
 	case 'z': {/* string, may be NULL (None) */
 		if (*format == '#') { /* any buffer-like object */
 			void **p = (void **)va_arg(*p_va, char **);
@@ -1595,6 +1621,7 @@
 	
 	case 's': /* string */
 	case 'z': /* string or None */
+	case 'y': /* bytes */
 	case 'u': /* unicode string */
 	case 't': /* buffer, read-only */
 	case 'w': /* buffer, read-write */