From 79f25d9a7b853a9f491a0cfe4b81eeb9e2d19569 Mon Sep 17 00:00:00 2001 From: Guido van Rossum Date: Tue, 29 Apr 1997 20:08:16 +0000 Subject: Quickly renamed the remaining files -- this directory is done. --- Python/bltinmodule.c | 1339 +++++++++++++++++++++++++------------------------ Python/compile.c | 643 ++++++++++++------------ Python/getargs.c | 191 +++---- Python/import.c | 552 ++++++++++---------- Python/importdl.c | 150 +++--- Python/importdl.h | 9 +- Python/marshal.c | 317 ++++++------ Python/modsupport.c | 206 ++++---- Python/sigcheck.c | 9 +- Python/structmember.c | 166 +++--- 10 files changed, 1834 insertions(+), 1748 deletions(-) diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c index da76f18..3fdaac9 100644 --- a/Python/bltinmodule.c +++ b/Python/bltinmodule.c @@ -31,12 +31,10 @@ PERFORMANCE OF THIS SOFTWARE. /* Built-in functions */ -#include "allobjects.h" +#include "Python.h" #include "node.h" #include "graminit.h" -#include "bltinmodule.h" -#include "import.h" #include "compile.h" #include "eval.h" @@ -49,104 +47,106 @@ PERFORMANCE OF THIS SOFTWARE. #endif /* Forward */ -static object *filterstring PROTO((object *, object *)); -static object *filtertuple PROTO((object *, object *)); -static object *int_from_string PROTO((object *)); -static object *long_from_string PROTO((object *)); -static object *float_from_string PROTO((object *)); +static PyObject *filterstring Py_PROTO((PyObject *, PyObject *)); +static PyObject *filtertuple Py_PROTO((PyObject *, PyObject *)); +static PyObject *int_from_string Py_PROTO((PyObject *)); +static PyObject *long_from_string Py_PROTO((PyObject *)); +static PyObject *float_from_string Py_PROTO((PyObject *)); -static object * +static PyObject * builtin___import__(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; - object *globals = NULL; - object *locals = NULL; - object *fromlist = NULL; + PyObject *globals = NULL; + PyObject *locals = NULL; + PyObject *fromlist = NULL; - if (!newgetargs(args, "s|OOO:__import__", + if (!PyArg_ParseTuple(args, "s|OOO:__import__", &name, &globals, &locals, &fromlist)) return NULL; - return import_module(name); + return PyImport_ImportModule(name); } -static object * +static PyObject * builtin_abs(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nm; + PyObject *v; + PyNumberMethods *nm; - if (!newgetargs(args, "O:abs", &v)) + if (!PyArg_ParseTuple(args, "O:abs", &v)) return NULL; if ((nm = v->ob_type->tp_as_number) == NULL) { - err_setstr(TypeError, "abs() requires numeric argument"); + PyErr_SetString(PyExc_TypeError, + "abs() requires numeric argument"); return NULL; } return (*nm->nb_absolute)(v); } -static object * +static PyObject * builtin_apply(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *func, *alist = NULL, *kwdict = NULL; + PyObject *func, *alist = NULL, *kwdict = NULL; - if (!newgetargs(args, "O|OO:apply", &func, &alist, &kwdict)) + if (!PyArg_ParseTuple(args, "O|OO:apply", &func, &alist, &kwdict)) return NULL; - if (alist != NULL && !is_tupleobject(alist)) { - err_setstr(TypeError, "apply() 2nd argument must be tuple"); + if (alist != NULL && !PyTuple_Check(alist)) { + PyErr_SetString(PyExc_TypeError, + "apply() 2nd argument must be tuple"); return NULL; } - if (kwdict != NULL && !is_dictobject(kwdict)) { - err_setstr(TypeError, + if (kwdict != NULL && !PyDict_Check(kwdict)) { + PyErr_SetString(PyExc_TypeError, "apply() 3rd argument must be dictionary"); return NULL; } return PyEval_CallObjectWithKeywords(func, alist, kwdict); } -static object * +static PyObject * builtin_callable(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:callable", &v)) + if (!PyArg_ParseTuple(args, "O:callable", &v)) return NULL; - return newintobject((long)callable(v)); + return PyInt_FromLong((long)PyCallable_Check(v)); } -static object * +static PyObject * builtin_filter(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *func, *seq, *result; - sequence_methods *sqf; + PyObject *func, *seq, *result; + PySequenceMethods *sqf; int len; register int i, j; - if (!newgetargs(args, "OO:filter", &func, &seq)) + if (!PyArg_ParseTuple(args, "OO:filter", &func, &seq)) return NULL; - if (is_stringobject(seq)) { - object *r = filterstring(func, seq); + if (PyString_Check(seq)) { + PyObject *r = filterstring(func, seq); return r; } - if (is_tupleobject(seq)) { - object *r = filtertuple(func, seq); + if (PyTuple_Check(seq)) { + PyObject *r = filtertuple(func, seq); return r; } if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "argument 2 to filter() must be a sequence type"); goto Fail_2; } @@ -154,132 +154,133 @@ builtin_filter(self, args) if ((len = (*sqf->sq_length)(seq)) < 0) goto Fail_2; - if (is_listobject(seq) && seq->ob_refcnt == 1) { - INCREF(seq); + if (PyList_Check(seq) && seq->ob_refcnt == 1) { + Py_INCREF(seq); result = seq; } else { - if ((result = newlistobject(len)) == NULL) + if ((result = PyList_New(len)) == NULL) goto Fail_2; } for (i = j = 0; ; ++i) { - object *item, *good; + PyObject *item, *good; int ok; if ((item = (*sqf->sq_item)(seq, i)) == NULL) { if (i < len) goto Fail_1; - if (err_occurred() == IndexError) { - err_clear(); + if (PyErr_Occurred() == PyExc_IndexError) { + PyErr_Clear(); break; } goto Fail_1; } - if (func == None) { + if (func == Py_None) { good = item; - INCREF(good); + Py_INCREF(good); } else { - object *arg = mkvalue("(O)", item); + PyObject *arg = Py_BuildValue("(O)", item); if (arg == NULL) goto Fail_1; - good = call_object(func, arg); - DECREF(arg); + good = PyEval_CallObject(func, arg); + Py_DECREF(arg); if (good == NULL) { - DECREF(item); + Py_DECREF(item); goto Fail_1; } } - ok = testbool(good); - DECREF(good); + ok = PyObject_IsTrue(good); + Py_DECREF(good); if (ok) { if (j < len) { - if (setlistitem(result, j++, item) < 0) + if (PyList_SetItem(result, j++, item) < 0) goto Fail_1; } else { j++; - if (addlistitem(result, item) < 0) + if (PyList_Append(result, item) < 0) goto Fail_1; } } else { - DECREF(item); + Py_DECREF(item); } } - if (j < len && setlistslice(result, j, len, NULL) < 0) + if (j < len && PyList_SetSlice(result, j, len, NULL) < 0) goto Fail_1; return result; Fail_1: - DECREF(result); + Py_DECREF(result); Fail_2: return NULL; } -static object * +static PyObject * builtin_chr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { long x; char s[1]; - if (!newgetargs(args, "l:chr", &x)) + if (!PyArg_ParseTuple(args, "l:chr", &x)) return NULL; if (x < 0 || x >= 256) { - err_setstr(ValueError, "chr() arg not in range(256)"); + PyErr_SetString(PyExc_ValueError, + "chr() arg not in range(256)"); return NULL; } s[0] = (char)x; - return newsizedstringobject(s, 1); + return PyString_FromStringAndSize(s, 1); } -static object * +static PyObject * builtin_cmp(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *a, *b; + PyObject *a, *b; - if (!newgetargs(args, "OO:cmp", &a, &b)) + if (!PyArg_ParseTuple(args, "OO:cmp", &a, &b)) return NULL; - return newintobject((long)cmpobject(a, b)); + return PyInt_FromLong((long)PyObject_Compare(a, b)); } -static object * +static PyObject * builtin_coerce(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v, *w; - object *res; + PyObject *v, *w; + PyObject *res; - if (!newgetargs(args, "OO:coerce", &v, &w)) + if (!PyArg_ParseTuple(args, "OO:coerce", &v, &w)) return NULL; - if (coerce(&v, &w) < 0) + if (PyNumber_Coerce(&v, &w) < 0) return NULL; - res = mkvalue("(OO)", v, w); - DECREF(v); - DECREF(w); + res = Py_BuildValue("(OO)", v, w); + Py_DECREF(v); + Py_DECREF(w); return res; } -static object * +static PyObject * builtin_compile(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *str; char *filename; char *startstr; int start; - if (!newgetargs(args, "sss:compile", &str, &filename, &startstr)) + if (!PyArg_ParseTuple(args, "sss:compile", &str, &filename, &startstr)) return NULL; if (strcmp(startstr, "exec") == 0) start = file_input; @@ -288,122 +289,122 @@ builtin_compile(self, args) else if (strcmp(startstr, "single") == 0) start = single_input; else { - err_setstr(ValueError, + PyErr_SetString(PyExc_ValueError, "compile() mode must be 'exec' or 'eval' or 'single'"); return NULL; } - return compile_string(str, filename, start); + return Py_CompileString(str, filename, start); } #ifndef WITHOUT_COMPLEX -static object * +static PyObject * builtin_complex(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *r, *i, *tmp; - number_methods *nbr, *nbi = NULL; + PyObject *r, *i, *tmp; + PyNumberMethods *nbr, *nbi = NULL; Py_complex cr, ci; int own_r = 0; i = NULL; - if (!newgetargs(args, "O|O:complex", &r, &i)) + if (!PyArg_ParseTuple(args, "O|O:complex", &r, &i)) return NULL; if ((nbr = r->ob_type->tp_as_number) == NULL || nbr->nb_float == NULL || (i != NULL && ((nbi = i->ob_type->tp_as_number) == NULL || nbi->nb_float == NULL))) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "complex() argument can't be converted to complex"); return NULL; } /* XXX Hack to support classes with __complex__ method */ - if (is_instanceobject(r)) { - static object *complexstr; - object *f; + if (PyInstance_Check(r)) { + static PyObject *complexstr; + PyObject *f; if (complexstr == NULL) { complexstr = PyString_InternFromString("__complex__"); if (complexstr == NULL) return NULL; } - f = getattro(r, complexstr); + f = PyObject_GetAttr(r, complexstr); if (f == NULL) - err_clear(); + PyErr_Clear(); else { - object *args = mkvalue("()"); + PyObject *args = Py_BuildValue("()"); if (args == NULL) return NULL; - r = call_object(f, args); - DECREF(args); + r = PyEval_CallObject(f, args); + Py_DECREF(args); if (r == NULL) return NULL; own_r = 1; } } - if (is_complexobject(r)) { - cr = ((complexobject*)r)->cval; + if (PyComplex_Check(r)) { + cr = ((PyComplexObject*)r)->cval; if (own_r) - DECREF(r); + Py_DECREF(r); } else { tmp = (*nbr->nb_float)(r); if (own_r) - DECREF(r); + Py_DECREF(r); if (tmp == NULL) return NULL; - cr.real = getfloatvalue(tmp); - DECREF(tmp); + cr.real = PyFloat_AsDouble(tmp); + Py_DECREF(tmp); cr.imag = 0.; } if (i == NULL) { ci.real = 0.; ci.imag = 0.; } - else if (is_complexobject(i)) - ci = ((complexobject*)i)->cval; + else if (PyComplex_Check(i)) + ci = ((PyComplexObject*)i)->cval; else { tmp = (*nbi->nb_float)(i); if (tmp == NULL) return NULL; - ci.real = getfloatvalue(tmp); - DECREF(tmp); + ci.real = PyFloat_AsDouble(tmp); + Py_DECREF(tmp); ci.imag = 0.; } cr.real -= ci.imag; cr.imag += ci.real; - return newcomplexobject(cr); + return PyComplex_FromCComplex(cr); } #endif -static object * +static PyObject * builtin_dir(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v = NULL; - object *d; + PyObject *v = NULL; + PyObject *d; - if (!newgetargs(args, "|O:dir", &v)) + if (!PyArg_ParseTuple(args, "|O:dir", &v)) return NULL; if (v == NULL) { - d = getlocals(); - INCREF(d); + d = PyEval_GetLocals(); + Py_INCREF(d); } else { - d = getattr(v, "__dict__"); + d = PyObject_GetAttrString(v, "__dict__"); if (d == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "dir() argument must have __dict__ attribute"); return NULL; } } - if (is_dictobject(d)) { - v = getdictkeys(d); - if (sortlist(v) != 0) { - DECREF(v); + if (PyDict_Check(d)) { + v = PyDict_Keys(d); + if (PyList_Sort(v) != 0) { + Py_DECREF(v); v = NULL; } } @@ -411,244 +412,247 @@ builtin_dir(self, args) v = PyObject_CallMethod(d, "keys", NULL); if (v == NULL) { PyErr_Clear(); - v = newlistobject(0); + v = PyList_New(0); } } - DECREF(d); + Py_DECREF(d); return v; } -static object * +static PyObject * do_divmod(v, w) - object *v, *w; + PyObject *v, *w; { - object *res; + PyObject *res; - if (is_instanceobject(v) || is_instanceobject(w)) - return instancebinop(v, w, "__divmod__", "__rdivmod__", + if (PyInstance_Check(v) || PyInstance_Check(w)) + return PyInstance_DoBinOp(v, w, "__divmod__", "__rdivmod__", do_divmod); if (v->ob_type->tp_as_number == NULL || w->ob_type->tp_as_number == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "divmod() requires numeric or class instance arguments"); return NULL; } - if (coerce(&v, &w) != 0) + if (PyNumber_Coerce(&v, &w) != 0) return NULL; res = (*v->ob_type->tp_as_number->nb_divmod)(v, w); - DECREF(v); - DECREF(w); + Py_DECREF(v); + Py_DECREF(w); return res; } -static object * +static PyObject * builtin_divmod(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v, *w; + PyObject *v, *w; - if (!newgetargs(args, "OO:divmod", &v, &w)) + if (!PyArg_ParseTuple(args, "OO:divmod", &v, &w)) return NULL; return do_divmod(v, w); } -static object * +static PyObject * builtin_eval(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *cmd; - object *globals = None, *locals = None; + PyObject *cmd; + PyObject *globals = Py_None, *locals = Py_None; char *str; - if (!newgetargs(args, "O|O!O!:eval", + if (!PyArg_ParseTuple(args, "O|O!O!:eval", &cmd, - &Mappingtype, &globals, - &Mappingtype, &locals)) + &PyDict_Type, &globals, + &PyDict_Type, &locals)) return NULL; - if (globals == None) { - globals = getglobals(); - if (locals == None) - locals = getlocals(); + if (globals == Py_None) { + globals = PyEval_GetGlobals(); + if (locals == Py_None) + locals = PyEval_GetLocals(); } - else if (locals == None) + else if (locals == Py_None) locals = globals; - if (dictlookup(globals, "__builtins__") == NULL) { - if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { + if (PyDict_SetItemString(globals, "__builtins__", + PyEval_GetBuiltins()) != 0) return NULL; } - if (is_codeobject(cmd)) - return eval_code((codeobject *) cmd, globals, locals); - if (!is_stringobject(cmd)) { - err_setstr(TypeError, + if (PyCode_Check(cmd)) + return PyEval_EvalCode((PyCodeObject *) cmd, globals, locals); + if (!PyString_Check(cmd)) { + PyErr_SetString(PyExc_TypeError, "eval() argument 1 must be string or code object"); return NULL; } - str = getstringvalue(cmd); - if ((int)strlen(str) != getstringsize(cmd)) { - err_setstr(ValueError, + str = PyString_AsString(cmd); + if ((int)strlen(str) != PyString_Size(cmd)) { + PyErr_SetString(PyExc_ValueError, "embedded '\\0' in string arg"); return NULL; } while (*str == ' ' || *str == '\t') str++; - return run_string(str, eval_input, globals, locals); + return PyRun_String(str, eval_input, globals, locals); } -static object * +static PyObject * builtin_execfile(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *filename; - object *globals = None, *locals = None; - object *res; + PyObject *globals = Py_None, *locals = Py_None; + PyObject *res; FILE* fp; - if (!newgetargs(args, "s|O!O!:execfile", + if (!PyArg_ParseTuple(args, "s|O!O!:execfile", &filename, - &Mappingtype, &globals, - &Mappingtype, &locals)) + &PyDict_Type, &globals, + &PyDict_Type, &locals)) return NULL; - if (globals == None) { - globals = getglobals(); - if (locals == None) - locals = getlocals(); + if (globals == Py_None) { + globals = PyEval_GetGlobals(); + if (locals == Py_None) + locals = PyEval_GetLocals(); } - else if (locals == None) + else if (locals == Py_None) locals = globals; - if (dictlookup(globals, "__builtins__") == NULL) { - if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { + if (PyDict_SetItemString(globals, "__builtins__", + PyEval_GetBuiltins()) != 0) return NULL; } - BGN_SAVE + Py_BEGIN_ALLOW_THREADS fp = fopen(filename, "r"); - END_SAVE + Py_END_ALLOW_THREADS if (fp == NULL) { - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); return NULL; } - res = run_file(fp, filename, file_input, globals, locals); - BGN_SAVE + res = PyRun_File(fp, filename, file_input, globals, locals); + Py_BEGIN_ALLOW_THREADS fclose(fp); - END_SAVE + Py_END_ALLOW_THREADS return res; } -static object * +static PyObject * builtin_float(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nb; + PyObject *v; + PyNumberMethods *nb; - if (!newgetargs(args, "O:float", &v)) + if (!PyArg_ParseTuple(args, "O:float", &v)) return NULL; - if (is_stringobject(v)) + if (PyString_Check(v)) return float_from_string(v); if ((nb = v->ob_type->tp_as_number) == NULL || nb->nb_float == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "float() argument can't be converted to float"); return NULL; } return (*nb->nb_float)(v); } -static object * +static PyObject * builtin_getattr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - object *name; + PyObject *v; + PyObject *name; - if (!newgetargs(args, "OS:getattr", &v, &name)) + if (!PyArg_ParseTuple(args, "OS:getattr", &v, &name)) return NULL; - return getattro(v, name); + return PyObject_GetAttr(v, name); } -static object * +static PyObject * builtin_globals(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *d; + PyObject *d; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; - d = getglobals(); - INCREF(d); + d = PyEval_GetGlobals(); + Py_INCREF(d); return d; } -static object * +static PyObject * builtin_hasattr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - object *name; + PyObject *v; + PyObject *name; - if (!newgetargs(args, "OS:hasattr", &v, &name)) + if (!PyArg_ParseTuple(args, "OS:hasattr", &v, &name)) return NULL; - v = getattro(v, name); + v = PyObject_GetAttr(v, name); if (v == NULL) { - err_clear(); - return newintobject(0L); + PyErr_Clear(); + return PyInt_FromLong(0L); } - DECREF(v); - return newintobject(1L); + Py_DECREF(v); + return PyInt_FromLong(1L); } -static object * +static PyObject * builtin_id(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:id", &v)) + if (!PyArg_ParseTuple(args, "O:id", &v)) return NULL; - return newintobject((long)v); + return PyInt_FromLong((long)v); } -static object * +static PyObject * builtin_map(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { typedef struct { - object *seq; - sequence_methods *sqf; + PyObject *seq; + PySequenceMethods *sqf; int len; } sequence; - object *func, *result; + PyObject *func, *result; sequence *seqs = NULL, *sqp; int n, len; register int i, j; - n = gettuplesize(args); + n = PyTuple_Size(args); if (n < 2) { - err_setstr(TypeError, "map() requires at least two args"); + PyErr_SetString(PyExc_TypeError, + "map() requires at least two args"); return NULL; } - func = gettupleitem(args, 0); + func = PyTuple_GetItem(args, 0); n--; - if ((seqs = NEW(sequence, n)) == NULL) { - err_nomem(); + if ((seqs = PyMem_NEW(sequence, n)) == NULL) { + PyErr_NoMemory(); goto Fail_2; } for (len = 0, i = 0, sqp = seqs; i < n; ++i, ++sqp) { int curlen; - if ((sqp->seq = gettupleitem(args, i + 1)) == NULL) + if ((sqp->seq = PyTuple_GetItem(args, i + 1)) == NULL) goto Fail_2; if (! (sqp->sqf = sqp->seq->ob_type->tp_as_sequence)) { @@ -657,7 +661,7 @@ builtin_map(self, args) char errbuf[sizeof(errmsg) + 3]; sprintf(errbuf, errmsg, i+2); - err_setstr(TypeError, errbuf); + PyErr_SetString(PyExc_TypeError, errbuf); goto Fail_2; } @@ -668,35 +672,36 @@ builtin_map(self, args) len = curlen; } - if ((result = (object *) newlistobject(len)) == NULL) + if ((result = (PyObject *) PyList_New(len)) == NULL) goto Fail_2; /* XXX Special case map(None, single_list) could be more efficient */ for (i = 0; ; ++i) { - object *alist, *item=NULL, *value; + PyObject *alist, *item=NULL, *value; int any = 0; - if (func == None && n == 1) + if (func == Py_None && n == 1) alist = NULL; else { - if ((alist = newtupleobject(n)) == NULL) + if ((alist = PyTuple_New(n)) == NULL) goto Fail_1; } for (j = 0, sqp = seqs; j < n; ++j, ++sqp) { if (sqp->len < 0) { - INCREF(None); - item = None; + Py_INCREF(Py_None); + item = Py_None; } else { item = (*sqp->sqf->sq_item)(sqp->seq, i); if (item == NULL) { if (i < sqp->len) goto Fail_0; - if (err_occurred() == IndexError) { - err_clear(); - INCREF(None); - item = None; + if (PyErr_Occurred() == + PyExc_IndexError) { + PyErr_Clear(); + Py_INCREF(Py_None); + item = Py_None; sqp->len = -1; } else { @@ -709,14 +714,14 @@ builtin_map(self, args) } if (!alist) break; - if (settupleitem(alist, j, item) < 0) { - DECREF(item); + if (PyTuple_SetItem(alist, j, item) < 0) { + Py_DECREF(item); goto Fail_0; } continue; Fail_0: - XDECREF(alist); + Py_XDECREF(alist); goto Fail_1; } @@ -724,134 +729,135 @@ builtin_map(self, args) alist = item; if (!any) { - DECREF(alist); + Py_DECREF(alist); break; } - if (func == None) + if (func == Py_None) value = alist; else { - value = call_object(func, alist); - DECREF(alist); + value = PyEval_CallObject(func, alist); + Py_DECREF(alist); if (value == NULL) goto Fail_1; } if (i >= len) { - if (addlistitem(result, value) < 0) + if (PyList_Append(result, value) < 0) goto Fail_1; } else { - if (setlistitem(result, i, value) < 0) + if (PyList_SetItem(result, i, value) < 0) goto Fail_1; } } - DEL(seqs); + PyMem_DEL(seqs); return result; Fail_1: - DECREF(result); + Py_DECREF(result); Fail_2: - if (seqs) DEL(seqs); + if (seqs) PyMem_DEL(seqs); return NULL; } -static object * +static PyObject * builtin_setattr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - object *name; - object *value; + PyObject *v; + PyObject *name; + PyObject *value; - if (!newgetargs(args, "OSO:setattr", &v, &name, &value)) + if (!PyArg_ParseTuple(args, "OSO:setattr", &v, &name, &value)) return NULL; - if (setattro(v, name, value) != 0) + if (PyObject_SetAttr(v, name, value) != 0) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * builtin_delattr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - object *name; + PyObject *v; + PyObject *name; - if (!newgetargs(args, "OS:delattr", &v, &name)) + if (!PyArg_ParseTuple(args, "OS:delattr", &v, &name)) return NULL; - if (setattro(v, name, (object *)NULL) != 0) + if (PyObject_SetAttr(v, name, (PyObject *)NULL) != 0) return NULL; - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * builtin_hash(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; long x; - if (!newgetargs(args, "O:hash", &v)) + if (!PyArg_ParseTuple(args, "O:hash", &v)) return NULL; - x = hashobject(v); + x = PyObject_Hash(v); if (x == -1) return NULL; - return newintobject(x); + return PyInt_FromLong(x); } -static object * +static PyObject * builtin_hex(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nb; + PyObject *v; + PyNumberMethods *nb; - if (!newgetargs(args, "O:hex", &v)) + if (!PyArg_ParseTuple(args, "O:hex", &v)) return NULL; if ((nb = v->ob_type->tp_as_number) == NULL || nb->nb_hex == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "hex() argument can't be converted to hex"); return NULL; } return (*nb->nb_hex)(v); } -static object *builtin_raw_input PROTO((object *, object *)); +static PyObject *builtin_raw_input Py_PROTO((PyObject *, PyObject *)); -static object * +static PyObject * builtin_input(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *line; + PyObject *line; char *str; - object *res; - object *globals, *locals; + PyObject *res; + PyObject *globals, *locals; line = builtin_raw_input(self, args); if (line == NULL) return line; - if (!getargs(line, "s;embedded '\\0' in input line", &str)) + if (!PyArg_Parse(line, "s;embedded '\\0' in input line", &str)) return NULL; while (*str == ' ' || *str == '\t') str++; - globals = getglobals(); - locals = getlocals(); - if (dictlookup(globals, "__builtins__") == NULL) { - if (dictinsert(globals, "__builtins__", getbuiltins()) != 0) + globals = PyEval_GetGlobals(); + locals = PyEval_GetLocals(); + if (PyDict_GetItemString(globals, "__builtins__") == NULL) { + if (PyDict_SetItemString(globals, "__builtins__", + PyEval_GetBuiltins()) != 0) return NULL; } - res = run_string(str, eval_input, globals, locals); - DECREF(line); + res = PyRun_String(str, eval_input, globals, locals); + Py_DECREF(line); return res; } @@ -868,37 +874,37 @@ builtin_intern(self, args) return s; } -static object * +static PyObject * builtin_int(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nb; + PyObject *v; + PyNumberMethods *nb; - if (!newgetargs(args, "O:int", &v)) + if (!PyArg_ParseTuple(args, "O:int", &v)) return NULL; - if (is_stringobject(v)) + if (PyString_Check(v)) return int_from_string(v); if ((nb = v->ob_type->tp_as_number) == NULL || nb->nb_int == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "int() argument can't be converted to int"); return NULL; } return (*nb->nb_int)(v); } -static object * +static PyObject * builtin_len(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; long len; - typeobject *tp; + PyTypeObject *tp; - if (!newgetargs(args, "O:len", &v)) + if (!PyArg_ParseTuple(args, "O:len", &v)) return NULL; tp = v->ob_type; if (tp->tp_as_sequence != NULL) { @@ -908,47 +914,47 @@ builtin_len(self, args) len = (*tp->tp_as_mapping->mp_length)(v); } else { - err_setstr(TypeError, "len() of unsized object"); + PyErr_SetString(PyExc_TypeError, "len() of unsized object"); return NULL; } if (len < 0) return NULL; else - return newintobject(len); + return PyInt_FromLong(len); } -static object * +static PyObject * builtin_list(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - sequence_methods *sqf; + PyObject *v; + PySequenceMethods *sqf; - if (!newgetargs(args, "O:list", &v)) + if (!PyArg_ParseTuple(args, "O:list", &v)) return NULL; if ((sqf = v->ob_type->tp_as_sequence) != NULL) { int n = (*sqf->sq_length)(v); int i; - object *l; + PyObject *l; if (n < 0) return NULL; - l = newlistobject(n); + l = PyList_New(n); if (l == NULL) return NULL; for (i = 0; i < n; i++) { - object *item = (*sqf->sq_item)(v, i); + PyObject *item = (*sqf->sq_item)(v, i); if (item == NULL) { - DECREF(l); + Py_DECREF(l); l = NULL; break; } - setlistitem(l, i, item); + PyList_SetItem(l, i, item); } /* XXX Should support indefinite-length sequences */ return l; } - err_setstr(TypeError, "list() argument must be a sequence"); + PyErr_SetString(PyExc_TypeError, "list() argument must be a sequence"); return NULL; } @@ -974,247 +980,252 @@ builtin_slice(self, args) return PySlice_New(start, stop, step); } -static object * +static PyObject * builtin_locals(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *d; + PyObject *d; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; - d = getlocals(); - INCREF(d); + d = PyEval_GetLocals(); + Py_INCREF(d); return d; } -static object * +static PyObject * builtin_long(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nb; + PyObject *v; + PyNumberMethods *nb; - if (!newgetargs(args, "O:long", &v)) + if (!PyArg_ParseTuple(args, "O:long", &v)) return NULL; - if (is_stringobject(v)) + if (PyString_Check(v)) return long_from_string(v); if ((nb = v->ob_type->tp_as_number) == NULL || nb->nb_long == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "long() argument can't be converted to long"); return NULL; } return (*nb->nb_long)(v); } -static object * +static PyObject * min_max(args, sign) - object *args; + PyObject *args; int sign; { int i; - object *v, *w, *x; - sequence_methods *sq; + PyObject *v, *w, *x; + PySequenceMethods *sq; - if (gettuplesize(args) > 1) + if (PyTuple_Size(args) > 1) v = args; - else if (!newgetargs(args, "O:min/max", &v)) + else if (!PyArg_ParseTuple(args, "O:min/max", &v)) return NULL; sq = v->ob_type->tp_as_sequence; if (sq == NULL) { - err_setstr(TypeError, "min() or max() of non-sequence"); + PyErr_SetString(PyExc_TypeError, + "min() or max() of non-sequence"); return NULL; } w = NULL; for (i = 0; ; i++) { x = (*sq->sq_item)(v, i); /* Implies INCREF */ if (x == NULL) { - if (err_occurred() == IndexError) { - err_clear(); + if (PyErr_Occurred() == PyExc_IndexError) { + PyErr_Clear(); break; } - XDECREF(w); + Py_XDECREF(w); return NULL; } if (w == NULL) w = x; else { - if (cmpobject(x, w) * sign > 0) { - DECREF(w); + if (PyObject_Compare(x, w) * sign > 0) { + Py_DECREF(w); w = x; } else - DECREF(x); + Py_DECREF(x); } } if (w == NULL) - err_setstr(ValueError, "min() or max() of empty sequence"); + PyErr_SetString(PyExc_ValueError, + "min() or max() of empty sequence"); return w; } -static object * +static PyObject * builtin_min(self, v) - object *self; - object *v; + PyObject *self; + PyObject *v; { return min_max(v, -1); } -static object * +static PyObject * builtin_max(self, v) - object *self; - object *v; + PyObject *self; + PyObject *v; { return min_max(v, 1); } -static object * +static PyObject * builtin_oct(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - number_methods *nb; + PyObject *v; + PyNumberMethods *nb; - if (!newgetargs(args, "O:oct", &v)) + if (!PyArg_ParseTuple(args, "O:oct", &v)) return NULL; if (v == NULL || (nb = v->ob_type->tp_as_number) == NULL || nb->nb_oct == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "oct() argument can't be converted to oct"); return NULL; } return (*nb->nb_oct)(v); } -static object * +static PyObject * builtin_open(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *mode = "r"; int bufsize = -1; - object *f; + PyObject *f; - if (!newgetargs(args, "s|si:open", &name, &mode, &bufsize)) + if (!PyArg_ParseTuple(args, "s|si:open", &name, &mode, &bufsize)) return NULL; - f = newfileobject(name, mode); + f = PyFile_FromString(name, mode); if (f != NULL) - setfilebufsize(f, bufsize); + PyFile_SetBufSize(f, bufsize); return f; } -static object * +static PyObject * builtin_ord(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char c; - if (!newgetargs(args, "c:ord", &c)) + if (!PyArg_ParseTuple(args, "c:ord", &c)) return NULL; - return newintobject((long)(c & 0xff)); + return PyInt_FromLong((long)(c & 0xff)); } -static object * +static PyObject * do_pow(v, w) - object *v, *w; + PyObject *v, *w; { - object *res; - if (is_instanceobject(v) || is_instanceobject(w)) - return instancebinop(v, w, "__pow__", "__rpow__", do_pow); + PyObject *res; + if (PyInstance_Check(v) || PyInstance_Check(w)) + return PyInstance_DoBinOp(v, w, "__pow__", "__rpow__", do_pow); if (v->ob_type->tp_as_number == NULL || w->ob_type->tp_as_number == NULL) { - err_setstr(TypeError, "pow() requires numeric arguments"); + PyErr_SetString(PyExc_TypeError, + "pow() requires numeric arguments"); return NULL; } if ( #ifndef WITHOUT_COMPLEX - !is_complexobject(v) && + !PyComplex_Check(v) && #endif - is_floatobject(w) && getfloatvalue(v) < 0.0) { - if (!err_occurred()) - err_setstr(ValueError, "negative number to float power"); + PyFloat_Check(w) && PyFloat_AsDouble(v) < 0.0) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ValueError, + "negative number to float power"); return NULL; } - if (coerce(&v, &w) != 0) + if (PyNumber_Coerce(&v, &w) != 0) return NULL; - res = (*v->ob_type->tp_as_number->nb_power)(v, w, None); - DECREF(v); - DECREF(w); + res = (*v->ob_type->tp_as_number->nb_power)(v, w, Py_None); + Py_DECREF(v); + Py_DECREF(w); return res; } -static object * +static PyObject * builtin_pow(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v, *w, *z = None, *res; - object *v1, *z1, *w2, *z2; + PyObject *v, *w, *z = Py_None, *res; + PyObject *v1, *z1, *w2, *z2; - if (!newgetargs(args, "OO|O:pow", &v, &w, &z)) + if (!PyArg_ParseTuple(args, "OO|O:pow", &v, &w, &z)) return NULL; - if (z == None) + if (z == Py_None) return do_pow(v, w); /* XXX The ternary version doesn't do class instance coercions */ - if (is_instanceobject(v)) + if (PyInstance_Check(v)) return v->ob_type->tp_as_number->nb_power(v, w, z); if (v->ob_type->tp_as_number == NULL || z->ob_type->tp_as_number == NULL || w->ob_type->tp_as_number == NULL) { - err_setstr(TypeError, "pow() requires numeric arguments"); + PyErr_SetString(PyExc_TypeError, + "pow() requires numeric arguments"); return NULL; } - if (coerce(&v, &w) != 0) + if (PyNumber_Coerce(&v, &w) != 0) return NULL; res = NULL; v1 = v; z1 = z; - if (coerce(&v1, &z1) != 0) + if (PyNumber_Coerce(&v1, &z1) != 0) goto error2; w2 = w; z2 = z1; - if (coerce(&w2, &z2) != 0) + if (PyNumber_Coerce(&w2, &z2) != 0) goto error1; res = (*v1->ob_type->tp_as_number->nb_power)(v1, w2, z2); - DECREF(w2); - DECREF(z2); + Py_DECREF(w2); + Py_DECREF(z2); error1: - DECREF(v1); - DECREF(z1); + Py_DECREF(v1); + Py_DECREF(z1); error2: - DECREF(v); - DECREF(w); + Py_DECREF(v); + Py_DECREF(w); return res; } -static object * +static PyObject * builtin_range(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { long ilow = 0, ihigh = 0, istep = 1; int i, n; - object *v; + PyObject *v; - if (gettuplesize(args) <= 1) { - if (!newgetargs(args, + if (PyTuple_Size(args) <= 1) { + if (!PyArg_ParseTuple(args, "l;range() requires 1-3 int arguments", &ihigh)) return NULL; } else { - if (!newgetargs(args, + if (!PyArg_ParseTuple(args, "ll|l;range() requires 1-3 int arguments", &ilow, &ihigh, &istep)) return NULL; } if (istep == 0) { - err_setstr(ValueError, "zero step for range()"); + PyErr_SetString(PyExc_ValueError, "zero step for range()"); return NULL; } /* XXX ought to check overflow of subtraction */ @@ -1224,43 +1235,43 @@ builtin_range(self, args) n = (ihigh - ilow + istep + 1) / istep; if (n < 0) n = 0; - v = newlistobject(n); + v = PyList_New(n); if (v == NULL) return NULL; for (i = 0; i < n; i++) { - object *w = newintobject(ilow); + PyObject *w = PyInt_FromLong(ilow); if (w == NULL) { - DECREF(v); + Py_DECREF(v); return NULL; } - setlistitem(v, i, w); + PyList_SetItem(v, i, w); ilow += istep; } return v; } -static object * +static PyObject * builtin_xrange(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { long ilow = 0, ihigh = 0, istep = 1; long n; - if (gettuplesize(args) <= 1) { - if (!newgetargs(args, + if (PyTuple_Size(args) <= 1) { + if (!PyArg_ParseTuple(args, "l;xrange() requires 1-3 int arguments", &ihigh)) return NULL; } else { - if (!newgetargs(args, + if (!PyArg_ParseTuple(args, "ll|l;xrange() requires 1-3 int arguments", &ilow, &ihigh, &istep)) return NULL; } if (istep == 0) { - err_setstr(ValueError, "zero step for xrange()"); + PyErr_SetString(PyExc_ValueError, "zero step for xrange()"); return NULL; } /* XXX ought to check overflow of subtraction */ @@ -1270,107 +1281,107 @@ builtin_xrange(self, args) n = (ihigh - ilow + istep + 1) / istep; if (n < 0) n = 0; - return newrangeobject(ilow, n, istep, 1); + return PyRange_New(ilow, n, istep, 1); } -extern char *my_readline PROTO((char *)); +extern char *PyOS_Readline Py_PROTO((char *)); -static object * +static PyObject * builtin_raw_input(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v = NULL; - object *f; + PyObject *v = NULL; + PyObject *f; - if (!newgetargs(args, "|O:[raw_]input", &v)) + if (!PyArg_ParseTuple(args, "|O:[raw_]input", &v)) return NULL; - if (getfilefile(sysget("stdin")) == stdin && - getfilefile(sysget("stdout")) == stdout && + if (PyFile_AsFile(PySys_GetObject("stdin")) == stdin && + PyFile_AsFile(PySys_GetObject("stdout")) == stdout && isatty(fileno(stdin)) && isatty(fileno(stdout))) { - object *po; + PyObject *po; char *prompt; char *s; - object *result; + PyObject *result; if (v != NULL) { - po = strobject(v); + po = PyObject_Str(v); if (po == NULL) return NULL; - prompt = getstringvalue(po); + prompt = PyString_AsString(po); } else { po = NULL; prompt = ""; } - s = my_readline(prompt); - XDECREF(po); + s = PyOS_Readline(prompt); + Py_XDECREF(po); if (s == NULL) { - err_set(KeyboardInterrupt); + PyErr_SetNone(PyExc_KeyboardInterrupt); return NULL; } if (*s == '\0') { - err_set(EOFError); + PyErr_SetNone(PyExc_EOFError); result = NULL; } else { /* strip trailing '\n' */ - result = newsizedstringobject(s, strlen(s)-1); + result = PyString_FromStringAndSize(s, strlen(s)-1); } free(s); return result; } if (v != NULL) { - f = sysget("stdout"); + f = PySys_GetObject("stdout"); if (f == NULL) { - err_setstr(RuntimeError, "lost sys.stdout"); + PyErr_SetString(PyExc_RuntimeError, "lost sys.stdout"); return NULL; } - flushline(); - if (writeobject(v, f, PRINT_RAW) != 0) + Py_FlushLine(); + if (PyFile_WriteObject(v, f, Py_PRINT_RAW) != 0) return NULL; } - f = sysget("stdin"); + f = PySys_GetObject("stdin"); if (f == NULL) { - err_setstr(RuntimeError, "lost sys.stdin"); + PyErr_SetString(PyExc_RuntimeError, "lost sys.stdin"); return NULL; } - return filegetline(f, -1); + return PyFile_GetLine(f, -1); } -static object * +static PyObject * builtin_reduce(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *seq, *func, *result = NULL; - sequence_methods *sqf; + PyObject *seq, *func, *result = NULL; + PySequenceMethods *sqf; register int i; - if (!newgetargs(args, "OO|O:reduce", &func, &seq, &result)) + if (!PyArg_ParseTuple(args, "OO|O:reduce", &func, &seq, &result)) return NULL; if (result != NULL) - INCREF(result); + Py_INCREF(result); if ((sqf = seq->ob_type->tp_as_sequence) == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "2nd argument to reduce() must be a sequence object"); return NULL; } - if ((args = newtupleobject(2)) == NULL) + if ((args = PyTuple_New(2)) == NULL) goto Fail; for (i = 0; ; ++i) { - object *op2; + PyObject *op2; if (args->ob_refcnt > 1) { - DECREF(args); - if ((args = newtupleobject(2)) == NULL) + Py_DECREF(args); + if ((args = PyTuple_New(2)) == NULL) goto Fail; } if ((op2 = (*sqf->sq_item)(seq, i)) == NULL) { - if (err_occurred() == IndexError) { - err_clear(); + if (PyErr_Occurred() == PyExc_IndexError) { + PyErr_Clear(); break; } goto Fail; @@ -1379,62 +1390,62 @@ builtin_reduce(self, args) if (result == NULL) result = op2; else { - settupleitem(args, 0, result); - settupleitem(args, 1, op2); - if ((result = call_object(func, args)) == NULL) + PyTuple_SetItem(args, 0, result); + PyTuple_SetItem(args, 1, op2); + if ((result = PyEval_CallObject(func, args)) == NULL) goto Fail; } } - DECREF(args); + Py_DECREF(args); if (result == NULL) - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "reduce of empty sequence with no initial value"); return result; Fail: - XDECREF(args); - XDECREF(result); + Py_XDECREF(args); + Py_XDECREF(result); return NULL; } -static object * +static PyObject * builtin_reload(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:reload", &v)) + if (!PyArg_ParseTuple(args, "O:reload", &v)) return NULL; - return reload_module(v); + return PyImport_ReloadModule(v); } -static object * +static PyObject * builtin_repr(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:repr", &v)) + if (!PyArg_ParseTuple(args, "O:repr", &v)) return NULL; - return reprobject(v); + return PyObject_Repr(v); } -static object * +static PyObject * builtin_round(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { double x; double f; int ndigits = 0; int i; - if (!newgetargs(args, "d|i:round", &x, &ndigits)) + if (!PyArg_ParseTuple(args, "d|i:round", &x, &ndigits)) return NULL; f = 1.0; for (i = ndigits; --i >= 0; ) @@ -1442,53 +1453,54 @@ builtin_round(self, args) for (i = ndigits; ++i <= 0; ) f = f*0.1; if (x >= 0.0) - return newfloatobject(floor(x*f + 0.5) / f); + return PyFloat_FromDouble(floor(x*f + 0.5) / f); else - return newfloatobject(ceil(x*f - 0.5) / f); + return PyFloat_FromDouble(ceil(x*f - 0.5) / f); } -static object * +static PyObject * builtin_str(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:str", &v)) + if (!PyArg_ParseTuple(args, "O:str", &v)) return NULL; - return strobject(v); + return PyObject_Str(v); } -static object * +static PyObject * builtin_tuple(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; - sequence_methods *sqf; + PyObject *v; + PySequenceMethods *sqf; - if (!newgetargs(args, "O:tuple", &v)) + if (!PyArg_ParseTuple(args, "O:tuple", &v)) return NULL; - if (is_tupleobject(v)) { - INCREF(v); + if (PyTuple_Check(v)) { + Py_INCREF(v); return v; } - if (is_listobject(v)) - return listtuple(v); - if (is_stringobject(v)) { - int n = getstringsize(v); - object *t = newtupleobject(n); + if (PyList_Check(v)) + return PyList_AsTuple(v); + if (PyString_Check(v)) { + int n = PyString_Size(v); + PyObject *t = PyTuple_New(n); if (t != NULL) { int i; - char *p = getstringvalue(v); + char *p = PyString_AsString(v); for (i = 0; i < n; i++) { - object *item = newsizedstringobject(p+i, 1); + PyObject *item = + PyString_FromStringAndSize(p+i, 1); if (item == NULL) { - DECREF(t); + Py_DECREF(t); t = NULL; break; } - settupleitem(t, i, item); + PyTuple_SetItem(t, i, item); } } return t; @@ -1497,66 +1509,68 @@ builtin_tuple(self, args) if ((sqf = v->ob_type->tp_as_sequence) != NULL) { int n = (*sqf->sq_length)(v); int i; - object *t; + PyObject *t; if (n < 0) return NULL; - t = newtupleobject(n); + t = PyTuple_New(n); if (t == NULL) return NULL; for (i = 0; i < n; i++) { - object *item = (*sqf->sq_item)(v, i); + PyObject *item = (*sqf->sq_item)(v, i); if (item == NULL) { - DECREF(t); + Py_DECREF(t); t = NULL; break; } - settupleitem(t, i, item); + PyTuple_SetItem(t, i, item); } /* XXX Should support indefinite-length sequences */ return t; } /* None of the above */ - err_setstr(TypeError, "tuple() argument must be a sequence"); + PyErr_SetString(PyExc_TypeError, + "tuple() argument must be a sequence"); return NULL; } -static object * +static PyObject * builtin_type(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v; + PyObject *v; - if (!newgetargs(args, "O:type", &v)) + if (!PyArg_ParseTuple(args, "O:type", &v)) return NULL; - v = (object *)v->ob_type; - INCREF(v); + v = (PyObject *)v->ob_type; + Py_INCREF(v); return v; } -static object * +static PyObject * builtin_vars(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *v = NULL; - object *d; + PyObject *v = NULL; + PyObject *d; - if (!newgetargs(args, "|O:vars", &v)) + if (!PyArg_ParseTuple(args, "|O:vars", &v)) return NULL; if (v == NULL) { - d = getlocals(); + d = PyEval_GetLocals(); if (d == NULL) { - if (!err_occurred()) - err_setstr(SystemError, "no locals!?"); + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_SystemError, + "no locals!?"); } else - INCREF(d); + Py_INCREF(d); } else { - d = getattr(v, "__dict__"); + d = PyObject_GetAttrString(v, "__dict__"); if (d == NULL) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "vars() argument must have __dict__ attribute"); return NULL; } @@ -1564,7 +1578,7 @@ builtin_vars(self, args) return d; } -static struct methodlist builtin_methods[] = { +static PyMethodDef builtin_methods[] = { {"__import__", builtin___import__, 1}, {"abs", builtin_abs, 1}, {"apply", builtin_apply, 1}, @@ -1619,201 +1633,202 @@ static struct methodlist builtin_methods[] = { {NULL, NULL}, }; -static object *builtin_mod; -static object *builtin_dict; +static PyObject *builtin_mod; +static PyObject *builtin_dict; -object * -getbuiltinmod() +PyObject * +PyBuiltin_GetModule() { return builtin_mod; } -object * -getbuiltindict() +PyObject * +PyBuiltin_GetDict() { return builtin_dict; } /* Predefined exceptions */ -object *AccessError; -object *PyExc_AssertionError; -object *AttributeError; -object *EOFError; -object *FloatingPointError; -object *IOError; -object *ImportError; -object *IndexError; -object *KeyError; -object *KeyboardInterrupt; -object *MemoryError; -object *NameError; -object *OverflowError; -object *RuntimeError; -object *SyntaxError; -object *SystemError; -object *SystemExit; -object *TypeError; -object *ValueError; -object *ZeroDivisionError; - -static object * +PyObject *PyExc_AccessError; +PyObject *PyExc_AssertionError; +PyObject *PyExc_AttributeError; +PyObject *PyExc_EOFError; +PyObject *FloatingPointError; +PyObject *PyExc_IOError; +PyObject *PyExc_ImportError; +PyObject *PyExc_IndexError; +PyObject *PyExc_KeyError; +PyObject *PyExc_KeyboardInterrupt; +PyObject *PyExc_MemoryError; +PyObject *PyExc_NameError; +PyObject *PyExc_OverflowError; +PyObject *PyExc_RuntimeError; +PyObject *PyExc_SyntaxError; +PyObject *PyExc_SystemError; +PyObject *PyExc_SystemExit; +PyObject *PyExc_TypeError; +PyObject *PyExc_ValueError; +PyObject *PyExc_ZeroDivisionError; + +static PyObject * newstdexception(name) char *name; { - object *v = newstringobject(name); - if (v == NULL || dictinsert(builtin_dict, name, v) != 0) - fatal("no mem for new standard exception"); + PyObject *v = PyString_FromString(name); + if (v == NULL || PyDict_SetItemString(builtin_dict, name, v) != 0) + Py_FatalError("no mem for new standard exception"); return v; } static void initerrors() { - AccessError = newstdexception("AccessError"); + PyExc_AccessError = newstdexception("AccessError"); PyExc_AssertionError = newstdexception("AssertionError"); - AttributeError = newstdexception("AttributeError"); - EOFError = newstdexception("EOFError"); + PyExc_AttributeError = newstdexception("AttributeError"); + PyExc_EOFError = newstdexception("EOFError"); FloatingPointError = newstdexception("FloatingPointError"); - IOError = newstdexception("IOError"); - ImportError = newstdexception("ImportError"); - IndexError = newstdexception("IndexError"); - KeyError = newstdexception("KeyError"); - KeyboardInterrupt = newstdexception("KeyboardInterrupt"); - MemoryError = newstdexception("MemoryError"); - NameError = newstdexception("NameError"); - OverflowError = newstdexception("OverflowError"); - RuntimeError = newstdexception("RuntimeError"); - SyntaxError = newstdexception("SyntaxError"); - SystemError = newstdexception("SystemError"); - SystemExit = newstdexception("SystemExit"); - TypeError = newstdexception("TypeError"); - ValueError = newstdexception("ValueError"); - ZeroDivisionError = newstdexception("ZeroDivisionError"); + PyExc_IOError = newstdexception("IOError"); + PyExc_ImportError = newstdexception("ImportError"); + PyExc_IndexError = newstdexception("IndexError"); + PyExc_KeyError = newstdexception("KeyError"); + PyExc_KeyboardInterrupt = newstdexception("KeyboardInterrupt"); + PyExc_MemoryError = newstdexception("MemoryError"); + PyExc_NameError = newstdexception("NameError"); + PyExc_OverflowError = newstdexception("OverflowError"); + PyExc_RuntimeError = newstdexception("RuntimeError"); + PyExc_SyntaxError = newstdexception("SyntaxError"); + PyExc_SystemError = newstdexception("SystemError"); + PyExc_SystemExit = newstdexception("SystemExit"); + PyExc_TypeError = newstdexception("TypeError"); + PyExc_ValueError = newstdexception("ValueError"); + PyExc_ZeroDivisionError = newstdexception("ZeroDivisionError"); } void -initbuiltin() +PyBuiltin_Init() { - builtin_mod = initmodule("__builtin__", builtin_methods); - builtin_dict = getmoduledict(builtin_mod); - INCREF(builtin_dict); + builtin_mod = Py_InitModule("__builtin__", builtin_methods); + builtin_dict = PyModule_GetDict(builtin_mod); + Py_INCREF(builtin_dict); initerrors(); - (void) dictinsert(builtin_dict, "None", None); - (void) dictinsert(builtin_dict, "Ellipsis", Py_Ellipsis); - (void) dictinsert(builtin_dict, "__debug__", - newintobject(Py_OptimizeFlag == 0)); - if (err_occurred()) - fatal("error creating None/Ellipsis/__debug__ in __builtin__"); + (void) PyDict_SetItemString(builtin_dict, "None", Py_None); + (void) PyDict_SetItemString(builtin_dict, "Ellipsis", Py_Ellipsis); + (void) PyDict_SetItemString(builtin_dict, "__debug__", + PyInt_FromLong(Py_OptimizeFlag == 0)); + if (PyErr_Occurred()) + Py_FatalError( + "error creating None/Ellipsis/__debug__ in __builtin__"); } /* Helper for filter(): filter a tuple through a function */ -static object * +static PyObject * filtertuple(func, tuple) - object *func; - object *tuple; + PyObject *func; + PyObject *tuple; { - object *result; + PyObject *result; register int i, j; - int len = gettuplesize(tuple); + int len = PyTuple_Size(tuple); if (len == 0) { - INCREF(tuple); + Py_INCREF(tuple); return tuple; } - if ((result = newtupleobject(len)) == NULL) + if ((result = PyTuple_New(len)) == NULL) return NULL; for (i = j = 0; i < len; ++i) { - object *item, *good; + PyObject *item, *good; int ok; - if ((item = gettupleitem(tuple, i)) == NULL) + if ((item = PyTuple_GetItem(tuple, i)) == NULL) goto Fail_1; - if (func == None) { - INCREF(item); + if (func == Py_None) { + Py_INCREF(item); good = item; } else { - object *arg = mkvalue("(O)", item); + PyObject *arg = Py_BuildValue("(O)", item); if (arg == NULL) goto Fail_1; - good = call_object(func, arg); - DECREF(arg); + good = PyEval_CallObject(func, arg); + Py_DECREF(arg); if (good == NULL) goto Fail_1; } - ok = testbool(good); - DECREF(good); + ok = PyObject_IsTrue(good); + Py_DECREF(good); if (ok) { - INCREF(item); - if (settupleitem(result, j++, item) < 0) + Py_INCREF(item); + if (PyTuple_SetItem(result, j++, item) < 0) goto Fail_1; } } - if (resizetuple(&result, j, 0) < 0) + if (_PyTuple_Resize(&result, j, 0) < 0) return NULL; return result; Fail_1: - DECREF(result); + Py_DECREF(result); return NULL; } /* Helper for filter(): filter a string through a function */ -static object * +static PyObject * filterstring(func, strobj) - object *func; - object *strobj; + PyObject *func; + PyObject *strobj; { - object *result; + PyObject *result; register int i, j; - int len = getstringsize(strobj); + int len = PyString_Size(strobj); - if (func == None) { + if (func == Py_None) { /* No character is ever false -- share input string */ - INCREF(strobj); + Py_INCREF(strobj); return strobj; } - if ((result = newsizedstringobject(NULL, len)) == NULL) + if ((result = PyString_FromStringAndSize(NULL, len)) == NULL) return NULL; for (i = j = 0; i < len; ++i) { - object *item, *arg, *good; + PyObject *item, *arg, *good; int ok; item = (*strobj->ob_type->tp_as_sequence->sq_item)(strobj, i); if (item == NULL) goto Fail_1; - arg = mkvalue("(O)", item); - DECREF(item); + arg = Py_BuildValue("(O)", item); + Py_DECREF(item); if (arg == NULL) goto Fail_1; - good = call_object(func, arg); - DECREF(arg); + good = PyEval_CallObject(func, arg); + Py_DECREF(arg); if (good == NULL) goto Fail_1; - ok = testbool(good); - DECREF(good); + ok = PyObject_IsTrue(good); + Py_DECREF(good); if (ok) - GETSTRINGVALUE((stringobject *)result)[j++] = - GETSTRINGVALUE((stringobject *)item)[0]; + PyString_AS_STRING((PyStringObject *)result)[j++] = + PyString_AS_STRING((PyStringObject *)item)[0]; } - if (j < len && resizestring(&result, j) < 0) + if (j < len && _PyString_Resize(&result, j) < 0) return NULL; return result; Fail_1: - DECREF(result); + Py_DECREF(result); return NULL; } diff --git a/Python/compile.c b/Python/compile.c index 21629ab..9d6d395 100644 --- a/Python/compile.c +++ b/Python/compile.c @@ -47,7 +47,7 @@ PERFORMANCE OF THIS SOFTWARE. #define PRIVATE_NAME_MANGLING #endif -#include "allobjects.h" +#include "Python.h" #include "node.h" #include "token.h" @@ -57,7 +57,6 @@ PERFORMANCE OF THIS SOFTWARE. #include "structmember.h" #include -#include int Py_OptimizeFlag = 0; @@ -65,7 +64,7 @@ int Py_OptimizeFlag = 0; #define OP_ASSIGN 1 #define OP_APPLY 2 -#define OFF(x) offsetof(codeobject, x) +#define OFF(x) offsetof(PyCodeObject, x) static struct memberlist code_memberlist[] = { {"co_argcount", T_INT, OFF(co_argcount), READONLY}, @@ -83,50 +82,50 @@ static struct memberlist code_memberlist[] = { {NULL} /* Sentinel */ }; -static object * +static PyObject * code_getattr(co, name) - codeobject *co; + PyCodeObject *co; char *name; { - return getmember((char *)co, code_memberlist, name); + return PyMember_Get((char *)co, code_memberlist, name); } static void code_dealloc(co) - codeobject *co; + PyCodeObject *co; { - XDECREF(co->co_code); - XDECREF(co->co_consts); - XDECREF(co->co_names); - XDECREF(co->co_filename); - XDECREF(co->co_name); - XDECREF(co->co_varnames); - DEL(co); + Py_XDECREF(co->co_code); + Py_XDECREF(co->co_consts); + Py_XDECREF(co->co_names); + Py_XDECREF(co->co_filename); + Py_XDECREF(co->co_name); + Py_XDECREF(co->co_varnames); + PyMem_DEL(co); } -static object * +static PyObject * code_repr(co) - codeobject *co; + PyCodeObject *co; { char buf[500]; int lineno = -1; - char *p = GETSTRINGVALUE(co->co_code); + char *p = PyString_AS_STRING(co->co_code); char *filename = "???"; char *name = "???"; if (*p == SET_LINENO) lineno = (p[1] & 0xff) | ((p[2] & 0xff) << 8); - if (co->co_filename && is_stringobject(co->co_filename)) - filename = getstringvalue(co->co_filename); - if (co->co_name && is_stringobject(co->co_name)) - name = getstringvalue(co->co_name); + if (co->co_filename && PyString_Check(co->co_filename)) + filename = PyString_AsString(co->co_filename); + if (co->co_name && PyString_Check(co->co_name)) + name = PyString_AsString(co->co_name); sprintf(buf, "", name, (long)co, filename, lineno); - return newstringobject(buf); + return PyString_FromString(buf); } static int code_compare(co, cp) - codeobject *co, *cp; + PyCodeObject *co, *cp; { int cmp; cmp = cp->co_argcount - cp->co_argcount; @@ -135,28 +134,29 @@ code_compare(co, cp) if (cmp) return cmp; cmp = cp->co_flags - cp->co_flags; if (cmp) return cmp; - cmp = cmpobject((object *)co->co_code, (object *)cp->co_code); + cmp = PyObject_Compare((PyObject *)co->co_code, + (PyObject *)cp->co_code); if (cmp) return cmp; - cmp = cmpobject(co->co_consts, cp->co_consts); + cmp = PyObject_Compare(co->co_consts, cp->co_consts); if (cmp) return cmp; - cmp = cmpobject(co->co_names, cp->co_names); + cmp = PyObject_Compare(co->co_names, cp->co_names); if (cmp) return cmp; - cmp = cmpobject(co->co_varnames, cp->co_varnames); + cmp = PyObject_Compare(co->co_varnames, cp->co_varnames); return cmp; } static long code_hash(co) - codeobject *co; + PyCodeObject *co; { long h, h1, h2, h3, h4; - h1 = hashobject((object *)co->co_code); + h1 = PyObject_Hash((PyObject *)co->co_code); if (h1 == -1) return -1; - h2 = hashobject(co->co_consts); + h2 = PyObject_Hash(co->co_consts); if (h2 == -1) return -1; - h3 = hashobject(co->co_names); + h3 = PyObject_Hash(co->co_names); if (h3 == -1) return -1; - h4 = hashobject(co->co_varnames); + h4 = PyObject_Hash(co->co_varnames); if (h4 == -1) return -1; h = h1 ^ h2 ^ h3 ^ h4 ^ co->co_argcount ^ co->co_nlocals ^ co->co_flags; @@ -164,11 +164,11 @@ code_hash(co) return h; } -typeobject Codetype = { - OB_HEAD_INIT(&Typetype) +PyTypeObject PyCode_Type = { + PyObject_HEAD_INIT(&PyType_Type) 0, "code", - sizeof(codeobject), + sizeof(PyCodeObject), 0, (destructor)code_dealloc, /*tp_dealloc*/ 0, /*tp_print*/ @@ -185,86 +185,86 @@ typeobject Codetype = { #define NAME_CHARS \ "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz" -codeobject * -newcodeobject(argcount, nlocals, stacksize, flags, +PyCodeObject * +PyCode_New(argcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno, lnotab) int argcount; int nlocals; int stacksize; int flags; - object *code; - object *consts; - object *names; - object *varnames; - object *filename; - object *name; + PyObject *code; + PyObject *consts; + PyObject *names; + PyObject *varnames; + PyObject *filename; + PyObject *name; int firstlineno; - object *lnotab; + PyObject *lnotab; { - codeobject *co; + PyCodeObject *co; int i; /* Check argument types */ if (argcount < 0 || nlocals < 0 || - code == NULL || !is_stringobject(code) || - consts == NULL || !is_tupleobject(consts) || - names == NULL || !is_tupleobject(names) || - varnames == NULL || !is_tupleobject(varnames) || - name == NULL || !is_stringobject(name) || - filename == NULL || !is_stringobject(filename) || - lnotab == NULL || !is_stringobject(lnotab)) { - err_badcall(); + code == NULL || !PyString_Check(code) || + consts == NULL || !PyTuple_Check(consts) || + names == NULL || !PyTuple_Check(names) || + varnames == NULL || !PyTuple_Check(varnames) || + name == NULL || !PyString_Check(name) || + filename == NULL || !PyString_Check(filename) || + lnotab == NULL || !PyString_Check(lnotab)) { + PyErr_BadInternalCall(); return NULL; } /* Make sure names and varnames are all strings, & intern them */ - for (i = gettuplesize(names); --i >= 0; ) { - object *v = gettupleitem(names, i); - if (v == NULL || !is_stringobject(v)) { - err_badcall(); + for (i = PyTuple_Size(names); --i >= 0; ) { + PyObject *v = PyTuple_GetItem(names, i); + if (v == NULL || !PyString_Check(v)) { + PyErr_BadInternalCall(); return NULL; } PyString_InternInPlace(&PyTuple_GET_ITEM(names, i)); } - for (i = gettuplesize(varnames); --i >= 0; ) { - object *v = gettupleitem(varnames, i); - if (v == NULL || !is_stringobject(v)) { - err_badcall(); + for (i = PyTuple_Size(varnames); --i >= 0; ) { + PyObject *v = PyTuple_GetItem(varnames, i); + if (v == NULL || !PyString_Check(v)) { + PyErr_BadInternalCall(); return NULL; } PyString_InternInPlace(&PyTuple_GET_ITEM(varnames, i)); } /* Intern selected string constants */ - for (i = gettuplesize(consts); --i >= 0; ) { - object *v = gettupleitem(consts, i); + for (i = PyTuple_Size(consts); --i >= 0; ) { + PyObject *v = PyTuple_GetItem(consts, i); char *p; - if (!is_stringobject(v)) + if (!PyString_Check(v)) continue; - p = getstringvalue(v); + p = PyString_AsString(v); if ((int)strspn(p, NAME_CHARS) - != getstringsize(v)) + != PyString_Size(v)) continue; PyString_InternInPlace(&PyTuple_GET_ITEM(consts, i)); } - co = NEWOBJ(codeobject, &Codetype); + co = PyObject_NEW(PyCodeObject, &PyCode_Type); if (co != NULL) { co->co_argcount = argcount; co->co_nlocals = nlocals; co->co_stacksize = stacksize; co->co_flags = flags; - INCREF(code); - co->co_code = (stringobject *)code; - INCREF(consts); + Py_INCREF(code); + co->co_code = (PyStringObject *)code; + Py_INCREF(consts); co->co_consts = consts; - INCREF(names); + Py_INCREF(names); co->co_names = names; - INCREF(varnames); + Py_INCREF(varnames); co->co_varnames = varnames; - INCREF(filename); + Py_INCREF(filename); co->co_filename = filename; - INCREF(name); + Py_INCREF(name); co->co_name = name; co->co_firstlineno = firstlineno; - INCREF(lnotab); + Py_INCREF(lnotab); co->co_lnotab = lnotab; } return co; @@ -274,12 +274,12 @@ newcodeobject(argcount, nlocals, stacksize, flags, /* Data structure used internally */ struct compiling { - object *c_code; /* string */ - object *c_consts; /* list of objects */ - object *c_names; /* list of strings (names) */ - object *c_globals; /* dictionary (value=None) */ - object *c_locals; /* dictionary (value=localID) */ - object *c_varnames; /* list (inverse of c_locals) */ + PyObject *c_code; /* string */ + PyObject *c_consts; /* list of objects */ + PyObject *c_names; /* list of strings (names) */ + PyObject *c_globals; /* dictionary (value=None) */ + PyObject *c_locals; /* dictionary (value=localID) */ + PyObject *c_varnames; /* list (inverse of c_locals) */ int c_nlocals; /* index of next local */ int c_argcount; /* number of top-level arguments */ int c_flags; /* same as co_flags */ @@ -297,7 +297,7 @@ struct compiling { int c_stacklevel; /* Current stack level */ int c_maxstacklevel; /* Maximum stack level */ int c_firstlineno; - object *c_lnotab; /* Table mapping address to line number */ + PyObject *c_lnotab; /* Table mapping address to line number */ int c_last_addr, c_last_line, c_lnotab_next; #ifdef PRIVATE_NAME_MANGLING char *c_private; /* for private name mangling */ @@ -310,28 +310,28 @@ struct compiling { static void com_error(c, exc, msg) struct compiling *c; - object *exc; + PyObject *exc; char *msg; { int n = strlen(msg); - object *v; + PyObject *v; char buffer[30]; char *s; c->c_errors++; if (c->c_lineno <= 1) { /* Unknown line number or single interactive command */ - err_setstr(exc, msg); + PyErr_SetString(exc, msg); return; } sprintf(buffer, " (line %d)", c->c_lineno); - v = newsizedstringobject((char *)NULL, n + strlen(buffer)); + v = PyString_FromStringAndSize((char *)NULL, n + strlen(buffer)); if (v == NULL) return; /* MemoryError, too bad */ - s = GETSTRINGVALUE((stringobject *)v); + s = PyString_AS_STRING((PyStringObject *)v); strcpy(s, msg); strcat(s, buffer); - err_setval(exc, v); - DECREF(v); + PyErr_SetObject(exc, v); + Py_DECREF(v); } @@ -343,7 +343,8 @@ block_push(c, type) int type; { if (c->c_nblocks >= CO_MAXBLOCKS) { - com_error(c, SystemError, "too many statically nested blocks"); + com_error(c, PyExc_SystemError, + "too many statically nested blocks"); } else { c->c_block[c->c_nblocks++] = type; @@ -358,55 +359,58 @@ block_pop(c, type) if (c->c_nblocks > 0) c->c_nblocks--; if (c->c_block[c->c_nblocks] != type && c->c_errors == 0) { - com_error(c, SystemError, "bad block pop"); + com_error(c, PyExc_SystemError, "bad block pop"); } } /* Prototype forward declarations */ -static int com_init PROTO((struct compiling *, char *)); -static void com_free PROTO((struct compiling *)); -static void com_push PROTO((struct compiling *, int)); -static void com_pop PROTO((struct compiling *, int)); -static void com_done PROTO((struct compiling *)); -static void com_node PROTO((struct compiling *, struct _node *)); -static void com_factor PROTO((struct compiling *, struct _node *)); -static void com_addbyte PROTO((struct compiling *, int)); -static void com_addint PROTO((struct compiling *, int)); -static void com_addoparg PROTO((struct compiling *, int, int)); -static void com_addfwref PROTO((struct compiling *, int, int *)); -static void com_backpatch PROTO((struct compiling *, int)); -static int com_add PROTO((struct compiling *, object *, object *)); -static int com_addconst PROTO((struct compiling *, object *)); -static int com_addname PROTO((struct compiling *, object *)); -static void com_addopname PROTO((struct compiling *, int, node *)); -static void com_list PROTO((struct compiling *, node *, int)); -static int com_argdefs PROTO((struct compiling *, node *)); -static int com_newlocal PROTO((struct compiling *, char *)); -static codeobject *icompile PROTO((struct _node *, struct compiling *)); -static codeobject *jcompile PROTO((struct _node *, char *, struct compiling *)); -static object *parsestrplus PROTO((node *)); -static object *parsestr PROTO((char *)); +static int com_init Py_PROTO((struct compiling *, char *)); +static void com_free Py_PROTO((struct compiling *)); +static void com_push Py_PROTO((struct compiling *, int)); +static void com_pop Py_PROTO((struct compiling *, int)); +static void com_done Py_PROTO((struct compiling *)); +static void com_node Py_PROTO((struct compiling *, struct _node *)); +static void com_factor Py_PROTO((struct compiling *, struct _node *)); +static void com_addbyte Py_PROTO((struct compiling *, int)); +static void com_addint Py_PROTO((struct compiling *, int)); +static void com_addoparg Py_PROTO((struct compiling *, int, int)); +static void com_addfwref Py_PROTO((struct compiling *, int, int *)); +static void com_backpatch Py_PROTO((struct compiling *, int)); +static int com_add Py_PROTO((struct compiling *, PyObject *, PyObject *)); +static int com_addconst Py_PROTO((struct compiling *, PyObject *)); +static int com_addname Py_PROTO((struct compiling *, PyObject *)); +static void com_addopname Py_PROTO((struct compiling *, int, node *)); +static void com_list Py_PROTO((struct compiling *, node *, int)); +static int com_argdefs Py_PROTO((struct compiling *, node *)); +static int com_newlocal Py_PROTO((struct compiling *, char *)); +static PyCodeObject *icompile Py_PROTO((struct _node *, struct compiling *)); +static PyCodeObject *jcompile Py_PROTO((struct _node *, char *, + struct compiling *)); +static PyObject *parsestrplus Py_PROTO((node *)); +static PyObject *parsestr Py_PROTO((char *)); static int com_init(c, filename) struct compiling *c; char *filename; { - if ((c->c_code = newsizedstringobject((char *)NULL, 1000)) == NULL) + if ((c->c_code = PyString_FromStringAndSize((char *)NULL, + 1000)) == NULL) goto fail_3; - if ((c->c_consts = newlistobject(0)) == NULL) + if ((c->c_consts = PyList_New(0)) == NULL) goto fail_2; - if ((c->c_names = newlistobject(0)) == NULL) + if ((c->c_names = PyList_New(0)) == NULL) goto fail_1; - if ((c->c_globals = newdictobject()) == NULL) + if ((c->c_globals = PyDict_New()) == NULL) goto fail_0; - if ((c->c_locals = newdictobject()) == NULL) + if ((c->c_locals = PyDict_New()) == NULL) goto fail_00; - if ((c->c_varnames = newlistobject(0)) == NULL) + if ((c->c_varnames = PyList_New(0)) == NULL) goto fail_000; - if ((c->c_lnotab = newsizedstringobject((char *)NULL, 1000)) == NULL) + if ((c->c_lnotab = PyString_FromStringAndSize((char *)NULL, + 1000)) == NULL) goto fail_0000; c->c_nlocals = 0; c->c_argcount = 0; @@ -430,17 +434,17 @@ com_init(c, filename) return 1; fail_0000: - DECREF(c->c_lnotab); + Py_DECREF(c->c_lnotab); fail_000: - DECREF(c->c_locals); + Py_DECREF(c->c_locals); fail_00: - DECREF(c->c_globals); + Py_DECREF(c->c_globals); fail_0: - DECREF(c->c_names); + Py_DECREF(c->c_names); fail_1: - DECREF(c->c_consts); + Py_DECREF(c->c_consts); fail_2: - DECREF(c->c_code); + Py_DECREF(c->c_code); fail_3: return 0; } @@ -449,13 +453,13 @@ static void com_free(c) struct compiling *c; { - XDECREF(c->c_code); - XDECREF(c->c_consts); - XDECREF(c->c_names); - XDECREF(c->c_globals); - XDECREF(c->c_locals); - XDECREF(c->c_varnames); - XDECREF(c->c_lnotab); + Py_XDECREF(c->c_code); + Py_XDECREF(c->c_consts); + Py_XDECREF(c->c_names); + Py_XDECREF(c->c_globals); + Py_XDECREF(c->c_locals); + Py_XDECREF(c->c_varnames); + Py_XDECREF(c->c_lnotab); } static void @@ -489,9 +493,9 @@ com_done(c) struct compiling *c; { if (c->c_code != NULL) - resizestring(&c->c_code, c->c_nexti); + _PyString_Resize(&c->c_code, c->c_nexti); if (c->c_lnotab != NULL) - resizestring(&c->c_lnotab, c->c_lnotab_next); + _PyString_Resize(&c->c_lnotab, c->c_lnotab_next); } static void @@ -506,18 +510,19 @@ com_addbyte(c, byte) fprintf(stderr, "XXX compiling bad byte: %d\n", byte); fatal("com_addbyte: byte out of range"); */ - com_error(c, SystemError, "com_addbyte: byte out of range"); + com_error(c, PyExc_SystemError, + "com_addbyte: byte out of range"); } if (c->c_code == NULL) return; - len = getstringsize(c->c_code); + len = PyString_Size(c->c_code); if (c->c_nexti >= len) { - if (resizestring(&c->c_code, len+1000) != 0) { + if (_PyString_Resize(&c->c_code, len+1000) != 0) { c->c_errors++; return; } } - getstringvalue(c->c_code)[c->c_nexti++] = byte; + PyString_AsString(c->c_code)[c->c_nexti++] = byte; } static void @@ -539,14 +544,14 @@ com_add_lnotab(c, addr, line) char *p; if (c->c_lnotab == NULL) return; - size = getstringsize(c->c_lnotab); + size = PyString_Size(c->c_lnotab); if (c->c_lnotab_next+2 > size) { - if (resizestring(&c->c_lnotab, size + 1000) < 0) { + if (_PyString_Resize(&c->c_lnotab, size + 1000) < 0) { c->c_errors++; return; } } - p = getstringvalue(c->c_lnotab) + c->c_lnotab_next; + p = PyString_AsString(c->c_lnotab) + c->c_lnotab_next; *p++ = addr; *p++ = line; c->c_lnotab_next += 2; @@ -616,7 +621,7 @@ com_backpatch(c, anchor) struct compiling *c; int anchor; /* Must be nonzero */ { - unsigned char *code = (unsigned char *) getstringvalue(c->c_code); + unsigned char *code = (unsigned char *) PyString_AsString(c->c_code); int target = c->c_nexti; int dist; int prev; @@ -637,17 +642,17 @@ com_backpatch(c, anchor) static int com_add(c, list, v) struct compiling *c; - object *list; - object *v; + PyObject *list; + PyObject *v; { - int n = getlistsize(list); + int n = PyList_Size(list); int i; for (i = n; --i >= 0; ) { - object *w = getlistitem(list, i); - if (v->ob_type == w->ob_type && cmpobject(v, w) == 0) + PyObject *w = PyList_GetItem(list, i); + if (v->ob_type == w->ob_type && PyObject_Compare(v, w) == 0) return i; } - if (addlistitem(list, v) != 0) + if (PyList_Append(list, v) != 0) c->c_errors++; return n; } @@ -655,7 +660,7 @@ com_add(c, list, v) static int com_addconst(c, v) struct compiling *c; - object *v; + PyObject *v; { return com_add(c, c->c_consts, v); } @@ -663,7 +668,7 @@ com_addconst(c, v) static int com_addname(c, v) struct compiling *c; - object *v; + PyObject *v; { return com_add(c, c->c_names, v); } @@ -709,7 +714,7 @@ com_addopnamestr(c, op, name) int op; char *name; { - object *v; + PyObject *v; int i; #ifdef PRIVATE_NAME_MANGLING char buffer[256]; @@ -724,14 +729,14 @@ com_addopnamestr(c, op, name) } else { i = com_addname(c, v); - DECREF(v); + Py_DECREF(v); } /* Hack to replace *_NAME opcodes by *_GLOBAL if necessary */ switch (op) { case LOAD_NAME: case STORE_NAME: case DELETE_NAME: - if (dictlookup(c->c_globals, name) != NULL) { + if (PyDict_GetItemString(c->c_globals, name) != NULL) { switch (op) { case LOAD_NAME: op = LOAD_GLOBAL; break; case STORE_NAME: op = STORE_GLOBAL; break; @@ -762,7 +767,7 @@ com_addopname(c, op, n) for (i = 0; i < NCH(n); i += 2) { char *s = STR(CHILD(n, i)); if (p + strlen(s) > buffer + (sizeof buffer) - 2) { - com_error(c, MemoryError, + com_error(c, PyExc_MemoryError, "dotted_name too long"); name = NULL; break; @@ -780,14 +785,15 @@ com_addopname(c, op, n) com_addopnamestr(c, op, name); } -static object * +static PyObject * parsenumber(co, s) struct compiling *co; char *s; { - extern long mystrtol PROTO((const char *, char **, int)); - extern unsigned long mystrtoul PROTO((const char *, char **, int)); - extern double atof PROTO((const char *)); + extern long PyOS_strtol Py_PROTO((const char *, char **, int)); + extern unsigned long PyOS_strtoul Py_PROTO((const char *, + char **, int)); + extern double atof Py_PROTO((const char *)); char *end; long x; double dx; @@ -802,18 +808,18 @@ parsenumber(co, s) imflag = *end == 'j' || *end == 'J'; #endif if (*end == 'l' || *end == 'L') - return long_scan(s, 0); + return PyLong_FromString(s, (char **)0, 0); if (s[0] == '0') - x = (long) mystrtoul(s, &end, 0); + x = (long) PyOS_strtoul(s, &end, 0); else - x = mystrtol(s, &end, 0); + x = PyOS_strtol(s, &end, 0); if (*end == '\0') { if (errno != 0) { - com_error(co, OverflowError, + com_error(co, PyExc_OverflowError, "integer literal too large"); return NULL; } - return newintobject(x); + return PyInt_FromLong(x); } /* XXX Huge floats may silently fail */ #ifndef WITHOUT_COMPLEX @@ -822,22 +828,22 @@ parsenumber(co, s) PyFPE_START_PROTECT("atof", return 0) c.imag = atof(s); PyFPE_END_PROTECT(c) - return newcomplexobject(c); + return PyComplex_FromCComplex(c); } else { #endif PyFPE_START_PROTECT("atof", return 0) dx = atof(s); PyFPE_END_PROTECT(dx) - return newfloatobject(dx); + return PyFloat_FromDouble(dx); } } -static object * +static PyObject * parsestr(s) char *s; { - object *v; + PyObject *v; int len; char *buf; char *p; @@ -848,27 +854,27 @@ parsestr(s) if (isalpha(quote) || quote == '_') quote = *++s; if (quote != '\'' && quote != '\"') { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } s++; len = strlen(s); if (s[--len] != quote) { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } if (len >= 4 && s[0] == quote && s[1] == quote) { s += 2; len -= 2; if (s[--len] != quote || s[--len] != quote) { - err_badcall(); + PyErr_BadInternalCall(); return NULL; } } if (first != quote || strchr(s, '\\') == NULL) - return newsizedstringobject(s, len); - v = newsizedstringobject((char *)NULL, len); - p = buf = getstringvalue(v); + return PyString_FromStringAndSize(s, len); + v = PyString_FromStringAndSize((char *)NULL, len); + p = buf = PyString_AsString(v); end = s + len; while (s < end) { if (*s != '\\') { @@ -912,21 +918,21 @@ parsestr(s) default: *p++ = '\\'; *p++ = s[-1]; break; } } - resizestring(&v, (int)(p - buf)); + _PyString_Resize(&v, (int)(p - buf)); return v; } -static object * +static PyObject * parsestrplus(n) node *n; { - object *v; + PyObject *v; int i; REQ(CHILD(n, 0), STRING); if ((v = parsestr(STR(CHILD(n, 0)))) != NULL) { /* String literal concatenation */ for (i = 1; i < NCH(n) && v != NULL; i++) { - joinstring_decref(&v, parsestr(STR(CHILD(n, i)))); + PyString_ConcatAndDel(&v, parsestr(STR(CHILD(n, i)))); } } return v; @@ -975,7 +981,7 @@ com_atom(c, n) node *n; { node *ch; - object *v; + PyObject *v; int i; REQ(n, atom); ch = CHILD(n, 0); @@ -1012,7 +1018,7 @@ com_atom(c, n) } else { i = com_addconst(c, v); - DECREF(v); + Py_DECREF(v); } com_addoparg(c, LOAD_CONST, i); com_push(c, 1); @@ -1025,7 +1031,7 @@ com_atom(c, n) } else { i = com_addconst(c, v); - DECREF(v); + Py_DECREF(v); } com_addoparg(c, LOAD_CONST, i); com_push(c, 1); @@ -1036,7 +1042,8 @@ com_atom(c, n) break; default: /* XXX fprintf(stderr, "node type %d\n", TYPE(ch)); */ - com_error(c, SystemError, "com_atom: unexpected node type"); + com_error(c, PyExc_SystemError, + "com_atom: unexpected node type"); } } @@ -1072,13 +1079,13 @@ static void com_argument(c, n, pkeywords) struct compiling *c; node *n; /* argument */ - object **pkeywords; + PyObject **pkeywords; { node *m; REQ(n, argument); /* [test '='] test; really [keyword '='] test */ if (NCH(n) == 1) { if (*pkeywords != NULL) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "non-keyword arg after keyword arg"); } else { @@ -1091,24 +1098,25 @@ com_argument(c, n, pkeywords) m = CHILD(m, 0); } while (NCH(m) == 1); if (TYPE(m) != NAME) { - com_error(c, SyntaxError, "keyword can't be an expression"); + com_error(c, PyExc_SyntaxError, + "keyword can't be an expression"); } else { - object *v = PyString_InternFromString(STR(m)); + PyObject *v = PyString_InternFromString(STR(m)); if (v != NULL && *pkeywords == NULL) - *pkeywords = newdictobject(); + *pkeywords = PyDict_New(); if (v == NULL || *pkeywords == NULL) c->c_errors++; else { - if (dict2lookup(*pkeywords, v) != NULL) - com_error(c, SyntaxError, + if (PyDict_GetItem(*pkeywords, v) != NULL) + com_error(c, PyExc_SyntaxError, "duplicate keyword argument"); else - if (dict2insert(*pkeywords, v, v) != 0) + if (PyDict_SetItem(*pkeywords, v, v) != 0) c->c_errors++; com_addoparg(c, LOAD_CONST, com_addconst(c, v)); com_push(c, 1); - DECREF(v); + Py_DECREF(v); } } com_node(c, CHILD(n, 2)); @@ -1123,7 +1131,7 @@ com_call_function(c, n) com_addoparg(c, CALL_FUNCTION, 0); } else { - object *keywords = NULL; + PyObject *keywords = NULL; int i, na, nk; REQ(n, arglist); na = 0; @@ -1135,9 +1143,10 @@ com_call_function(c, n) else nk++; } - XDECREF(keywords); + Py_XDECREF(keywords); if (na > 255 || nk > 255) { - com_error(c, SyntaxError, "more than 255 arguments"); + com_error(c, PyExc_SyntaxError, + "more than 255 arguments"); } com_addoparg(c, CALL_FUNCTION, na | (nk << 8)); com_pop(c, na + 2*nk); @@ -1163,7 +1172,7 @@ com_sliceobj(c, n) /* first argument */ if (TYPE(CHILD(n,i)) == COLON) { - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); i++; } @@ -1179,7 +1188,7 @@ com_sliceobj(c, n) i++; } else { - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); } /* remaining arguments */ @@ -1189,7 +1198,7 @@ com_sliceobj(c, n) REQ(ch, sliceop); if (NCH(ch) == 1) { /* right argument of ':' missing */ - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); } else @@ -1243,7 +1252,8 @@ com_subscriptlist(c, n, assigning) if (assigning == OP_APPLY) op = SLICE; else - op = ((assigning == OP_ASSIGN) ? STORE_SLICE : DELETE_SLICE); + op = ((assigning == OP_ASSIGN) ? + STORE_SLICE : DELETE_SLICE); com_slice(c, sub, op); if (op == STORE_SLICE) com_pop(c, 2); @@ -1294,7 +1304,7 @@ com_apply_trailer(c, n) com_subscriptlist(c, CHILD(n, 1), OP_APPLY); break; default: - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_apply_trailer: unknown trailer type"); } } @@ -1364,7 +1374,7 @@ com_term(c, n) op = BINARY_MODULO; break; default: - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_term: operator not *, / or %"); op = 255; } @@ -1392,7 +1402,7 @@ com_arith_expr(c, n) op = BINARY_SUBTRACT; break; default: - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_arith_expr: operator not + or -"); op = 255; } @@ -1420,7 +1430,7 @@ com_shift_expr(c, n) op = BINARY_RSHIFT; break; default: - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_shift_expr: operator not << or >>"); op = 255; } @@ -1444,7 +1454,7 @@ com_and_expr(c, n) op = BINARY_AND; } else { - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_and_expr: operator not &"); op = 255; } @@ -1468,7 +1478,7 @@ com_xor_expr(c, n) op = BINARY_XOR; } else { - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_xor_expr: operator not ^"); op = 255; } @@ -1492,7 +1502,7 @@ com_expr(c, n) op = BINARY_OR; } else { - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_expr: expr operator not |"); op = 255; } @@ -1590,7 +1600,7 @@ com_comparison(c, n) } op = cmp_type(CHILD(n, i-1)); if (op == BAD) { - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "com_comparison: unknown comparison op"); } com_addoparg(c, COMPARE_OP, op); @@ -1656,17 +1666,17 @@ com_test(c, n) { REQ(n, test); /* and_test ('or' and_test)* | lambdef */ if (NCH(n) == 1 && TYPE(CHILD(n, 0)) == lambdef) { - object *v; + PyObject *v; int i; int ndefs = com_argdefs(c, CHILD(n, 0)); - v = (object *) icompile(CHILD(n, 0), c); + v = (PyObject *) icompile(CHILD(n, 0), c); if (v == NULL) { c->c_errors++; i = 255; } else { i = com_addconst(c, v); - DECREF(v); + Py_DECREF(v); } com_addoparg(c, LOAD_CONST, i); com_push(c, 1); @@ -1713,8 +1723,8 @@ com_list(c, n, toplevel) /* Begin of assignment compilation */ -static void com_assign_name PROTO((struct compiling *, node *, int)); -static void com_assign PROTO((struct compiling *, node *, int)); +static void com_assign_name Py_PROTO((struct compiling *, node *, int)); +static void com_assign Py_PROTO((struct compiling *, node *, int)); static void com_assign_attr(c, n, assigning) @@ -1735,7 +1745,8 @@ com_assign_trailer(c, n, assigning) REQ(n, trailer); switch (TYPE(CHILD(n, 0))) { case LPAR: /* '(' [exprlist] ')' */ - com_error(c, SyntaxError, "can't assign to function call"); + com_error(c, PyExc_SyntaxError, + "can't assign to function call"); break; case DOT: /* '.' NAME */ com_assign_attr(c, CHILD(n, 1), assigning); @@ -1744,7 +1755,7 @@ com_assign_trailer(c, n, assigning) com_subscriptlist(c, CHILD(n, 1), assigning); break; default: - com_error(c, SystemError, "unknown trailer type"); + com_error(c, PyExc_SystemError, "unknown trailer type"); } } @@ -1825,7 +1836,7 @@ com_assign(c, n, assigning) case term: case factor: if (NCH(n) > 1) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to operator"); return; } @@ -1835,7 +1846,7 @@ com_assign(c, n, assigning) case power: /* atom trailer* ('**' power)* */ /* ('+'|'-'|'~') factor | atom trailer* */ if (TYPE(CHILD(n, 0)) != atom) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to operator"); return; } @@ -1844,7 +1855,7 @@ com_assign(c, n, assigning) com_node(c, CHILD(n, 0)); for (i = 1; i+1 < NCH(n); i++) { if (TYPE(CHILD(n, i)) == DOUBLESTAR) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to operator"); return; } @@ -1863,7 +1874,7 @@ com_assign(c, n, assigning) n = CHILD(n, 1); if (TYPE(n) == RPAR) { /* XXX Should allow () = () ??? */ - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to ()"); return; } @@ -1871,7 +1882,7 @@ com_assign(c, n, assigning) case LSQB: n = CHILD(n, 1); if (TYPE(n) == RSQB) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to []"); return; } @@ -1881,26 +1892,28 @@ com_assign(c, n, assigning) com_assign_name(c, CHILD(n, 0), assigning); return; default: - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "can't assign to literal"); return; } break; case lambdef: - com_error(c, SyntaxError, "can't assign to lambda"); + com_error(c, PyExc_SyntaxError, + "can't assign to lambda"); return; default: /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ - com_error(c, SystemError, "com_assign: bad node"); + com_error(c, PyExc_SystemError, + "com_assign: bad node"); return; } } } -/* Forward */ static node *get_rawdocstring PROTO((node *)); +/* Forward */ static node *get_rawdocstring Py_PROTO((node *)); static void com_expr_stmt(c, n) @@ -1997,10 +2010,10 @@ com_return_stmt(c, n) { REQ(n, return_stmt); /* 'return' [testlist] */ if (!c->c_infunction) { - com_error(c, SyntaxError, "'return' outside function"); + com_error(c, PyExc_SyntaxError, "'return' outside function"); } if (NCH(n) < 2) { - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); } else @@ -2075,10 +2088,11 @@ com_global_stmt(c, n) com_mangle(c, s, buffer, (int)sizeof(buffer))) s = buffer; #endif - if (dictlookup(c->c_locals, s) != NULL) { - com_error(c, SyntaxError, "name is local and global"); + if (PyDict_GetItemString(c->c_locals, s) != NULL) { + com_error(c, PyExc_SyntaxError, + "name is local and global"); } - else if (dictinsert(c->c_globals, s, None) != 0) + else if (PyDict_SetItemString(c->c_globals, s, Py_None) != 0) c->c_errors++; } } @@ -2086,36 +2100,37 @@ com_global_stmt(c, n) static int com_newlocal_o(c, nameval) struct compiling *c; - object *nameval; + PyObject *nameval; { int i; - object *ival; - if (getlistsize(c->c_varnames) != c->c_nlocals) { + PyObject *ival; + if (PyList_Size(c->c_varnames) != c->c_nlocals) { /* This is usually caused by an error on a previous call */ if (c->c_errors == 0) { - com_error(c, SystemError, "mixed up var name/index"); + com_error(c, PyExc_SystemError, + "mixed up var name/index"); } return 0; } - ival = newintobject(i = c->c_nlocals++); + ival = PyInt_FromLong(i = c->c_nlocals++); if (ival == NULL) c->c_errors++; - else if (mappinginsert(c->c_locals, nameval, ival) != 0) + else if (PyDict_SetItem(c->c_locals, nameval, ival) != 0) c->c_errors++; - else if (addlistitem(c->c_varnames, nameval) != 0) + else if (PyList_Append(c->c_varnames, nameval) != 0) c->c_errors++; - XDECREF(ival); + Py_XDECREF(ival); return i; } static int com_addlocal_o(c, nameval) struct compiling *c; - object *nameval; + PyObject *nameval; { - object *ival = mappinglookup(c->c_locals, nameval); + PyObject *ival = PyDict_GetItem(c->c_locals, nameval); if (ival != NULL) - return getintvalue(ival); + return PyInt_AsLong(ival); return com_newlocal_o(c, nameval); } @@ -2124,14 +2139,14 @@ com_newlocal(c, name) struct compiling *c; char *name; { - object *nameval = PyString_InternFromString(name); + PyObject *nameval = PyString_InternFromString(name); int i; if (nameval == NULL) { c->c_errors++; return 0; } i = com_newlocal_o(c, nameval); - DECREF(nameval); + Py_DECREF(nameval); return i; } @@ -2145,7 +2160,7 @@ com_access_stmt(c, n) node *n; { int i, j, k, mode, imode; - object *vmode; + PyObject *vmode; REQ(n, access_stmt); /* 'access' NAME (',' NAME)* ':' accesstype (',' accesstype)* accesstype: NAME+ */ @@ -2187,9 +2202,9 @@ com_access_stmt(c, n) if (w == 1) mode |= AC_W_PRIVATE; } } - vmode = newintobject((long)mode); + vmode = PyInt_FromLong((long)mode); imode = com_addconst(c, vmode); - XDECREF(vmode); + Py_XDECREF(vmode); for (i = 1; TYPE(CHILD(n,i-1)) != COLON; i+=2) { com_addoparg(c, LOAD_CONST, imode); com_addopname(c, ACCESS_MODE, CHILD(n, i)); @@ -2208,7 +2223,7 @@ com_exec_stmt(c, n) if (NCH(n) >= 4) com_node(c, CHILD(n, 3)); else { - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); } if (NCH(n) >= 6) @@ -2226,7 +2241,7 @@ is_constant_false(c, n) struct compiling *c; node *n; { - object *v; + PyObject *v; int i; /* Label to avoid tail recursion */ @@ -2284,21 +2299,21 @@ is_constant_false(c, n) case NUMBER: v = parsenumber(c, STR(n)); if (v == NULL) { - err_clear(); + PyErr_Clear(); break; } - i = testbool(v); - DECREF(v); + i = PyObject_IsTrue(v); + Py_DECREF(v); return i == 0; case STRING: v = parsestr(STR(n)); if (v == NULL) { - err_clear(); + PyErr_Clear(); break; } - i = testbool(v); - DECREF(v); + i = PyObject_IsTrue(v); + Py_DECREF(v); return i == 0; } @@ -2374,7 +2389,7 @@ com_for_stmt(c, n) struct compiling *c; node *n; { - object *v; + PyObject *v; int break_anchor = 0; int anchor = 0; int save_begin = c->c_begin; @@ -2383,12 +2398,12 @@ com_for_stmt(c, n) com_addfwref(c, SETUP_LOOP, &break_anchor); block_push(c, SETUP_LOOP); com_node(c, CHILD(n, 3)); - v = newintobject(0L); + v = PyInt_FromLong(0L); if (v == NULL) c->c_errors++; com_addoparg(c, LOAD_CONST, com_addconst(c, v)); com_push(c, 1); - XDECREF(v); + Py_XDECREF(v); c->c_begin = c->c_nexti; com_addoparg(c, SET_LINENO, n->n_lineno); com_addfwref(c, FOR_LOOP, &anchor); @@ -2497,7 +2512,7 @@ com_try_except(c, n) i += 3) { /* except_clause: 'except' [expr [',' var]] */ if (except_anchor == 0) { - com_error(c, SyntaxError, + com_error(c, PyExc_SyntaxError, "default 'except:' must be last"); break; } @@ -2559,7 +2574,7 @@ com_try_finally(c, n) com_addbyte(c, POP_BLOCK); block_pop(c, SETUP_FINALLY); block_push(c, END_FINALLY); - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); /* While the generated code pushes only one item, the try-finally handling can enter here with up to three items. OK, here are the details: @@ -2649,7 +2664,7 @@ get_rawdocstring(n) return NULL; } -static object * +static PyObject * get_docstring(n) node *n; { @@ -2690,7 +2705,8 @@ com_continue_stmt(c, n) com_addoparg(c, JUMP_ABSOLUTE, c->c_begin); } else { - com_error(c, SyntaxError, "'continue' not properly in loop"); + com_error(c, PyExc_SyntaxError, + "'continue' not properly in loop"); } /* XXX Could allow it inside a 'finally' clause XXX if we could pop the exception still on the stack */ @@ -2722,7 +2738,8 @@ com_argdefs(c, n) ndefs = 0; for (i = 0; i < nch; i++) { int t; - if (TYPE(CHILD(n, i)) == STAR || TYPE(CHILD(n, i)) == DOUBLESTAR) + if (TYPE(CHILD(n, i)) == STAR || + TYPE(CHILD(n, i)) == DOUBLESTAR) break; nargs++; i++; @@ -2743,7 +2760,7 @@ com_argdefs(c, n) /* Treat "(a=1, b)" as "(a=1, b=None)" */ if (ndefs) { com_addoparg(c, LOAD_CONST, - com_addconst(c, None)); + com_addconst(c, Py_None)); com_push(c, 1); ndefs++; } @@ -2759,9 +2776,9 @@ com_funcdef(c, n) struct compiling *c; node *n; { - object *v; + PyObject *v; REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ - v = (object *)icompile(n, c); + v = (PyObject *)icompile(n, c); if (v == NULL) c->c_errors++; else { @@ -2773,7 +2790,7 @@ com_funcdef(c, n) com_pop(c, ndefs); com_addopname(c, STORE_NAME, CHILD(n, 1)); com_pop(c, 1); - DECREF(v); + Py_DECREF(v); } } @@ -2798,7 +2815,7 @@ com_classdef(c, n) node *n; { int i; - object *v; + PyObject *v; REQ(n, classdef); /* classdef: class NAME ['(' testlist ')'] ':' suite */ if ((v = PyString_InternFromString(STR(CHILD(n, 1)))) == NULL) { @@ -2809,7 +2826,7 @@ com_classdef(c, n) i = com_addconst(c, v); com_addoparg(c, LOAD_CONST, i); com_push(c, 1); - DECREF(v); + Py_DECREF(v); /* Push the tuple of base classes on the stack */ if (TYPE(CHILD(n, 2)) != LPAR) { com_addoparg(c, BUILD_TUPLE, 0); @@ -2817,7 +2834,7 @@ com_classdef(c, n) } else com_bases(c, CHILD(n, 3)); - v = (object *)icompile(n, c); + v = (PyObject *)icompile(n, c); if (v == NULL) c->c_errors++; else { @@ -2829,7 +2846,7 @@ com_classdef(c, n) com_addbyte(c, BUILD_CLASS); com_pop(c, 2); com_addopname(c, STORE_NAME, CHILD(n, 1)); - DECREF(v); + Py_DECREF(v); } } @@ -2887,7 +2904,8 @@ com_node(c, n) break; case break_stmt: if (c->c_loops == 0) { - com_error(c, SyntaxError, "'break' outside loop"); + com_error(c, PyExc_SyntaxError, + "'break' outside loop"); } com_addbyte(c, BREAK_LOOP); break; @@ -2983,11 +3001,12 @@ com_node(c, n) default: /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ - com_error(c, SystemError, "com_node: unexpected node type"); + com_error(c, PyExc_SystemError, + "com_node: unexpected node type"); } } -static void com_fplist PROTO((struct compiling *, node *)); +static void com_fplist Py_PROTO((struct compiling *, node *)); static void com_fpdef(c, n) @@ -3121,12 +3140,12 @@ com_file_input(c, n) node *n; { int i; - object *doc; + PyObject *doc; REQ(n, file_input); /* (NEWLINE | stmt)* ENDMARKER */ doc = get_docstring(n); if (doc != NULL) { int i = com_addconst(c, doc); - DECREF(doc); + Py_DECREF(doc); com_addoparg(c, LOAD_CONST, i); com_push(c, 1); com_addopnamestr(c, STORE_NAME, "__doc__"); @@ -3146,17 +3165,17 @@ compile_funcdef(c, n) struct compiling *c; node *n; { - object *doc; + PyObject *doc; node *ch; REQ(n, funcdef); /* funcdef: 'def' NAME parameters ':' suite */ c->c_name = STR(CHILD(n, 1)); doc = get_docstring(CHILD(n, 4)); if (doc != NULL) { (void) com_addconst(c, doc); - DECREF(doc); + Py_DECREF(doc); } else - (void) com_addconst(c, None); /* No docstring */ + (void) com_addconst(c, Py_None); /* No docstring */ ch = CHILD(n, 2); /* parameters: '(' [varargslist] ')' */ ch = CHILD(ch, 1); /* ')' | varargslist */ if (TYPE(ch) == varargslist) @@ -3164,7 +3183,7 @@ compile_funcdef(c, n) c->c_infunction = 1; com_node(c, CHILD(n, 4)); c->c_infunction = 0; - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); com_addbyte(c, RETURN_VALUE); com_pop(c, 1); @@ -3180,7 +3199,7 @@ compile_lambdef(c, n) c->c_name = ""; ch = CHILD(n, 1); - (void) com_addconst(c, None); /* No docstring */ + (void) com_addconst(c, Py_None); /* No docstring */ if (TYPE(ch) == varargslist) { com_arglist(c, ch); ch = CHILD(n, 3); @@ -3198,7 +3217,7 @@ compile_classdef(c, n) node *n; { node *ch; - object *doc; + PyObject *doc; REQ(n, classdef); /* classdef: 'class' NAME ['(' testlist ')'] ':' suite */ c->c_name = STR(CHILD(n, 1)); @@ -3209,14 +3228,14 @@ compile_classdef(c, n) doc = get_docstring(ch); if (doc != NULL) { int i = com_addconst(c, doc); - DECREF(doc); + Py_DECREF(doc); com_addoparg(c, LOAD_CONST, i); com_push(c, 1); com_addopnamestr(c, STORE_NAME, "__doc__"); com_pop(c, 1); } else - (void) com_addconst(c, None); + (void) com_addconst(c, Py_None); com_node(c, ch); com_addbyte(c, LOAD_LOCALS); com_push(c, 1); @@ -3239,7 +3258,7 @@ compile_node(c, n) n = CHILD(n, 0); if (TYPE(n) != NEWLINE) com_node(c, n); - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); com_addbyte(c, RETURN_VALUE); com_pop(c, 1); @@ -3248,7 +3267,7 @@ compile_node(c, n) case file_input: /* A whole file, or built-in function exec() */ com_file_input(c, n); - com_addoparg(c, LOAD_CONST, com_addconst(c, None)); + com_addoparg(c, LOAD_CONST, com_addconst(c, Py_None)); com_push(c, 1); com_addbyte(c, RETURN_VALUE); com_pop(c, 1); @@ -3274,7 +3293,7 @@ compile_node(c, n) default: /* XXX fprintf(stderr, "node type %d\n", TYPE(n)); */ - com_error(c, SystemError, + com_error(c, PyExc_SystemError, "compile_node: unexpected node type"); } } @@ -3310,19 +3329,19 @@ optimize(c) unsigned char *next_instr, *cur_instr; int opcode; int oparg = 0; - object *name; - object *error_type, *error_value, *error_traceback; + PyObject *name; + PyObject *error_type, *error_value, *error_traceback; #define NEXTOP() (*next_instr++) #define NEXTARG() (next_instr += 2, (next_instr[-1]<<8) + next_instr[-2]) -#define GETITEM(v, i) (getlistitem((v), (i))) +#define GETITEM(v, i) (PyList_GetItem((v), (i))) #define GETNAMEOBJ(i) (GETITEM(c->c_names, (i))) - err_fetch(&error_type, &error_value, &error_traceback); + PyErr_Fetch(&error_type, &error_value, &error_traceback); c->c_flags |= CO_OPTIMIZED; - next_instr = (unsigned char *) getstringvalue(c->c_code); + next_instr = (unsigned char *) PyString_AsString(c->c_code); for (;;) { opcode = NEXTOP(); if (opcode == STOP_CODE) @@ -3341,10 +3360,10 @@ optimize(c) } } - if (dictlookup(c->c_locals, "*") != NULL) + if (PyDict_GetItemString(c->c_locals, "*") != NULL) c->c_flags &= ~CO_OPTIMIZED; - next_instr = (unsigned char *) getstringvalue(c->c_code); + next_instr = (unsigned char *) PyString_AsString(c->c_code); for (;;) { cur_instr = next_instr; opcode = NEXTOP(); @@ -3355,18 +3374,18 @@ optimize(c) if (opcode == LOAD_NAME || opcode == STORE_NAME || opcode == DELETE_NAME) { - object *v; + PyObject *v; int i; name = GETNAMEOBJ(oparg); - v = dict2lookup(c->c_locals, name); + v = PyDict_GetItem(c->c_locals, name); if (v == NULL) { - err_clear(); + PyErr_Clear(); if (opcode == LOAD_NAME && (c->c_flags&CO_OPTIMIZED)) cur_instr[0] = LOAD_GLOBAL; continue; } - i = getintvalue(v); + i = PyInt_AsLong(v); switch (opcode) { case LOAD_NAME: cur_instr[0] = LOAD_FAST; break; case STORE_NAME: cur_instr[0] = STORE_FAST; break; @@ -3378,18 +3397,18 @@ optimize(c) } if (c->c_errors == 0) - err_restore(error_type, error_value, error_traceback); + PyErr_Restore(error_type, error_value, error_traceback); } -codeobject * -compile(n, filename) +PyCodeObject * +PyNode_Compile(n, filename) node *n; char *filename; { return jcompile(n, filename, NULL); } -static codeobject * +static PyCodeObject * icompile(n, base) node *n; struct compiling *base; @@ -3397,14 +3416,14 @@ icompile(n, base) return jcompile(n, base->c_filename, base); } -static codeobject * +static PyCodeObject * jcompile(n, filename, base) node *n; char *filename; struct compiling *base; { struct compiling sc; - codeobject *co; + PyCodeObject *co; if (!com_init(&sc, filename)) return NULL; #ifdef PRIVATE_NAME_MANGLING @@ -3423,14 +3442,14 @@ jcompile(n, filename, base) sc.c_flags |= CO_NEWLOCALS; co = NULL; if (sc.c_errors == 0) { - object *consts, *names, *varnames, *filename, *name; - consts = listtuple(sc.c_consts); - names = listtuple(sc.c_names); - varnames = listtuple(sc.c_varnames); + PyObject *consts, *names, *varnames, *filename, *name; + consts = PyList_AsTuple(sc.c_consts); + names = PyList_AsTuple(sc.c_names); + varnames = PyList_AsTuple(sc.c_varnames); filename = PyString_InternFromString(sc.c_filename); name = PyString_InternFromString(sc.c_name); - if (!err_occurred()) - co = newcodeobject(sc.c_argcount, + if (!PyErr_Occurred()) + co = PyCode_New(sc.c_argcount, sc.c_nlocals, sc.c_maxstacklevel, sc.c_flags, @@ -3442,11 +3461,11 @@ jcompile(n, filename, base) name, sc.c_firstlineno, sc.c_lnotab); - XDECREF(consts); - XDECREF(names); - XDECREF(varnames); - XDECREF(filename); - XDECREF(name); + Py_XDECREF(consts); + Py_XDECREF(names); + Py_XDECREF(varnames); + Py_XDECREF(filename); + Py_XDECREF(name); } com_free(&sc); return co; diff --git a/Python/getargs.c b/Python/getargs.c index 4ac934d..121b021 100644 --- a/Python/getargs.c +++ b/Python/getargs.c @@ -36,37 +36,38 @@ PERFORMANCE OF THIS SOFTWARE. XXX Python source (or in an extension) uses ridiculously long names XXX or riduculously deep nesting in format strings. */ -#include "allobjects.h" +#include "Python.h" #include -int getargs PROTO((object *, char *, ...)); -int newgetargs PROTO((object *, char *, ...)); -int vgetargs PROTO((object *, char *, va_list)); +int PyArg_Parse Py_PROTO((PyObject *, char *, ...)); +int PyArg_ParseTuple Py_PROTO((PyObject *, char *, ...)); +int PyArgs_VaParse Py_PROTO((PyObject *, char *, va_list)); -int PyArg_ParseTupleAndKeywords PROTO((object *, object *, +int PyArg_ParseTupleAndKeywords Py_PROTO((PyObject *, PyObject *, char *, char **, ...)); /* Forward */ -static int vgetargs1 PROTO((object *, char *, va_list *, int)); -static void seterror PROTO((int, char *, int *, char *, char *)); -static char *convertitem PROTO((object *, char **, va_list *, int *, char *)); -static char *converttuple PROTO((object *, char **, va_list *, +static int vgetargs1 Py_PROTO((PyObject *, char *, va_list *, int)); +static void seterror Py_PROTO((int, char *, int *, char *, char *)); +static char *convertitem Py_PROTO((PyObject *, char **, va_list *, + int *, char *)); +static char *converttuple Py_PROTO((PyObject *, char **, va_list *, int *, char *, int)); -static char *convertsimple PROTO((object *, char **, va_list *, char *)); -static char *convertsimple1 PROTO((object *, char **, va_list *)); +static char *convertsimple Py_PROTO((PyObject *, char **, va_list *, char *)); +static char *convertsimple1 Py_PROTO((PyObject *, char **, va_list *)); -static int vgetargskeywords PROTO((object *, object *, +static int vgetargskeywords Py_PROTO((PyObject *, PyObject *, char *, char **, va_list *)); -static char *skipitem PROTO((char **, va_list *)); +static char *skipitem Py_PROTO((char **, va_list *)); #ifdef HAVE_STDARG_PROTOTYPES /* VARARGS2 */ -int getargs(object *args, char *format, ...) +int PyArg_Parse(PyObject *args, char *format, ...) #else /* VARARGS */ -int getargs(va_alist) va_dcl +int PyArg_Parse(va_alist) va_dcl #endif { int retval; @@ -75,11 +76,11 @@ int getargs(va_alist) va_dcl va_start(va, format); #else - object *args; + PyObject *args; char *format; va_start(va); - args = va_arg(va, object *); + args = va_arg(va, PyObject *); format = va_arg(va, char *); #endif retval = vgetargs1(args, format, &va, 1); @@ -90,10 +91,10 @@ int getargs(va_alist) va_dcl #ifdef HAVE_STDARG_PROTOTYPES /* VARARGS2 */ -int newgetargs(object *args, char *format, ...) +int PyArg_ParseTuple(PyObject *args, char *format, ...) #else /* VARARGS */ -int newgetargs(va_alist) va_dcl +int PyArg_ParseTuple(va_alist) va_dcl #endif { int retval; @@ -102,11 +103,11 @@ int newgetargs(va_alist) va_dcl va_start(va, format); #else - object *args; + PyObject *args; char *format; va_start(va); - args = va_arg(va, object *); + args = va_arg(va, PyObject *); format = va_arg(va, char *); #endif retval = vgetargs1(args, format, &va, 0); @@ -116,8 +117,8 @@ int newgetargs(va_alist) va_dcl int -vgetargs(args, format, va) - object *args; +PyArgs_VaParse(args, format, va) + PyObject *args; char *format; va_list va; { @@ -135,7 +136,7 @@ vgetargs(args, format, va) static int vgetargs1(args, format, p_va, compat) - object *args; + PyObject *args; char *format; va_list *p_va; int compat; @@ -160,7 +161,7 @@ vgetargs1(args, format, p_va, compat) } else if (/* '(' */ c == ')') { if (level == 0) - fatal(/* '(' */ + Py_FatalError(/* '(' */ "excess ')' in getargs format"); else level--; @@ -184,7 +185,7 @@ vgetargs1(args, format, p_va, compat) } if (level != 0) - fatal(/* '(' */ "missing ')' in getargs format"); + Py_FatalError(/* '(' */ "missing ')' in getargs format"); if (min < 0) min = max; @@ -197,7 +198,7 @@ vgetargs1(args, format, p_va, compat) return 1; sprintf(msgbuf, "%s requires no arguments", fname==NULL ? "function" : fname); - err_setstr(TypeError, msgbuf); + PyErr_SetString(PyExc_TypeError, msgbuf); return 0; } else if (min == 1 && max == 1) { @@ -205,7 +206,7 @@ vgetargs1(args, format, p_va, compat) sprintf(msgbuf, "%s requires at least one argument", fname==NULL ? "function" : fname); - err_setstr(TypeError, msgbuf); + PyErr_SetString(PyExc_TypeError, msgbuf); return 0; } msg = convertitem(args, &format, p_va, levels, msgbuf); @@ -215,19 +216,19 @@ vgetargs1(args, format, p_va, compat) return 0; } else { - err_setstr(SystemError, + PyErr_SetString(PyExc_SystemError, "old style getargs format uses new features"); return 0; } } - if (!is_tupleobject(args)) { - err_setstr(SystemError, + if (!PyTuple_Check(args)) { + PyErr_SetString(PyExc_SystemError, "new style getargs format but argument is not a tuple"); return 0; } - len = gettuplesize(args); + len = PyTuple_Size(args); if (len < min || max < len) { if (message == NULL) { @@ -241,14 +242,14 @@ vgetargs1(args, format, p_va, compat) len); message = msgbuf; } - err_setstr(TypeError, message); + PyErr_SetString(PyExc_TypeError, message); return 0; } for (i = 0; i < len; i++) { if (*format == '|') format++; - msg = convertitem(gettupleitem(args, i), &format, p_va, + msg = convertitem(PyTuple_GetItem(args, i), &format, p_va, levels, msgbuf); if (msg) { seterror(i+1, msg, levels, fname, message); @@ -273,7 +274,7 @@ seterror(iarg, msg, levels, fname, message) int i; char *p = buf; - if (err_occurred()) + if (PyErr_Occurred()) return; if (iarg == 0 && message == NULL) message = msg; @@ -293,7 +294,7 @@ seterror(iarg, msg, levels, fname, message) sprintf(p, ": expected %s found", msg); message = buf; } - err_setstr(TypeError, message); + PyErr_SetString(PyExc_TypeError, message); } @@ -318,7 +319,7 @@ seterror(iarg, msg, levels, fname, message) static char * converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) - object *arg; + PyObject *arg; char **p_format; va_list *p_va; int *levels; @@ -348,15 +349,15 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) n++; } - if (!is_tupleobject(arg)) { + if (!PyTuple_Check(arg)) { levels[0] = 0; sprintf(msgbuf, toplevel ? "%d arguments, %s" : "%d-tuple, %s", - n, arg == None ? "None" : arg->ob_type->tp_name); + n, arg == Py_None ? "None" : arg->ob_type->tp_name); return msgbuf; } - if ((i = gettuplesize(arg)) != n) { + if ((i = PyTuple_Size(arg)) != n) { levels[0] = 0; sprintf(msgbuf, toplevel ? "%d arguments, %d" : "%d-tuple, %d-tuple", @@ -367,7 +368,7 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) format = *p_format; for (i = 0; i < n; i++) { char *msg; - msg = convertitem(gettupleitem(arg, i), &format, p_va, + msg = convertitem(PyTuple_GetItem(arg, i), &format, p_va, levels+1, msgbuf); if (msg != NULL) { levels[0] = i+1; @@ -384,7 +385,7 @@ converttuple(arg, p_format, p_va, levels, msgbuf, toplevel) static char * convertitem(arg, p_format, p_va, levels, msgbuf) - object *arg; + PyObject *arg; char **p_format; va_list *p_va; int *levels; @@ -415,7 +416,7 @@ convertitem(arg, p_format, p_va, levels, msgbuf) static char * convertsimple(arg, p_format, p_va, msgbuf) - object *arg; + PyObject *arg; char **p_format; va_list *p_va; char *msgbuf; @@ -423,7 +424,7 @@ convertsimple(arg, p_format, p_va, msgbuf) char *msg = convertsimple1(arg, p_format, p_va); if (msg != NULL) { sprintf(msgbuf, "%.50s, %.50s", msg, - arg == None ? "None" : arg->ob_type->tp_name); + arg == Py_None ? "None" : arg->ob_type->tp_name); msg = msgbuf; } return msg; @@ -437,7 +438,7 @@ convertsimple(arg, p_format, p_va, msgbuf) static char * convertsimple1(arg, p_format, p_va) - object *arg; + PyObject *arg; char **p_format; va_list *p_va; { @@ -449,8 +450,8 @@ convertsimple1(arg, p_format, p_va) case 'b': /* byte -- very short int */ { char *p = va_arg(*p_va, char *); - long ival = getintvalue(arg); - if (ival == -1 && err_occurred()) + long ival = PyInt_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) return "integer"; else *p = (char) ival; @@ -460,8 +461,8 @@ convertsimple1(arg, p_format, p_va) case 'h': /* short int */ { short *p = va_arg(*p_va, short *); - long ival = getintvalue(arg); - if (ival == -1 && err_occurred()) + long ival = PyInt_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) return "integer"; else *p = (short) ival; @@ -471,8 +472,8 @@ convertsimple1(arg, p_format, p_va) case 'i': /* int */ { int *p = va_arg(*p_va, int *); - long ival = getintvalue(arg); - if (ival == -1 && err_occurred()) + long ival = PyInt_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) return "integer"; else *p = ival; @@ -482,8 +483,8 @@ convertsimple1(arg, p_format, p_va) case 'l': /* long int */ { long *p = va_arg(*p_va, long *); - long ival = getintvalue(arg); - if (ival == -1 && err_occurred()) + long ival = PyInt_AsLong(arg); + if (ival == -1 && PyErr_Occurred()) return "integer"; else *p = ival; @@ -493,8 +494,8 @@ convertsimple1(arg, p_format, p_va) case 'f': /* float */ { float *p = va_arg(*p_va, float *); - double dval = getfloatvalue(arg); - if (err_occurred()) + double dval = PyFloat_AsDouble(arg); + if (PyErr_Occurred()) return "float"; else *p = (float) dval; @@ -504,8 +505,8 @@ convertsimple1(arg, p_format, p_va) case 'd': /* double */ { double *p = va_arg(*p_va, double *); - double dval = getfloatvalue(arg); - if (err_occurred()) + double dval = PyFloat_AsDouble(arg); + if (PyErr_Occurred()) return "float"; else *p = dval; @@ -518,7 +519,7 @@ convertsimple1(arg, p_format, p_va) Py_complex *p = va_arg(*p_va, Py_complex *); Py_complex cval; cval = PyComplex_AsCComplex(arg); - if (err_occurred()) + if (PyErr_Occurred()) return "complex"; else *p = cval; @@ -529,8 +530,8 @@ convertsimple1(arg, p_format, p_va) case 'c': /* char */ { char *p = va_arg(*p_va, char *); - if (is_stringobject(arg) && getstringsize(arg) == 1) - *p = getstringvalue(arg)[0]; + if (PyString_Check(arg) && PyString_Size(arg) == 1) + *p = PyString_AsString(arg)[0]; else return "char"; break; @@ -539,16 +540,16 @@ convertsimple1(arg, p_format, p_va) case 's': /* string */ { char **p = va_arg(*p_va, char **); - if (is_stringobject(arg)) - *p = getstringvalue(arg); + if (PyString_Check(arg)) + *p = PyString_AsString(arg); else return "string"; if (*format == '#') { int *q = va_arg(*p_va, int *); - *q = getstringsize(arg); + *q = PyString_Size(arg); format++; } - else if ((int)strlen(*p) != getstringsize(arg)) + else if ((int)strlen(*p) != PyString_Size(arg)) return "string without null bytes"; break; } @@ -556,30 +557,30 @@ convertsimple1(arg, p_format, p_va) case 'z': /* string, may be NULL (None) */ { char **p = va_arg(*p_va, char **); - if (arg == None) + if (arg == Py_None) *p = 0; - else if (is_stringobject(arg)) - *p = getstringvalue(arg); + else if (PyString_Check(arg)) + *p = PyString_AsString(arg); else return "None or string"; if (*format == '#') { int *q = va_arg(*p_va, int *); - if (arg == None) + if (arg == Py_None) *q = 0; else - *q = getstringsize(arg); + *q = PyString_Size(arg); format++; } else if (*p != NULL && - (int)strlen(*p) != getstringsize(arg)) + (int)strlen(*p) != PyString_Size(arg)) return "None or string without null bytes"; break; } case 'S': /* string object */ { - object **p = va_arg(*p_va, object **); - if (is_stringobject(arg)) + PyObject **p = va_arg(*p_va, PyObject **); + if (PyString_Check(arg)) *p = arg; else return "string"; @@ -588,15 +589,15 @@ convertsimple1(arg, p_format, p_va) case 'O': /* object */ { - typeobject *type; - object **p; + PyTypeObject *type; + PyObject **p; if (*format == '!') { format++; - type = va_arg(*p_va, typeobject*); + type = va_arg(*p_va, PyTypeObject*); if (arg->ob_type != type) return type->tp_name; else { - p = va_arg(*p_va, object **); + p = va_arg(*p_va, PyObject **); *p = arg; } } @@ -604,13 +605,13 @@ convertsimple1(arg, p_format, p_va) inquiry pred = va_arg(*p_va, inquiry); format++; if ((*pred)(arg)) { - p = va_arg(*p_va, object **); + p = va_arg(*p_va, PyObject **); *p = arg; } } else if (*format == '&') { typedef int (*converter) - PROTO((object *, void *)); + Py_PROTO((PyObject *, void *)); converter convert = va_arg(*p_va, converter); void *addr = va_arg(*p_va, void *); format++; @@ -618,7 +619,7 @@ convertsimple1(arg, p_format, p_va) return "(unspecified)"; } else { - p = va_arg(*p_va, object **); + p = va_arg(*p_va, PyObject **); *p = arg; } break; @@ -639,8 +640,8 @@ convertsimple1(arg, p_format, p_va) #ifdef HAVE_STDARG_PROTOTYPES /* VARARGS2 */ -int PyArg_ParseTupleAndKeywords(object *args, - object *keywords, +int PyArg_ParseTupleAndKeywords(PyObject *args, + PyObject *keywords, char *format, char **kwlist, ...) #else @@ -654,14 +655,14 @@ int PyArg_ParseTupleAndKeywords(va_alist) va_dcl va_start(va, kwlist); #else - object *args; - object *keywords; + PyObject *args; + PyObject *keywords; char *format; char **kwlist; va_start(va); - args = va_arg(va, object *); - keywords = va_arg(va, object *); + args = va_arg(va, PyObject *); + keywords = va_arg(va, PyObject *); format = va_arg(va, char *); kwlist = va_arg(va, char **); #endif @@ -673,8 +674,8 @@ int PyArg_ParseTupleAndKeywords(va_alist) va_dcl static int vgetargskeywords(args, keywords, format, kwlist, p_va) - object *args; - object *keywords; + PyObject *args; + PyObject *keywords; char *format; char **kwlist; va_list *p_va; @@ -689,7 +690,7 @@ vgetargskeywords(args, keywords, format, kwlist, p_va) int i, len, tplen, kwlen; char *msg, *ks, **p; int nkwds, pos, match, converted; - object *key, *value; + PyObject *key, *value; /* nested tuples cannot be parsed when using keyword arguments */ @@ -830,7 +831,7 @@ vgetargskeywords(args, keywords, format, kwlist, p_va) converted = 0; for (i = tplen; i < nkwds; i++) { - object *item; + PyObject *item; if (*format == '|') format++; item = PyMapping_GetItemString(keywords, kwlist[i]); @@ -961,7 +962,7 @@ skipitem(p_format, p_va) case 'S': /* string object */ { - (void) va_arg(*p_va, object **); + (void) va_arg(*p_va, PyObject **); break; } @@ -969,8 +970,8 @@ skipitem(p_format, p_va) { if (*format == '!') { format++; - (void) va_arg(*p_va, typeobject*); - (void) va_arg(*p_va, object **); + (void) va_arg(*p_va, PyTypeObject*); + (void) va_arg(*p_va, PyObject **); } #if 0 /* I don't know what this is for */ @@ -978,19 +979,19 @@ skipitem(p_format, p_va) inquiry pred = va_arg(*p_va, inquiry); format++; if ((*pred)(arg)) { - (void) va_arg(*p_va, object **); + (void) va_arg(*p_va, PyObject **); } } #endif else if (*format == '&') { typedef int (*converter) - PROTO((object *, void *)); + Py_PROTO((PyObject *, void *)); (void) va_arg(*p_va, converter); (void) va_arg(*p_va, void *); format++; } else { - (void) va_arg(*p_va, object **); + (void) va_arg(*p_va, PyObject **); } break; } diff --git a/Python/import.c b/Python/import.c index 8dadc0e..64f4e29 100644 --- a/Python/import.c +++ b/Python/import.c @@ -31,17 +31,12 @@ PERFORMANCE OF THIS SOFTWARE. /* Module definition and import implementation */ -#include "allobjects.h" +#include "Python.h" -/* XXX Some of the following are duplicate with allobjects.h... */ #include "node.h" #include "token.h" #include "graminit.h" -#include "import.h" #include "errcode.h" -#include "sysmodule.h" -#include "bltinmodule.h" -#include "pythonrun.h" #include "marshal.h" #include "compile.h" #include "eval.h" @@ -57,7 +52,7 @@ PERFORMANCE OF THIS SOFTWARE. #include #endif -extern long getmtime(); /* In getmtime.c */ +extern long PyOS_GetLastModificationTime(); /* In getmtime.c */ /* Magic word to reject .pyc files generated by other Python versions */ /* Change for each incompatible change */ @@ -69,22 +64,22 @@ extern long getmtime(); /* In getmtime.c */ /* New way to come up with the magic number: (YEAR-1995), MONTH, DAY */ #define MAGIC (20121 | ((long)'\r'<<16) | ((long)'\n'<<24)) -object *import_modules; /* This becomes sys.modules */ +PyObject *import_modules; /* This becomes sys.modules */ /* Initialize things */ void -initimport() +PyImport_Init() { if (import_modules != NULL) - fatal("duplicate initimport() call"); - if ((import_modules = newdictobject()) == NULL) - fatal("no mem for dictionary of modules"); + Py_FatalError("duplicate initimport() call"); + if ((import_modules = PyDict_New()) == NULL) + Py_FatalError("no mem for dictionary of modules"); if (Py_OptimizeFlag) { /* Replace ".pyc" with ".pyo" in import_filetab */ struct filedescr *p; - for (p = import_filetab; p->suffix != NULL; p++) { + for (p = _PyImport_Filetab; p->suffix != NULL; p++) { if (strcmp(p->suffix, ".pyc") == 0) p->suffix = ".pyo"; } @@ -95,19 +90,19 @@ initimport() /* Un-initialize things, as good as we can */ void -doneimport() +PyImport_Cleanup() { if (import_modules != NULL) { - object *tmp = import_modules; + PyObject *tmp = import_modules; import_modules = NULL; /* This deletes all modules from sys.modules. - When a module is deallocated, it in turn clears its dictionary, - thus hopefully breaking any circular references between modules - and between a module's dictionary and its functions. - Note that "import" will fail while we are cleaning up. - */ - mappingclear(tmp); - DECREF(tmp); + When a module is deallocated, it in turn clears its + dictionary, thus hopefully breaking any circular + references between modules and between a module's + dictionary and its functions. Note that "import" + will fail while we are cleaning up. */ + PyDict_Clear(tmp); + Py_DECREF(tmp); } } @@ -115,7 +110,7 @@ doneimport() /* Helper for pythonrun.c -- return magic number */ long -get_pyc_magic() +PyImport_GetMagicNumber() { return MAGIC; } @@ -123,8 +118,8 @@ get_pyc_magic() /* Helper for sysmodule.c -- return modules dictionary */ -object * -get_modules() +PyObject * +PyImport_GetModuleDict() { return import_modules; } @@ -136,27 +131,28 @@ get_modules() Because the former action is most common, THIS DOES NOT RETURN A 'NEW' REFERENCE! */ -object * -add_module(name) +PyObject * +PyImport_AddModule(name) char *name; { - object *m; + PyObject *m; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if ((m = dictlookup(import_modules, name)) != NULL && - is_moduleobject(m)) + if ((m = PyDict_GetItemString(import_modules, name)) != NULL && + PyModule_Check(m)) return m; - m = newmoduleobject(name); + m = PyModule_New(name); if (m == NULL) return NULL; - if (dictinsert(import_modules, name, m) != 0) { - DECREF(m); + if (PyDict_SetItemString(import_modules, name, m) != 0) { + Py_DECREF(m); return NULL; } - DECREF(m); /* Yes, it still exists, in modules! */ + Py_DECREF(m); /* Yes, it still exists, in modules! */ return m; } @@ -165,29 +161,31 @@ add_module(name) /* Execute a code object in a module and return the module object WITH INCREMENTED REFERENCE COUNT */ -object * -exec_code_module(name, co) +PyObject * +PyImport_ExecCodeModule(name, co) char *name; - object *co; + PyObject *co; { - object *m, *d, *v; + PyObject *m, *d, *v; - m = add_module(name); + m = PyImport_AddModule(name); if (m == NULL) return NULL; - d = getmoduledict(m); - if (dictlookup(d, "__builtins__") == NULL) { - if (dictinsert(d, "__builtins__", getbuiltins()) != 0) + d = PyModule_GetDict(m); + if (PyDict_GetItemString(d, "__builtins__") == NULL) { + if (PyDict_SetItemString(d, "__builtins__", + PyEval_GetBuiltins()) != 0) return NULL; } /* Remember the filename as the __file__ attribute */ - if (dictinsert(d, "__file__", ((codeobject *)co)->co_filename) != 0) - err_clear(); /* Not important enough to report */ - v = eval_code((codeobject *)co, d, d); /* XXX owner? */ + if (PyDict_SetItemString(d, "__file__", + ((PyCodeObject *)co)->co_filename) != 0) + PyErr_Clear(); /* Not important enough to report */ + v = PyEval_EvalCode((PyCodeObject *)co, d, d); /* XXX owner? */ if (v == NULL) return NULL; - DECREF(v); - INCREF(m); + Py_DECREF(v); + Py_INCREF(m); return m; } @@ -236,21 +234,21 @@ check_compiled_module(pathname, mtime, cpathname) fp = fopen(cpathname, "rb"); if (fp == NULL) return NULL; - magic = rd_long(fp); + magic = PyMarshal_ReadLongFromFile(fp); if (magic != MAGIC) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s has bad magic\n", cpathname); fclose(fp); return NULL; } - pyc_mtime = rd_long(fp); + pyc_mtime = PyMarshal_ReadLongFromFile(fp); if (pyc_mtime != mtime) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s has bad mtime\n", cpathname); fclose(fp); return NULL; } - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# %s matches %s\n", cpathname, pathname); return fp; } @@ -258,71 +256,72 @@ check_compiled_module(pathname, mtime, cpathname) /* Read a code object from a file and check it for validity */ -static codeobject * +static PyCodeObject * read_compiled_module(fp) FILE *fp; { - object *co; + PyObject *co; - co = rd_object(fp); + co = PyMarshal_ReadObjectFromFile(fp); /* Ugly: rd_object() may return NULL with or without error */ - if (co == NULL || !is_codeobject(co)) { - if (!err_occurred()) - err_setstr(ImportError, + if (co == NULL || !PyCode_Check(co)) { + if (!PyErr_Occurred()) + PyErr_SetString(PyExc_ImportError, "Non-code object in .pyc file"); - XDECREF(co); + Py_XDECREF(co); return NULL; } - return (codeobject *)co; + return (PyCodeObject *)co; } /* Load a module from a compiled file, execute it, and return its module object WITH INCREMENTED REFERENCE COUNT */ -static object * +static PyObject * load_compiled_module(name, cpathname, fp) char *name; char *cpathname; FILE *fp; { long magic; - codeobject *co; - object *m; + PyCodeObject *co; + PyObject *m; - magic = rd_long(fp); + magic = PyMarshal_ReadLongFromFile(fp); if (magic != MAGIC) { - err_setstr(ImportError, "Bad magic number in .pyc file"); + PyErr_SetString(PyExc_ImportError, + "Bad magic number in .pyc file"); return NULL; } - (void) rd_long(fp); + (void) PyMarshal_ReadLongFromFile(fp); co = read_compiled_module(fp); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # precompiled from %s\n", name, cpathname); - m = exec_code_module(name, (object *)co); - DECREF(co); + m = PyImport_ExecCodeModule(name, (PyObject *)co); + Py_DECREF(co); return m; } /* Parse a source file and return the corresponding code object */ -static codeobject * +static PyCodeObject * parse_source_module(pathname, fp) char *pathname; FILE *fp; { - codeobject *co; + PyCodeObject *co; node *n; - n = parse_file(fp, pathname, file_input); + n = PyParser_SimpleParseFile(fp, pathname, file_input); if (n == NULL) return NULL; - co = compile(n, pathname); - freetree(n); + co = PyNode_Compile(n, pathname); + PyNode_Free(n); return co; } @@ -335,7 +334,7 @@ parse_source_module(pathname, fp) static void write_compiled_module(co, cpathname, mtime) - codeobject *co; + PyCodeObject *co; char *cpathname; long mtime; { @@ -343,17 +342,17 @@ write_compiled_module(co, cpathname, mtime) fp = fopen(cpathname, "wb"); if (fp == NULL) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# can't create %s\n", cpathname); return; } - wr_long(MAGIC, fp); + PyMarshal_WriteLongToFile(MAGIC, fp); /* First write a 0 for mtime */ - wr_long(0L, fp); - wr_object((object *)co, fp); + PyMarshal_WriteLongToFile(0L, fp); + PyMarshal_WriteObjectToFile((PyObject *)co, fp); if (ferror(fp)) { - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# can't write %s\n", cpathname); /* Don't keep partial file */ fclose(fp); @@ -362,10 +361,10 @@ write_compiled_module(co, cpathname, mtime) } /* Now write the true mtime */ fseek(fp, 4L, 0); - wr_long(mtime, fp); + PyMarshal_WriteLongToFile(mtime, fp); fflush(fp); fclose(fp); - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "# wrote %s\n", cpathname); #ifdef macintosh setfiletype(cpathname, 'Pyth', 'PYC '); @@ -377,7 +376,7 @@ write_compiled_module(co, cpathname, mtime) object WITH INCREMENTED REFERENCE COUNT. If there's a matching byte-compiled file, use that instead. */ -static object * +static PyObject * load_source_module(name, pathname, fp) char *name; char *pathname; @@ -387,10 +386,10 @@ load_source_module(name, pathname, fp) FILE *fpc; char buf[MAXPATHLEN+1]; char *cpathname; - codeobject *co; - object *m; + PyCodeObject *co; + PyObject *m; - mtime = getmtime(pathname); + mtime = PyOS_GetLastModificationTime(pathname); cpathname = make_compiled_pathname(pathname, buf, MAXPATHLEN+1); if (cpathname != NULL && (fpc = check_compiled_module(pathname, mtime, cpathname))) { @@ -398,7 +397,7 @@ load_source_module(name, pathname, fp) fclose(fpc); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # precompiled from %s\n", name, cpathname); } @@ -406,13 +405,13 @@ load_source_module(name, pathname, fp) co = parse_source_module(pathname, fp); if (co == NULL) return NULL; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # from %s\n", name, pathname); write_compiled_module(co, cpathname, mtime); } - m = exec_code_module(name, (object *)co); - DECREF(co); + m = PyImport_ExecCodeModule(name, (PyObject *)co); + Py_DECREF(co); return m; } @@ -425,7 +424,7 @@ load_source_module(name, pathname, fp) static struct filedescr * find_module(name, path, buf, buflen, p_fp) char *name; - object *path; + PyObject *path; /* Output parameters: */ char *buf; int buflen; @@ -445,27 +444,28 @@ find_module(name, path, buf, buflen, p_fp) if (path == NULL) - path = sysget("path"); - if (path == NULL || !is_listobject(path)) { - err_setstr(ImportError, + path = PySys_GetObject("path"); + if (path == NULL || !PyList_Check(path)) { + PyErr_SetString(PyExc_ImportError, "sys.path must be a list of directory names"); return NULL; } - npath = getlistsize(path); + npath = PyList_Size(path); namelen = strlen(name); for (i = 0; i < npath; i++) { - object *v = getlistitem(path, i); - if (!is_stringobject(v)) + PyObject *v = PyList_GetItem(path, i); + if (!PyString_Check(v)) continue; - len = getstringsize(v); - if (len + 2 + namelen + import_maxsuffixsize >= buflen) + len = PyString_Size(v); + if (len + 2 + namelen + _PyImport_MaxSuffixSize >= buflen) continue; /* Too long */ - strcpy(buf, getstringvalue(v)); + strcpy(buf, PyString_AsString(v)); if ((int)strlen(buf) != len) continue; /* v contains '\0' */ #ifdef macintosh if ( PyMac_FindResourceModule(name, buf) ) { - static struct filedescr resfiledescr = { "", "", PY_RESOURCE}; + static struct filedescr resfiledescr = + {"", "", PY_RESOURCE}; return &resfiledescr; } @@ -476,7 +476,7 @@ find_module(name, path, buf, buflen, p_fp) /* see if we are searching in directory dos_8x3 */ if (len > 7 && !strncmp(buf + len - 8, "dos_8x3", 7)){ int j; - char ch; /* limit name to eight lower-case characters */ + char ch; /* limit name to 8 lower-case characters */ for (j = 0; (ch = name[j]) && j < 8; j++) if (isupper(ch)) buf[len++] = tolower(ch); @@ -489,9 +489,9 @@ find_module(name, path, buf, buflen, p_fp) strcpy(buf+len, name); len += namelen; } - for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { + for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { strcpy(buf+len, fdp->suffix); - if (verbose > 1) + if (Py_VerboseFlag > 1) fprintf(stderr, "# trying %s\n", buf); fp = fopen(buf, fdp->mode); if (fp != NULL) @@ -503,7 +503,7 @@ find_module(name, path, buf, buflen, p_fp) if (fp == NULL) { char buf[256]; sprintf(buf, "No module named %.200s", name); - err_setstr(ImportError, buf); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } @@ -515,16 +515,16 @@ find_module(name, path, buf, buflen, p_fp) /* Load an external module using the default search path and return its module object WITH INCREMENTED REFERENCE COUNT */ -static object * +static PyObject * load_module(name) char *name; { char buf[MAXPATHLEN+1]; struct filedescr *fdp; FILE *fp = NULL; - object *m; + PyObject *m; - fdp = find_module(name, (object *)NULL, buf, MAXPATHLEN+1, &fp); + fdp = find_module(name, (PyObject *)NULL, buf, MAXPATHLEN+1, &fp); if (fdp == NULL) return NULL; @@ -539,7 +539,7 @@ load_module(name) break; case C_EXTENSION: - m = load_dynamic_module(name, buf, fp); + m = _PyImport_LoadDynamicModule(name, buf, fp); break; #ifdef macintosh @@ -549,7 +549,7 @@ load_module(name) #endif default: - err_setstr(SystemError, + PyErr_SetString(PyExc_SystemError, "find_module returned unexpected result"); m = NULL; @@ -573,15 +573,15 @@ init_builtin(name) for (i = 0; inittab[i].name != NULL; i++) { if (strcmp(name, inittab[i].name) == 0) { if (inittab[i].initfunc == NULL) { - err_setstr(ImportError, + PyErr_SetString(PyExc_ImportError, "Cannot re-init internal module"); return -1; } - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # builtin\n", name); (*inittab[i].initfunc)(); - if (err_occurred()) + if (PyErr_Occurred()) return -1; return 1; } @@ -598,7 +598,7 @@ find_frozen(name) { struct _frozen *p; - for (p = frozen_modules; ; p++) { + for (p = PyImport_FrozenModules; ; p++) { if (p->name == NULL) return NULL; if (strcmp(p->name, name) == 0) @@ -607,17 +607,17 @@ find_frozen(name) return p; } -static object * +static PyObject * get_frozen_object(name) char *name; { struct _frozen *p = find_frozen(name); if (p == NULL) { - err_setstr(ImportError, "No such frozen object"); + PyErr_SetString(PyExc_ImportError, "No such frozen object"); return NULL; } - return rds_object((char *)p->code, p->size); + return PyMarshal_ReadObjectFromString((char *)p->code, p->size); } /* Initialize a frozen module. @@ -626,30 +626,31 @@ get_frozen_object(name) This function is also used from frozenmain.c */ int -init_frozen(name) +PyImport_ImportFrozenModule(name) char *name; { struct _frozen *p = find_frozen(name); - object *co; - object *m; + PyObject *co; + PyObject *m; if (p == NULL) return 0; - if (verbose) + if (Py_VerboseFlag) fprintf(stderr, "import %s # frozen\n", name); - co = rds_object((char *)p->code, p->size); + co = PyMarshal_ReadObjectFromString((char *)p->code, p->size); if (co == NULL) return -1; - if (!is_codeobject(co)) { - DECREF(co); - err_setstr(TypeError, "frozen object is not a code object"); + if (!PyCode_Check(co)) { + Py_DECREF(co); + PyErr_SetString(PyExc_TypeError, + "frozen object is not a code object"); return -1; } - m = exec_code_module(name, co); - DECREF(co); + m = PyImport_ExecCodeModule(name, co); + Py_DECREF(co); if (m == NULL) return -1; - DECREF(m); + Py_DECREF(m); return 1; } @@ -657,31 +658,34 @@ init_frozen(name) /* Import a module, either built-in, frozen, or external, and return its module object WITH INCREMENTED REFERENCE COUNT */ -object * -import_module(name) +PyObject * +PyImport_ImportModule(name) char *name; { - object *m; + PyObject *m; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if ((m = dictlookup(import_modules, name)) != NULL) { - INCREF(m); + if ((m = PyDict_GetItemString(import_modules, name)) != NULL) { + Py_INCREF(m); } else { int i; - if ((i = init_builtin(name)) || (i = init_frozen(name))) { + if ((i = init_builtin(name)) || + (i = PyImport_ImportFrozenModule(name))) { if (i < 0) return NULL; - if ((m = dictlookup(import_modules, name)) == NULL) { - if (err_occurred() == NULL) - err_setstr(SystemError, + if ((m = PyDict_GetItemString(import_modules, + name)) == NULL) { + if (PyErr_Occurred() == NULL) + PyErr_SetString(PyExc_SystemError, "built-in module not initialized properly"); } else - INCREF(m); + Py_INCREF(m); } else m = load_module(name); @@ -694,33 +698,37 @@ import_module(name) /* Re-import a module of any kind and return its module object, WITH INCREMENTED REFERENCE COUNT */ -object * -reload_module(m) - object *m; +PyObject * +PyImport_ReloadModule(m) + PyObject *m; { char *name; int i; - if (m == NULL || !is_moduleobject(m)) { - err_setstr(TypeError, "reload() argument must be module"); + if (m == NULL || !PyModule_Check(m)) { + PyErr_SetString(PyExc_TypeError, + "reload() argument must be module"); return NULL; } - name = getmodulename(m); + name = PyModule_GetName(m); if (name == NULL) return NULL; if (import_modules == NULL) { - err_setstr(SystemError, "sys.modules has been deleted"); + PyErr_SetString(PyExc_SystemError, + "sys.modules has been deleted"); return NULL; } - if (m != dictlookup(import_modules, name)) { - err_setstr(ImportError, "reload() module not in sys.modules"); + if (m != PyDict_GetItemString(import_modules, name)) { + PyErr_SetString(PyExc_ImportError, + "reload() module not in sys.modules"); return NULL; } /* Check for built-in and frozen modules */ - if ((i = init_builtin(name)) || (i = init_frozen(name))) { + if ((i = init_builtin(name)) || + (i = PyImport_ImportFrozenModule(name))) { if (i < 0) return NULL; - INCREF(m); + Py_INCREF(m); } else m = load_module(name); @@ -732,206 +740,208 @@ reload_module(m) importing modules. */ -static object * +static PyObject * imp_get_magic(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char buf[4]; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; buf[0] = (char) ((MAGIC >> 0) & 0xff); buf[1] = (char) ((MAGIC >> 8) & 0xff); buf[2] = (char) ((MAGIC >> 16) & 0xff); buf[3] = (char) ((MAGIC >> 24) & 0xff); - return newsizedstringobject(buf, 4); + return PyString_FromStringAndSize(buf, 4); } -static object * +static PyObject * imp_get_suffixes(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *list; + PyObject *list; struct filedescr *fdp; - if (!newgetargs(args, "")) + if (!PyArg_ParseTuple(args, "")) return NULL; - list = newlistobject(0); + list = PyList_New(0); if (list == NULL) return NULL; - for (fdp = import_filetab; fdp->suffix != NULL; fdp++) { - object *item = mkvalue("ssi", + for (fdp = _PyImport_Filetab; fdp->suffix != NULL; fdp++) { + PyObject *item = Py_BuildValue("ssi", fdp->suffix, fdp->mode, fdp->type); if (item == NULL) { - DECREF(list); + Py_DECREF(list); return NULL; } - if (addlistitem(list, item) < 0) { - DECREF(list); - DECREF(item); + if (PyList_Append(list, item) < 0) { + Py_DECREF(list); + Py_DECREF(item); return NULL; } - DECREF(item); + Py_DECREF(item); } return list; } -static object * +static PyObject * imp_find_module(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - extern int fclose PROTO((FILE *)); + extern int fclose Py_PROTO((FILE *)); char *name; - object *path = NULL; - object *fob, *ret; + PyObject *path = NULL; + PyObject *fob, *ret; struct filedescr *fdp; char pathname[MAXPATHLEN+1]; FILE *fp; - if (!newgetargs(args, "s|O!", &name, &Listtype, &path)) + if (!PyArg_ParseTuple(args, "s|O!", &name, &PyList_Type, &path)) return NULL; fdp = find_module(name, path, pathname, MAXPATHLEN+1, &fp); if (fdp == NULL) return NULL; - fob = newopenfileobject(fp, pathname, fdp->mode, fclose); + fob = PyFile_FromFile(fp, pathname, fdp->mode, fclose); if (fob == NULL) { fclose(fp); return NULL; } - ret = mkvalue("Os(ssi)", + ret = Py_BuildValue("Os(ssi)", fob, pathname, fdp->suffix, fdp->mode, fdp->type); - DECREF(fob); + Py_DECREF(fob); return ret; } -static object * +static PyObject * imp_init_builtin(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; int ret; - object *m; - if (!newgetargs(args, "s", &name)) + PyObject *m; + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; ret = init_builtin(name); if (ret < 0) return NULL; if (ret == 0) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } - m = add_module(name); - XINCREF(m); + m = PyImport_AddModule(name); + Py_XINCREF(m); return m; } -static object * +static PyObject * imp_init_frozen(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; int ret; - object *m; - if (!newgetargs(args, "s", &name)) + PyObject *m; + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - ret = init_frozen(name); + ret = PyImport_ImportFrozenModule(name); if (ret < 0) return NULL; if (ret == 0) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } - m = add_module(name); - XINCREF(m); + m = PyImport_AddModule(name); + Py_XINCREF(m); return m; } -static object * +static PyObject * imp_get_frozen_object(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; return get_frozen_object(name); } -static object * +static PyObject * imp_is_builtin(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { int i; char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; for (i = 0; inittab[i].name != NULL; i++) { if (strcmp(name, inittab[i].name) == 0) { if (inittab[i].initfunc == NULL) - return newintobject(-1); + return PyInt_FromLong(-1); else - return newintobject(1); + return PyInt_FromLong(1); } } - return newintobject(0); + return PyInt_FromLong(0); } -static object * +static PyObject * imp_is_frozen(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { struct _frozen *p; char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - for (p = frozen_modules; ; p++) { + for (p = PyImport_FrozenModules; ; p++) { if (p->name == NULL) break; if (strcmp(p->name, name) == 0) - return newintobject(1); + return PyInt_FromLong(1); } - return newintobject(0); + return PyInt_FromLong(0); } static FILE * get_file(pathname, fob, mode) char *pathname; - object *fob; + PyObject *fob; char *mode; { FILE *fp; if (fob == NULL) { fp = fopen(pathname, mode); if (fp == NULL) - err_errno(IOError); + PyErr_SetFromErrno(PyExc_IOError); } else { - fp = getfilefile(fob); + fp = PyFile_AsFile(fob); if (fp == NULL) - err_setstr(ValueError, "bad/closed file object"); + PyErr_SetString(PyExc_ValueError, + "bad/closed file object"); } return fp; } -static object * +static PyObject * imp_load_compiled(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp; - if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; fp = get_file(pathname, fob, "rb"); if (fp == NULL) @@ -940,35 +950,37 @@ imp_load_compiled(self, args) return m; } -static object * +static PyObject * imp_load_dynamic(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp = NULL; - if (!newgetargs(args, "ss|O!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ss|O!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; if (fob) fp = get_file(pathname, fob, "r"); - m = load_dynamic_module(name, pathname, fp); + m = _PyImport_LoadDynamicModule(name, pathname, fp); return m; } -static object * +static PyObject * imp_load_source(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *fob = NULL; - object *m; + PyObject *fob = NULL; + PyObject *m; FILE *fp; - if (!newgetargs(args, "ssO!", &name, &pathname, &Filetype, &fob)) + if (!PyArg_ParseTuple(args, "ssO!", &name, &pathname, + &PyFile_Type, &fob)) return NULL; fp = get_file(pathname, fob, "r"); if (fp == NULL) @@ -978,34 +990,34 @@ imp_load_source(self, args) } #ifdef macintosh -static object * +static PyObject * imp_load_resource(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; char *pathname; - object *m; + PyObject *m; - if (!newgetargs(args, "ss", &name, &pathname)) + if (!PyArg_ParseTuple(args, "ss", &name, &pathname)) return NULL; m = PyMac_LoadResourceModule(name, pathname); return m; } #endif /* macintosh */ -static object * +static PyObject * imp_new_module(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { char *name; - if (!newgetargs(args, "s", &name)) + if (!PyArg_ParseTuple(args, "s", &name)) return NULL; - return newmoduleobject(name); + return PyModule_New(name); } -static struct methodlist imp_methods[] = { +static PyMethodDef imp_methods[] = { {"get_frozen_object", imp_get_frozen_object, 1}, {"get_magic", imp_get_magic, 1}, {"get_suffixes", imp_get_suffixes, 1}, @@ -1027,34 +1039,34 @@ static struct methodlist imp_methods[] = { void initimp() { - object *m, *d, *v; + PyObject *m, *d, *v; - m = initmodule("imp", imp_methods); - d = getmoduledict(m); + m = Py_InitModule("imp", imp_methods); + d = PyModule_GetDict(m); - v = newintobject(SEARCH_ERROR); - dictinsert(d, "SEARCH_ERROR", v); - XDECREF(v); + v = PyInt_FromLong(SEARCH_ERROR); + PyDict_SetItemString(d, "SEARCH_ERROR", v); + Py_XDECREF(v); - v = newintobject(PY_SOURCE); - dictinsert(d, "PY_SOURCE", v); - XDECREF(v); + v = PyInt_FromLong(PY_SOURCE); + PyDict_SetItemString(d, "PY_SOURCE", v); + Py_XDECREF(v); - v = newintobject(PY_COMPILED); - dictinsert(d, "PY_COMPILED", v); - XDECREF(v); + v = PyInt_FromLong(PY_COMPILED); + PyDict_SetItemString(d, "PY_COMPILED", v); + Py_XDECREF(v); - v = newintobject(C_EXTENSION); - dictinsert(d, "C_EXTENSION", v); - XDECREF(v); + v = PyInt_FromLong(C_EXTENSION); + PyDict_SetItemString(d, "C_EXTENSION", v); + Py_XDECREF(v); #ifdef macintosh - v = newintobject(PY_RESOURCE); - dictinsert(d, "PY_RESOURCE", v); - XDECREF(v); + v = PyInt_FromLong(PY_RESOURCE); + PyDict_SetItemString(d, "PY_RESOURCE", v); + Py_XDECREF(v); #endif - if (err_occurred()) - fatal("imp module initialization failed"); + if (PyErr_Occurred()) + Py_FatalError("imp module initialization failed"); } diff --git a/Python/importdl.c b/Python/importdl.c index 6541e09..16271d6 100644 --- a/Python/importdl.c +++ b/Python/importdl.c @@ -32,7 +32,7 @@ PERFORMANCE OF THIS SOFTWARE. /* Support for dynamic loading of extension modules */ /* If no dynamic linking is supported, this file still generates some code! */ -#include "allobjects.h" +#include "Python.h" #include "osdefs.h" #include "importdl.h" @@ -167,7 +167,7 @@ typedef void (*dl_funcptr)(); #ifdef USE_MAC_DYNAMIC_LOADING #include #include -#ifdef SYMANTEC__CFM68K__ /* Really just an older version of Universal Headers */ +#ifdef SYMANTEC__CFM68K__ /* Really an older version of Universal Headers */ #define CFragConnectionID ConnectionID #define kLoadCFrag 0x01 #endif @@ -184,7 +184,7 @@ typedef void (*dl_funcptr)(); #endif #endif /* USE_RLD */ -extern char *getprogramname(); +extern char *Py_GetProgramName(); #ifndef FUNCNAME_PATTERN #if defined(__hp9000s300) || defined(__NetBSD__) || defined(__FreeBSD__) || defined(__BORLANDC__) @@ -207,9 +207,9 @@ extern char *getprogramname(); #endif /* Pass it on to import.c */ -int import_maxsuffixsize = MAXSUFFIXSIZE; +int _PyImport_MaxSuffixSize = MAXSUFFIXSIZE; -struct filedescr import_filetab[] = { +struct filedescr _PyImport_Filetab[] = { #ifdef SHORT_EXT {SHORT_EXT, "rb", C_EXTENSION}, #endif /* !SHORT_EXT */ @@ -225,17 +225,18 @@ struct filedescr import_filetab[] = { #undef DYNAMIC_LINK #endif -object * -load_dynamic_module(name, pathname, fp) +PyObject * +_PyImport_LoadDynamicModule(name, pathname, fp) char *name; char *pathname; FILE *fp; { #ifndef DYNAMIC_LINK - err_setstr(ImportError, "dynamically linked modules not supported"); + PyErr_SetString(PyExc_ImportError, + "dynamically linked modules not supported"); return NULL; #else - object *m, *d, *s; + PyObject *m, *d, *s; char funcname[258]; dl_funcptr p = NULL; #ifdef USE_SHLIB @@ -274,11 +275,12 @@ load_dynamic_module(name, pathname, fp) #endif /* USE_SHLIB */ #ifdef USE_MAC_DYNAMIC_LOADING /* - ** Dynamic loading of CFM shared libraries on the Mac. - ** The code has become more convoluted than it was, because we want to be able - ** to put multiple modules in a single file. For this reason, we have to determine - ** the fragment name, and we cannot use the library entry point but we have to locate - ** the correct init routine "by hand". + ** Dynamic loading of CFM shared libraries on the Mac. The + ** code has become more convoluted than it was, because we + ** want to be able to put multiple modules in a single + ** file. For this reason, we have to determine the fragment + ** name, and we cannot use the library entry point but we have + ** to locate the correct init routine "by hand". */ { FSSpec libspec; @@ -297,30 +299,35 @@ load_dynamic_module(name, pathname, fp) err = ResolveAliasFile(&libspec, 1, &isfolder, &didsomething); if ( err ) { sprintf(buf, "%s: %s", pathname, PyMac_StrError(err)); - err_setstr(ImportError, buf); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } - /* Next, determine the fragment name, by stripping '.slb' and 'module' */ + /* Next, determine the fragment name, + by stripping '.slb' and 'module' */ memcpy(fragname+1, libspec.name+1, libspec.name[0]); fragname[0] = libspec.name[0]; - if( strncmp((char *)(fragname+1+fragname[0]-4), ".slb", 4) == 0 ) + if( strncmp((char *)(fragname+1+fragname[0]-4), + ".slb", 4) == 0 ) fragname[0] -= 4; - if ( strncmp((char *)(fragname+1+fragname[0]-6), "module", 6) == 0 ) + if ( strncmp((char *)(fragname+1+fragname[0]-6), + "module", 6) == 0 ) fragname[0] -= 6; - /* Load the fragment (or return the connID if it is already loaded */ + /* Load the fragment + (or return the connID if it is already loaded */ err = GetDiskFragment(&libspec, 0, 0, fragname, kLoadCFrag, &connID, &mainAddr, errMessage); if ( err ) { - sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, PyMac_StrError(err)); - err_setstr(ImportError, buf); + sprintf(buf, "%.*s: %s", errMessage[0], errMessage+1, + PyMac_StrError(err)); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } /* Locate the address of the correct init function */ err = FindSymbol(connID, Pstring(funcname), &symAddr, &class); if ( err ) { sprintf(buf, "%s: %s", funcname, PyMac_StrError(err)); - err_setstr(ImportError, buf); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } p = (dl_funcptr)symAddr; @@ -334,12 +341,12 @@ load_dynamic_module(name, pathname, fp) void *handle = dlopen(pathname, RTLD_NOW); #else void *handle; - if (verbose) + if (Py_VerboseFlag) printf("dlopen(\"%s\", %d);\n", pathname, RTLD_LAZY); handle = dlopen(pathname, RTLD_LAZY); #endif /* RTLD_NOW */ if (handle == NULL) { - err_setstr(ImportError, dlerror()); + PyErr_SetString(PyExc_ImportError, dlerror()); return NULL; } if (fp != NULL && nhandles < 128) @@ -382,35 +389,44 @@ load_dynamic_module(name, pathname, fp) unsigned int errorCode; /* Get an error string from Win32 error code */ - char theInfo[256]; /* Pointer to error text from system */ - int theLength; /* Length of error text */ + char theInfo[256]; /* Pointer to error text + from system */ + int theLength; /* Length of error text */ errorCode = GetLastError(); - theLength = FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, /* flags */ - NULL, /* message source */ - errorCode, /* the message (error) ID */ - 0, /* default language environment */ - (LPTSTR) theInfo, /* the buffer */ - sizeof(theInfo), /* the buffer size */ - NULL); /* no additional format args. */ - - /* Problem: could not get the error message. This should not happen if called correctly. */ + theLength = FormatMessage( + FORMAT_MESSAGE_FROM_SYSTEM, /* flags */ + NULL, /* message source */ + errorCode, /* the message (error) ID */ + 0, /* default language environment */ + (LPTSTR) theInfo, /* the buffer */ + sizeof(theInfo), /* the buffer size */ + NULL); /* no additional format args. */ + + /* Problem: could not get the error message. + This should not happen if called correctly. */ if (theLength == 0) { - sprintf(errBuf, "DLL load failed with error code %d", errorCode); + sprintf(errBuf, + "DLL load failed with error code %d", + errorCode); } else { int len; - /* For some reason a \r\n is appended to the text */ - if (theLength >= 2 && theInfo[theLength-2] == '\r' && theInfo[theLength-1] == '\n') { + /* For some reason a \r\n + is appended to the text */ + if (theLength >= 2 && + theInfo[theLength-2] == '\r' && + theInfo[theLength-1] == '\n') { theLength -= 2; theInfo[theLength] = '\0'; } strcpy(errBuf, "DLL load failed: "); len = strlen(errBuf); - strncpy(errBuf+len, theInfo, sizeof(errBuf)-len); + strncpy(errBuf+len, theInfo, + sizeof(errBuf)-len); errBuf[sizeof(errBuf)-1] = '\0'; } - err_setstr(ImportError, errBuf); + PyErr_SetString(PyExc_ImportError, errBuf); return NULL; } p = GetProcAddress(hDLL, funcname); @@ -422,15 +438,16 @@ load_dynamic_module(name, pathname, fp) hDLL = LoadLibrary(pathname); if (hDLL < HINSTANCE_ERROR){ char errBuf[256]; - sprintf(errBuf, "DLL load failed with error code %d", hDLL); - err_setstr(ImportError, errBuf); + sprintf(errBuf, + "DLL load failed with error code %d", hDLL); + PyErr_SetString(PyExc_ImportError, errBuf); return NULL; } p = GetProcAddress(hDLL, funcname); } #endif /* MS_WIN16 */ #ifdef USE_DL - p = dl_loadmod(getprogramname(), pathname, funcname); + p = dl_loadmod(Py_GetProgramName(), pathname, funcname); #endif /* USE_DL */ #ifdef USE_RLD { @@ -455,7 +472,7 @@ load_dynamic_module(name, pathname, fp) NXGetMemoryBuffer(errorStream, &streamBuf, &len, &maxLen); - err_setstr(ImportError, streamBuf); + PyErr_SetString(PyExc_ImportError, streamBuf); } if(ret && rld_lookup(errorStream, funcname, &ptr)) @@ -473,25 +490,26 @@ load_dynamic_module(name, pathname, fp) int flags; flags = BIND_FIRST | BIND_DEFERRED; - if (verbose) + if (Py_VerboseFlag) { - flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE | BIND_NONFATAL | BIND_VERBOSE; + flags = DYNAMIC_PATH | BIND_FIRST | BIND_IMMEDIATE | + BIND_NONFATAL | BIND_VERBOSE; printf("shl_load %s\n",pathname); } lib = shl_load(pathname, flags, 0); if (lib == NULL) { char buf[256]; - if (verbose) + if (Py_VerboseFlag) perror(pathname); sprintf(buf, "Failed to load %.200s", pathname); - err_setstr(ImportError, buf); + PyErr_SetString(PyExc_ImportError, buf); return NULL; } - if (verbose) + if (Py_VerboseFlag) printf("shl_findsym %s\n", funcname); shl_findsym(&lib, funcname, TYPE_UNDEFINED, (void *) &p); - if (p == NULL && verbose) + if (p == NULL && Py_VerboseFlag) perror(funcname); } #endif /* hpux */ @@ -499,31 +517,31 @@ load_dynamic_module(name, pathname, fp) got_it: #endif if (p == NULL) { - err_setstr(ImportError, + PyErr_SetString(PyExc_ImportError, "dynamic module does not define init function"); return NULL; } (*p)(); /* XXX Need check for err_occurred() here */ - m = dictlookup(import_modules, name); + m = PyDict_GetItemString(import_modules, name); if (m == NULL) { - if (err_occurred() == NULL) - err_setstr(SystemError, + if (PyErr_Occurred() == NULL) + PyErr_SetString(PyExc_SystemError, "dynamic module not initialized properly"); return NULL; } /* Remember the filename as the __file__ attribute */ - d = getmoduledict(m); - s = newstringobject(pathname); - if (s == NULL || dictinsert(d, "__file__", s) != 0) - err_clear(); /* Not important enough to report */ - XDECREF(s); - if (verbose) + d = PyModule_GetDict(m); + s = PyString_FromString(pathname); + if (s == NULL || PyDict_SetItemString(d, "__file__", s) != 0) + PyErr_Clear(); /* Not important enough to report */ + Py_XDECREF(s); + if (Py_VerboseFlag) fprintf(stderr, "import %s # dynamically loaded from %s\n", name, pathname); - INCREF(m); + Py_INCREF(m); return m; #endif /* DYNAMIC_LINK */ } @@ -555,7 +573,7 @@ aix_getoldmodules(modlistptr) -- Get the list of loaded modules into ld_info structures. */ if ((ldibuf = malloc(bufsize)) == NULL) { - err_setstr(ImportError, strerror(errno)); + PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } while ((errflag = loadquery(L_GETINFO, ldibuf, bufsize)) == -1 @@ -563,12 +581,12 @@ aix_getoldmodules(modlistptr) free(ldibuf); bufsize += 1024; if ((ldibuf = malloc(bufsize)) == NULL) { - err_setstr(ImportError, strerror(errno)); + PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } } if (errflag == -1) { - err_setstr(ImportError, strerror(errno)); + PyErr_SetString(PyExc_ImportError, strerror(errno)); return -1; } /* @@ -578,7 +596,7 @@ aix_getoldmodules(modlistptr) prevmodptr = NULL; do { if ((modptr = (ModulePtr)malloc(sizeof(Module))) == NULL) { - err_setstr(ImportError, strerror(errno)); + PyErr_SetString(PyExc_ImportError, strerror(errno)); while (*modlistptr) { modptr = (ModulePtr)*modlistptr; *modlistptr = (void *)modptr->next; @@ -662,7 +680,7 @@ aix_loaderror(pathname) ERRBUF_APPEND("\n"); } errbuf[strlen(errbuf)-1] = '\0'; /* trim off last newline */ - err_setstr(ImportError, errbuf); + PyErr_SetString(PyExc_ImportError, errbuf); return; } diff --git a/Python/importdl.h b/Python/importdl.h index 24f061d..48aa1a4 100644 --- a/Python/importdl.h +++ b/Python/importdl.h @@ -40,10 +40,11 @@ extern struct filedescr { char *suffix; char *mode; enum filetype type; -} import_filetab[]; +} _PyImport_Filetab[]; -extern object *import_modules; +extern PyObject *import_modules; -extern object *load_dynamic_module PROTO((char *name, char *pathname, FILE *)); +extern PyObject *_PyImport_LoadDynamicModule + Py_PROTO((char *name, char *pathname, FILE *)); -extern int import_maxsuffixsize; +extern int _PyImport_MaxSuffixSize; diff --git a/Python/marshal.c b/Python/marshal.c index 22d0242..6880bdf 100644 --- a/Python/marshal.c +++ b/Python/marshal.c @@ -34,14 +34,11 @@ PERFORMANCE OF THIS SOFTWARE. a true persistent storage facility would be much harder, since it would have to take circular links and sharing into account. */ -#include "allobjects.h" -#include "modsupport.h" +#include "Python.h" #include "longintrepr.h" #include "compile.h" #include "marshal.h" -#include - #define TYPE_NULL '0' #define TYPE_NONE 'N' #define TYPE_ELLIPSIS '.' @@ -61,7 +58,7 @@ typedef struct { FILE *fp; int error; /* If fp == NULL, the following are valid: */ - object *str; + PyObject *str; char *ptr; char *end; } WFILE; @@ -78,14 +75,15 @@ w_more(c, p) int size, newsize; if (p->str == NULL) return; /* An error already occurred */ - size = getstringsize(p->str); + size = PyString_Size(p->str); newsize = size + 1024; - if (resizestring(&p->str, newsize) != 0) { + if (_PyString_Resize(&p->str, newsize) != 0) { p->ptr = p->end = NULL; } else { - p->ptr = GETSTRINGVALUE((stringobject *)p->str) + size; - p->end = GETSTRINGVALUE((stringobject *)p->str) + newsize; + p->ptr = PyString_AS_STRING((PyStringObject *)p->str) + size; + p->end = + PyString_AS_STRING((PyStringObject *)p->str) + newsize; *p->ptr++ = c; } } @@ -140,19 +138,19 @@ w_long64(x, p) static void w_object(v, p) - object *v; + PyObject *v; WFILE *p; { int i, n; if (v == NULL) w_byte(TYPE_NULL, p); - else if (v == None) + else if (v == Py_None) w_byte(TYPE_NONE, p); else if (v == Py_Ellipsis) w_byte(TYPE_ELLIPSIS, p); - else if (is_intobject(v)) { - long x = GETINTVALUE((intobject *)v); + else if (PyInt_Check(v)) { + long x = PyInt_AS_LONG((PyIntObject *)v); #if SIZEOF_LONG > 4 long y = x>>31; if (y && y != -1) { @@ -166,8 +164,8 @@ w_object(v, p) w_long(x, p); } } - else if (is_longobject(v)) { - longobject *ob = (longobject *)v; + else if (PyLong_Check(v)) { + PyLongObject *ob = (PyLongObject *)v; w_byte(TYPE_LONG, p); n = ob->ob_size; w_long((long)n, p); @@ -176,77 +174,81 @@ w_object(v, p) for (i = 0; i < n; i++) w_short(ob->ob_digit[i], p); } - else if (is_floatobject(v)) { - extern void float_buf_repr PROTO((char *, floatobject *)); + else if (PyFloat_Check(v)) { + extern void PyFloat_AsString + Py_PROTO((char *, PyFloatObject *)); char buf[256]; /* Plenty to format any double */ - float_buf_repr(buf, (floatobject *)v); + PyFloat_AsString(buf, (PyFloatObject *)v); n = strlen(buf); w_byte(TYPE_FLOAT, p); w_byte(n, p); w_string(buf, n, p); } #ifndef WITHOUT_COMPLEX - else if (is_complexobject(v)) { - extern void float_buf_repr PROTO((char *, floatobject *)); + else if (PyComplex_Check(v)) { + extern void PyFloat_AsString + Py_PROTO((char *, PyFloatObject *)); char buf[256]; /* Plenty to format any double */ - floatobject *temp; + PyFloatObject *temp; w_byte(TYPE_COMPLEX, p); - temp = (floatobject*)newfloatobject(PyComplex_RealAsDouble(v)); - float_buf_repr(buf, temp); - DECREF(temp); + temp = (PyFloatObject*)PyFloat_FromDouble( + PyComplex_RealAsDouble(v)); + PyFloat_AsString(buf, temp); + Py_DECREF(temp); n = strlen(buf); w_byte(n, p); w_string(buf, n, p); - temp = (floatobject*)newfloatobject(PyComplex_ImagAsDouble(v)); - float_buf_repr(buf, temp); - DECREF(temp); + temp = (PyFloatObject*)PyFloat_FromDouble( + PyComplex_ImagAsDouble(v)); + PyFloat_AsString(buf, temp); + Py_DECREF(temp); n = strlen(buf); w_byte(n, p); w_string(buf, n, p); } #endif - else if (is_stringobject(v)) { + else if (PyString_Check(v)) { w_byte(TYPE_STRING, p); - n = getstringsize(v); + n = PyString_Size(v); w_long((long)n, p); - w_string(getstringvalue(v), n, p); + w_string(PyString_AsString(v), n, p); } - else if (is_tupleobject(v)) { + else if (PyTuple_Check(v)) { w_byte(TYPE_TUPLE, p); - n = gettuplesize(v); + n = PyTuple_Size(v); w_long((long)n, p); for (i = 0; i < n; i++) { - w_object(GETTUPLEITEM(v, i), p); + w_object(PyTuple_GET_ITEM(v, i), p); } } - else if (is_listobject(v)) { + else if (PyList_Check(v)) { w_byte(TYPE_LIST, p); - n = getlistsize(v); + n = PyList_Size(v); w_long((long)n, p); for (i = 0; i < n; i++) { - w_object(getlistitem(v, i), p); + w_object(PyList_GetItem(v, i), p); } } - else if (is_dictobject(v)) { + else if (PyDict_Check(v)) { int pos; - object *key, *value; + PyObject *key, *value; w_byte(TYPE_DICT, p); /* This one is NULL object terminated! */ pos = 0; - while (mappinggetnext(v, &pos, &key, &value)) { + while (PyDict_Next(v, &pos, &key, &value)) { w_object(key, p); w_object(value, p); } - w_object((object *)NULL, p); + w_object((PyObject *)NULL, p); } - else if (is_codeobject(v)) { - codeobject *co = (codeobject *)v; + else if (PyCode_Check(v)) { + PyCodeObject *co = (PyCodeObject *)v; w_byte(TYPE_CODE, p); w_short(co->co_argcount, p); w_short(co->co_nlocals, p); w_short(co->co_stacksize, p); w_short(co->co_flags, p); - w_object((object *)co->co_code, p); + w_object((PyObject *)co->co_code, p); w_object(co->co_consts, p); w_object(co->co_names, p); w_object(co->co_varnames, p); @@ -262,7 +264,7 @@ w_object(v, p) } void -wr_long(x, fp) +PyMarshal_WriteLongToFile(x, fp) long x; FILE *fp; { @@ -273,8 +275,8 @@ wr_long(x, fp) } void -wr_object(x, fp) - object *x; +PyMarshal_WriteObjectToFile(x, fp) + PyObject *x; FILE *fp; { WFILE wf; @@ -351,10 +353,10 @@ r_long64(p) x = (x & 0xFFFFFFFF) | (r_long(p) << 32); #else if (r_long(p) != 0) { - object *f = sysget("stderr"); - err_clear(); + PyObject *f = PySys_GetObject("stderr"); + PyErr_Clear(); if (f != NULL) - writestring( + PyFile_WriteString( "Warning: un-marshal 64-bit int in 32-bit mode\n", f); } @@ -362,60 +364,61 @@ r_long64(p) return x; } -static object * +static PyObject * r_object(p) RFILE *p; { - object *v, *v2; + PyObject *v, *v2; long i, n; int type = r_byte(p); switch (type) { case EOF: - err_setstr(EOFError, "EOF read where object expected"); + PyErr_SetString(PyExc_EOFError, + "EOF read where object expected"); return NULL; case TYPE_NULL: return NULL; case TYPE_NONE: - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; case TYPE_ELLIPSIS: - INCREF(Py_Ellipsis); + Py_INCREF(Py_Ellipsis); return Py_Ellipsis; case TYPE_INT: - return newintobject(r_long(p)); + return PyInt_FromLong(r_long(p)); case TYPE_INT64: - return newintobject(r_long64(p)); + return PyInt_FromLong(r_long64(p)); case TYPE_LONG: { int size; - longobject *ob; + PyLongObject *ob; n = r_long(p); size = n<0 ? -n : n; - ob = alloclongobject(size); + ob = _PyLong_New(size); if (ob == NULL) return NULL; ob->ob_size = n; for (i = 0; i < size; i++) ob->ob_digit[i] = r_short(p); - return (object *)ob; + return (PyObject *)ob; } case TYPE_FLOAT: { - extern double atof PROTO((const char *)); + extern double atof Py_PROTO((const char *)); char buf[256]; double dx; n = r_byte(p); if (r_string(buf, (int)n, p) != n) { - err_setstr(EOFError, + PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); return NULL; } @@ -423,18 +426,18 @@ r_object(p) PyFPE_START_PROTECT("atof", return 0) dx = atof(buf); PyFPE_END_PROTECT(dx) - return newfloatobject(dx); + return PyFloat_FromDouble(dx); } #ifndef WITHOUT_COMPLEX case TYPE_COMPLEX: { - extern double atof PROTO((const char *)); + extern double atof Py_PROTO((const char *)); char buf[256]; Py_complex c; n = r_byte(p); if (r_string(buf, (int)n, p) != n) { - err_setstr(EOFError, + PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); return NULL; } @@ -444,7 +447,7 @@ r_object(p) PyFPE_END_PROTECT(c) n = r_byte(p); if (r_string(buf, (int)n, p) != n) { - err_setstr(EOFError, + PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); return NULL; } @@ -452,18 +455,18 @@ r_object(p) PyFPE_START_PROTECT("atof", return 0) c.imag = atof(buf); PyFPE_END_PROTECT(c) - return newcomplexobject(c); + return PyComplex_FromCComplex(c); } #endif case TYPE_STRING: n = r_long(p); - v = newsizedstringobject((char *)NULL, n); + v = PyString_FromStringAndSize((char *)NULL, n); if (v != NULL) { - if (r_string(getstringvalue(v), (int)n, p) != n) { - DECREF(v); + if (r_string(PyString_AsString(v), (int)n, p) != n) { + Py_DECREF(v); v = NULL; - err_setstr(EOFError, + PyErr_SetString(PyExc_EOFError, "EOF read where object expected"); } } @@ -471,50 +474,50 @@ r_object(p) case TYPE_TUPLE: n = r_long(p); - v = newtupleobject((int)n); + v = PyTuple_New((int)n); if (v == NULL) return v; for (i = 0; i < n; i++) { v2 = r_object(p); if ( v2 == NULL ) { - DECREF(v); + Py_DECREF(v); v = NULL; break; } - SETTUPLEITEM(v, (int)i, v2); + PyTuple_SET_ITEM(v, (int)i, v2); } return v; case TYPE_LIST: n = r_long(p); - v = newlistobject((int)n); + v = PyList_New((int)n); if (v == NULL) return v; for (i = 0; i < n; i++) { v2 = r_object(p); if ( v2 == NULL ) { - DECREF(v); + Py_DECREF(v); v = NULL; break; } - setlistitem(v, (int)i, v2); + PyList_SetItem(v, (int)i, v2); } return v; case TYPE_DICT: - v = newdictobject(); + v = PyDict_New(); if (v == NULL) return NULL; for (;;) { - object *key, *val; + PyObject *key, *val; key = r_object(p); if (key == NULL) break; /* XXX Assume TYPE_NULL, not an error */ val = r_object(p); if (val != NULL) - dict2insert(v, key, val); - DECREF(key); - XDECREF(val); + PyDict_SetItem(v, key, val); + Py_DECREF(key); + Py_XDECREF(val); } return v; @@ -524,14 +527,14 @@ r_object(p) int nlocals = r_short(p); int stacksize = r_short(p); int flags = r_short(p); - object *code = NULL; - object *consts = NULL; - object *names = NULL; - object *varnames = NULL; - object *filename = NULL; - object *name = NULL; + PyObject *code = NULL; + PyObject *consts = NULL; + PyObject *names = NULL; + PyObject *varnames = NULL; + PyObject *filename = NULL; + PyObject *name = NULL; int firstlineno = 0; - object *lnotab = NULL; + PyObject *lnotab = NULL; code = r_object(p); if (code) consts = r_object(p); @@ -544,20 +547,20 @@ r_object(p) lnotab = r_object(p); } - if (!err_occurred()) { - v = (object *) newcodeobject( + if (!PyErr_Occurred()) { + v = (PyObject *) PyCode_New( argcount, nlocals, stacksize, flags, code, consts, names, varnames, filename, name, firstlineno, lnotab); } else v = NULL; - XDECREF(code); - XDECREF(consts); - XDECREF(names); - XDECREF(varnames); - XDECREF(filename); - XDECREF(name); + Py_XDECREF(code); + Py_XDECREF(consts); + Py_XDECREF(names); + Py_XDECREF(varnames); + Py_XDECREF(filename); + Py_XDECREF(name); } return v; @@ -565,14 +568,14 @@ r_object(p) default: /* Bogus data got written, which isn't ideal. This will let you keep working and recover. */ - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } } long -rd_long(fp) +PyMarshal_ReadLongFromFile(fp) FILE *fp; { RFILE rf; @@ -580,13 +583,12 @@ rd_long(fp) return r_long(&rf); } -object * -rd_object(fp) +PyObject * +PyMarshal_ReadObjectFromFile(fp) FILE *fp; { RFILE rf; - if (err_occurred()) { - fatal("XXX rd_object called with exception set"); /* tmp */ + if (PyErr_Occurred()) { fprintf(stderr, "XXX rd_object called with exception set\n"); return NULL; } @@ -594,13 +596,13 @@ rd_object(fp) return r_object(&rf); } -object * -rds_object(str, len) +PyObject * +PyMarshal_ReadObjectFromString(str, len) char *str; int len; { RFILE rf; - if (err_occurred()) { + if (PyErr_Occurred()) { fprintf(stderr, "XXX rds_object called with exception set\n"); return NULL; } @@ -611,25 +613,26 @@ rds_object(str, len) return r_object(&rf); } -object * +PyObject * PyMarshal_WriteObjectToString(x) /* wrs_object() */ - object *x; + PyObject *x; { WFILE wf; wf.fp = NULL; - wf.str = newsizedstringobject((char *)NULL, 50); + wf.str = PyString_FromStringAndSize((char *)NULL, 50); if (wf.str == NULL) return NULL; - wf.ptr = GETSTRINGVALUE((stringobject *)wf.str); - wf.end = wf.ptr + getstringsize(wf.str); + wf.ptr = PyString_AS_STRING((PyStringObject *)wf.str); + wf.end = wf.ptr + PyString_Size(wf.str); wf.error = 0; w_object(x, &wf); if (wf.str != NULL) - resizestring(&wf.str, - (int) (wf.ptr - GETSTRINGVALUE((stringobject *)wf.str))); + _PyString_Resize(&wf.str, + (int) (wf.ptr - + PyString_AS_STRING((PyStringObject *)wf.str))); if (wf.error) { - XDECREF(wf.str); - err_setstr(ValueError, "unmarshallable object"); + Py_XDECREF(wf.str); + PyErr_SetString(PyExc_ValueError, "unmarshallable object"); return NULL; } return wf.str; @@ -637,95 +640,97 @@ PyMarshal_WriteObjectToString(x) /* wrs_object() */ /* And an interface for Python programs... */ -static object * +static PyObject * marshal_dump(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { WFILE wf; - object *x; - object *f; - if (!getargs(args, "(OO)", &x, &f)) + PyObject *x; + PyObject *f; + if (!PyArg_Parse(args, "(OO)", &x, &f)) return NULL; - if (!is_fileobject(f)) { - err_setstr(TypeError, "marshal.dump() 2nd arg must be file"); + if (!PyFile_Check(f)) { + PyErr_SetString(PyExc_TypeError, + "marshal.dump() 2nd arg must be file"); return NULL; } - wf.fp = getfilefile(f); + wf.fp = PyFile_AsFile(f); wf.str = NULL; wf.ptr = wf.end = NULL; wf.error = 0; w_object(x, &wf); if (wf.error) { - err_setstr(ValueError, "unmarshallable object"); + PyErr_SetString(PyExc_ValueError, "unmarshallable object"); return NULL; } - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } -static object * +static PyObject * marshal_load(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { RFILE rf; - object *f; - object *v; - if (!getargs(args, "O", &f)) + PyObject *f; + PyObject *v; + if (!PyArg_Parse(args, "O", &f)) return NULL; - if (!is_fileobject(f)) { - err_setstr(TypeError, "marshal.load() arg must be file"); + if (!PyFile_Check(f)) { + PyErr_SetString(PyExc_TypeError, + "marshal.load() arg must be file"); return NULL; } - rf.fp = getfilefile(f); + rf.fp = PyFile_AsFile(f); rf.str = NULL; rf.ptr = rf.end = NULL; - err_clear(); + PyErr_Clear(); v = r_object(&rf); - if (err_occurred()) { - XDECREF(v); + if (PyErr_Occurred()) { + Py_XDECREF(v); v = NULL; } return v; } -static object * +static PyObject * marshal_dumps(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { - object *x; - if (!getargs(args, "O", &x)) + PyObject *x; + if (!PyArg_Parse(args, "O", &x)) return NULL; return PyMarshal_WriteObjectToString(x); } -static object * +static PyObject * marshal_loads(self, args) - object *self; - object *args; + PyObject *self; + PyObject *args; { RFILE rf; - object *v; + PyObject *v; char *s; int n; - if (!getargs(args, "s#", &s, &n)) + if (!PyArg_Parse(args, "s#", &s, &n)) return NULL; rf.fp = NULL; rf.str = args; rf.ptr = s; rf.end = s + n; - err_clear(); + PyErr_Clear(); v = r_object(&rf); - if (err_occurred()) { - XDECREF(v); + if (PyErr_Occurred()) { + Py_XDECREF(v); v = NULL; } return v; } -static struct methodlist marshal_methods[] = { +static PyMethodDef marshal_methods[] = { {"dump", marshal_dump}, {"load", marshal_load}, {"dumps", marshal_dumps}, @@ -734,7 +739,7 @@ static struct methodlist marshal_methods[] = { }; void -initmarshal() +PyMarshal_Init() { - (void) initmodule("marshal", marshal_methods); + (void) Py_InitModule("marshal", marshal_methods); } diff --git a/Python/modsupport.c b/Python/modsupport.c index 6538323..df5ebfa 100644 --- a/Python/modsupport.c +++ b/Python/modsupport.c @@ -31,8 +31,7 @@ PERFORMANCE OF THIS SOFTWARE. /* Module support implementation */ -#include "allobjects.h" -#include "import.h" +#include "Python.h" #ifdef MPW /* MPW pushes 'extended' for float and double types with varargs */ typedef extended va_double; @@ -53,37 +52,39 @@ static char api_version_warning[] = "WARNING: Python C API version mismatch for module %s:\n\ This Python has API version %d, module %s has version %d.\n"; -object * -initmodule4(name, methods, doc, passthrough, module_api_version) +PyObject * +Py_InitModule4(name, methods, doc, passthrough, module_api_version) char *name; - struct methodlist *methods; + PyMethodDef *methods; char *doc; - object *passthrough; + PyObject *passthrough; int module_api_version; { - object *m, *d, *v; - struct methodlist *ml; + PyObject *m, *d, *v; + PyMethodDef *ml; if (module_api_version != PYTHON_API_VERSION) fprintf(stderr, api_version_warning, name, PYTHON_API_VERSION, name, module_api_version); - if ((m = add_module(name)) == NULL) { + if ((m = PyImport_AddModule(name)) == NULL) { fprintf(stderr, "initializing module: %s\n", name); - fatal("can't create a module"); + Py_FatalError("can't create a module"); } - d = getmoduledict(m); + d = PyModule_GetDict(m); for (ml = methods; ml->ml_name != NULL; ml++) { - v = newmethodobject(ml, passthrough); - if (v == NULL || dictinsert(d, ml->ml_name, v) != 0) { + v = PyCFunction_New(ml, passthrough); + if (v == NULL || + PyDict_SetItemString(d, ml->ml_name, v) != 0) + { fprintf(stderr, "initializing module: %s\n", name); - fatal("can't initialize module"); + Py_FatalError("can't initialize module"); } - DECREF(v); + Py_DECREF(v); } if (doc != NULL) { - v = newstringobject(doc); - if (v == NULL || dictinsert(d, "__doc__", v) != 0) - fatal("can't add doc string"); - DECREF(v); + v = PyString_FromString(doc); + if (v == NULL || PyDict_SetItemString(d, "__doc__", v) != 0) + Py_FatalError("can't add doc string"); + Py_DECREF(v); } return m; } @@ -91,7 +92,7 @@ initmodule4(name, methods, doc, passthrough, module_api_version) /* Helper for mkvalue() to scan the length of a format */ -static int countformat PROTO((char *format, int endchar)); +static int countformat Py_PROTO((char *format, int endchar)); static int countformat(format, endchar) char *format; int endchar; @@ -102,7 +103,8 @@ static int countformat(format, endchar) switch (*format) { case '\0': /* Premature end */ - err_setstr(SystemError, "unmatched paren in format"); + PyErr_SetString(PyExc_SystemError, + "unmatched paren in format"); return -1; case '(': case '[': @@ -136,118 +138,121 @@ static int countformat(format, endchar) /* Generic function to create a value -- the inverse of getargs() */ /* After an original idea and first implementation by Steven Miale */ -static object *do_mktuple PROTO((char**, va_list *, int, int)); -static object *do_mklist PROTO((char**, va_list *, int, int)); -static object *do_mkdict PROTO((char**, va_list *, int, int)); -static object *do_mkvalue PROTO((char**, va_list *)); +static PyObject *do_mktuple Py_PROTO((char**, va_list *, int, int)); +static PyObject *do_mklist Py_PROTO((char**, va_list *, int, int)); +static PyObject *do_mkdict Py_PROTO((char**, va_list *, int, int)); +static PyObject *do_mkvalue Py_PROTO((char**, va_list *)); -static object * +static PyObject * do_mkdict(p_format, p_va, endchar, n) char **p_format; va_list *p_va; int endchar; int n; { - object *d; + PyObject *d; int i; if (n < 0) return NULL; - if ((d = newdictobject()) == NULL) + if ((d = PyDict_New()) == NULL) return NULL; for (i = 0; i < n; i+= 2) { - object *k, *v; + PyObject *k, *v; k = do_mkvalue(p_format, p_va); if (k == NULL) { - DECREF(d); + Py_DECREF(d); return NULL; } v = do_mkvalue(p_format, p_va); if (v == NULL) { - DECREF(k); - DECREF(d); + Py_DECREF(k); + Py_DECREF(d); return NULL; } - if (dict2insert(d, k, v) < 0) { - DECREF(k); - DECREF(v); - DECREF(d); + if (PyDict_SetItem(d, k, v) < 0) { + Py_DECREF(k); + Py_DECREF(v); + Py_DECREF(d); return NULL; } } if (d != NULL && **p_format != endchar) { - DECREF(d); + Py_DECREF(d); d = NULL; - err_setstr(SystemError, "Unmatched paren in format"); + PyErr_SetString(PyExc_SystemError, + "Unmatched paren in format"); } else if (endchar) ++*p_format; return d; } -static object * +static PyObject * do_mklist(p_format, p_va, endchar, n) char **p_format; va_list *p_va; int endchar; int n; { - object *v; + PyObject *v; int i; if (n < 0) return NULL; - if ((v = newlistobject(n)) == NULL) + if ((v = PyList_New(n)) == NULL) return NULL; for (i = 0; i < n; i++) { - object *w = do_mkvalue(p_format, p_va); + PyObject *w = do_mkvalue(p_format, p_va); if (w == NULL) { - DECREF(v); + Py_DECREF(v); return NULL; } - setlistitem(v, i, w); + PyList_SetItem(v, i, w); } if (v != NULL && **p_format != endchar) { - DECREF(v); + Py_DECREF(v); v = NULL; - err_setstr(SystemError, "Unmatched paren in format"); + PyErr_SetString(PyExc_SystemError, + "Unmatched paren in format"); } else if (endchar) ++*p_format; return v; } -static object * +static PyObject * do_mktuple(p_format, p_va, endchar, n) char **p_format; va_list *p_va; int endchar; int n; { - object *v; + PyObject *v; int i; if (n < 0) return NULL; - if ((v = newtupleobject(n)) == NULL) + if ((v = PyTuple_New(n)) == NULL) return NULL; for (i = 0; i < n; i++) { - object *w = do_mkvalue(p_format, p_va); + PyObject *w = do_mkvalue(p_format, p_va); if (w == NULL) { - DECREF(v); + Py_DECREF(v); return NULL; } - settupleitem(v, i, w); + PyTuple_SetItem(v, i, w); } if (v != NULL && **p_format != endchar) { - DECREF(v); + Py_DECREF(v); v = NULL; - err_setstr(SystemError, "Unmatched paren in format"); + PyErr_SetString(PyExc_SystemError, + "Unmatched paren in format"); } else if (endchar) ++*p_format; return v; } -static object * +static PyObject * do_mkvalue(p_format, p_va) char **p_format; va_list *p_va; @@ -269,26 +274,27 @@ do_mkvalue(p_format, p_va) case 'b': case 'h': case 'i': - return newintobject((long)va_arg(*p_va, int)); + return PyInt_FromLong((long)va_arg(*p_va, int)); case 'l': - return newintobject((long)va_arg(*p_va, long)); + return PyInt_FromLong((long)va_arg(*p_va, long)); case 'f': case 'd': - return newfloatobject((double)va_arg(*p_va, va_double)); + return PyFloat_FromDouble( + (double)va_arg(*p_va, va_double)); case 'c': { char p[1]; p[0] = va_arg(*p_va, int); - return newsizedstringobject(p, 1); + return PyString_FromStringAndSize(p, 1); } case 's': case 'z': { - object *v; + PyObject *v; char *str = va_arg(*p_va, char *); int n; if (**p_format == '#') { @@ -298,13 +304,13 @@ do_mkvalue(p_format, p_va) else n = -1; if (str == NULL) { - v = None; - INCREF(v); + v = Py_None; + Py_INCREF(v); } else { if (n < 0) n = strlen(str); - v = newsizedstringobject(str, n); + v = PyString_FromStringAndSize(str, n); } return v; } @@ -312,18 +318,18 @@ do_mkvalue(p_format, p_va) case 'S': case 'O': if (**p_format == '&') { - typedef object *(*converter) PROTO((void *)); + typedef PyObject *(*converter) Py_PROTO((void *)); converter func = va_arg(*p_va, converter); void *arg = va_arg(*p_va, void *); ++*p_format; return (*func)(arg); } else { - object *v; - v = va_arg(*p_va, object *); + PyObject *v; + v = va_arg(*p_va, PyObject *); if (v != NULL) - INCREF(v); - else if (!err_occurred()) + Py_INCREF(v); + else if (!PyErr_Occurred()) /* If a NULL was passed * because a call that should * have constructed a value @@ -332,7 +338,7 @@ do_mkvalue(p_format, p_va) * no error occurred it's not * clear that the caller knew * what she was doing. */ - err_setstr(SystemError, + PyErr_SetString(PyExc_SystemError, "NULL object passed to mkvalue"); return v; } @@ -344,7 +350,7 @@ do_mkvalue(p_format, p_va) break; default: - err_setstr(SystemError, + PyErr_SetString(PyExc_SystemError, "bad format char passed to mkvalue"); return NULL; @@ -355,14 +361,14 @@ do_mkvalue(p_format, p_va) #ifdef HAVE_STDARG_PROTOTYPES /* VARARGS 2 */ -object *mkvalue(char *format, ...) +PyObject *Py_BuildValue(char *format, ...) #else /* VARARGS */ -object *mkvalue(va_alist) va_dcl +PyObject *Py_BuildValue(va_alist) va_dcl #endif { va_list va; - object* retval; + PyObject* retval; #ifdef HAVE_STDARG_PROTOTYPES va_start(va, format); #else @@ -370,13 +376,13 @@ object *mkvalue(va_alist) va_dcl va_start(va); format = va_arg(va, char *); #endif - retval = vmkvalue(format, va); + retval = Py_VaBuildValue(format, va); va_end(va); return retval; } -object * -vmkvalue(format, va) +PyObject * +Py_VaBuildValue(format, va) char *format; va_list va; { @@ -393,8 +399,8 @@ vmkvalue(format, va) if (n < 0) return NULL; if (n == 0) { - INCREF(None); - return None; + Py_INCREF(Py_None); + return Py_None; } if (n == 1) return do_mkvalue(&f, &lva); @@ -403,19 +409,19 @@ vmkvalue(format, va) #ifdef HAVE_STDARG_PROTOTYPES -object * -PyEval_CallFunction(object *obj, char *format, ...) +PyObject * +PyEval_CallFunction(PyObject *obj, char *format, ...) #else -object * +PyObject * PyEval_CallFunction(obj, format, va_alist) - object *obj; + PyObject *obj; char *format; va_dcl #endif { va_list vargs; - object *args; - object *res; + PyObject *args; + PyObject *res; #ifdef HAVE_STDARG_PROTOTYPES va_start(vargs, format); @@ -423,37 +429,37 @@ PyEval_CallFunction(obj, format, va_alist) va_start(vargs); #endif - args = vmkvalue(format, vargs); + args = Py_VaBuildValue(format, vargs); va_end(vargs); if (args == NULL) return NULL; - res = call_object(obj, args); - DECREF(args); + res = PyEval_CallObject(obj, args); + Py_DECREF(args); return res; } #ifdef HAVE_STDARG_PROTOTYPES -object * -PyEval_CallMethod(object *obj, char *methonname, char *format, ...) +PyObject * +PyEval_CallMethod(PyObject *obj, char *methonname, char *format, ...) #else -object * +PyObject * PyEval_CallMethod(obj, methonname, format, va_alist) - object *obj; + PyObject *obj; char *methonname; char *format; va_dcl #endif { va_list vargs; - object *meth; - object *args; - object *res; + PyObject *meth; + PyObject *args; + PyObject *res; - meth = getattr(obj, methonname); + meth = PyObject_GetAttrString(obj, methonname); if (meth == NULL) return NULL; @@ -463,17 +469,17 @@ PyEval_CallMethod(obj, methonname, format, va_alist) va_start(vargs); #endif - args = vmkvalue(format, vargs); + args = Py_VaBuildValue(format, vargs); va_end(vargs); if (args == NULL) { - DECREF(meth); + Py_DECREF(meth); return NULL; } - res = call_object(meth, args); - DECREF(meth); - DECREF(args); + res = PyEval_CallObject(meth, args); + Py_DECREF(meth); + Py_DECREF(args); return res; } diff --git a/Python/sigcheck.c b/Python/sigcheck.c index a0f8179..81785ef 100644 --- a/Python/sigcheck.c +++ b/Python/sigcheck.c @@ -36,15 +36,14 @@ PERFORMANCE OF THIS SOFTWARE. overridden (at link time) by a more powerful version implemented in signalmodule.c. */ -#include "allobjects.h" -#include "intrcheck.h" +#include "Python.h" /* ARGSUSED */ int -sigcheck() +PyErr_CheckSignals() { - if (!intrcheck()) + if (!PyOS_InterruptOccurred()) return 0; - err_set(KeyboardInterrupt); + PyErr_SetNone(PyExc_KeyboardInterrupt); return -1; } diff --git a/Python/structmember.c b/Python/structmember.c index f5cca97..02464c6 100644 --- a/Python/structmember.c +++ b/Python/structmember.c @@ -31,35 +31,36 @@ PERFORMANCE OF THIS SOFTWARE. /* Map C struct members to Python object attributes */ -#include "allobjects.h" +#include "Python.h" #include "structmember.h" -static object * +static PyObject * listmembers(mlist) struct memberlist *mlist; { int i, n; - object *v; + PyObject *v; for (n = 0; mlist[n].name != NULL; n++) ; - v = newlistobject(n); + v = PyList_New(n); if (v != NULL) { for (i = 0; i < n; i++) - setlistitem(v, i, newstringobject(mlist[i].name)); - if (err_occurred()) { - DECREF(v); + PyList_SetItem(v, i, + PyString_FromString(mlist[i].name)); + if (PyErr_Occurred()) { + Py_DECREF(v); v = NULL; } else { - sortlist(v); + PyList_Sort(v); } } return v; } -object * -getmember(addr, mlist, name) +PyObject * +PyMember_Get(addr, mlist, name) char *addr; struct memberlist *mlist; char *name; @@ -70,111 +71,118 @@ getmember(addr, mlist, name) return listmembers(mlist); for (l = mlist; l->name != NULL; l++) { if (strcmp(l->name, name) == 0) { - object *v; + PyObject *v; addr += l->offset; switch (l->type) { case T_BYTE: - v = newintobject((long) + v = PyInt_FromLong((long) (((*(char*)addr & 0xff) ^ 0x80) - 0x80)); break; case T_UBYTE: - v = newintobject((long) *(char*)addr & 0xff); + v = PyInt_FromLong((long) *(char*)addr & 0xff); break; case T_SHORT: - v = newintobject((long) *(short*)addr); + v = PyInt_FromLong((long) *(short*)addr); break; case T_USHORT: - v = newintobject((long) + v = PyInt_FromLong((long) *(unsigned short*)addr); break; case T_INT: - v = newintobject((long) *(int*)addr); + v = PyInt_FromLong((long) *(int*)addr); break; case T_UINT: - v = newintobject((long) *(unsigned int*)addr); + v = PyInt_FromLong((long) + *(unsigned int*)addr); break; case T_LONG: - v = newintobject(*(long*)addr); + v = PyInt_FromLong(*(long*)addr); break; case T_ULONG: - v = dnewlongobject((double) + v = PyLong_FromDouble((double) *(unsigned long*)addr); break; case T_FLOAT: - v = newfloatobject((double)*(float*)addr); + v = PyFloat_FromDouble((double)*(float*)addr); break; case T_DOUBLE: - v = newfloatobject(*(double*)addr); + v = PyFloat_FromDouble(*(double*)addr); break; case T_STRING: if (*(char**)addr == NULL) { - INCREF(None); - v = None; + Py_INCREF(Py_None); + v = Py_None; } else - v = newstringobject(*(char**)addr); + v = PyString_FromString(*(char**)addr); break; case T_STRING_INPLACE: - v = newstringobject((char*)addr); + v = PyString_FromString((char*)addr); break; #ifdef macintosh case T_PSTRING: if (*(char**)addr == NULL) { - INCREF(None); - v = None; + Py_INCREF(Py_None); + v = Py_None; } else - v = newsizedstringobject((*(char**)addr)+1, - **(unsigned char**)addr); + v = PyString_FromStringAndSize( + (*(char**)addr)+1, + **(unsigned char**)addr); break; case T_PSTRING_INPLACE: - v = newsizedstringobject(((char*)addr)+1, - *(unsigned char*)addr); + v = PyString_FromStringAndSize( + ((char*)addr)+1, + *(unsigned char*)addr); break; #endif /* macintosh */ case T_CHAR: - v = newsizedstringobject((char*)addr, 1); + v = PyString_FromStringAndSize((char*)addr, 1); break; case T_OBJECT: - v = *(object **)addr; + v = *(PyObject **)addr; if (v == NULL) - v = None; - INCREF(v); + v = Py_None; + Py_INCREF(v); break; default: - err_setstr(SystemError, "bad memberlist type"); + PyErr_SetString(PyExc_SystemError, + "bad memberlist type"); v = NULL; } return v; } } - err_setstr(AttributeError, name); + PyErr_SetString(PyExc_AttributeError, name); return NULL; } int -setmember(addr, mlist, name, v) +PyMember_Set(addr, mlist, name, v) char *addr; struct memberlist *mlist; char *name; - object *v; + PyObject *v; { struct memberlist *l; for (l = mlist; l->name != NULL; l++) { if (strcmp(l->name, name) == 0) { #ifdef macintosh - if (l->readonly || l->type == T_STRING || l->type == T_PSTRING) { + if (l->readonly || l->type == T_STRING || + l->type == T_PSTRING) + { #else if (l->readonly || l->type == T_STRING ) { #endif /* macintosh */ - err_setstr(TypeError, "readonly attribute"); + PyErr_SetString(PyExc_TypeError, + "readonly attribute"); return -1; } if (v == NULL && l->type != T_OBJECT) { - err_setstr(TypeError, + PyErr_SetString(PyExc_TypeError, "can't delete numeric/char attribute"); return -1; } @@ -182,90 +190,92 @@ setmember(addr, mlist, name, v) switch (l->type) { case T_BYTE: case T_UBYTE: - if (!is_intobject(v)) { - err_badarg(); + if (!PyInt_Check(v)) { + PyErr_BadArgument(); return -1; } - *(char*)addr = (char) getintvalue(v); + *(char*)addr = (char) PyInt_AsLong(v); break; case T_SHORT: case T_USHORT: - if (!is_intobject(v)) { - err_badarg(); + if (!PyInt_Check(v)) { + PyErr_BadArgument(); return -1; } - *(short*)addr = (short) getintvalue(v); + *(short*)addr = (short) PyInt_AsLong(v); break; case T_UINT: case T_INT: - if (!is_intobject(v)) { - err_badarg(); + if (!PyInt_Check(v)) { + PyErr_BadArgument(); return -1; } - *(int*)addr = (int) getintvalue(v); + *(int*)addr = (int) PyInt_AsLong(v); break; case T_LONG: - if (!is_intobject(v)) { - err_badarg(); + if (!PyInt_Check(v)) { + PyErr_BadArgument(); return -1; } - *(long*)addr = getintvalue(v); + *(long*)addr = PyInt_AsLong(v); break; case T_ULONG: - if (is_intobject(v)) - *(long*)addr = getintvalue(v); - else if (is_longobject(v)) - *(long*)addr = getlongvalue(v); + if (PyInt_Check(v)) + *(long*)addr = PyInt_AsLong(v); + else if (PyLong_Check(v)) + *(long*)addr = PyLong_AsLong(v); else { - err_badarg(); + PyErr_BadArgument(); return -1; } break; case T_FLOAT: - if (is_intobject(v)) - *(float*)addr = (float) getintvalue(v); - else if (is_floatobject(v)) + if (PyInt_Check(v)) *(float*)addr = - (float) getfloatvalue(v); + (float) PyInt_AsLong(v); + else if (PyFloat_Check(v)) + *(float*)addr = + (float) PyFloat_AsDouble(v); else { - err_badarg(); + PyErr_BadArgument(); return -1; } break; case T_DOUBLE: - if (is_intobject(v)) + if (PyInt_Check(v)) *(double*)addr = - (double) getintvalue(v); - else if (is_floatobject(v)) - *(double*)addr = getfloatvalue(v); + (double) PyInt_AsLong(v); + else if (PyFloat_Check(v)) + *(double*)addr = PyFloat_AsDouble(v); else { - err_badarg(); + PyErr_BadArgument(); return -1; } break; case T_OBJECT: - XDECREF(*(object **)addr); - XINCREF(v); - *(object **)addr = v; + Py_XDECREF(*(PyObject **)addr); + Py_XINCREF(v); + *(PyObject **)addr = v; break; case T_CHAR: - if (is_stringobject(v) && - getstringsize(v) == 1) { + if (PyString_Check(v) && + PyString_Size(v) == 1) { *(char*)addr = - getstringvalue(v)[0]; + PyString_AsString(v)[0]; } else { - err_badarg(); + PyErr_BadArgument(); return -1; } default: - err_setstr(SystemError, "bad memberlist type"); + PyErr_SetString(PyExc_SystemError, + "bad memberlist type"); return -1; } return 0; } } - err_setstr(AttributeError, name); + PyErr_SetString(PyExc_AttributeError, name); return -1; } -- cgit v0.12