[Python-checkins] python/dist/src/Objects stringobject.c,2.147.6.1,2.147.6.2 unicodeobject.c,2.124.6.6,2.124.6.7

doerwalter@sourceforge.net doerwalter@sourceforge.net
Mon, 22 Apr 2002 04:57:08 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv17892/Objects

Modified Files:
      Tag: release22-maint
	stringobject.c unicodeobject.c 
Log Message:
Backport the following changes:

Misc/NEWS 1.387->1.388
Lib/test/string_tests.py 1.10->1.11, 1.12->1.14, 
Lib/test/test_unicode.py 1.50->1.51, 1.53->1.54, 1.55->1.56
Lib/test/test_string.py 1.15->1.16
Lib/string.py 1.61->1.63
Lib/test/test_userstring.py 1.5->1.6, 1.11, 1.12
Objects/stringobject.c 2.156->2.159
Objects/unicodeobject.c 2.137->2.139
Doc/lib/libstdtypes.tec 1.87->1.88

Add a method zfill to str, unicode and UserString 
and change Lib/string.py accordingly 
(see SF patch http://www.python.org/sf/536241)

This also adds Guido's fix to test_userstring.py
and the subinstance checks in test_string.py
and test_unicode.py.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.147.6.1
retrieving revision 2.147.6.2
diff -C2 -d -r2.147.6.1 -r2.147.6.2
*** stringobject.c	18 Apr 2002 05:16:37 -0000	2.147.6.1
--- stringobject.c	22 Apr 2002 11:57:05 -0000	2.147.6.2
***************
*** 1,3 ****
- 
  /* String object implementation */
  
--- 1,2 ----
***************
*** 606,610 ****
  	/* figure out which quote to use; single is preferred */
  	quote = '\'';
! 	if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
  		quote = '"';
  
--- 605,610 ----
  	/* figure out which quote to use; single is preferred */
  	quote = '\'';
! 	if (strchr(op->ob_sval, '\'') &&
! 	    !strchr(op->ob_sval, '"'))
  		quote = '"';
  
***************
*** 650,654 ****
  		/* figure out which quote to use; single is preferred */
  		quote = '\'';
! 		if (strchr(op->ob_sval, '\'') && !strchr(op->ob_sval, '"'))
  			quote = '"';
  
--- 650,655 ----
  		/* figure out which quote to use; single is preferred */
  		quote = '\'';
! 		if (strchr(op->ob_sval, '\'') &&
! 		    !strchr(op->ob_sval, '"'))
  			quote = '"';
  
***************
*** 2415,2418 ****
--- 2416,2465 ----
  }
  
+ static char zfill__doc__[] =
+ "S.zfill(width) -> string\n"
+ "\n"
+ "Pad a numeric string S with zeros on the left, to fill a field\n"
+ "of the specified width.  The string S is never truncated.";
+ 
+ static PyObject *
+ string_zfill(PyStringObject *self, PyObject *args)
+ {
+     int fill;
+     PyObject *s;
+     char *p;
+ 
+     int width;
+     if (!PyArg_ParseTuple(args, "i:zfill", &width))
+         return NULL;
+ 
+     if (PyString_GET_SIZE(self) >= width) {
+         if (PyString_CheckExact(self)) {
+             Py_INCREF(self);
+             return (PyObject*) self;
+         }
+         else
+             return PyString_FromStringAndSize(
+                 PyString_AS_STRING(self),
+                 PyString_GET_SIZE(self)
+             );
+     }
+ 
+     fill = width - PyString_GET_SIZE(self);
+ 
+     s = pad(self, fill, 0, '0');
+ 
+     if (s == NULL)
+         return NULL;
+ 
+     p = PyString_AS_STRING(s);
+     if (p[fill] == '+' || p[fill] == '-') {
+         /* move sign to beginning of string */
+         p[0] = p[fill];
+         p[fill] = '0';
+     }
+ 
+     return (PyObject*) s;
+ }
+ 
  static char isspace__doc__[] =
  "S.isspace() -> int\n"
