summaryrefslogtreecommitdiffstats
path: root/Objects/funcobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1990-10-14 12:07:46 (GMT)
committerGuido van Rossum <guido@python.org>1990-10-14 12:07:46 (GMT)
commit85a5fbbdfea617f6cc8fae82c9e8c2b5c424436d (patch)
treea1bf57db1c75e2a7029c8f2fad5f8dba4b9ba25c /Objects/funcobject.c
parentc636014c430620325f8d213e9ba10d925991b8d7 (diff)
downloadcpython-85a5fbbdfea617f6cc8fae82c9e8c2b5c424436d.zip
cpython-85a5fbbdfea617f6cc8fae82c9e8c2b5c424436d.tar.gz
cpython-85a5fbbdfea617f6cc8fae82c9e8c2b5c424436d.tar.bz2
Initial revision
Diffstat (limited to 'Objects/funcobject.c')
-rw-r--r--Objects/funcobject.c101
1 files changed, 101 insertions, 0 deletions
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
new file mode 100644
index 0000000..1c6f6a6
--- /dev/null
+++ b/Objects/funcobject.c
@@ -0,0 +1,101 @@
+/* Function object implementation */
+
+#include <stdio.h>
+
+#include "PROTO.h"
+#include "object.h"
+#include "node.h"
+#include "stringobject.h"
+#include "funcobject.h"
+#include "objimpl.h"
+#include "token.h"
+
+typedef struct {
+ OB_HEAD
+ node *func_node;
+ object *func_globals;
+} funcobject;
+
+object *
+newfuncobject(n, globals)
+ node *n;
+ object *globals;
+{
+ funcobject *op = NEWOBJ(funcobject, &Functype);
+ if (op != NULL) {
+ op->func_node = n;
+ if (globals != NULL)
+ INCREF(globals);
+ op->func_globals = globals;
+ }
+ return (object *)op;
+}
+
+node *
+getfuncnode(op)
+ object *op;
+{
+ if (!is_funcobject(op)) {
+ errno = EBADF;
+ return NULL;
+ }
+ return ((funcobject *) op) -> func_node;
+}
+
+object *
+getfuncglobals(op)
+ object *op;
+{
+ if (!is_funcobject(op)) {
+ errno = EBADF;
+ return NULL;
+ }
+ return ((funcobject *) op) -> func_globals;
+}
+
+/* Methods */
+
+static void
+funcdealloc(op)
+ funcobject *op;
+{
+ /* XXX free node? */
+ DECREF(op->func_globals);
+ free((char *)op);
+}
+
+static void
+funcprint(op, fp, flags)
+ funcobject *op;
+ FILE *fp;
+ int flags;
+{
+ node *n = op->func_node;
+ n = CHILD(n, 1);
+ fprintf(fp, "<user function %s>", STR(n));
+}
+
+static object *
+funcrepr(op)
+ funcobject *op;
+{
+ char buf[100];
+ node *n = op->func_node;
+ n = CHILD(n, 1);
+ sprintf(buf, "<user function %.80s>", STR(n));
+ return newstringobject(buf);
+}
+
+typeobject Functype = {
+ OB_HEAD_INIT(&Typetype)
+ 0,
+ "function",
+ sizeof(funcobject),
+ 0,
+ funcdealloc, /*tp_dealloc*/
+ funcprint, /*tp_print*/
+ 0, /*tp_getattr*/
+ 0, /*tp_setattr*/
+ 0, /*tp_compare*/
+ funcrepr, /*tp_repr*/
+};