Commit 5574f2d0 authored by Guido van Rossum's avatar Guido van Rossum

Quickly renamed the remaining files -- this directory is done.

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