[Python-checkins] python/dist/src/Objects stringobject.c, 2.221, 2.222

jhylton at users.sourceforge.net jhylton at users.sourceforge.net
Sat Aug 7 21:20:08 CEST 2004


Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv9225/Objects

Modified Files:
	stringobject.c 
Log Message:
Subclasses of string can no longer be interned.  The semantics of
interning were not clear here -- a subclass could be mutable, for
example -- and had bugs.  Explicitly interning a subclass of string
via intern() will raise a TypeError.  Internal operations that attempt
to intern a string subclass will have no effect.

Added a few tests to test_builtin that includes the old buggy code and
verifies that calls like PyObject_SetAttr() don't fail.  Perhaps these
tests should have gone in test_string.


Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.221
retrieving revision 2.222
diff -C2 -d -r2.221 -r2.222
*** stringobject.c	8 Jul 2004 19:13:55 -0000	2.221
--- stringobject.c	7 Aug 2004 19:20:05 -0000	2.222
***************
*** 4314,4317 ****
--- 4314,4321 ----
  	if (s == NULL || !PyString_Check(s))
  		Py_FatalError("PyString_InternInPlace: strings only please!");
+ 	/* If it's a string subclass, we don't really know what putting
+ 	   it in the interned dict might do. */
+ 	if (!PyString_CheckExact(s))
+ 		return;
  	if (PyString_CHECK_INTERNED(s))
  		return;
***************
*** 4323,4327 ****
  		}
  	}
! 	if ((t = PyDict_GetItem(interned, (PyObject *)s)) != NULL) {
  		Py_INCREF(t);
  		Py_DECREF(*p);
--- 4327,4332 ----
  		}
  	}
! 	t = PyDict_GetItem(interned, (PyObject *)s);
! 	if (t) {
  		Py_INCREF(t);
  		Py_DECREF(*p);
***************
*** 4329,4356 ****
  		return;
  	}
- 	/* Ensure that only true string objects appear in the intern dict */
- 	if (!PyString_CheckExact(s)) {
- 		t = PyString_FromStringAndSize(PyString_AS_STRING(s),
- 						PyString_GET_SIZE(s));
- 		if (t == NULL) {
- 			PyErr_Clear();
- 			return;
- 		}
- 	} else {
- 		t = (PyObject*) s;
- 		Py_INCREF(t);
- 	}
  
! 	if (PyDict_SetItem(interned, t, t) == 0) {
! 		/* The two references in interned are not counted by
! 		refcnt.  The string deallocator will take care of this */
! 		((PyObject *)t)->ob_refcnt-=2;
! 		PyString_CHECK_INTERNED(t) = SSTATE_INTERNED_MORTAL;
! 		Py_DECREF(*p);
! 		*p = t;
  		return;
  	}
! 	Py_DECREF(t);
! 	PyErr_Clear();
  }
  
--- 4334,4346 ----
  		return;
  	}
  
! 	if (PyDict_SetItem(interned, s, s) < 0) {
! 		PyErr_Clear();
  		return;
  	}
! 	/* The two references in interned are not counted by refcnt.
! 	   The string deallocator will take care of this */
! 	(*p)->ob_refcnt -= 2;
! 	PyString_CHECK_INTERNED(s) = SSTATE_INTERNED_MORTAL;
  }
  



More information about the Python-checkins mailing list