summaryrefslogtreecommitdiffstats
path: root/Modules/newmodule.c
blob: 383ee4a65851df874c41f49990bf1644faf8a411 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/* newmodule.c */
#include "allobjects.h"
#include "compile.h"
#include "modsupport.h"


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;
    object* argcount;
    object* argdefs;
  
    if (!getargs(args, "(OOOOO)", &code, &globals, &name, &argcount, &argdefs)) {
	return NULL;
    } else if (!is_codeobject(code) || !is_mappingobject(globals) || !is_stringobject(name) || !is_intobject(argcount) || !is_tupleobject(argdefs)) {
	err_setstr(TypeError, "expected a code object, a dict for globals, a string name, an integer default argument count and a tuple of default argument definitions.");
	return NULL;
    }

    newfunc = newfuncobject(code, globals);

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

    ((funcobject *)newfunc)->func_argcount = getintvalue(argcount);
    ((funcobject *)newfunc)->func_argdefs  = argdefs;
    XINCREF( argdefs );

    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);
}