| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 5e8c691 commit 8852ad4
9 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -33,10 +33,11 @@ Reflection | |||
| 33 | 33 | ||
| 34 | 34 | .. c:function:: int PyFrame_GetCode(PyFrameObject *frame) | |
| 35 | 35 | ||
| 36 | - Return a borrowed reference to the *frame* code. | ||
| 37 | - The frame code cannot be ``NULL``. | ||
| 36 | + Get the *frame* code. | ||
| 38 | 37 | ||
| 39 | - *frame* must not be ``NULL``. | ||
| 38 | + Return a strong reference. | ||
| 39 | + | ||
| 40 | + *frame* must not be ``NULL``. The result (frame code) cannot be ``NULL``. | ||
| 40 | 41 | ||
| 41 | 42 | .. versionadded:: 3.9 | |
| 42 | 43 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -537,8 +537,7 @@ Optimizations | |||
| 537 | 537 | Build and C API Changes | |
| 538 | 538 | ======================= | |
| 539 | 539 | ||
| 540 | - * New :c:func:`PyFrame_GetCode` function: return a borrowed reference to the | ||
| 541 | - frame code. | ||
| 540 | + * New :c:func:`PyFrame_GetCode` function: get a frame code. | ||
| 542 | 541 | (Contributed by Victor Stinner in :issue:`40421`.) | |
| 543 | 542 | ||
| 544 | 543 | * Add :c:func:`PyFrame_GetLineNumber` to the limited C API. | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -390,14 +390,19 @@ profiler_callback(PyObject *self, PyFrameObject *frame, int what, | |||
| 390 | 390 | { | |
| 391 | 391 | PyCodeObject *code = PyFrame_GetCode(frame); | |
| 392 | 392 | ptrace_enter_call(self, (void *)code, (PyObject *)code); | |
| 393 | + Py_DECREF(code); | ||
| 393 | 394 | break; | |
| 394 | 395 | } | |
| 395 | 396 | ||
| 396 | 397 | /* the 'frame' of a called function is about to finish | |
| 397 | 398 | (either normally or with an exception) */ | |
| 398 | 399 | case PyTrace_RETURN: | |
| 399 | - ptrace_leave_call(self, (void *)PyFrame_GetCode(frame)); | ||
| 400 | + { | ||
| 401 | + PyCodeObject *code = PyFrame_GetCode(frame); | ||
| 402 | + ptrace_leave_call(self, (void *)code); | ||
| 403 | + Py_DECREF(code); | ||
| 400 | 404 | break; | |
| 405 | + } | ||
| 401 | 406 | ||
| 402 | 407 | /* case PyTrace_EXCEPTION: | |
| 403 | 408 | If the exception results in the function exiting, a | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -335,26 +335,24 @@ hashtable_compare_traceback(_Py_hashtable_t *ht, const void *pkey, | |||
| 335 | 335 | static void | |
| 336 | 336 | tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame) | |
| 337 | 337 | { | |
| 338 | - PyCodeObject *code; | ||
| 339 | - PyObject *filename; | ||
| 340 | - _Py_hashtable_entry_t *entry; | ||
| 341 | - int lineno; | ||
| 342 | - | ||
| 343 | 338 | frame->filename = unknown_filename; | |
| 344 | - lineno = PyFrame_GetLineNumber(pyframe); | ||
| 345 | - if (lineno < 0) | ||
| 339 | + int lineno = PyFrame_GetLineNumber(pyframe); | ||
| 340 | + if (lineno < 0) { | ||
| 346 | 341 | lineno = 0; | |
| 342 | + } | ||
| 347 | 343 | frame->lineno = (unsigned int)lineno; | |
| 348 | 344 | ||
| 349 | - code = PyFrame_GetCode(pyframe); | ||
| 350 | - if (code->co_filename == NULL) { | ||
| 345 | + PyCodeObject *code = PyFrame_GetCode(pyframe); | ||
| 346 | + PyObject *filename = code->co_filename; | ||
| 347 | + Py_DECREF(code); | ||
| 348 | + | ||
| 349 | + if (filename == NULL) { | ||
| 351 | 350 | #ifdef TRACE_DEBUG | |
| 352 | 351 | tracemalloc_error("failed to get the filename of the code object"); | |
| 353 | 352 | #endif | |
| 354 | 353 | return; | |
| 355 | 354 | } | |
| 356 | 355 | ||
| 357 | - filename = code->co_filename; | ||
| 358 | 356 | assert(filename != NULL); | |
| 359 | 357 | if (filename == NULL) | |
| 360 | 358 | return; | |
@@ -375,6 +373,7 @@ tracemalloc_get_frame(PyFrameObject *pyframe, frame_t *frame) | |||
| 375 | 373 | } | |
| 376 | 374 | ||
| 377 | 375 | /* intern the filename */ | |
| 376 | + _Py_hashtable_entry_t *entry; | ||
| 378 | 377 | entry = _Py_HASHTABLE_GET_ENTRY(tracemalloc_filenames, filename); | |
| 379 | 378 | if (entry != NULL) { | |
| 380 | 379 | _Py_HASHTABLE_ENTRY_READ_KEY(tracemalloc_filenames, entry, filename); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1237,5 +1237,6 @@ PyFrame_GetCode(PyFrameObject *frame) | |||
| 1237 | 1237 | assert(frame != NULL); | |
| 1238 | 1238 | PyCodeObject *code = frame->f_code; | |
| 1239 | 1239 | assert(code != NULL); | |
| 1240 | + Py_INCREF(code); | ||
| 1240 | 1241 | return code; | |
| 1241 | 1242 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -8031,15 +8031,15 @@ super_init(PyObject *self, PyObject *args, PyObject *kwds) | |||
| 8031 | 8031 | /* Call super(), without args -- fill in from __class__ | |
| 8032 | 8032 | and first local variable on the stack. */ | |
| 8033 | 8033 | PyFrameObject *f; | |
| 8034 | - PyCodeObject *co; | ||
| 8035 | 8034 | Py_ssize_t i, n; | |
| 8036 | 8035 | f = PyThreadState_GetFrame(_PyThreadState_GET()); | |
| 8037 | 8036 | if (f == NULL) { | |
| 8038 | 8037 | PyErr_SetString(PyExc_RuntimeError, | |
| 8039 | 8038 | "super(): no current frame"); | |
| 8040 | 8039 | return -1; | |
| 8041 | 8040 | } | |
| 8042 | - co = PyFrame_GetCode(f); | ||
| 8041 | + PyCodeObject *co = PyFrame_GetCode(f); | ||
| 8042 | + Py_DECREF(co); // use a borrowed reference | ||
| 8043 | 8043 | if (co->co_argcount == 0) { | |
| 8044 | 8044 | PyErr_SetString(PyExc_RuntimeError, | |
| 8045 | 8045 | "super(): no arguments"); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -785,6 +785,8 @@ is_internal_frame(PyFrameObject *frame) | |||
| 785 | 785 | ||
| 786 | 786 | PyCodeObject *code = PyFrame_GetCode(frame); | |
| 787 | 787 | PyObject *filename = code->co_filename; | |
| 788 | + Py_DECREF(code); | ||
| 789 | + | ||
| 788 | 790 | if (filename == NULL) { | |
| 789 | 791 | return 0; | |
| 790 | 792 | } | |
@@ -850,7 +852,9 @@ setup_context(Py_ssize_t stack_level, PyObject **filename, int *lineno, | |||
| 850 | 852 | } | |
| 851 | 853 | else { | |
| 852 | 854 | globals = f->f_globals; | |
| 853 | - *filename = PyFrame_GetCode(f)->co_filename; | ||
| 855 | + PyCodeObject *code = PyFrame_GetCode(f); | ||
| 856 | + *filename = code->co_filename; | ||
| 857 | + Py_DECREF(code); | ||
| 854 | 858 | Py_INCREF(*filename); | |
| 855 | 859 | *lineno = PyFrame_GetLineNumber(f); | |
| 856 | 860 | } | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1557,6 +1557,7 @@ remove_importlib_frames(PyThreadState *tstate) | |||
| 1557 | 1557 | else { | |
| 1558 | 1558 | prev_link = (PyObject **) &traceback->tb_next; | |
| 1559 | 1559 | } | |
| 1560 | + Py_DECREF(code); | ||
| 1560 | 1561 | tb = next; | |
| 1561 | 1562 | } | |
| 1562 | 1563 | done: | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -581,6 +581,7 @@ tb_printinternal(PyTracebackObject *tb, PyObject *f, long limit) | |||
| 581 | 581 | err = PyErr_CheckSignals(); | |
| 582 | 582 | } | |
| 583 | 583 | } | |
| 584 | + Py_DECREF(code); | ||
| 584 | 585 | tb = tb->tb_next; | |
| 585 | 586 | } | |
| 586 | 587 | if (err == 0 && cnt > TB_RECURSIVE_CUTOFF) { | |
@@ -752,12 +753,9 @@ _Py_DumpASCII(int fd, PyObject *text) | |||
| 752 | 753 | static void | |
| 753 | 754 | dump_frame(int fd, PyFrameObject *frame) | |
| 754 | 755 | { | |
| 755 | - PyCodeObject *code; | ||
| 756 | - int lineno; | ||
| 757 | - | ||
| 758 | - code = PyFrame_GetCode(frame); | ||
| 756 | + PyCodeObject *code = PyFrame_GetCode(frame); | ||
| 759 | 757 | PUTS(fd, " File "); | |
| 760 | - if (code != NULL && code->co_filename != NULL | ||
| 758 | + if (code->co_filename != NULL | ||
| 761 | 759 | && PyUnicode_Check(code->co_filename)) | |
| 762 | 760 | { | |
| 763 | 761 | PUTS(fd, "\""); | |
@@ -768,7 +766,7 @@ dump_frame(int fd, PyFrameObject *frame) | |||
| 768 | 766 | } | |
| 769 | 767 | ||
| 770 | 768 | /* PyFrame_GetLineNumber() was introduced in Python 2.7.0 and 3.2.0 */ | |
| 771 | - lineno = PyCode_Addr2Line(code, frame->f_lasti); | ||
| 769 | + int lineno = PyCode_Addr2Line(code, frame->f_lasti); | ||
| 772 | 770 | PUTS(fd, ", line "); | |
| 773 | 771 | if (lineno >= 0) { | |
| 774 | 772 | _Py_DumpDecimal(fd, (unsigned long)lineno); | |
@@ -778,7 +776,7 @@ dump_frame(int fd, PyFrameObject *frame) | |||
| 778 | 776 | } | |
| 779 | 777 | PUTS(fd, " in "); | |
| 780 | 778 | ||
| 781 | - if (code != NULL && code->co_name != NULL | ||
| 779 | + if (code->co_name != NULL | ||
| 782 | 780 | && PyUnicode_Check(code->co_name)) { | |
| 783 | 781 | _Py_DumpASCII(fd, code->co_name); | |
| 784 | 782 | } | |
@@ -787,6 +785,7 @@ dump_frame(int fd, PyFrameObject *frame) | |||
| 787 | 785 | } | |
| 788 | 786 | ||
| 789 | 787 | PUTS(fd, "\n"); | |
| 788 | + Py_DECREF(code); | ||
| 790 | 789 | } | |
| 791 | 790 | ||
| 792 | 791 | static void | |
| Back | FazBrowse Home | New Git URL |
0 commit comments