bug-fix for NEW_OBJECT module (is now new module)

tnb2d@henson.cs.virginia.edu
Mon, 11 Apr 94 13:26:31 EDT

I went ahead and made it mandatory that when creating a new
function object you give it a name explicitly. I had tried to make it
optional, but it was memory faulting on me whn I did'nt pass in a
name, so I just got frsutrated and decided a little fascism never hurt
anyone ;-) So here's the fixed up c code, and I also changed the name
to 'new' instead of NEW_OBJECT because I think all that all-caps
typing was hurting Steve's little pinky :)

---------%< snip %<------------------%< snip %<---------

#include "allobjects.h"
#include "compile.h"
#include "modsupport.h"

/* extern "C" { */
/* void initnew(void); */
/* } */

object*
new_instancemethod(unused, args)
object* unused;
object* args;
{
object* func;
object* self;
object* classObj;

if (!getargs(args, "(OOO)", &func, &self, &classObj)) {
return NULL;
} else if (!is_funcobject(func) || !is_instanceobject(self) || !is_classobject(classObj)) {
err_setstr(TypeError, "expected a function, instance and classobject as args");
return NULL;
}
return newinstancemethodobject(func, self, classObj);
}

object*
new_function(unused, args)
object* unused;
object* args;
{
object* code;
object* globals;
object* name;
object* newfunc;

if (!getargs(args, "(OOO)", &code, &globals, &name)) {
return NULL;
} else if (!is_codeobject(code) || !is_mappingobject(globals) || !is_stringobject(name)) {
err_setstr(TypeError, "expected a code object, a dict for globals and an \
(optional) string name");
return NULL;
}

newfunc = newfuncobject(code, globals);

((funcobject *)newfunc)->func_name = name;
XINCREF( name );
XDECREF( ((codeobject*)(((funcobject *)(newfunc))->func_code))->co_name );

return newfunc;
}

object*
new_code(unused, args)
object* unused;
object* args;
{
object* code;
object* consts;
object* names;
object* filename;
object* name;

if (!getargs(args, "(OOOOO)", &code, &consts, &names, &filename, &name)) {
return NULL;
} else if (!is_stringobject(code) || !is_listobject(consts) || \
!is_listobject(names) || !is_stringobject(filename) || \
!is_stringobject(name)) {
err_setstr(TypeError, "expected a string of compiled code, a list of constants, \
a list of names used, a string filename, and a string name \
as args");
return NULL;
}
return (object *)newcodeobject(code, consts, names, filename, name);
}

object*
new_module(unused, args)
object* unused;
object* args;
{
object* name;

if (!getargs(args, "S", &name)) {
err_setstr(TypeError, "expected a string name as args");
return NULL;
}
return newmoduleobject(getstringvalue(name));
}

static struct methodlist new_methods[] = {
{ "instancemethod", new_instancemethod },
{ "function", new_function },
{ "code", new_code },
{ "module", new_module },
{NULL, NULL} /* sentinel */
};

void
initnew()
{
initmodule("new", new_methods);
}

-------> Tommy.

"Subvert the parental paradigm - Refuse Birth."