Author: Armin Rigo <[email protected]>
Branch: py3k-faulthandler
Changeset: r87355:95095bc757b8
Date: 2016-09-24 09:37 +0200
http://bitbucket.org/pypy/pypy/changeset/95095bc757b8/
Log: fixes fixes
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -423,7 +423,7 @@
self.threadlocals.enter_thread(self)
# Set up faulthandler even if not imported explicitly
- if self.config.objspace.usemodule.faulthandler:
+ if self.config.objspace.usemodules.faulthandler:
from pypy.module.faulthandler import handler
handler.startup(self)
@@ -452,7 +452,7 @@
w_mod.shutdown(self)
#
# Shut down faulthandler
- if self.config.objspace.usemodule.faulthandler:
+ if self.config.objspace.usemodules.faulthandler:
from pypy.module.faulthandler import handler
handler.finish(self)
diff --git a/pypy/interpreter/interactive.py b/pypy/interpreter/interactive.py
--- a/pypy/interpreter/interactive.py
+++ b/pypy/interpreter/interactive.py
@@ -169,7 +169,8 @@
def runsource(self, source, ignored_filename="<input>", symbol="single"):
# the following hacked file name is recognized specially by error.py
- hacked_filename = '<inline>\n' + source
+ hacked_filename = '<inline>\n' + source.encode(
+ 'ascii', 'backslashreplace')
compiler = self.space.getexecutioncontext().compiler
# CPython 2.6 turns console input into unicode
diff --git a/pypy/module/faulthandler/__init__.py
b/pypy/module/faulthandler/__init__.py
--- a/pypy/module/faulthandler/__init__.py
+++ b/pypy/module/faulthandler/__init__.py
@@ -10,7 +10,7 @@
'is_enabled': 'handler.is_enabled',
# 'register': 'interp_faulthandler.register',
#
-# 'dump_traceback': 'interp_faulthandler.dump_traceback',
+ 'dump_traceback': 'handler.dump_traceback',
#
# '_read_null': 'interp_faulthandler.read_null',
# '_sigsegv': 'interp_faulthandler.sigsegv',
diff --git a/pypy/module/faulthandler/cintf.py
b/pypy/module/faulthandler/cintf.py
--- a/pypy/module/faulthandler/cintf.py
+++ b/pypy/module/faulthandler/cintf.py
@@ -1,3 +1,4 @@
+import py
from rpython.rtyper.lltypesystem import lltype, rffi, rstr
from rpython.translator import cdir
from rpython.translator.tool.cbuild import ExternalCompilationInfo
@@ -32,7 +33,10 @@
'pypy_faulthandler_is_enabled', [], rffi.INT)
pypy_faulthandler_write = direct_llexternal(
- 'pypy_faulthandler_write', [lltype.Ptr(rstr.STR)])
+ 'pypy_faulthandler_write', [rffi.CCHARP], lltype.Void)
pypy_faulthandler_write_int = direct_llexternal(
- 'pypy_faulthandler_write_int', [lltype.Signed])
+ 'pypy_faulthandler_write_int', [lltype.Signed], lltype.Void)
+
+pypy_faulthandler_dump_traceback = direct_llexternal(
+ 'pypy_faulthandler_dump_traceback', [rffi.INT, rffi.INT], lltype.Void)
diff --git a/pypy/module/faulthandler/dumper.py
b/pypy/module/faulthandler/dumper.py
--- a/pypy/module/faulthandler/dumper.py
+++ b/pypy/module/faulthandler/dumper.py
@@ -1,4 +1,4 @@
-from rpython.rtyper.annlowlevel import llstr
+from rpython.rtyper.lltypesystem import lltype, rffi
from rpython.rlib import rgc
from rpython.rlib.rvmprof import enum_all_code_objs
from rpython.rlib.rvmprof import cintf as rvmprof_cintf
@@ -13,10 +13,22 @@
MAX_FRAME_DEPTH = 100
+MAX_STRING_LENGTH = 500
+global_buf = lltype.malloc(rffi.CCHARP.TO, MAX_STRING_LENGTH, flavor='raw',
+ immortal=True, zero=True)
def _dump(s):
- pypy_faulthandler_write(llstr(s))
+ assert isinstance(s, str)
+ l = len(s)
+ if l >= MAX_STRING_LENGTH:
+ l = MAX_STRING_LENGTH - 1
+ i = 0
+ while i < l:
+ global_buf[i] = s[i]
+ i += 1
+ global_buf[l] = '\x00'
+ pypy_faulthandler_write(global_buf)
def _dump_int(i):
pypy_faulthandler_write_int(i)
@@ -29,9 +41,9 @@
_dump(pycode.co_filename)
_dump('" in ')
_dump(pycode.co_name)
- _dump(" (starting at line ")
+ _dump(" starting at line ")
_dump_int(pycode.co_firstlineno)
- _dump(")\n")
+ _dump("\n")
return 1
diff --git a/pypy/module/faulthandler/faulthandler.c
b/pypy/module/faulthandler/faulthandler.c
--- a/pypy/module/faulthandler/faulthandler.c
+++ b/pypy/module/faulthandler/faulthandler.c
@@ -1,9 +1,11 @@
#include "faulthandler.h"
#include <stdlib.h>
+#include <stdio.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
+#include <unistd.h>
typedef struct sigaction _Py_sighandler_t;
@@ -19,7 +21,7 @@
int initialized;
int enabled;
volatile int fd, all_threads;
- volatile void (*dump_traceback)(void);
+ void (*volatile dump_traceback)(void);
int _current_fd;
} fatal_error;
@@ -43,41 +45,44 @@
sizeof(faulthandler_handlers) / sizeof(fault_handler_t);
static void
-fh_write(int fd, char *str)
+fh_write(int fd, const char *str)
{
(void)write(fd, str, strlen(str));
}
RPY_EXTERN
-void pypy_faulthandler_write(RPyString *s)
+void pypy_faulthandler_write(char *str)
{
- (void)write(fatal_error._current_fd,
- _RPyString_AsString(s), RPyString_Size(s));
+ fh_write(fatal_error._current_fd, str);
}
RPY_EXTERN
void pypy_faulthandler_write_int(long x)
{
char buf[32];
- int count = sprintf(buf, "%ld", x);
- (void)write(fatal_error._current_fd, buf, count);
+ sprintf(buf, "%ld", x);
+ fh_write(fatal_error._current_fd, buf);
}
-static void
-faulthandler_dump_traceback(int fd, int all_threads)
+RPY_EXTERN
+void pypy_faulthandler_dump_traceback(int fd, int all_threads)
+{
+ fatal_error._current_fd = fd;
+
+ /* XXX 'all_threads' ignored */
+ if (fatal_error.dump_traceback)
+ fatal_error.dump_traceback();
+}
+
+void faulthandler_dump_traceback(int fd, int all_threads)
{
static volatile int reentrant = 0;
if (reentrant)
return;
reentrant = 1;
- fatal_error._current_fd = fd;
-
- /* XXX 'all_threads' ignored */
- if (fatal_error.dump_traceback)
- fatal_error.dump_traceback();
-
+ pypy_faulthandler_dump_traceback(fd, all_threads);
reentrant = 0;
}
@@ -139,9 +144,9 @@
char *pypy_faulthandler_setup(void dump_callback(void))
{
if (fatal_error.initialized)
- return;
+ return NULL;
assert(!fatal_error.enabled);
- fatal_error.dump_callback = dump_callback;
+ fatal_error.dump_traceback = dump_callback;
/* Try to allocate an alternate stack for faulthandler() signal handler to
* be able to allocate memory on the stack, even on a stack overflow. If it
diff --git a/pypy/module/faulthandler/faulthandler.h
b/pypy/module/faulthandler/faulthandler.h
--- a/pypy/module/faulthandler/faulthandler.h
+++ b/pypy/module/faulthandler/faulthandler.h
@@ -1,7 +1,7 @@
#ifndef PYPY_FAULTHANDLER_H
#define PYPY_FAULTHANDLER_H
-#include "common_header.h"
+#include "src/precommondefs.h"
RPY_EXTERN char *pypy_faulthandler_setup(void dump_callback(void));
RPY_EXTERN void pypy_faulthandler_teardown(void);
@@ -10,9 +10,11 @@
RPY_EXTERN void pypy_faulthandler_disable(void);
RPY_EXTERN int pypy_faulthandler_is_enabled(void);
-RPY_EXTERN void pypy_faulthandler_write(RPyString *);
+RPY_EXTERN void pypy_faulthandler_write(char *);
RPY_EXTERN void pypy_faulthandler_write_int(long);
+RPY_EXTERN void pypy_faulthandler_dump_traceback(int fd, int all_threads);
+
/*
RPY_EXTERN int pypy_faulthandler_read_null(void);
RPY_EXTERN void pypy_faulthandler_sigsegv(void);
diff --git a/pypy/module/faulthandler/handler.py
b/pypy/module/faulthandler/handler.py
--- a/pypy/module/faulthandler/handler.py
+++ b/pypy/module/faulthandler/handler.py
@@ -2,6 +2,7 @@
from rpython.rtyper.lltypesystem import rffi
from rpython.rlib.rposix import is_valid_fd
from rpython.rlib.rarithmetic import widen
+from rpython.rlib.objectmodel import keepalive_until_here
from rpython.rtyper.annlowlevel import llhelper
from pypy.interpreter.error import oefmt, exception_from_saved_errno
@@ -13,7 +14,6 @@
def __init__(self, space):
"NOT_RPYTHON"
self.space = space
- dumper.glob.space = space
self._cleanup_()
def _cleanup_(self):
@@ -64,6 +64,17 @@
def is_enabled(self):
return bool(widen(cintf.pypy_faulthandler_is_enabled()))
+ def dump_traceback(self, w_file, all_threads):
+ fileno, w_file = self.get_fileno_and_file(w_file)
+ #
+ dump_callback = llhelper(cintf.DUMP_CALLBACK, dumper._dump_callback)
+ self.check_err(cintf.pypy_faulthandler_setup(dump_callback))
+ #
+ cintf.pypy_faulthandler_dump_traceback(
+ rffi.cast(rffi.INT, fileno),
+ rffi.cast(rffi.INT, all_threads))
+ keepalive_until_here(w_file)
+
def finish(self):
cintf.pypy_faulthandler_teardown()
self._cleanup_()
@@ -77,7 +88,8 @@
# is defined, or if sys._xoptions has a 'faulthandler' key.
if not os.environ.get('PYTHONFAULTHANDLER'):
w_options = space.sys.get('_xoptions')
- if not space.contains(w_options, space.wrap('faulthandler')):
+ if not space.is_true(space.contains(w_options,
+ space.wrap('faulthandler'))):
return
#
# Like CPython. Why not just call enable(space)? Maybe someone
@@ -105,3 +117,9 @@
def is_enabled(space):
"is_enabled()->bool: check if the handler is enabled"
return space.wrap(space.fromcache(Handler).is_enabled())
+
+@unwrap_spec(all_threads=int)
+def dump_traceback(space, w_file=None, all_threads=0):
+ """dump the traceback of the current thread into file
+ including all threads if all_threads is True"""
+ space.fromcache(Handler).dump_traceback(w_file, all_threads)
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit