[pypy-svn] r64754 - in pypy/branch/pyjitpl5/pypy/translator/c: . src

arigo at codespeak.net arigo at codespeak.net
Tue Apr 28 10:36:48 CEST 2009


Author: arigo
Date: Tue Apr 28 10:36:46 2009
New Revision: 64754

Modified:
   pypy/branch/pyjitpl5/pypy/translator/c/funcgen.py
   pypy/branch/pyjitpl5/pypy/translator/c/src/rtyper.h
   pypy/branch/pyjitpl5/pypy/translator/c/src/support.h
Log:
Merge r62931 and r62976 from trunk, to avoid strange characters at the end of debug_print()s.

Modified: pypy/branch/pyjitpl5/pypy/translator/c/funcgen.py
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/funcgen.py	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/funcgen.py	Tue Apr 28 10:36:46 2009
@@ -718,7 +718,7 @@
                     format.append(''.join(arg.value.chars).replace('%', '%%'))
                 else:
                     format.append('%s')
-                    argv.append('RPyString_AsString(%s)' % self.expr(arg))
+                    argv.append('RPyString_AsCharP(%s)' % self.expr(arg))
                 continue
             elif T == Signed:
                 format.append('%d')
@@ -738,7 +738,7 @@
             else:
                 raise Exception("don't know how to debug_print %r" % (T,))
             argv.append(self.expr(arg))
-        return "fprintf(stderr, %s%s);" % (
+        return "fprintf(stderr, %s%s); RPyString_FreeCache();" % (
             c_string_constant(' '.join(format) + '\n\000'),
             ''.join([', ' + s for s in argv]))
 
@@ -755,7 +755,7 @@
         if isinstance(msg, Constant):
             msg = c_string_constant(''.join(msg.value.chars))
         else:
-            msg = 'RPyString_AsString(%s)' % self.expr(msg)
+            msg = 'RPyString_AsCharP(%s)' % self.expr(msg)
 
         return 'fprintf(stderr, "%%s\\n", %s); abort();' % msg
 

Modified: pypy/branch/pyjitpl5/pypy/translator/c/src/rtyper.h
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/src/rtyper.h	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/src/rtyper.h	Tue Apr 28 10:36:46 2009
@@ -4,14 +4,18 @@
 #include <string.h>
 
 
+/* Note that RPython strings are not 0-terminated!  For debugging,
+   use PyString_FromRPyString or RPyString_AsCharP */
 #define RPyString_Size(rps)		((rps)->rs_chars.length)
-#define RPyString_AsString(rps)		((rps)->rs_chars.items)
+#define _RPyString_AsString(rps)        ((rps)->rs_chars.items)
 
 #define RPyUnicode_Size(rpu)		((rpu)->ru_chars.length)
-#define RPyUnicode_AsUnicode(rpu)	((rpu)->ru_chars.items)
+#define _RPyUnicode_AsUnicode(rpu)	((rpu)->ru_chars.items)
 
 /* prototypes */
 
+char *RPyString_AsCharP(RPyString *rps);
+void RPyString_FreeCache(void);
 RPyString *RPyString_FromString(char *buf);
 
 
@@ -19,11 +23,39 @@
 
 #ifndef PYPY_NOT_MAIN_FILE
 
+struct _RPyString_dump_t {
+	struct _RPyString_dump_t *next;
+	char data[1];
+} *_RPyString_dump = NULL;
+
+char *RPyString_AsCharP(RPyString *rps)
+{
+	long len = RPyString_Size(rps);
+	struct _RPyString_dump_t *dump = \
+			malloc(sizeof(struct _RPyString_dump_t) + len);
+	if (!dump)
+		return "(out of memory!)";
+	dump->next = _RPyString_dump;
+	_RPyString_dump = dump;
+	memcpy(dump->data, rps->rs_chars.items, len);
+	dump->data[len] = 0;
+	return dump->data;
+}
+
+void RPyString_FreeCache(void)
+{
+	while (_RPyString_dump) {
+		struct _RPyString_dump_t *dump = _RPyString_dump;
+		_RPyString_dump = dump->next;
+		free(dump);
+	}
+}
+
 RPyString *RPyString_FromString(char *buf)
 {
 	int length = strlen(buf);
 	RPyString *rps = RPyString_New(length);
-	memcpy(RPyString_AsString(rps), buf, length);
+	memcpy(rps->rs_chars.items, buf, length);
 	return rps;
 }
 

Modified: pypy/branch/pyjitpl5/pypy/translator/c/src/support.h
==============================================================================
--- pypy/branch/pyjitpl5/pypy/translator/c/src/support.h	(original)
+++ pypy/branch/pyjitpl5/pypy/translator/c/src/support.h	Tue Apr 28 10:36:46 2009
@@ -20,15 +20,16 @@
 #define FAIL_ZER(msg) FAIL_EXCEPTION(PyExc_ZeroDivisionError, msg)
 #define CFAIL()       RPyConvertExceptionFromCPython()
 
+/* the following macros are used by rpython/lltypesystem/rstr.py */
 #define PyString_FromRPyString(rpystr) \
-	PyString_FromStringAndSize(RPyString_AsString(rpystr), RPyString_Size(rpystr))
+	PyString_FromStringAndSize(_RPyString_AsString(rpystr), RPyString_Size(rpystr))
 
 #define PyUnicode_FromRPyUnicode(rpystr) \
-	PyUnicode_FromUnicode(RPyUnicode_AsUnicode(rpystr), RPyUnicode_Size(rpystr))
+	PyUnicode_FromUnicode(_RPyUnicode_AsUnicode(rpystr), RPyUnicode_Size(rpystr))
 
-#define PyString_ToRPyString(s, rpystr)                           \
-	memcpy(RPyString_AsString(rpystr), PyString_AS_STRING(s), \
-               RPyString_Size(rpystr))
+#define PyString_ToRPyString(s, rpystr)                            \
+	memcpy(_RPyString_AsString(rpystr), PyString_AS_STRING(s), \
+		RPyString_Size(rpystr))
 
 /* Extra checks can be enabled with the RPY_ASSERT or RPY_LL_ASSERT
  * macros.  They differ in the level at which the tests are made.



More information about the Pypy-commit mailing list