summaryrefslogtreecommitdiff
path: root/Python/compile.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1998-10-07 19:42:25 +0000
committerGuido van Rossum <guido@python.org>1998-10-07 19:42:25 +0000
commitd076c73cc81a974794c9aa7e812931b74d6e03c2 (patch)
tree55b5c7b6c145d5491c2dd63780c9654c51aa2306 /Python/compile.c
parent437ff8600a2959e87194f1491ba99116d73ea543 (diff)
downloadcpython-git-d076c73cc81a974794c9aa7e812931b74d6e03c2.tar.gz
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.
Diffstat (limited to 'Python/compile.c')
-rw-r--r--Python/compile.c23
1 files changed, 17 insertions, 6 deletions
diff --git a/Python/compile.c b/Python/compile.c
index 9871b0ff03..19f18e692d 100644
--- a/Python/compile.c
+++ b/Python/compile.c
@@ -121,9 +121,11 @@ code_repr(co)
{
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 @@ code_compare(co, cp)
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 @@ code_hash(co)
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 @@ PyCode_New(argcount, nlocals, stacksize, flags,
{
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 @@ PyCode_New(argcount, nlocals, stacksize, flags,
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 @@ PyCode_New(argcount, nlocals, stacksize, flags,
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);