@@ -288,6 +288,17 @@ CTracer_set_pdata_stack(CTracer *self)
288
288
return ret ;
289
289
}
290
290
291
+ // Thanks for the idea, memray!
292
+ inline PyCodeObject *
293
+ MyFrame_BorrowCode (PyFrameObject * frame )
294
+ {
295
+ // Return a borrowed reference.
296
+ PyCodeObject * pCode = PyFrame_GetCode (frame );
297
+ assert (Py_REFCNT (pCode ) >= 2 );
298
+ Py_DECREF (pCode );
299
+ return pCode ;
300
+ }
301
+
291
302
/*
292
303
* Parts of the trace function.
293
304
*/
@@ -359,7 +370,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
359
370
}
360
371
361
372
/* Check if we should trace this line. */
362
- filename = PyFrame_GetCode (frame )-> co_filename ;
373
+ filename = MyFrame_BorrowCode (frame )-> co_filename ;
363
374
disposition = PyDict_GetItem (self -> should_trace_cache , filename );
364
375
if (disposition == NULL ) {
365
376
if (PyErr_Occurred ()) {
@@ -554,15 +565,15 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
554
565
* The current opcode is guaranteed to be RESUME. The argument
555
566
* determines what kind of resume it is.
556
567
*/
557
- pCode = MyCode_GetCode (PyFrame_GetCode (frame ));
568
+ pCode = MyCode_GetCode (MyFrame_BorrowCode (frame ));
558
569
real_call = (PyBytes_AS_STRING (pCode )[MyFrame_GetLasti (frame ) + 1 ] == 0 );
559
570
#else
560
571
// f_lasti is -1 for a true call, and a real byte offset for a generator re-entry.
561
572
real_call = (MyFrame_GetLasti (frame ) < 0 );
562
573
#endif
563
574
564
575
if (real_call ) {
565
- self -> pcur_entry -> last_line = - PyFrame_GetCode (frame )-> co_firstlineno ;
576
+ self -> pcur_entry -> last_line = - MyFrame_BorrowCode (frame )-> co_firstlineno ;
566
577
}
567
578
else {
568
579
self -> pcur_entry -> last_line = PyFrame_GetLineNumber (frame );
@@ -649,7 +660,7 @@ CTracer_handle_line(CTracer *self, PyFrameObject *frame)
649
660
650
661
STATS ( self -> stats .lines ++ ; )
651
662
if (self -> pdata_stack -> depth >= 0 ) {
652
- SHOWLOG (PyFrame_GetLineNumber (frame ), PyFrame_GetCode (frame )-> co_filename , "line" );
663
+ SHOWLOG (PyFrame_GetLineNumber (frame ), MyFrame_BorrowCode (frame )-> co_filename , "line" );
653
664
if (self -> pcur_entry -> file_data ) {
654
665
int lineno_from = -1 ;
655
666
int lineno_to = -1 ;
@@ -727,7 +738,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
727
738
self -> pcur_entry = & self -> pdata_stack -> stack [self -> pdata_stack -> depth ];
728
739
if (self -> tracing_arcs && self -> pcur_entry -> file_data ) {
729
740
BOOL real_return = FALSE;
730
- pCode = MyCode_GetCode (PyFrame_GetCode (frame ));
741
+ pCode = MyCode_GetCode (MyFrame_BorrowCode (frame ));
731
742
int lasti = MyFrame_GetLasti (frame );
732
743
Py_ssize_t code_size = PyBytes_GET_SIZE (pCode );
733
744
unsigned char * code_bytes = (unsigned char * )PyBytes_AS_STRING (pCode );
@@ -759,7 +770,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
759
770
real_return = !(is_yield || is_yield_from );
760
771
#endif
761
772
if (real_return ) {
762
- int first = PyFrame_GetCode (frame )-> co_firstlineno ;
773
+ int first = MyFrame_BorrowCode (frame )-> co_firstlineno ;
763
774
if (CTracer_record_pair (self , self -> pcur_entry -> last_line , - first ) < 0 ) {
764
775
goto error ;
765
776
}
@@ -782,7 +793,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
782
793
}
783
794
784
795
/* Pop the stack. */
785
- SHOWLOG (PyFrame_GetLineNumber (frame ), PyFrame_GetCode (frame )-> co_filename , "return" );
796
+ SHOWLOG (PyFrame_GetLineNumber (frame ), MyFrame_BorrowCode (frame )-> co_filename , "return" );
786
797
self -> pdata_stack -> depth -- ;
787
798
self -> pcur_entry = & self -> pdata_stack -> stack [self -> pdata_stack -> depth ];
788
799
}
@@ -824,13 +835,13 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse
824
835
if (what <= (int )(sizeof (what_sym )/sizeof (const char * ))) {
825
836
w = what_sym [what ];
826
837
}
827
- ascii = PyUnicode_AsASCIIString (PyFrame_GetCode (frame )-> co_filename );
838
+ ascii = PyUnicode_AsASCIIString (MyFrame_BorrowCode (frame )-> co_filename );
828
839
printf ("%x trace: f:%x %s @ %s %d\n" , (int )self , (int )frame , what_sym [what ], PyBytes_AS_STRING (ascii ), PyFrame_GetLineNumber (frame ));
829
840
Py_DECREF (ascii );
830
841
#endif
831
842
832
843
#if TRACE_LOG
833
- ascii = PyUnicode_AsASCIIString (PyFrame_GetCode (frame )-> co_filename );
844
+ ascii = PyUnicode_AsASCIIString (MyFrame_BorrowCode (frame )-> co_filename );
834
845
if (strstr (PyBytes_AS_STRING (ascii ), start_file ) && PyFrame_GetLineNumber (frame ) == start_line ) {
835
846
logging = TRUE;
836
847
}
@@ -926,7 +937,7 @@ CTracer_call(CTracer *self, PyObject *args, PyObject *kwds)
926
937
}
927
938
928
939
#if WHAT_LOG
929
- ascii = PyUnicode_AsASCIIString (PyFrame_GetCode (frame )-> co_filename );
940
+ ascii = PyUnicode_AsASCIIString (MyFrame_BorrowCode (frame )-> co_filename );
930
941
printf ("pytrace: %s @ %s %d\n" , what_sym [what ], PyBytes_AS_STRING (ascii ), PyFrame_GetLineNumber (frame ));
931
942
Py_DECREF (ascii );
932
943
#endif
0 commit comments