[Python-3000-checkins] r51634 - in python/branches/p3yk: Lib/test/test_set.py Objects/setobject.c

georg.brandl python-3000-checkins at python.org
Mon Aug 28 21:37:12 CEST 2006


Author: georg.brandl
Date: Mon Aug 28 21:37:11 2006
New Revision: 51634

Modified:
   python/branches/p3yk/Lib/test/test_set.py
   python/branches/p3yk/Objects/setobject.c
Log:
Fix str() and repr() of empty sets.



Modified: python/branches/p3yk/Lib/test/test_set.py
==============================================================================
--- python/branches/p3yk/Lib/test/test_set.py	(original)
+++ python/branches/p3yk/Lib/test/test_set.py	Mon Aug 28 21:37:11 2006
@@ -631,7 +631,7 @@
         self.set    = set(self.values)
         self.dup    = set(self.values)
         self.length = 0
-        self.repr   = "{}"
+        self.repr   = "set()"
 
 #------------------------------------------------------------------------------
 

Modified: python/branches/p3yk/Objects/setobject.c
==============================================================================
--- python/branches/p3yk/Objects/setobject.c	(original)
+++ python/branches/p3yk/Objects/setobject.c	Mon Aug 28 21:37:11 2006
@@ -529,10 +529,17 @@
 	Py_ssize_t pos=0;
 	char *emit = "";	/* No separator emitted on first pass */
 	char *separator = ", ";
+	int literalform = 0;
 
-	if (so->ob_type == &PySet_Type)
+	if (!so->used) {
+		fprintf(fp, "%s()", so->ob_type->tp_name);
+		return 0;
+	}
+
+	if (so->ob_type == &PySet_Type) {
+		literalform = 1;
 		fprintf(fp, "{");
-	else
+	} else
 		fprintf(fp, "%s([", so->ob_type->tp_name);
 	while (set_next(so, &pos, &entry)) {
 		fputs(emit, fp);
@@ -540,7 +547,7 @@
 		if (PyObject_Print(entry->key, fp, 0) != 0)
 			return -1;
 	}
-	if (so->ob_type == &PySet_Type)
+	if (literalform)
 		fputs("}", fp);
 	else
 		fputs("])", fp);
@@ -552,6 +559,10 @@
 {
 	PyObject *keys, *result, *listrepr;
 
+	/* shortcut for the empty set */
+	if (!so->used)
+		return PyString_FromFormat("%s()", so->ob_type->tp_name);
+
 	keys = PySequence_List((PyObject *)so);
 	if (keys == NULL)
 		return NULL;
@@ -567,7 +578,7 @@
 		result = PyString_FromFormat("{%s}", s);
 	} else {
 		result = PyString_FromFormat("%s(%s)", so->ob_type->tp_name,
-			 PyString_AS_STRING(listrepr));
+			 	PyString_AS_STRING(listrepr));
 	}
 	Py_DECREF(listrepr);
 	return result;


More information about the Python-3000-checkins mailing list