[Python-checkins] CVS: python/dist/src/Objects stringobject.c,2.127,2.128 tupleobject.c,2.55,2.56

Guido van Rossum gvanrossum@users.sourceforge.net
Wed, 29 Aug 2001 20:12:01 -0700


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

Modified Files:
	stringobject.c tupleobject.c 
Log Message:
Make str and tuple types subclassable.

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.127
retrieving revision 2.128
diff -C2 -d -r2.127 -r2.128
*** stringobject.c	2001/08/27 03:11:09	2.127
--- stringobject.c	2001/08/30 03:11:59	2.128
***************
*** 2684,2687 ****
--- 2684,2690 ----
  };
  
+ staticforward PyObject *
+ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+ 
  static PyObject *
  string_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
***************
*** 2690,2694 ****
  	static char *kwlist[] = {"object", 0};
  
! 	assert(type == &PyString_Type);
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
  		return NULL;
--- 2693,2698 ----
  	static char *kwlist[] = {"object", 0};
  
! 	if (type != &PyString_Type)
! 		return str_subtype_new(type, args, kwds);
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:str", kwlist, &x))
  		return NULL;
***************
*** 2698,2701 ****
--- 2702,2723 ----
  }
  
+ static PyObject *
+ str_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *tmp, *new;
+ 	int n;
+ 
+ 	assert(PyType_IsSubtype(type, &PyString_Type));
+ 	tmp = string_new(&PyString_Type, args, kwds);
+ 	if (tmp == NULL)
+ 		return NULL;
+ 	assert(PyString_Check(tmp));
+ 	new = type->tp_alloc(type, n = PyString_GET_SIZE(tmp));
+ 	if (new == NULL)
+ 		return NULL;
+ 	memcpy(PyString_AS_STRING(new), PyString_AS_STRING(tmp), n+1);
+ 	return new;
+ }
+ 
  static char string_doc[] =
  "str(object) -> string\n\
***************
*** 2725,2729 ****
  	0,					/* tp_setattro */
  	&string_as_buffer,			/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT,			/* tp_flags */
  	string_doc,				/* tp_doc */
  	0,					/* tp_traverse */
--- 2747,2751 ----
  	0,					/* tp_setattro */
  	&string_as_buffer,			/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
  	string_doc,				/* tp_doc */
  	0,					/* tp_traverse */

Index: tupleobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/tupleobject.c,v
retrieving revision 2.55
retrieving revision 2.56
diff -C2 -d -r2.55 -r2.56
*** tupleobject.c	2001/08/29 23:54:21	2.55
--- tupleobject.c	2001/08/30 03:11:59	2.56
***************
*** 476,479 ****
--- 476,482 ----
  }
  
+ staticforward PyObject *
+ tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
+ 
  static PyObject *
  tuple_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
***************
*** 482,486 ****
  	static char *kwlist[] = {"sequence", 0};
  
! 	assert(type == &PyTuple_Type);
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
  		return NULL;
--- 485,490 ----
  	static char *kwlist[] = {"sequence", 0};
  
! 	if (type != &PyTuple_Type)
! 		return tuple_subtype_new(type, args, kwds);
  	if (!PyArg_ParseTupleAndKeywords(args, kwds, "|O:tuple", kwlist, &arg))
  		return NULL;
***************
*** 492,495 ****
--- 496,522 ----
  }
  
+ static PyObject *
+ tuple_subtype_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
+ {
+ 	PyObject *tmp, *new, *item;
+ 	int i, n;
+ 
+ 	assert(PyType_IsSubtype(type, &PyTuple_Type));
+ 	tmp = tuple_new(&PyTuple_Type, args, kwds);
+ 	if (tmp == NULL)
+ 		return NULL;
+ 	assert(PyTuple_Check(tmp));
+ 	new = type->tp_alloc(type, n = PyTuple_GET_SIZE(tmp));
+ 	if (new == NULL)
+ 		return NULL;
+ 	for (i = 0; i < n; i++) {
+ 		item = PyTuple_GET_ITEM(tmp, i);
+ 		Py_INCREF(item);
+ 		PyTuple_SET_ITEM(new, i, item);
+ 	}
+ 	Py_DECREF(tmp);
+ 	return new;
+ }
+ 
  static char tuple_doc[] =
  "tuple(sequence) -> list\n\
***************
*** 530,534 ****
  	0,					/* tp_setattro */
  	0,					/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  	tuple_doc,				/* tp_doc */
   	(traverseproc)tupletraverse,		/* tp_traverse */
--- 557,562 ----
  	0,					/* tp_setattro */
  	0,					/* tp_as_buffer */
! 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
! 		Py_TPFLAGS_BASETYPE,		/* tp_flags */
  	tuple_doc,				/* tp_doc */
   	(traverseproc)tupletraverse,		/* tp_traverse */