summaryrefslogtreecommitdiffstats
path: root/Python/traceback.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/traceback.c')
-rw-r--r--Python/traceback.c183
1 files changed, 116 insertions, 67 deletions
diff --git a/Python/traceback.c b/Python/traceback.c
index 941d1cb..59552ca 100644
--- a/Python/traceback.c
+++ b/Python/traceback.c
@@ -479,40 +479,26 @@ PyTraceBack_Print(PyObject *v, PyObject *f)
This function is signal safe. */
-static void
-reverse_string(char *text, const size_t len)
-{
- char tmp;
- size_t i, j;
- if (len == 0)
- return;
- for (i=0, j=len-1; i < j; i++, j--) {
- tmp = text[i];
- text[i] = text[j];
- text[j] = tmp;
- }
-}
-
-/* Format an integer in range [0; 999999] to decimal,
- and write it into the file fd.
-
- This function is signal safe. */
-
-static void
-dump_decimal(int fd, int value)
+void
+_Py_DumpDecimal(int fd, unsigned long value)
{
- char buffer[7];
- int len;
- if (value < 0 || 999999 < value)
- return;
- len = 0;
+ /* maximum number of characters required for output of %lld or %p.
+ We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits,
+ plus 1 for the null byte. 53/22 is an upper bound for log10(256). */
+ char buffer[1 + (sizeof(unsigned long)*53-1) / 22 + 1];
+ char *ptr, *end;
+
+ end = &buffer[Py_ARRAY_LENGTH(buffer) - 1];
+ ptr = end;
+ *ptr = '\0';
do {
- buffer[len] = '0' + (value % 10);
+ --ptr;
+ assert(ptr >= buffer);
+ *ptr = '0' + (value % 10);
value /= 10;
- len++;
} while (value);
- reverse_string(buffer, len);
- _Py_write_noraise(fd, buffer, len);
+
+ _Py_write_noraise(fd, ptr, end - ptr);
}
/* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits,
@@ -520,27 +506,31 @@ dump_decimal(int fd, int value)
This function is signal safe. */
-static void
-dump_hexadecimal(int fd, unsigned long value, int width)
+void
+_Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width)
{
- int len;
- char buffer[sizeof(unsigned long) * 2 + 1];
- len = 0;
+ char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end;
+ const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
+
+ if (width > size)
+ width = size;
+ /* it's ok if width is negative */
+
+ end = &buffer[size];
+ ptr = end;
+ *ptr = '\0';
do {
- buffer[len] = Py_hexdigits[value & 15];
+ --ptr;
+ assert(ptr >= buffer);
+ *ptr = Py_hexdigits[value & 15];
value >>= 4;
- len++;
- } while (len < width || value);
- reverse_string(buffer, len);
- _Py_write_noraise(fd, buffer, len);
-}
+ } while ((end - ptr) < width || value);
-/* Write an unicode object into the file fd using ascii+backslashreplace.
-
- This function is signal safe. */
+ _Py_write_noraise(fd, ptr, end - ptr);
+}
-static void
-dump_ascii(int fd, PyObject *text)
+void
+_Py_DumpASCII(int fd, PyObject *text)
{
PyASCIIObject *ascii = (PyASCIIObject *)text;
Py_ssize_t i, size;
@@ -550,32 +540,36 @@ dump_ascii(int fd, PyObject *text)
wchar_t *wstr = NULL;
Py_UCS4 ch;
+ if (!PyUnicode_Check(text))
+ return;
+
size = ascii->length;
kind = ascii->state.kind;
- if (ascii->state.compact) {
+ if (kind == PyUnicode_WCHAR_KIND) {
+ wstr = ((PyASCIIObject *)text)->wstr;
+ if (wstr == NULL)
+ return;
+ size = ((PyCompactUnicodeObject *)text)->wstr_length;
+ }
+ else if (ascii->state.compact) {
if (ascii->state.ascii)
data = ((PyASCIIObject*)text) + 1;
else
data = ((PyCompactUnicodeObject*)text) + 1;
}
- else if (kind != PyUnicode_WCHAR_KIND) {
+ else {
data = ((PyUnicodeObject *)text)->data.any;
if (data == NULL)
return;
}
- else {
- wstr = ((PyASCIIObject *)text)->wstr;
- if (wstr == NULL)
- return;
- size = ((PyCompactUnicodeObject *)text)->wstr_length;
- }
if (MAX_STRING_LENGTH < size) {
size = MAX_STRING_LENGTH;
truncated = 1;
}
- else
+ else {
truncated = 0;
+ }
for (i=0; i < size; i++) {
if (kind != PyUnicode_WCHAR_KIND)
@@ -589,19 +583,20 @@ dump_ascii(int fd, PyObject *text)
}
else if (ch <= 0xff) {
PUTS(fd, "\\x");
- dump_hexadecimal(fd, ch, 2);
+ _Py_DumpHexadecimal(fd, ch, 2);
}
else if (ch <= 0xffff) {
PUTS(fd, "\\u");
- dump_hexadecimal(fd, ch, 4);
+ _Py_DumpHexadecimal(fd, ch, 4);
}
else {
PUTS(fd, "\\U");
- dump_hexadecimal(fd, ch, 8);
+ _Py_DumpHexadecimal(fd, ch, 8);
}
}
- if (truncated)
+ if (truncated) {
PUTS(fd, "...");
+ }
}
/* Write a frame into the file fd: "File "xxx", line xxx in xxx".
@@ -620,7 +615,7 @@ dump_frame(int fd, PyFrameObject *frame)
&& PyUnicode_Check(code->co_filename))
{
PUTS(fd, "\"");
- dump_ascii(fd, code->co_filename);
+ _Py_DumpASCII(fd, code->co_filename);
PUTS(fd, "\"");
} else {
PUTS(fd, "???");
@@ -629,14 +624,21 @@ dump_frame(int fd, PyFrameObject *frame)
/* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
lineno = PyCode_Addr2Line(code, frame->f_lasti);
PUTS(fd, ", line ");
- dump_decimal(fd, lineno);
+ if (lineno >= 0) {
+ _Py_DumpDecimal(fd, (unsigned long)lineno);
+ }
+ else {
+ PUTS(fd, "???");
+ }
PUTS(fd, " in ");
if (code != NULL && code->co_name != NULL
- && PyUnicode_Check(code->co_name))
- dump_ascii(fd, code->co_name);
- else
+ && PyUnicode_Check(code->co_name)) {
+ _Py_DumpASCII(fd, code->co_name);
+ }
+ else {
PUTS(fd, "???");
+ }
PUTS(fd, "\n");
}
@@ -692,7 +694,9 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
PUTS(fd, "Current thread 0x");
else
PUTS(fd, "Thread 0x");
- dump_hexadecimal(fd, (unsigned long)tstate->thread_id, sizeof(unsigned long)*2);
+ _Py_DumpHexadecimal(fd,
+ (unsigned long)tstate->thread_id,
+ sizeof(unsigned long) * 2);
PUTS(fd, " (most recent call first):\n");
}
@@ -704,11 +708,56 @@ write_thread_id(int fd, PyThreadState *tstate, int is_current)
handlers if signals were received. */
const char*
_Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
- PyThreadState *current_thread)
+ PyThreadState *current_tstate)
{
PyThreadState *tstate;
unsigned int nthreads;
+#ifdef WITH_THREAD
+ if (current_tstate == NULL) {
+ /* _Py_DumpTracebackThreads() is called from signal handlers by
+ faulthandler.
+
+ SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals
+ and are thus delivered to the thread that caused the fault. Get the
+ Python thread state of the current thread.
+
+ PyThreadState_Get() doesn't give the state of the thread that caused
+ the fault if the thread released the GIL, and so this function
+ cannot be used. Read the thread local storage (TLS) instead: call
+ PyGILState_GetThisThreadState(). */
+ current_tstate = PyGILState_GetThisThreadState();
+ }
+
+ if (interp == NULL) {
+ if (current_tstate == NULL) {
+ interp = _PyGILState_GetInterpreterStateUnsafe();
+ if (interp == NULL) {
+ /* We need the interpreter state to get Python threads */
+ return "unable to get the interpreter state";
+ }
+ }
+ else {
+ interp = current_tstate->interp;
+ }
+ }
+#else
+ if (current_tstate == NULL) {
+ /* Call _PyThreadState_UncheckedGet() instead of PyThreadState_Get()
+ to not fail with a fatal error if the thread state is NULL. */
+ current_tstate = _PyThreadState_UncheckedGet();
+ }
+
+ if (interp == NULL) {
+ if (current_tstate == NULL) {
+ /* We need the interpreter state to get Python threads */
+ return "unable to get the interpreter state";
+ }
+ interp = current_tstate->interp;
+ }
+#endif
+ assert(interp != NULL);
+
/* Get the current interpreter from the current thread */
tstate = PyInterpreterState_ThreadHead(interp);
if (tstate == NULL)
@@ -726,7 +775,7 @@ _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
PUTS(fd, "...\n");
break;
}
- write_thread_id(fd, tstate, tstate == current_thread);
+ write_thread_id(fd, tstate, tstate == current_tstate);
dump_traceback(fd, tstate, 0);
tstate = PyThreadState_Next(tstate);
nthreads++;
wa">if os.path.isfile(path): os.remove(path) elif os.path.isdir(path): shutil.rmtree(path) def test_get_path_names(self): self.assertEqual(get_path_names(), sysconfig._SCHEME_KEYS) def test_get_paths(self): scheme = get_paths() default_scheme = _get_default_scheme() wanted = _expand_vars(default_scheme, None) wanted = wanted.items() wanted.sort() scheme = scheme.items() scheme.sort() self.assertEqual(scheme, wanted) def test_get_path(self): # xxx make real tests here for scheme in _INSTALL_SCHEMES: for name in _INSTALL_SCHEMES[scheme]: res = get_path(name, scheme) def test_get_config_vars(self): cvars = get_config_vars() self.assertIsInstance(cvars, dict) self.assertTrue(cvars) def test_get_platform(self): # windows XP, 32bits os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Intel)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win32') # windows XP, amd64 os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Amd64)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win-amd64') # windows XP, itanium os.name = 'nt' sys.version = ('2.4.4 (#71, Oct 18 2006, 08:34:43) ' '[MSC v.1310 32 bit (Itanium)]') sys.platform = 'win32' self.assertEqual(get_platform(), 'win-ia64') # macbook os.name = 'posix' sys.version = ('2.5 (r25:51918, Sep 19 2006, 08:49:13) ' '\n[GCC 4.0.1 (Apple Computer, Inc. build 5341)]') sys.platform = 'darwin' self._set_uname(('Darwin', 'macziade', '8.11.1', ('Darwin Kernel Version 8.11.1: ' 'Wed Oct 10 18:23:28 PDT 2007; ' 'root:xnu-792.25.20~1/RELEASE_I386'), 'PowerPC')) os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' '-fwrapv -O3 -Wall -Wstrict-prototypes') maxint = sys.maxint try: sys.maxint = 2147483647 self.assertEqual(get_platform(), 'macosx-10.3-ppc') sys.maxint = 9223372036854775807 self.assertEqual(get_platform(), 'macosx-10.3-ppc64') finally: sys.maxint = maxint self._set_uname(('Darwin', 'macziade', '8.11.1', ('Darwin Kernel Version 8.11.1: ' 'Wed Oct 10 18:23:28 PDT 2007; ' 'root:xnu-792.25.20~1/RELEASE_I386'), 'i386')) get_config_vars()['MACOSX_DEPLOYMENT_TARGET'] = '10.3' os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.3' get_config_vars()['CFLAGS'] = ('-fno-strict-aliasing -DNDEBUG -g ' '-fwrapv -O3 -Wall -Wstrict-prototypes') maxint = sys.maxint try: sys.maxint = 2147483647 self.assertEqual(get_platform(), 'macosx-10.3-i386') sys.maxint = 9223372036854775807 self.assertEqual(get_platform(), 'macosx-10.3-x86_64') finally: sys.maxint = maxint # macbook with fat binaries (fat, universal or fat64) os.environ['MACOSX_DEPLOYMENT_TARGET'] = '10.4' get_config_vars()['CFLAGS'] = ('-arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat') get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-intel') get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat3') get_config_vars()['CFLAGS'] = ('-arch ppc64 -arch x86_64 -arch ppc -arch i386 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-universal') get_config_vars()['CFLAGS'] = ('-arch x86_64 -arch ppc64 -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3') self.assertEqual(get_platform(), 'macosx-10.4-fat64') for arch in ('ppc', 'i386', 'x86_64', 'ppc64'): get_config_vars()['CFLAGS'] = ('-arch %s -isysroot ' '/Developer/SDKs/MacOSX10.4u.sdk ' '-fno-strict-aliasing -fno-common ' '-dynamic -DNDEBUG -g -O3'%(arch,)) self.assertEqual(get_platform(), 'macosx-10.4-%s'%(arch,)) # linux debian sarge os.name = 'posix' sys.version = ('2.3.5 (#1, Jul 4 2007, 17:28:59) ' '\n[GCC 4.1.2 20061115 (prerelease) (Debian 4.1.1-21)]') sys.platform = 'linux2' self._set_uname(('Linux', 'aglae', '2.6.21.1dedibox-r7', '#1 Mon Apr 30 17:25:38 CEST 2007', 'i686')) self.assertEqual(get_platform(), 'linux-i686') # XXX more platforms to tests here def test_get_config_h_filename(self): config_h = sysconfig.get_config_h_filename() self.assertTrue(os.path.isfile(config_h), config_h) def test_get_scheme_names(self): wanted = ('nt', 'nt_user', 'os2', 'os2_home', 'osx_framework_user', 'posix_home', 'posix_prefix', 'posix_user') self.assertEqual(get_scheme_names(), wanted) def test_symlink(self): # Issue 7880 symlink = get_attribute(os, "symlink") def get(python): cmd = [python, '-c', 'import sysconfig; print sysconfig.get_platform()'] p = subprocess.Popen(cmd, stdout=subprocess.PIPE) return p.communicate() real = os.path.realpath(sys.executable) link = os.path.abspath(TESTFN) symlink(real, link) try: self.assertEqual(get(real), get(link)) finally: unlink(link) def test_user_similar(self): # Issue 8759 : make sure the posix scheme for the users # is similar to the global posix_prefix one base = get_config_var('base') user = get_config_var('userbase') for name in ('stdlib', 'platstdlib', 'purelib', 'platlib'): global_path = get_path(name, 'posix_prefix') user_path = get_path(name, 'posix_user') self.assertEqual(user_path, global_path.replace(base, user)) def test_main(): run_unittest(TestSysConfig) if __name__ == "__main__": test_main()