You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

808 lines
22 KiB

29 years ago
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62260 | gregory.p.smith | 2008-04-10 01:11:56 +0200 (Thu, 10 Apr 2008) | 2 lines better diagnostics ........ r62261 | gregory.p.smith | 2008-04-10 01:16:37 +0200 (Thu, 10 Apr 2008) | 3 lines Raise SystemError when size < 0 is passed into PyString_FromStringAndSize, PyBytes_FromStringAndSize or PyUnicode_FromStringAndSize. [issue2587] ........ r62266 | neal.norwitz | 2008-04-10 07:46:39 +0200 (Thu, 10 Apr 2008) | 5 lines Remove the test file before writing it in case there is no write permission. This might help fix some of the failures on Windows box(es). It doesn't hurt either way and ensure the tests are a little more self contained (ie have less assumptions). ........ r62271 | gregory.p.smith | 2008-04-10 21:50:36 +0200 (Thu, 10 Apr 2008) | 2 lines get rid of assert (size >= 0) now that an explicit if (size < 0) is in the code. ........ r62277 | andrew.kuchling | 2008-04-10 23:27:10 +0200 (Thu, 10 Apr 2008) | 1 line Remove forward-looking statement ........ r62278 | andrew.kuchling | 2008-04-10 23:28:51 +0200 (Thu, 10 Apr 2008) | 1 line Add punctuation ........ r62279 | andrew.kuchling | 2008-04-10 23:29:01 +0200 (Thu, 10 Apr 2008) | 1 line Use issue directive ........ r62289 | thomas.heller | 2008-04-11 15:05:38 +0200 (Fri, 11 Apr 2008) | 3 lines Move backwards compatibility macro to the correct place; PyIndex_Check() was introduced in Python 2.5. ........ r62290 | thomas.heller | 2008-04-11 16:20:26 +0200 (Fri, 11 Apr 2008) | 2 lines Performance improvements. ........ r62293 | christian.heimes | 2008-04-12 15:03:03 +0200 (Sat, 12 Apr 2008) | 2 lines Applied patch #2617 from Frank Wierzbicki wit some extras from me -J and -X are now reserved for Jython and non-standard arguments (e.g. IronPython). I've added some extra comments to make sure the reservation don't get missed in the future. ........ r62294 | georg.brandl | 2008-04-12 20:11:18 +0200 (Sat, 12 Apr 2008) | 2 lines Use absolute path in sys.path. ........ r62295 | georg.brandl | 2008-04-12 20:36:09 +0200 (Sat, 12 Apr 2008) | 2 lines #2615: small consistency update by Jeroen Ruigrok van der Werven. ........ r62296 | georg.brandl | 2008-04-12 21:00:20 +0200 (Sat, 12 Apr 2008) | 2 lines Add Jeroen. ........ r62297 | georg.brandl | 2008-04-12 21:05:37 +0200 (Sat, 12 Apr 2008) | 2 lines Don't offend snake lovers. ........ r62298 | gregory.p.smith | 2008-04-12 22:37:48 +0200 (Sat, 12 Apr 2008) | 2 lines fix compiler warnings ........ r62302 | gregory.p.smith | 2008-04-13 00:24:04 +0200 (Sun, 13 Apr 2008) | 3 lines socket.error inherits from IOError, it no longer needs listing in the all_errors tuple. ........ r62303 | brett.cannon | 2008-04-13 01:44:07 +0200 (Sun, 13 Apr 2008) | 8 lines Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171. ........ r62304 | gregory.p.smith | 2008-04-13 02:03:25 +0200 (Sun, 13 Apr 2008) | 3 lines Adds a profile-opt target for easy compilation of a python binary using gcc's profile guided optimization. ........ r62305 | brett.cannon | 2008-04-13 02:18:44 +0200 (Sun, 13 Apr 2008) | 3 lines Fix a bug in PySys_HasWarnOption() where it was not properly checking the length of the list storing the warning options. ........ r62306 | brett.cannon | 2008-04-13 02:25:15 +0200 (Sun, 13 Apr 2008) | 2 lines Fix an accidental bug of an non-existent init function. ........ r62308 | andrew.kuchling | 2008-04-13 03:05:59 +0200 (Sun, 13 Apr 2008) | 1 line Mention -J, -X ........ r62311 | benjamin.peterson | 2008-04-13 04:20:05 +0200 (Sun, 13 Apr 2008) | 2 lines Give the "Interactive Interpreter Changes" section in 2.6 whatsnew a unique link name ........ r62313 | brett.cannon | 2008-04-13 04:42:36 +0200 (Sun, 13 Apr 2008) | 3 lines Fix test_warnings by making the state of things more consistent for each test when it is run. ........ r62314 | skip.montanaro | 2008-04-13 05:17:30 +0200 (Sun, 13 Apr 2008) | 2 lines spelling ........ r62315 | georg.brandl | 2008-04-13 09:07:44 +0200 (Sun, 13 Apr 2008) | 2 lines Fix markup. ........ r62319 | christian.heimes | 2008-04-13 11:30:17 +0200 (Sun, 13 Apr 2008) | 1 line Fix compiler warning Include/warnings.h:19:28: warning: no newline at end of file ........ r62320 | christian.heimes | 2008-04-13 11:33:24 +0200 (Sun, 13 Apr 2008) | 1 line Use PyString_InternFromString instead of PyString_FromString for static vars ........ r62321 | christian.heimes | 2008-04-13 11:37:05 +0200 (Sun, 13 Apr 2008) | 1 line Added new files to the pcbuild files ........
18 years ago
13 years ago
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62260 | gregory.p.smith | 2008-04-10 01:11:56 +0200 (Thu, 10 Apr 2008) | 2 lines better diagnostics ........ r62261 | gregory.p.smith | 2008-04-10 01:16:37 +0200 (Thu, 10 Apr 2008) | 3 lines Raise SystemError when size < 0 is passed into PyString_FromStringAndSize, PyBytes_FromStringAndSize or PyUnicode_FromStringAndSize. [issue2587] ........ r62266 | neal.norwitz | 2008-04-10 07:46:39 +0200 (Thu, 10 Apr 2008) | 5 lines Remove the test file before writing it in case there is no write permission. This might help fix some of the failures on Windows box(es). It doesn't hurt either way and ensure the tests are a little more self contained (ie have less assumptions). ........ r62271 | gregory.p.smith | 2008-04-10 21:50:36 +0200 (Thu, 10 Apr 2008) | 2 lines get rid of assert (size >= 0) now that an explicit if (size < 0) is in the code. ........ r62277 | andrew.kuchling | 2008-04-10 23:27:10 +0200 (Thu, 10 Apr 2008) | 1 line Remove forward-looking statement ........ r62278 | andrew.kuchling | 2008-04-10 23:28:51 +0200 (Thu, 10 Apr 2008) | 1 line Add punctuation ........ r62279 | andrew.kuchling | 2008-04-10 23:29:01 +0200 (Thu, 10 Apr 2008) | 1 line Use issue directive ........ r62289 | thomas.heller | 2008-04-11 15:05:38 +0200 (Fri, 11 Apr 2008) | 3 lines Move backwards compatibility macro to the correct place; PyIndex_Check() was introduced in Python 2.5. ........ r62290 | thomas.heller | 2008-04-11 16:20:26 +0200 (Fri, 11 Apr 2008) | 2 lines Performance improvements. ........ r62293 | christian.heimes | 2008-04-12 15:03:03 +0200 (Sat, 12 Apr 2008) | 2 lines Applied patch #2617 from Frank Wierzbicki wit some extras from me -J and -X are now reserved for Jython and non-standard arguments (e.g. IronPython). I've added some extra comments to make sure the reservation don't get missed in the future. ........ r62294 | georg.brandl | 2008-04-12 20:11:18 +0200 (Sat, 12 Apr 2008) | 2 lines Use absolute path in sys.path. ........ r62295 | georg.brandl | 2008-04-12 20:36:09 +0200 (Sat, 12 Apr 2008) | 2 lines #2615: small consistency update by Jeroen Ruigrok van der Werven. ........ r62296 | georg.brandl | 2008-04-12 21:00:20 +0200 (Sat, 12 Apr 2008) | 2 lines Add Jeroen. ........ r62297 | georg.brandl | 2008-04-12 21:05:37 +0200 (Sat, 12 Apr 2008) | 2 lines Don't offend snake lovers. ........ r62298 | gregory.p.smith | 2008-04-12 22:37:48 +0200 (Sat, 12 Apr 2008) | 2 lines fix compiler warnings ........ r62302 | gregory.p.smith | 2008-04-13 00:24:04 +0200 (Sun, 13 Apr 2008) | 3 lines socket.error inherits from IOError, it no longer needs listing in the all_errors tuple. ........ r62303 | brett.cannon | 2008-04-13 01:44:07 +0200 (Sun, 13 Apr 2008) | 8 lines Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171. ........ r62304 | gregory.p.smith | 2008-04-13 02:03:25 +0200 (Sun, 13 Apr 2008) | 3 lines Adds a profile-opt target for easy compilation of a python binary using gcc's profile guided optimization. ........ r62305 | brett.cannon | 2008-04-13 02:18:44 +0200 (Sun, 13 Apr 2008) | 3 lines Fix a bug in PySys_HasWarnOption() where it was not properly checking the length of the list storing the warning options. ........ r62306 | brett.cannon | 2008-04-13 02:25:15 +0200 (Sun, 13 Apr 2008) | 2 lines Fix an accidental bug of an non-existent init function. ........ r62308 | andrew.kuchling | 2008-04-13 03:05:59 +0200 (Sun, 13 Apr 2008) | 1 line Mention -J, -X ........ r62311 | benjamin.peterson | 2008-04-13 04:20:05 +0200 (Sun, 13 Apr 2008) | 2 lines Give the "Interactive Interpreter Changes" section in 2.6 whatsnew a unique link name ........ r62313 | brett.cannon | 2008-04-13 04:42:36 +0200 (Sun, 13 Apr 2008) | 3 lines Fix test_warnings by making the state of things more consistent for each test when it is run. ........ r62314 | skip.montanaro | 2008-04-13 05:17:30 +0200 (Sun, 13 Apr 2008) | 2 lines spelling ........ r62315 | georg.brandl | 2008-04-13 09:07:44 +0200 (Sun, 13 Apr 2008) | 2 lines Fix markup. ........ r62319 | christian.heimes | 2008-04-13 11:30:17 +0200 (Sun, 13 Apr 2008) | 1 line Fix compiler warning Include/warnings.h:19:28: warning: no newline at end of file ........ r62320 | christian.heimes | 2008-04-13 11:33:24 +0200 (Sun, 13 Apr 2008) | 1 line Use PyString_InternFromString instead of PyString_FromString for static vars ........ r62321 | christian.heimes | 2008-04-13 11:37:05 +0200 (Sun, 13 Apr 2008) | 1 line Added new files to the pcbuild files ........
18 years ago
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62260 | gregory.p.smith | 2008-04-10 01:11:56 +0200 (Thu, 10 Apr 2008) | 2 lines better diagnostics ........ r62261 | gregory.p.smith | 2008-04-10 01:16:37 +0200 (Thu, 10 Apr 2008) | 3 lines Raise SystemError when size < 0 is passed into PyString_FromStringAndSize, PyBytes_FromStringAndSize or PyUnicode_FromStringAndSize. [issue2587] ........ r62266 | neal.norwitz | 2008-04-10 07:46:39 +0200 (Thu, 10 Apr 2008) | 5 lines Remove the test file before writing it in case there is no write permission. This might help fix some of the failures on Windows box(es). It doesn't hurt either way and ensure the tests are a little more self contained (ie have less assumptions). ........ r62271 | gregory.p.smith | 2008-04-10 21:50:36 +0200 (Thu, 10 Apr 2008) | 2 lines get rid of assert (size >= 0) now that an explicit if (size < 0) is in the code. ........ r62277 | andrew.kuchling | 2008-04-10 23:27:10 +0200 (Thu, 10 Apr 2008) | 1 line Remove forward-looking statement ........ r62278 | andrew.kuchling | 2008-04-10 23:28:51 +0200 (Thu, 10 Apr 2008) | 1 line Add punctuation ........ r62279 | andrew.kuchling | 2008-04-10 23:29:01 +0200 (Thu, 10 Apr 2008) | 1 line Use issue directive ........ r62289 | thomas.heller | 2008-04-11 15:05:38 +0200 (Fri, 11 Apr 2008) | 3 lines Move backwards compatibility macro to the correct place; PyIndex_Check() was introduced in Python 2.5. ........ r62290 | thomas.heller | 2008-04-11 16:20:26 +0200 (Fri, 11 Apr 2008) | 2 lines Performance improvements. ........ r62293 | christian.heimes | 2008-04-12 15:03:03 +0200 (Sat, 12 Apr 2008) | 2 lines Applied patch #2617 from Frank Wierzbicki wit some extras from me -J and -X are now reserved for Jython and non-standard arguments (e.g. IronPython). I've added some extra comments to make sure the reservation don't get missed in the future. ........ r62294 | georg.brandl | 2008-04-12 20:11:18 +0200 (Sat, 12 Apr 2008) | 2 lines Use absolute path in sys.path. ........ r62295 | georg.brandl | 2008-04-12 20:36:09 +0200 (Sat, 12 Apr 2008) | 2 lines #2615: small consistency update by Jeroen Ruigrok van der Werven. ........ r62296 | georg.brandl | 2008-04-12 21:00:20 +0200 (Sat, 12 Apr 2008) | 2 lines Add Jeroen. ........ r62297 | georg.brandl | 2008-04-12 21:05:37 +0200 (Sat, 12 Apr 2008) | 2 lines Don't offend snake lovers. ........ r62298 | gregory.p.smith | 2008-04-12 22:37:48 +0200 (Sat, 12 Apr 2008) | 2 lines fix compiler warnings ........ r62302 | gregory.p.smith | 2008-04-13 00:24:04 +0200 (Sun, 13 Apr 2008) | 3 lines socket.error inherits from IOError, it no longer needs listing in the all_errors tuple. ........ r62303 | brett.cannon | 2008-04-13 01:44:07 +0200 (Sun, 13 Apr 2008) | 8 lines Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171. ........ r62304 | gregory.p.smith | 2008-04-13 02:03:25 +0200 (Sun, 13 Apr 2008) | 3 lines Adds a profile-opt target for easy compilation of a python binary using gcc's profile guided optimization. ........ r62305 | brett.cannon | 2008-04-13 02:18:44 +0200 (Sun, 13 Apr 2008) | 3 lines Fix a bug in PySys_HasWarnOption() where it was not properly checking the length of the list storing the warning options. ........ r62306 | brett.cannon | 2008-04-13 02:25:15 +0200 (Sun, 13 Apr 2008) | 2 lines Fix an accidental bug of an non-existent init function. ........ r62308 | andrew.kuchling | 2008-04-13 03:05:59 +0200 (Sun, 13 Apr 2008) | 1 line Mention -J, -X ........ r62311 | benjamin.peterson | 2008-04-13 04:20:05 +0200 (Sun, 13 Apr 2008) | 2 lines Give the "Interactive Interpreter Changes" section in 2.6 whatsnew a unique link name ........ r62313 | brett.cannon | 2008-04-13 04:42:36 +0200 (Sun, 13 Apr 2008) | 3 lines Fix test_warnings by making the state of things more consistent for each test when it is run. ........ r62314 | skip.montanaro | 2008-04-13 05:17:30 +0200 (Sun, 13 Apr 2008) | 2 lines spelling ........ r62315 | georg.brandl | 2008-04-13 09:07:44 +0200 (Sun, 13 Apr 2008) | 2 lines Fix markup. ........ r62319 | christian.heimes | 2008-04-13 11:30:17 +0200 (Sun, 13 Apr 2008) | 1 line Fix compiler warning Include/warnings.h:19:28: warning: no newline at end of file ........ r62320 | christian.heimes | 2008-04-13 11:33:24 +0200 (Sun, 13 Apr 2008) | 1 line Use PyString_InternFromString instead of PyString_FromString for static vars ........ r62321 | christian.heimes | 2008-04-13 11:37:05 +0200 (Sun, 13 Apr 2008) | 1 line Added new files to the pcbuild files ........
18 years ago
Merged revisions 62260-62261,62266,62271,62277-62279,62289-62290,62293-62298,62302-62306,62308,62311,62313-62315,62319-62321 via svnmerge from svn+ssh://pythondev@svn.python.org/python/trunk ........ r62260 | gregory.p.smith | 2008-04-10 01:11:56 +0200 (Thu, 10 Apr 2008) | 2 lines better diagnostics ........ r62261 | gregory.p.smith | 2008-04-10 01:16:37 +0200 (Thu, 10 Apr 2008) | 3 lines Raise SystemError when size < 0 is passed into PyString_FromStringAndSize, PyBytes_FromStringAndSize or PyUnicode_FromStringAndSize. [issue2587] ........ r62266 | neal.norwitz | 2008-04-10 07:46:39 +0200 (Thu, 10 Apr 2008) | 5 lines Remove the test file before writing it in case there is no write permission. This might help fix some of the failures on Windows box(es). It doesn't hurt either way and ensure the tests are a little more self contained (ie have less assumptions). ........ r62271 | gregory.p.smith | 2008-04-10 21:50:36 +0200 (Thu, 10 Apr 2008) | 2 lines get rid of assert (size >= 0) now that an explicit if (size < 0) is in the code. ........ r62277 | andrew.kuchling | 2008-04-10 23:27:10 +0200 (Thu, 10 Apr 2008) | 1 line Remove forward-looking statement ........ r62278 | andrew.kuchling | 2008-04-10 23:28:51 +0200 (Thu, 10 Apr 2008) | 1 line Add punctuation ........ r62279 | andrew.kuchling | 2008-04-10 23:29:01 +0200 (Thu, 10 Apr 2008) | 1 line Use issue directive ........ r62289 | thomas.heller | 2008-04-11 15:05:38 +0200 (Fri, 11 Apr 2008) | 3 lines Move backwards compatibility macro to the correct place; PyIndex_Check() was introduced in Python 2.5. ........ r62290 | thomas.heller | 2008-04-11 16:20:26 +0200 (Fri, 11 Apr 2008) | 2 lines Performance improvements. ........ r62293 | christian.heimes | 2008-04-12 15:03:03 +0200 (Sat, 12 Apr 2008) | 2 lines Applied patch #2617 from Frank Wierzbicki wit some extras from me -J and -X are now reserved for Jython and non-standard arguments (e.g. IronPython). I've added some extra comments to make sure the reservation don't get missed in the future. ........ r62294 | georg.brandl | 2008-04-12 20:11:18 +0200 (Sat, 12 Apr 2008) | 2 lines Use absolute path in sys.path. ........ r62295 | georg.brandl | 2008-04-12 20:36:09 +0200 (Sat, 12 Apr 2008) | 2 lines #2615: small consistency update by Jeroen Ruigrok van der Werven. ........ r62296 | georg.brandl | 2008-04-12 21:00:20 +0200 (Sat, 12 Apr 2008) | 2 lines Add Jeroen. ........ r62297 | georg.brandl | 2008-04-12 21:05:37 +0200 (Sat, 12 Apr 2008) | 2 lines Don't offend snake lovers. ........ r62298 | gregory.p.smith | 2008-04-12 22:37:48 +0200 (Sat, 12 Apr 2008) | 2 lines fix compiler warnings ........ r62302 | gregory.p.smith | 2008-04-13 00:24:04 +0200 (Sun, 13 Apr 2008) | 3 lines socket.error inherits from IOError, it no longer needs listing in the all_errors tuple. ........ r62303 | brett.cannon | 2008-04-13 01:44:07 +0200 (Sun, 13 Apr 2008) | 8 lines Re-implement the 'warnings' module in C. This allows for usage of the 'warnings' code in places where it was previously not possible (e.g., the parser). It could also potentially lead to a speed-up in interpreter start-up if the C version of the code (_warnings) is imported over the use of the Python version in key places. Closes issue #1631171. ........ r62304 | gregory.p.smith | 2008-04-13 02:03:25 +0200 (Sun, 13 Apr 2008) | 3 lines Adds a profile-opt target for easy compilation of a python binary using gcc's profile guided optimization. ........ r62305 | brett.cannon | 2008-04-13 02:18:44 +0200 (Sun, 13 Apr 2008) | 3 lines Fix a bug in PySys_HasWarnOption() where it was not properly checking the length of the list storing the warning options. ........ r62306 | brett.cannon | 2008-04-13 02:25:15 +0200 (Sun, 13 Apr 2008) | 2 lines Fix an accidental bug of an non-existent init function. ........ r62308 | andrew.kuchling | 2008-04-13 03:05:59 +0200 (Sun, 13 Apr 2008) | 1 line Mention -J, -X ........ r62311 | benjamin.peterson | 2008-04-13 04:20:05 +0200 (Sun, 13 Apr 2008) | 2 lines Give the "Interactive Interpreter Changes" section in 2.6 whatsnew a unique link name ........ r62313 | brett.cannon | 2008-04-13 04:42:36 +0200 (Sun, 13 Apr 2008) | 3 lines Fix test_warnings by making the state of things more consistent for each test when it is run. ........ r62314 | skip.montanaro | 2008-04-13 05:17:30 +0200 (Sun, 13 Apr 2008) | 2 lines spelling ........ r62315 | georg.brandl | 2008-04-13 09:07:44 +0200 (Sun, 13 Apr 2008) | 2 lines Fix markup. ........ r62319 | christian.heimes | 2008-04-13 11:30:17 +0200 (Sun, 13 Apr 2008) | 1 line Fix compiler warning Include/warnings.h:19:28: warning: no newline at end of file ........ r62320 | christian.heimes | 2008-04-13 11:33:24 +0200 (Sun, 13 Apr 2008) | 1 line Use PyString_InternFromString instead of PyString_FromString for static vars ........ r62321 | christian.heimes | 2008-04-13 11:37:05 +0200 (Sun, 13 Apr 2008) | 1 line Added new files to the pcbuild files ........
18 years ago
  1. /* Traceback implementation */
  2. #include "Python.h"
  3. #include "internal/pystate.h"
  4. #include "code.h"
  5. #include "frameobject.h"
  6. #include "structmember.h"
  7. #include "osdefs.h"
  8. #ifdef HAVE_FCNTL_H
  9. #include <fcntl.h>
  10. #endif
  11. #define OFF(x) offsetof(PyTracebackObject, x)
  12. #define PUTS(fd, str) _Py_write_noraise(fd, str, (int)strlen(str))
  13. #define MAX_STRING_LENGTH 500
  14. #define MAX_FRAME_DEPTH 100
  15. #define MAX_NTHREADS 100
  16. /* Function from Parser/tokenizer.c */
  17. extern char * PyTokenizer_FindEncodingFilename(int, PyObject *);
  18. _Py_IDENTIFIER(TextIOWrapper);
  19. _Py_IDENTIFIER(close);
  20. _Py_IDENTIFIER(open);
  21. _Py_IDENTIFIER(path);
  22. static PyObject *
  23. tb_dir(PyTracebackObject *self)
  24. {
  25. return Py_BuildValue("[ssss]", "tb_frame", "tb_next",
  26. "tb_lasti", "tb_lineno");
  27. }
  28. static PyMethodDef tb_methods[] = {
  29. {"__dir__", (PyCFunction)tb_dir, METH_NOARGS},
  30. {NULL, NULL, 0, NULL},
  31. };
  32. static PyMemberDef tb_memberlist[] = {
  33. {"tb_next", T_OBJECT, OFF(tb_next), READONLY},
  34. {"tb_frame", T_OBJECT, OFF(tb_frame), READONLY},
  35. {"tb_lasti", T_INT, OFF(tb_lasti), READONLY},
  36. {"tb_lineno", T_INT, OFF(tb_lineno), READONLY},
  37. {NULL} /* Sentinel */
  38. };
  39. static void
  40. tb_dealloc(PyTracebackObject *tb)
  41. {
  42. PyObject_GC_UnTrack(tb);
  43. Py_TRASHCAN_SAFE_BEGIN(tb)
  44. Py_XDECREF(tb->tb_next);
  45. Py_XDECREF(tb->tb_frame);
  46. PyObject_GC_Del(tb);
  47. Py_TRASHCAN_SAFE_END(tb)
  48. }
  49. static int
  50. tb_traverse(PyTracebackObject *tb, visitproc visit, void *arg)
  51. {
  52. Py_VISIT(tb->tb_next);
  53. Py_VISIT(tb->tb_frame);
  54. return 0;
  55. }
  56. static void
  57. tb_clear(PyTracebackObject *tb)
  58. {
  59. Py_CLEAR(tb->tb_next);
  60. Py_CLEAR(tb->tb_frame);
  61. }
  62. PyTypeObject PyTraceBack_Type = {
  63. PyVarObject_HEAD_INIT(&PyType_Type, 0)
  64. "traceback",
  65. sizeof(PyTracebackObject),
  66. 0,
  67. (destructor)tb_dealloc, /*tp_dealloc*/
  68. 0, /*tp_print*/
  69. 0, /*tp_getattr*/
  70. 0, /*tp_setattr*/
  71. 0, /*tp_reserved*/
  72. 0, /*tp_repr*/
  73. 0, /*tp_as_number*/
  74. 0, /*tp_as_sequence*/
  75. 0, /*tp_as_mapping*/
  76. 0, /* tp_hash */
  77. 0, /* tp_call */
  78. 0, /* tp_str */
  79. PyObject_GenericGetAttr, /* tp_getattro */
  80. 0, /* tp_setattro */
  81. 0, /* tp_as_buffer */
  82. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
  83. 0, /* tp_doc */
  84. (traverseproc)tb_traverse, /* tp_traverse */
  85. (inquiry)tb_clear, /* tp_clear */
  86. 0, /* tp_richcompare */
  87. 0, /* tp_weaklistoffset */
  88. 0, /* tp_iter */
  89. 0, /* tp_iternext */
  90. tb_methods, /* tp_methods */
  91. tb_memberlist, /* tp_members */
  92. 0, /* tp_getset */
  93. 0, /* tp_base */
  94. 0, /* tp_dict */
  95. };
  96. static PyTracebackObject *
  97. newtracebackobject(PyTracebackObject *next, PyFrameObject *frame)
  98. {
  99. PyTracebackObject *tb;
  100. if ((next != NULL && !PyTraceBack_Check(next)) ||
  101. frame == NULL || !PyFrame_Check(frame)) {
  102. PyErr_BadInternalCall();
  103. return NULL;
  104. }
  105. tb = PyObject_GC_New(PyTracebackObject, &PyTraceBack_Type);
  106. if (tb != NULL) {
  107. Py_XINCREF(next);
  108. tb->tb_next = next;
  109. Py_XINCREF(frame);
  110. tb->tb_frame = frame;
  111. tb->tb_lasti = frame->f_lasti;
  112. tb->tb_lineno = PyFrame_GetLineNumber(frame);
  113. PyObject_GC_Track(tb);
  114. }
  115. return tb;
  116. }
  117. int
  118. PyTraceBack_Here(PyFrameObject *frame)
  119. {
  120. PyObject *exc, *val, *tb, *newtb;
  121. PyErr_Fetch(&exc, &val, &tb);
  122. newtb = (PyObject *)newtracebackobject((PyTracebackObject *)tb, frame);
  123. if (newtb == NULL) {
  124. _PyErr_ChainExceptions(exc, val, tb);
  125. return -1;
  126. }
  127. PyErr_Restore(exc, val, newtb);
  128. Py_XDECREF(tb);
  129. return 0;
  130. }
  131. /* Insert a frame into the traceback for (funcname, filename, lineno). */
  132. void _PyTraceback_Add(const char *funcname, const char *filename, int lineno)
  133. {
  134. PyObject *globals;
  135. PyCodeObject *code;
  136. PyFrameObject *frame;
  137. PyObject *exc, *val, *tb;
  138. /* Save and clear the current exception. Python functions must not be
  139. called with an exception set. Calling Python functions happens when
  140. the codec of the filesystem encoding is implemented in pure Python. */
  141. PyErr_Fetch(&exc, &val, &tb);
  142. globals = PyDict_New();
  143. if (!globals)
  144. goto error;
  145. code = PyCode_NewEmpty(filename, funcname, lineno);
  146. if (!code) {
  147. Py_DECREF(globals);
  148. goto error;
  149. }
  150. frame = PyFrame_New(PyThreadState_Get(), code, globals, NULL);
  151. Py_DECREF(globals);
  152. Py_DECREF(code);
  153. if (!frame)
  154. goto error;
  155. frame->f_lineno = lineno;
  156. PyErr_Restore(exc, val, tb);
  157. PyTraceBack_Here(frame);
  158. Py_DECREF(frame);
  159. return;
  160. error:
  161. _PyErr_ChainExceptions(exc, val, tb);
  162. }
  163. static PyObject *
  164. _Py_FindSourceFile(PyObject *filename, char* namebuf, size_t namelen, PyObject *io)
  165. {
  166. Py_ssize_t i;
  167. PyObject *binary;
  168. PyObject *v;
  169. Py_ssize_t npath;
  170. size_t taillen;
  171. PyObject *syspath;
  172. PyObject *path;
  173. const char* tail;
  174. PyObject *filebytes;
  175. const char* filepath;
  176. Py_ssize_t len;
  177. PyObject* result;
  178. filebytes = PyUnicode_EncodeFSDefault(filename);
  179. if (filebytes == NULL) {
  180. PyErr_Clear();
  181. return NULL;
  182. }
  183. filepath = PyBytes_AS_STRING(filebytes);
  184. /* Search tail of filename in sys.path before giving up */
  185. tail = strrchr(filepath, SEP);
  186. if (tail == NULL)
  187. tail = filepath;
  188. else
  189. tail++;
  190. taillen = strlen(tail);
  191. syspath = _PySys_GetObjectId(&PyId_path);
  192. if (syspath == NULL || !PyList_Check(syspath))
  193. goto error;
  194. npath = PyList_Size(syspath);
  195. for (i = 0; i < npath; i++) {
  196. v = PyList_GetItem(syspath, i);
  197. if (v == NULL) {
  198. PyErr_Clear();
  199. break;
  200. }
  201. if (!PyUnicode_Check(v))
  202. continue;
  203. path = PyUnicode_EncodeFSDefault(v);
  204. if (path == NULL) {
  205. PyErr_Clear();
  206. continue;
  207. }
  208. len = PyBytes_GET_SIZE(path);
  209. if (len + 1 + (Py_ssize_t)taillen >= (Py_ssize_t)namelen - 1) {
  210. Py_DECREF(path);
  211. continue; /* Too long */
  212. }
  213. strcpy(namebuf, PyBytes_AS_STRING(path));
  214. Py_DECREF(path);
  215. if (strlen(namebuf) != (size_t)len)
  216. continue; /* v contains '\0' */
  217. if (len > 0 && namebuf[len-1] != SEP)
  218. namebuf[len++] = SEP;
  219. strcpy(namebuf+len, tail);
  220. binary = _PyObject_CallMethodId(io, &PyId_open, "ss", namebuf, "rb");
  221. if (binary != NULL) {
  222. result = binary;
  223. goto finally;
  224. }
  225. PyErr_Clear();
  226. }
  227. goto error;
  228. error:
  229. result = NULL;
  230. finally:
  231. Py_DECREF(filebytes);
  232. return result;
  233. }
  234. int
  235. _Py_DisplaySourceLine(PyObject *f, PyObject *filename, int lineno, int indent)
  236. {
  237. int err = 0;
  238. int fd;
  239. int i;
  240. char *found_encoding;
  241. char *encoding;
  242. PyObject *io;
  243. PyObject *binary;
  244. PyObject *fob = NULL;
  245. PyObject *lineobj = NULL;
  246. PyObject *res;
  247. char buf[MAXPATHLEN+1];
  248. int kind;
  249. void *data;
  250. /* open the file */
  251. if (filename == NULL)
  252. return 0;
  253. io = PyImport_ImportModuleNoBlock("io");
  254. if (io == NULL)
  255. return -1;
  256. binary = _PyObject_CallMethodId(io, &PyId_open, "Os", filename, "rb");
  257. if (binary == NULL) {
  258. PyErr_Clear();
  259. binary = _Py_FindSourceFile(filename, buf, sizeof(buf), io);
  260. if (binary == NULL) {
  261. Py_DECREF(io);
  262. return -1;
  263. }
  264. }
  265. /* use the right encoding to decode the file as unicode */
  266. fd = PyObject_AsFileDescriptor(binary);
  267. if (fd < 0) {
  268. Py_DECREF(io);
  269. Py_DECREF(binary);
  270. return 0;
  271. }
  272. found_encoding = PyTokenizer_FindEncodingFilename(fd, filename);
  273. if (found_encoding == NULL)
  274. PyErr_Clear();
  275. encoding = (found_encoding != NULL) ? found_encoding : "utf-8";
  276. /* Reset position */
  277. if (lseek(fd, 0, SEEK_SET) == (off_t)-1) {
  278. Py_DECREF(io);
  279. Py_DECREF(binary);
  280. PyMem_FREE(found_encoding);
  281. return 0;
  282. }
  283. fob = _PyObject_CallMethodId(io, &PyId_TextIOWrapper, "Os", binary, encoding);
  284. Py_DECREF(io);
  285. PyMem_FREE(found_encoding);
  286. if (fob == NULL) {
  287. PyErr_Clear();
  288. res = _PyObject_CallMethodId(binary, &PyId_close, NULL);
  289. Py_DECREF(binary);
  290. if (res)
  291. Py_DECREF(res);
  292. else
  293. PyErr_Clear();
  294. return 0;
  295. }
  296. Py_DECREF(binary);
  297. /* get the line number lineno */
  298. for (i = 0; i < lineno; i++) {
  299. Py_XDECREF(lineobj);
  300. lineobj = PyFile_GetLine(fob, -1);
  301. if (!lineobj) {
  302. PyErr_Clear();
  303. err = -1;
  304. break;
  305. }
  306. }
  307. res = _PyObject_CallMethodId(fob, &PyId_close, NULL);
  308. if (res)
  309. Py_DECREF(res);
  310. else
  311. PyErr_Clear();
  312. Py_DECREF(fob);
  313. if (!lineobj || !PyUnicode_Check(lineobj)) {
  314. Py_XDECREF(lineobj);
  315. return err;
  316. }
  317. /* remove the indentation of the line */
  318. kind = PyUnicode_KIND(lineobj);
  319. data = PyUnicode_DATA(lineobj);
  320. for (i=0; i < PyUnicode_GET_LENGTH(lineobj); i++) {
  321. Py_UCS4 ch = PyUnicode_READ(kind, data, i);
  322. if (ch != ' ' && ch != '\t' && ch != '\014')
  323. break;
  324. }
  325. if (i) {
  326. PyObject *truncated;
  327. truncated = PyUnicode_Substring(lineobj, i, PyUnicode_GET_LENGTH(lineobj));
  328. if (truncated) {
  329. Py_DECREF(lineobj);
  330. lineobj = truncated;
  331. } else {
  332. PyErr_Clear();
  333. }
  334. }
  335. /* Write some spaces before the line */
  336. strcpy(buf, " ");
  337. assert (strlen(buf) == 10);
  338. while (indent > 0) {
  339. if (indent < 10)
  340. buf[indent] = '\0';
  341. err = PyFile_WriteString(buf, f);
  342. if (err != 0)
  343. break;
  344. indent -= 10;
  345. }
  346. /* finally display the line */
  347. if (err == 0)
  348. err = PyFile_WriteObject(lineobj, f, Py_PRINT_RAW);
  349. Py_DECREF(lineobj);
  350. if (err == 0)
  351. err = PyFile_WriteString("\n", f);
  352. return err;
  353. }
  354. static int
  355. tb_displayline(PyObject *f, PyObject *filename, int lineno, PyObject *name)
  356. {
  357. int err;
  358. PyObject *line;
  359. if (filename == NULL || name == NULL)
  360. return -1;
  361. line = PyUnicode_FromFormat(" File \"%U\", line %d, in %U\n",
  362. filename, lineno, name);
  363. if (line == NULL)
  364. return -1;
  365. err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  366. Py_DECREF(line);
  367. if (err != 0)
  368. return err;
  369. /* ignore errors since we can't report them, can we? */
  370. if (_Py_DisplaySourceLine(f, filename, lineno, 4))
  371. PyErr_Clear();
  372. return err;
  373. }
  374. static int
  375. tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit)
  376. {
  377. int err = 0;
  378. long depth = 0;
  379. PyObject *last_file = NULL;
  380. int last_line = -1;
  381. PyObject *last_name = NULL;
  382. long cnt = 0;
  383. PyObject *line;
  384. PyTracebackObject *tb1 = tb;
  385. while (tb1 != NULL) {
  386. depth++;
  387. tb1 = tb1->tb_next;
  388. }
  389. while (tb != NULL && err == 0) {
  390. if (depth <= limit) {
  391. if (last_file != NULL &&
  392. tb->tb_frame->f_code->co_filename == last_file &&
  393. last_line != -1 && tb->tb_lineno == last_line &&
  394. last_name != NULL &&
  395. tb->tb_frame->f_code->co_name == last_name) {
  396. cnt++;
  397. } else {
  398. if (cnt > 3) {
  399. line = PyUnicode_FromFormat(
  400. " [Previous line repeated %d more times]\n", cnt-3);
  401. err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  402. Py_DECREF(line);
  403. }
  404. last_file = tb->tb_frame->f_code->co_filename;
  405. last_line = tb->tb_lineno;
  406. last_name = tb->tb_frame->f_code->co_name;
  407. cnt = 0;
  408. }
  409. if (cnt < 3)
  410. err = tb_displayline(f,
  411. tb->tb_frame->f_code->co_filename,
  412. tb->tb_lineno,
  413. tb->tb_frame->f_code->co_name);
  414. }
  415. depth--;
  416. tb = tb->tb_next;
  417. if (err == 0)
  418. err = PyErr_CheckSignals();
  419. }
  420. if (cnt > 3) {
  421. line = PyUnicode_FromFormat(
  422. " [Previous line repeated %d more times]\n", cnt-3);
  423. err = PyFile_WriteObject(line, f, Py_PRINT_RAW);
  424. Py_DECREF(line);
  425. }
  426. return err;
  427. }
  428. #define PyTraceBack_LIMIT 1000
  429. int
  430. PyTraceBack_Print(PyObject *v, PyObject *f)
  431. {
  432. int err;
  433. PyObject *limitv;
  434. long limit = PyTraceBack_LIMIT;
  435. if (v == NULL)
  436. return 0;
  437. if (!PyTraceBack_Check(v)) {
  438. PyErr_BadInternalCall();
  439. return -1;
  440. }
  441. limitv = PySys_GetObject("tracebacklimit");
  442. if (limitv) {
  443. PyObject *exc_type, *exc_value, *exc_tb;
  444. PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
  445. limit = PyLong_AsLong(limitv);
  446. if (limit == -1 && PyErr_Occurred()) {
  447. if (PyErr_ExceptionMatches(PyExc_OverflowError)) {
  448. limit = PyTraceBack_LIMIT;
  449. }
  450. else {
  451. Py_XDECREF(exc_type);
  452. Py_XDECREF(exc_value);
  453. Py_XDECREF(exc_tb);
  454. return 0;
  455. }
  456. }
  457. else if (limit <= 0) {
  458. limit = PyTraceBack_LIMIT;
  459. }
  460. PyErr_Restore(exc_type, exc_value, exc_tb);
  461. }
  462. err = PyFile_WriteString("Traceback (most recent call last):\n", f);
  463. if (!err)
  464. err = tb_printinternal((PyTracebackObject *)v, f, limit);
  465. return err;
  466. }
  467. /* Reverse a string. For example, "abcd" becomes "dcba".
  468. This function is signal safe. */
  469. void
  470. _Py_DumpDecimal(int fd, unsigned long value)
  471. {
  472. /* maximum number of characters required for output of %lld or %p.
  473. We need at most ceil(log10(256)*SIZEOF_LONG_LONG) digits,
  474. plus 1 for the null byte. 53/22 is an upper bound for log10(256). */
  475. char buffer[1 + (sizeof(unsigned long)*53-1) / 22 + 1];
  476. char *ptr, *end;
  477. end = &buffer[Py_ARRAY_LENGTH(buffer) - 1];
  478. ptr = end;
  479. *ptr = '\0';
  480. do {
  481. --ptr;
  482. assert(ptr >= buffer);
  483. *ptr = '0' + (value % 10);
  484. value /= 10;
  485. } while (value);
  486. _Py_write_noraise(fd, ptr, end - ptr);
  487. }
  488. /* Format an integer in range [0; 0xffffffff] to hexadecimal of 'width' digits,
  489. and write it into the file fd.
  490. This function is signal safe. */
  491. void
  492. _Py_DumpHexadecimal(int fd, unsigned long value, Py_ssize_t width)
  493. {
  494. char buffer[sizeof(unsigned long) * 2 + 1], *ptr, *end;
  495. const Py_ssize_t size = Py_ARRAY_LENGTH(buffer) - 1;
  496. if (width > size)
  497. width = size;
  498. /* it's ok if width is negative */
  499. end = &buffer[size];
  500. ptr = end;
  501. *ptr = '\0';
  502. do {
  503. --ptr;
  504. assert(ptr >= buffer);
  505. *ptr = Py_hexdigits[value & 15];
  506. value >>= 4;
  507. } while ((end - ptr) < width || value);
  508. _Py_write_noraise(fd, ptr, end - ptr);
  509. }
  510. void
  511. _Py_DumpASCII(int fd, PyObject *text)
  512. {
  513. PyASCIIObject *ascii = (PyASCIIObject *)text;
  514. Py_ssize_t i, size;
  515. int truncated;
  516. int kind;
  517. void *data = NULL;
  518. wchar_t *wstr = NULL;
  519. Py_UCS4 ch;
  520. if (!PyUnicode_Check(text))
  521. return;
  522. size = ascii->length;
  523. kind = ascii->state.kind;
  524. if (kind == PyUnicode_WCHAR_KIND) {
  525. wstr = ((PyASCIIObject *)text)->wstr;
  526. if (wstr == NULL)
  527. return;
  528. size = ((PyCompactUnicodeObject *)text)->wstr_length;
  529. }
  530. else if (ascii->state.compact) {
  531. if (ascii->state.ascii)
  532. data = ((PyASCIIObject*)text) + 1;
  533. else
  534. data = ((PyCompactUnicodeObject*)text) + 1;
  535. }
  536. else {
  537. data = ((PyUnicodeObject *)text)->data.any;
  538. if (data == NULL)
  539. return;
  540. }
  541. if (MAX_STRING_LENGTH < size) {
  542. size = MAX_STRING_LENGTH;
  543. truncated = 1;
  544. }
  545. else {
  546. truncated = 0;
  547. }
  548. for (i=0; i < size; i++) {
  549. if (kind != PyUnicode_WCHAR_KIND)
  550. ch = PyUnicode_READ(kind, data, i);
  551. else
  552. ch = wstr[i];
  553. if (' ' <= ch && ch <= 126) {
  554. /* printable ASCII character */
  555. char c = (char)ch;
  556. _Py_write_noraise(fd, &c, 1);
  557. }
  558. else if (ch <= 0xff) {
  559. PUTS(fd, "\\x");
  560. _Py_DumpHexadecimal(fd, ch, 2);
  561. }
  562. else if (ch <= 0xffff) {
  563. PUTS(fd, "\\u");
  564. _Py_DumpHexadecimal(fd, ch, 4);
  565. }
  566. else {
  567. PUTS(fd, "\\U");
  568. _Py_DumpHexadecimal(fd, ch, 8);
  569. }
  570. }
  571. if (truncated) {
  572. PUTS(fd, "...");
  573. }
  574. }
  575. /* Write a frame into the file fd: "File "xxx", line xxx in xxx".
  576. This function is signal safe. */
  577. static void
  578. dump_frame(int fd, PyFrameObject *frame)
  579. {
  580. PyCodeObject *code;
  581. int lineno;
  582. code = frame->f_code;
  583. PUTS(fd, " File ");
  584. if (code != NULL && code->co_filename != NULL
  585. && PyUnicode_Check(code->co_filename))
  586. {
  587. PUTS(fd, "\"");
  588. _Py_DumpASCII(fd, code->co_filename);
  589. PUTS(fd, "\"");
  590. } else {
  591. PUTS(fd, "???");
  592. }
  593. /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */
  594. lineno = PyCode_Addr2Line(code, frame->f_lasti);
  595. PUTS(fd, ", line ");
  596. if (lineno >= 0) {
  597. _Py_DumpDecimal(fd, (unsigned long)lineno);
  598. }
  599. else {
  600. PUTS(fd, "???");
  601. }
  602. PUTS(fd, " in ");
  603. if (code != NULL && code->co_name != NULL
  604. && PyUnicode_Check(code->co_name)) {
  605. _Py_DumpASCII(fd, code->co_name);
  606. }
  607. else {
  608. PUTS(fd, "???");
  609. }
  610. PUTS(fd, "\n");
  611. }
  612. static void
  613. dump_traceback(int fd, PyThreadState *tstate, int write_header)
  614. {
  615. PyFrameObject *frame;
  616. unsigned int depth;
  617. if (write_header)
  618. PUTS(fd, "Stack (most recent call first):\n");
  619. frame = _PyThreadState_GetFrame(tstate);
  620. if (frame == NULL)
  621. return;
  622. depth = 0;
  623. while (frame != NULL) {
  624. if (MAX_FRAME_DEPTH <= depth) {
  625. PUTS(fd, " ...\n");
  626. break;
  627. }
  628. if (!PyFrame_Check(frame))
  629. break;
  630. dump_frame(fd, frame);
  631. frame = frame->f_back;
  632. depth++;
  633. }
  634. }
  635. /* Dump the traceback of a Python thread into fd. Use write() to write the
  636. traceback and retry if write() is interrupted by a signal (failed with
  637. EINTR), but don't call the Python signal handler.
  638. The caller is responsible to call PyErr_CheckSignals() to call Python signal
  639. handlers if signals were received. */
  640. void
  641. _Py_DumpTraceback(int fd, PyThreadState *tstate)
  642. {
  643. dump_traceback(fd, tstate, 1);
  644. }
  645. /* Write the thread identifier into the file 'fd': "Current thread 0xHHHH:\" if
  646. is_current is true, "Thread 0xHHHH:\n" otherwise.
  647. This function is signal safe. */
  648. static void
  649. write_thread_id(int fd, PyThreadState *tstate, int is_current)
  650. {
  651. if (is_current)
  652. PUTS(fd, "Current thread 0x");
  653. else
  654. PUTS(fd, "Thread 0x");
  655. _Py_DumpHexadecimal(fd,
  656. tstate->thread_id,
  657. sizeof(unsigned long) * 2);
  658. PUTS(fd, " (most recent call first):\n");
  659. }
  660. /* Dump the traceback of all Python threads into fd. Use write() to write the
  661. traceback and retry if write() is interrupted by a signal (failed with
  662. EINTR), but don't call the Python signal handler.
  663. The caller is responsible to call PyErr_CheckSignals() to call Python signal
  664. handlers if signals were received. */
  665. const char*
  666. _Py_DumpTracebackThreads(int fd, PyInterpreterState *interp,
  667. PyThreadState *current_tstate)
  668. {
  669. PyThreadState *tstate;
  670. unsigned int nthreads;
  671. if (current_tstate == NULL) {
  672. /* _Py_DumpTracebackThreads() is called from signal handlers by
  673. faulthandler.
  674. SIGSEGV, SIGFPE, SIGABRT, SIGBUS and SIGILL are synchronous signals
  675. and are thus delivered to the thread that caused the fault. Get the
  676. Python thread state of the current thread.
  677. PyThreadState_Get() doesn't give the state of the thread that caused
  678. the fault if the thread released the GIL, and so this function
  679. cannot be used. Read the thread local storage (TLS) instead: call
  680. PyGILState_GetThisThreadState(). */
  681. current_tstate = PyGILState_GetThisThreadState();
  682. }
  683. if (interp == NULL) {
  684. if (current_tstate == NULL) {
  685. interp = _PyGILState_GetInterpreterStateUnsafe();
  686. if (interp == NULL) {
  687. /* We need the interpreter state to get Python threads */
  688. return "unable to get the interpreter state";
  689. }
  690. }
  691. else {
  692. interp = current_tstate->interp;
  693. }
  694. }
  695. assert(interp != NULL);
  696. /* Get the current interpreter from the current thread */
  697. tstate = PyInterpreterState_ThreadHead(interp);
  698. if (tstate == NULL)
  699. return "unable to get the thread head state";
  700. /* Dump the traceback of each thread */
  701. tstate = PyInterpreterState_ThreadHead(interp);
  702. nthreads = 0;
  703. _Py_BEGIN_SUPPRESS_IPH
  704. do
  705. {
  706. if (nthreads != 0)
  707. PUTS(fd, "\n");
  708. if (nthreads >= MAX_NTHREADS) {
  709. PUTS(fd, "...\n");
  710. break;
  711. }
  712. write_thread_id(fd, tstate, tstate == current_tstate);
  713. dump_traceback(fd, tstate, 0);
  714. tstate = PyThreadState_Next(tstate);
  715. nthreads++;
  716. } while (tstate != NULL);
  717. _Py_END_SUPPRESS_IPH
  718. return NULL;
  719. }