diff Python/compile.c @ 9293:0a67db47e1d0 legacy-trunk

Changes to support other object types besides strings as the code string of code objects, as long as they support the (readonly) buffer interface. By Greg Stein.
author Guido van Rossum <guido@python.org>
date Wed, 07 Oct 1998 19:42:25 +0000
parents 1995cdbe3384
children 1fe48009d8ea
line wrap: on
line diff
--- a/Python/compile.c	Wed Oct 07 16:46:54 1998 +0000
+++ b/Python/compile.c	Wed Oct 07 19:42:25 1998 +0000
@@ -121,9 +121,11 @@
 {
 	char buf[500];
 	int lineno = -1;
-	char *p = PyString_AS_STRING(co->co_code);
+	unsigned char *p;
 	char *filename = "???";
 	char *name = "???";
+
+	_PyCode_GETCODEPTR(co, &p);
 	if (*p == SET_LINENO)
 		lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8);
 	if (co->co_filename && PyString_Check(co->co_filename))
@@ -146,8 +148,7 @@
 	if (cmp) return cmp;
 	cmp = co->co_flags - cp->co_flags;
 	if (cmp) return cmp;
-	cmp = PyObject_Compare((PyObject *)co->co_code,
-			       (PyObject *)cp->co_code);
+	cmp = PyObject_Compare(co->co_code, cp->co_code);
 	if (cmp) return cmp;
 	cmp = PyObject_Compare(co->co_consts, cp->co_consts);
 	if (cmp) return cmp;
@@ -162,7 +163,7 @@
 	PyCodeObject *co;
 {
 	long h, h1, h2, h3, h4;
-	h1 = PyObject_Hash((PyObject *)co->co_code);
+	h1 = PyObject_Hash(co->co_code);
 	if (h1 == -1) return -1;
 	h2 = PyObject_Hash(co->co_consts);
 	if (h2 == -1) return -1;
@@ -216,9 +217,10 @@
 {
 	PyCodeObject *co;
 	int i;
+	PyBufferProcs *pb;
 	/* Check argument types */
 	if (argcount < 0 || nlocals < 0 ||
-	    code == NULL || !PyString_Check(code) ||
+	    code == NULL ||
 	    consts == NULL || !PyTuple_Check(consts) ||
 	    names == NULL || !PyTuple_Check(names) ||
 	    varnames == NULL || !PyTuple_Check(varnames) ||
@@ -228,6 +230,15 @@
 		PyErr_BadInternalCall();
 		return NULL;
 	}
+	pb = code->ob_type->tp_as_buffer;
+	if (pb == NULL ||
+	    pb->bf_getreadbuffer == NULL ||
+	    pb->bf_getsegcount == NULL ||
+	    (*pb->bf_getsegcount)(code, NULL) != 1)
+	{
+		PyErr_BadInternalCall();
+		return NULL;
+	}
 	/* Make sure names and varnames are all strings, & intern them */
 	for (i = PyTuple_Size(names); --i >= 0; ) {
 		PyObject *v = PyTuple_GetItem(names, i);
@@ -264,7 +275,7 @@
 		co->co_stacksize = stacksize;
 		co->co_flags = flags;
 		Py_INCREF(code);
-		co->co_code = (PyStringObject *)code;
+		co->co_code = code;
 		Py_INCREF(consts);
 		co->co_consts = consts;
 		Py_INCREF(names);