***************
*** 2744,2750 ****
  	{"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
  	{"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
! 	{"capitalize", (PyCFunction)string_capitalize,  METH_NOARGS, capitalize__doc__},
  	{"count",      (PyCFunction)string_count,       METH_VARARGS, count__doc__},
! 	{"endswith",   (PyCFunction)string_endswith,    METH_VARARGS, endswith__doc__},
  	{"find",       (PyCFunction)string_find,        METH_VARARGS, find__doc__},
  	{"index",      (PyCFunction)string_index,       METH_VARARGS, index__doc__},
--- 2791,2799 ----
  	{"isalpha", (PyCFunction)string_isalpha, METH_NOARGS, isalpha__doc__},
  	{"isalnum", (PyCFunction)string_isalnum, METH_NOARGS, isalnum__doc__},
! 	{"capitalize", (PyCFunction)string_capitalize,  METH_NOARGS,
! 	 capitalize__doc__},
  	{"count",      (PyCFunction)string_count,       METH_VARARGS, count__doc__},
! 	{"endswith",   (PyCFunction)string_endswith,    METH_VARARGS,
! 	 endswith__doc__},
  	{"find",       (PyCFunction)string_find,        METH_VARARGS, find__doc__},
  	{"index",      (PyCFunction)string_index,       METH_VARARGS, index__doc__},
***************
*** 2754,2772 ****
  	{"rindex",      (PyCFunction)string_rindex,     METH_VARARGS, rindex__doc__},
  	{"rstrip",      (PyCFunction)string_rstrip,     METH_VARARGS, rstrip__doc__},
! 	{"startswith",  (PyCFunction)string_startswith, METH_VARARGS, startswith__doc__},
  	{"strip",       (PyCFunction)string_strip,      METH_VARARGS, strip__doc__},
! 	{"swapcase",    (PyCFunction)string_swapcase,   METH_NOARGS, swapcase__doc__},
! 	{"translate",   (PyCFunction)string_translate,  METH_VARARGS, translate__doc__},
  	{"title",       (PyCFunction)string_title,      METH_NOARGS, title__doc__},
  	{"ljust",       (PyCFunction)string_ljust,      METH_VARARGS, ljust__doc__},
  	{"rjust",       (PyCFunction)string_rjust,      METH_VARARGS, rjust__doc__},
  	{"center",      (PyCFunction)string_center,     METH_VARARGS, center__doc__},
  	{"encode",      (PyCFunction)string_encode,     METH_VARARGS, encode__doc__},
  	{"decode",      (PyCFunction)string_decode,     METH_VARARGS, decode__doc__},
! 	{"expandtabs",  (PyCFunction)string_expandtabs, METH_VARARGS, expandtabs__doc__},
! 	{"splitlines",  (PyCFunction)string_splitlines, METH_VARARGS, splitlines__doc__},
! #if 0
! 	{"zfill",       (PyCFunction)string_zfill,      METH_VARARGS, zfill__doc__},
! #endif
  	{NULL,     NULL}		     /* sentinel */
  };
--- 2803,2824 ----
  	{"rindex",      (PyCFunction)string_rindex,     METH_VARARGS, rindex__doc__},
  	{"rstrip",      (PyCFunction)string_rstrip,     METH_VARARGS, rstrip__doc__},
! 	{"startswith",  (PyCFunction)string_startswith, METH_VARARGS,
! 	 startswith__doc__},
  	{"strip",       (PyCFunction)string_strip,      METH_VARARGS, strip__doc__},
! 	{"swapcase",    (PyCFunction)string_swapcase,   METH_NOARGS,
! 	 swapcase__doc__},
! 	{"translate",   (PyCFunction)string_translate,  METH_VARARGS,
! 	 translate__doc__},
  	{"title",       (PyCFunction)string_title,      METH_NOARGS, title__doc__},
  	{"ljust",       (PyCFunction)string_ljust,      METH_VARARGS, ljust__doc__},
  	{"rjust",       (PyCFunction)string_rjust,      METH_VARARGS, rjust__doc__},
  	{"center",      (PyCFunction)string_center,     METH_VARARGS, center__doc__},
+ 	{"zfill",       (PyCFunction)string_zfill,      METH_VARARGS, zfill__doc__},
  	{"encode",      (PyCFunction)string_encode,     METH_VARARGS, encode__doc__},
  	{"decode",      (PyCFunction)string_decode,     METH_VARARGS, decode__doc__},
! 	{"expandtabs",  (PyCFunction)string_expandtabs, METH_VARARGS,
! 	 expandtabs__doc__},
! 	{"splitlines",  (PyCFunction)string_splitlines, METH_VARARGS,
! 	 splitlines__doc__},
  	{NULL,     NULL}		     /* sentinel */
  };
