[Python-checkins] CVS: python/dist/src/Python getargs.c,2.47,2.48

M.-A. Lemburg python-dev@python.org
Thu, 21 Sep 2000 14:08:34 -0700


Update of /cvsroot/python/python/dist/src/Python
In directory slayer.i.sourceforge.net:/tmp/cvs-serv3039/Python

Modified Files:
	getargs.c 
Log Message:
Special case the "s#" PyArg_Parse() token for Unicode objects:
"s#" will now return a pointer to the default encoded string data
of the Unicode object instead of a pointer to the raw UTF-16
data.

The latter is still available via PyObject_AsReadBuffer().

The patch also adds an optimization for string objects which is
based on the fact that string objects return the raw character data
for getreadbuffer access and are always single-segment.

Index: getargs.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Python/getargs.c,v
retrieving revision 2.47
retrieving revision 2.48
diff -C2 -r2.47 -r2.48
*** getargs.c	2000/09/15 12:52:19	2.47
--- getargs.c	2000/09/21 21:08:30	2.48
***************
*** 557,576 ****
  	case 's': /* string */
  		{
! 			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)";
! 				*q = count;
  				format++;
  			} else {
--- 557,590 ----
  	case 's': /* string */
  		{
! 			if (*format == '#') {
  				void **p = (void **)va_arg(*p_va, char **);
  				int *q = va_arg(*p_va, int *);
  
! 				if (PyString_Check(arg)) {
! 				    *p = PyString_AS_STRING(arg);
! 				    *q = PyString_GET_SIZE(arg);
! 				}
! 				else if (PyUnicode_Check(arg)) {
! 				    arg = _PyUnicode_AsDefaultEncodedString(
! 							            arg, NULL);
! 				    if (arg == NULL)
! 					return "unicode conversion error";
! 				    *p = PyString_AS_STRING(arg);
! 				    *q = PyString_GET_SIZE(arg);
! 				}
! 				else { /* any buffer-like object */
! 				    PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
! 				    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)";
! 				    *q = count;
! 				}
  				format++;
  			} else {
***************
*** 598,619 ****
  			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 (arg == Py_None) {
  				  *p = 0;
  				  *q = 0;
! 				} else {
! 				  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)";
! 				  *q = count;
  				}
  				format++;
--- 612,646 ----
  			if (*format == '#') { /* any buffer-like object */
  				void **p = (void **)va_arg(*p_va, char **);
  				int *q = va_arg(*p_va, int *);
  
  				if (arg == Py_None) {
  				  *p = 0;
  				  *q = 0;
! 				}
! 				else if (PyString_Check(arg)) {
! 				    *p = PyString_AS_STRING(arg);
! 				    *q = PyString_GET_SIZE(arg);
! 				}
! 				else if (PyUnicode_Check(arg)) {
! 				    arg = _PyUnicode_AsDefaultEncodedString(
! 							            arg, NULL);
! 				    if (arg == NULL)
! 					return "unicode conversion error";
! 				    *p = PyString_AS_STRING(arg);
! 				    *q = PyString_GET_SIZE(arg);
! 				}
! 				else { /* any buffer-like object */
! 				    PyBufferProcs *pb = arg->ob_type->tp_as_buffer;
! 				    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)";
! 				    *q = count;
  				}
  				format++;