diff Objects/stringobject.c @ 43378:7630248b593c legacy-trunk

Add a bunch of GIL release/acquire points in tp_print implementations and for PyObject_Print(). Closes issue #1164.
author Brett Cannon <bcannon@gmail.com>
date Mon, 17 Sep 2007 03:28:34 +0000
parents 9620134010a5
children 124cbf1a0e3e
line wrap: on
line diff
--- a/Objects/stringobject.c	Mon Sep 17 00:55:00 2007 +0000
+++ b/Objects/stringobject.c	Mon Sep 17 03:28:34 2007 +0000
@@ -788,7 +788,7 @@
 static int
 string_print(PyStringObject *op, FILE *fp, int flags)
 {
-	Py_ssize_t i;
+	Py_ssize_t i, str_len;
 	char c;
 	int quote;
 
@@ -806,6 +806,7 @@
 	if (flags & Py_PRINT_RAW) {
 		char *data = op->ob_sval;
 		Py_ssize_t size = Py_Size(op);
+		Py_BEGIN_ALLOW_THREADS
 		while (size > INT_MAX) {
 			/* Very long strings cannot be written atomically.
 			 * But don't write exactly INT_MAX bytes at a time
@@ -821,6 +822,7 @@
 #else
                 fwrite(data, 1, (int)size, fp);
 #endif
+		Py_END_ALLOW_THREADS
 		return 0;
 	}
 
@@ -830,8 +832,13 @@
 	    !memchr(op->ob_sval, '"', Py_Size(op)))
 		quote = '"';
 
+	str_len = Py_Size(op);
+	Py_BEGIN_ALLOW_THREADS
 	fputc(quote, fp);
-	for (i = 0; i < Py_Size(op); i++) {
+	for (i = 0; i < str_len; i++) {
+		/* Since strings are immutable and the caller should have a
+		reference, accessing the interal buffer should not be an issue
+		with the GIL released. */
 		c = op->ob_sval[i];
 		if (c == quote || c == '\\')
 			fprintf(fp, "\\%c", c);
@@ -847,6 +854,7 @@
 			fputc(c, fp);
 	}
 	fputc(quote, fp);
+	Py_END_ALLOW_THREADS
 	return 0;
 }