[pypy-svn] r7472 - pypy/trunk/src/pypy/translator

bob at codespeak.net bob at codespeak.net
Sat Nov 20 00:12:25 CET 2004


Author: bob
Date: Sat Nov 20 00:12:25 2004
New Revision: 7472

Modified:
   pypy/trunk/src/pypy/translator/genc.h
   pypy/trunk/src/pypy/translator/genc.py
Log:
more crazy debugging hacks



Modified: pypy/trunk/src/pypy/translator/genc.h
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.h	(original)
+++ pypy/trunk/src/pypy/translator/genc.h	Sat Nov 20 00:12:25 2004
@@ -7,9 +7,13 @@
 #include "frameobject.h"
 #include "structmember.h"
 
+static PyObject *this_module_globals;
+
 /* Turn this off if you don't want the call trace frames to be built */
 #define USE_CALL_TRACE
+#if 0
 #define OBNOXIOUS_PRINT_STATEMENTS
+#endif
 #define INSIDE_FUNCTION "<unknown>"
 
 #define op_richcmp(x,y,r,err,dir)   \
@@ -179,10 +183,16 @@
 	0,					/* tp_descr_set */
 };
 
-#define SETUP_MODULE						\
+#define MODULE_INITFUNC(modname) \
+	static PyMethodDef no_methods[] = { NULL, NULL }; \
+	void init##modname(void)
+
+#define SETUP_MODULE(modname)					\
+	PyObject *m = Py_InitModule(#modname, no_methods); \
+	this_module_globals = PyModule_GetDict(m); \
 	PyGenCFunction_Type.tp_base = &PyCFunction_Type;	\
 	PyType_Ready(&PyGenCFunction_Type);			\
-	PyExc_OperationError = PyErr_NewException("bah.OperationError", NULL, NULL);
+	PyExc_OperationError = PyErr_NewException(#modname ".OperationError", NULL, NULL);
 
 
 /*** operations with a variable number of arguments ***/
@@ -194,7 +204,7 @@
 #if defined(USE_CALL_TRACE)
 
 static int callstack_depth = -1;
-static PyCodeObject* getcode(char* func_name, int lineno);
+static PyCodeObject* getcode(char *func_name, char *func_filename, int lineno);
 static int trace_frame(PyThreadState *tstate, PyFrameObject *f, int code, PyObject *val);
 static int trace_frame_exc(PyThreadState *tstate, PyFrameObject *f);
 
@@ -206,6 +216,11 @@
 	PyObject *locals_lineno;
 	PyObject *locals_filename;
 
+	if (function == NULL || args == NULL || tstate == NULL) {
+		printf("BAD ARGUMENTS!\n");
+		printf("function = 0x%08X args = %08X tstate = %08X\n", function, args, tstate);
+		return NULL;
+	}
 	locals = PyDict_New();
 	locals_signature = PyString_FromString(c_signature);
 	locals_lineno = PyInt_FromLong(c_lineno);
@@ -227,13 +242,16 @@
 	Py_DECREF(locals_signature);
 	Py_DECREF(locals_lineno);
 	Py_DECREF(locals_filename);
-	c = getcode(c_signature, c_lineno);
+	callstack_depth++;
+	c = getcode(c_signature, filename, c_lineno);
 	if (c == NULL) {
 		Py_DECREF(locals);
+		callstack_depth--;
 		return NULL;
 	}
-	f = PyFrame_New(tstate, c, PyEval_GetGlobals(), locals);
+	f = PyFrame_New(tstate, c, this_module_globals, locals);
 	if (f == NULL) {
+		callstack_depth--;
 		return NULL;
 	}
 	Py_DECREF(c);
@@ -241,6 +259,7 @@
 	tstate->frame = f;
 	if (trace_frame(tstate, f, PyTrace_CALL, Py_None) < 0) {
 		Py_DECREF(args);
+		callstack_depth--;
 		return NULL;
 	}
 
@@ -248,6 +267,12 @@
 }
 
 static PyObject *traced_function_tail(PyObject *rval, PyFrameObject *f, PyThreadState *tstate) {
+	/*
+		STEALS a reference to f
+	*/
+	if (f == NULL) {
+		goto bad_args;
+	}
 	if (rval == NULL) {
 		if (tstate->curexc_traceback == NULL) {
 			PyTraceBack_Here(f);
@@ -263,6 +288,9 @@
 	}
 end:
 	tstate->frame = f->f_back;
+	Py_DECREF(f);
+bad_args:
+	callstack_depth--;
 	return rval;
 }
 
@@ -288,10 +316,8 @@
 	Py_DECREF(allargs);
 
 	tstate = PyThreadState_GET();
-	callstack_depth++;
 	f = traced_function_head(function, args, c_signature, filename, c_lineno, tstate);
 	if (f == NULL) {
-		callstack_depth--;
 		Py_DECREF(function);
 		Py_DECREF(args);
 		return NULL;
@@ -300,19 +326,31 @@
 	rval = PyObject_Call(function, args, NULL);
 	Py_DECREF(function);
 	Py_DECREF(args);
-	rval = traced_function_tail(rval, f, tstate);
-	callstack_depth--;
-	return rval;
+	return traced_function_tail(rval, f, tstate);
 }
 
 #define OP_SIMPLE_CALL(args, r, err) if ((r = traced_function_call(PyTuple_CrazyPack args, INSIDE_FUNCTION " OP_SIMPLE_CALL" #args, __FILE__, __LINE__)) == NULL) \
 					goto err;
 
+#define FUNCTION_HEAD(signature, self, args) \
+	PyThreadState *__tstate = PyThreadState_GET(); \
+	PyFrameObject *__f = traced_function_head(self, args, signature, __FILE__, __LINE__, __tstate); \
+	if (__f == NULL) { \
+		printf("frame is null, wtf?!\n"); \
+		return NULL; \
+	}
+
+#define FUNCTION_RETURN(rval) return traced_function_tail(rval, __f, __tstate);
+
+
 #else
 
 #define OP_SIMPLE_CALL(args,r,err) if (!(r=PyObject_CallFunctionObjArgs args)) \
 					goto err;
 
+#define FUNCTION_HEAD(signature, self, args)
+#define FUNCTION_RETURN(rval) return rval;
+
 #endif
 
 static PyObject* PyTuple_CrazyPack(PyObject *begin, ...)
@@ -562,7 +600,7 @@
 }
 
 static PyCodeObject*
-getcode(char* func_name, int lineno)
+getcode(char *func_name, char *func_filename, int lineno)
 {
 	PyObject *code = NULL;
 	PyObject *name = NULL;
@@ -590,7 +628,7 @@
 	nulltuple = PyTuple_New(0);
 	if (nulltuple == NULL)
 		goto failed;
-	filename = PyString_FromString(__FILE__);
+	filename = PyString_FromString(func_filename);
 	tb_code = PyCode_New(0,       /* argcount */
 						 0,       /* nlocals */
 						 0,       /* stacksize */

Modified: pypy/trunk/src/pypy/translator/genc.py
==============================================================================
--- pypy/trunk/src/pypy/translator/genc.py	(original)
+++ pypy/trunk/src/pypy/translator/genc.py	Sat Nov 20 00:12:25 2004
@@ -16,6 +16,9 @@
 
 # ____________________________________________________________
 
+def c_string(s):
+    return '"%s"' % (s.replace('\\', '\\\\').replace('"', '\"'),)
+
 def uniquemodulename(name, SEEN={}):
     # never reuse the same module name within a Python session!
     i = 0
@@ -418,8 +421,11 @@
         print >> f, 'static PyObject* %s(PyObject* self, PyObject* args)' % (
             f_name,)
         print >> f, '{'
-        print >> f, '#undef INSIDE_FUNCTION'
-        print >> f, '#define INSIDE_FUNCTION "%s"' % (f_name,)
+        print >> f, '\t#undef INSIDE_FUNCTION'
+        print >> f, '\t#define INSIDE_FUNCTION "%s"' % (f_name,)
+        print >> f, '\tFUNCTION_HEAD(%s, %s, args)' % (
+            c_string('%s(%s)' % (name, ', '.join(name_of_defaults))),
+            name)
 
         # collect and print all the local variables
         graph = self.translator.getflowgraph(func)
@@ -439,22 +445,22 @@
             print >> f, '\t%s = PyTuple_GetSlice(args, %d, INT_MAX);' % (
                 vararg, len(positional_args))
             print >> f, '\tif (%s == NULL)' % vararg
-            print >> f, '\t\treturn NULL;'
+            print >> f, '\t\tFUNCTION_RETURN(NULL)'
             print >> f, '\targs = PyTuple_GetSlice(args, 0, %d);' % (
                 len(positional_args),)
             print >> f, '\tif (args == NULL) {'
             print >> f, '\t\tPy_DECREF(%s);' % vararg
-            print >> f, '\t\treturn NULL;'
+            print >> f, '\t\tFUNCTION_RETURN(NULL)'
             print >> f, '\t}'
             tail = """{
 \t\tPy_DECREF(args);
 \t\tPy_DECREF(%s);
-\t\treturn NULL;
+\t\tFUNCTION_RETURN(NULL);
 \t}
 \tPy_DECREF(args);""" % vararg
         else:
             positional_args = graph.getargs()
-            tail = '\n\t\treturn NULL;'
+            tail = '\n\t\tFUNCTION_RETURN(NULL)'
         min_number_of_args = len(positional_args) - len(name_of_defaults)
         for i in range(len(name_of_defaults)):
             print >> f, '\t%s = %s;' % (
@@ -558,10 +564,10 @@
                     # exceptional return block
                     yield 'PyErr_SetObject(PyExc_%s, %s);' % (
                         block.exc_type.__name__, retval)
-                    yield 'return NULL;'
+                    yield 'FUNCTION_RETURN(NULL)'
                 else:
                     # regular return block
-                    yield 'return %s;' % retval
+                    yield 'FUNCTION_RETURN(%s)' % retval
                 continue
             elif block.exitswitch is None:
                 # single-exit block
@@ -613,7 +619,7 @@
                 yield 'err%d_%d:' % (blocknum[block], len(to_release))
                 err_reachable = True
             if err_reachable:
-                yield 'return NULL;'
+                yield 'FUNCTION_RETURN(NULL)'
 
 # ____________________________________________________________
 
@@ -623,11 +629,9 @@
 
     C_INIT_HEADER = C_SEP + '''
 
-static PyMethodDef no_methods[] = { NULL, NULL };
-void init%(modname)s(void)
+MODULE_INITFUNC(%(modname)s)
 {
-\tPyObject* m = Py_InitModule("%(modname)s", no_methods);
-\tSETUP_MODULE
+\tSETUP_MODULE(%(modname)s)
 '''
 
     C_INIT_FOOTER = '''



More information about the Pypy-commit mailing list