***************
*** 3263,3267 ****
  			int sign;
  			int len;
! 			char formatbuf[FORMATBUFLEN]; /* For format{float,int,char}() */
  #ifdef Py_USING_UNICODE
  			char *fmt_start = fmt;
--- 3315,3320 ----
  			int sign;
  			int len;
! 			char formatbuf[FORMATBUFLEN];
! 			     /* For format{float,int,char}() */
  #ifdef Py_USING_UNICODE
  			char *fmt_start = fmt;
***************
*** 3461,3465 ****
  				else {
  					pbuf = formatbuf;
! 					len = formatint(pbuf, sizeof(formatbuf),
  							flags, prec, c, v);
  					if (len < 0)
--- 3514,3519 ----
  				else {
  					pbuf = formatbuf;
! 					len = formatint(pbuf,
! 							sizeof(formatbuf),
  							flags, prec, c, v);
  					if (len < 0)
***************
*** 3477,3481 ****
  			case 'G':
  				pbuf = formatbuf;
! 				len = formatfloat(pbuf, sizeof(formatbuf), flags, prec, c, v);
  				if (len < 0)
  					goto error;
--- 3531,3536 ----
  			case 'G':
  				pbuf = formatbuf;
! 				len = formatfloat(pbuf, sizeof(formatbuf),
! 					  flags, prec, c, v);
  				if (len < 0)
  					goto error;

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.124.6.6
retrieving revision 2.124.6.7
diff -C2 -d -r2.124.6.6 -r2.124.6.7
*** unicodeobject.c	18 Mar 2002 12:47:52 -0000	2.124.6.6
--- unicodeobject.c	22 Apr 2002 11:57:06 -0000	2.124.6.7
***************
*** 4811,4815 ****
  }
  
- #if 0
  static char zfill__doc__[] =
  "S.zfill(width) -> unicode\n\
--- 4811,4814 ----
***************
*** 4829,4834 ****
  
      if (self->length >= width) {
!         Py_INCREF(self);
!         return (PyObject*) self;
      }
  
--- 4828,4840 ----
  
      if (self->length >= width) {
!         if (PyUnicode_CheckExact(self)) {
!             Py_INCREF(self);
!             return (PyObject*) self;
!         }
!         else
!             return PyUnicode_FromUnicode(
!                 PyUnicode_AS_UNICODE(self),
!                 PyUnicode_GET_SIZE(self)
!             );
      }
  
***************
*** 4837,4840 ****
--- 4843,4849 ----
      u = pad(self, fill, 0, '0');
  
+     if (u == NULL)
+         return NULL;
+ 
      if (u->str[fill] == '+' || u->str[fill] == '-') {
          /* move sign to beginning of string */
***************
*** 4845,4849 ****
      return (PyObject*) u;
  }
- #endif
  
  #if 0
--- 4854,4857 ----
***************
*** 4957,4962 ****
      {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
      {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
- #if 0
      {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
      {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
  #endif
--- 4965,4970 ----
      {"isalpha", (PyCFunction) unicode_isalpha, METH_NOARGS, isalpha__doc__},
      {"isalnum", (PyCFunction) unicode_isalnum, METH_NOARGS, isalnum__doc__},
      {"zfill", (PyCFunction) unicode_zfill, METH_VARARGS, zfill__doc__},
+ #if 0
      {"capwords", (PyCFunction) unicode_capwords, METH_NOARGS, capwords__doc__},
  #endif