Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 62c1e3c

Browse files
committed
Make sure to propogate errors that arise when profiling data cannot be
written to the log file, and turn off the profiler. This closes SF bug #483925.
1 parent bebfe03 commit 62c1e3c

1 file changed

Lines changed: 112 additions & 71 deletions

File tree

Modules/_hotshot.c

Lines changed: 112 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ logreader_next(LogReaderObject *self, PyObject *args)
590590
return result;
591591
}
592592

593+
static void
594+
do_stop(ProfilerObject *self);
593595

594596
static int
595597
flush_data(ProfilerObject *self)
@@ -605,20 +607,22 @@ flush_data(ProfilerObject *self)
605607
if (written == 0) {
606608
char *s = PyString_AsString(self->logfilename);
607609
PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
610+
do_stop(self);
608611
return -1;
609612
}
610613
}
611614
if (written > 0) {
612615
if (fflush(self->logfp)) {
613616
char *s = PyString_AsString(self->logfilename);
614617
PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
618+
do_stop(self);
615619
return -1;
616620
}
617621
}
618622
return 0;
619623
}
620624

621-
static inline void
625+
static inline int
622626
pack_packed_int(ProfilerObject *self, int value)
623627
{
624628
unsigned char partial;
@@ -631,13 +635,14 @@ pack_packed_int(ProfilerObject *self, int value)
631635
self->buffer[self->index] = partial;
632636
self->index++;
633637
} while (value);
638+
return 0;
634639
}
635640

636641
/* Encode a modified packed integer, with a subfield of modsize bits
637642
* containing the value "subfield". The value of subfield is not
638643
* checked to ensure it actually fits in modsize bits.
639644
*/
640-
static inline void
645+
static inline int
641646
pack_modified_packed_int(ProfilerObject *self, int value,
642647
int modsize, int subfield)
643648
{
@@ -651,125 +656,154 @@ pack_modified_packed_int(ProfilerObject *self, int value,
651656
b |= 0x80;
652657
self->buffer[self->index] = b;
653658
self->index++;
654-
pack_packed_int(self, value >> bits);
655-
}
656-
else {
657-
self->buffer[self->index] = b;
658-
self->index++;
659+
return pack_packed_int(self, value >> bits);
659660
}
661+
self->buffer[self->index] = b;
662+
self->index++;
663+
return 0;
660664
}
661665

662-
static void
666+
static int
663667
pack_string(ProfilerObject *self, const char *s, int len)
664668
{
665-
if (len + PISIZE + self->index >= BUFFERSIZE)
666-
(void) flush_data(self);
667-
pack_packed_int(self, len);
669+
if (len + PISIZE + self->index >= BUFFERSIZE) {
670+
if (flush_data(self) < 0)
671+
return -1;
672+
}
673+
if (pack_packed_int(self, len) < 0)
674+
return -1;
668675
memcpy(self->buffer + self->index, s, len);
669676
self->index += len;
677+
return 0;
670678
}
671679

672-
static void
680+
static int
673681
pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
674682
{
675683
int len1 = strlen(s1);
676684
int len2 = strlen(s2);
677685

678-
if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
679-
(void) flush_data(self);
686+
if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
687+
if (flush_data(self) < 0)
688+
return -1;
689+
}
680690
self->buffer[self->index] = WHAT_ADD_INFO;
681691
self->index++;
682-
pack_string(self, s1, len1);
683-
pack_string(self, s2, len2);
692+
if (pack_string(self, s1, len1) < 0)
693+
return -1;
694+
return pack_string(self, s2, len2);
684695
}
685696

686-
static void
697+
static int
687698
pack_define_file(ProfilerObject *self, int fileno, const char *filename)
688699
{
689700
int len = strlen(filename);
690701

691-
if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
692-
(void) flush_data(self);
702+
if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
703+
if (flush_data(self) < 0)
704+
return -1;
705+
}
693706
self->buffer[self->index] = WHAT_DEFINE_FILE;
694707
self->index++;
695-
pack_packed_int(self, fileno);
696-
pack_string(self, filename, len);
708+
if (pack_packed_int(self, fileno) < 0)
709+
return -1;
710+
return pack_string(self, filename, len);
697711
}
698712

699-
static void
713+
static int
700714
pack_define_func(ProfilerObject *self, int fileno, int lineno,
701715
const char *funcname)
702716
{
703717
int len = strlen(funcname);
704718

705-
if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE)
706-
(void) flush_data(self);
719+
if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
720+
if (flush_data(self) < 0)
721+
return -1;
722+
}
707723
self->buffer[self->index] = WHAT_DEFINE_FUNC;
708724
self->index++;
709-
pack_packed_int(self, fileno);
710-
pack_packed_int(self, lineno);
711-
pack_string(self, funcname, len);
725+
if (pack_packed_int(self, fileno) < 0)
726+
return -1;
727+
if (pack_packed_int(self, lineno) < 0)
728+
return -1;
729+
return pack_string(self, funcname, len);
712730
}
713731

714-
static void
732+
static int
715733
pack_line_times(ProfilerObject *self)
716734
{
717-
if (2 + self->index >= BUFFERSIZE)
718-
(void) flush_data(self);
735+
if (2 + self->index >= BUFFERSIZE) {
736+
if (flush_data(self) < 0)
737+
return -1;
738+
}
719739
self->buffer[self->index] = WHAT_LINE_TIMES;
720740
self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
721741
self->index += 2;
742+
return 0;
722743
}
723744

724-
static void
745+
static int
725746
pack_frame_times(ProfilerObject *self)
726747
{
727-
if (2 + self->index >= BUFFERSIZE)
728-
(void) flush_data(self);
748+
if (2 + self->index >= BUFFERSIZE) {
749+
if (flush_data(self) < 0)
750+
return -1;
751+
}
729752
self->buffer[self->index] = WHAT_FRAME_TIMES;
730753
self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
731754
self->index += 2;
755+
return 0;
732756
}
733757

734-
static inline void
758+
static inline int
735759
pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
736760
{
737-
if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE)
738-
(void) flush_data(self);
761+
if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
762+
if (flush_data(self) < 0)
763+
return -1;
764+
}
739765
pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
740766
pack_packed_int(self, lineno);
741767
if (self->frametimings)
742-
pack_packed_int(self, tdelta);
768+
return pack_packed_int(self, tdelta);
769+
else
770+
return 0;
743771
}
744772

745-
static inline void
773+
static inline int
746774
pack_exit(ProfilerObject *self, int tdelta)
747775
{
748-
if (MPISIZE + self->index >= BUFFERSIZE)
749-
(void) flush_data(self);
750-
if (self->frametimings)
751-
pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
752-
else {
753-
self->buffer[self->index] = WHAT_EXIT;
754-
self->index++;
776+
if (MPISIZE + self->index >= BUFFERSIZE) {
777+
if (flush_data(self) < 0)
778+
return -1;
755779
}
780+
if (self->frametimings)
781+
return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
782+
self->buffer[self->index] = WHAT_EXIT;
783+
self->index++;
784+
return 0;
756785
}
757786

758-
static inline void
787+
static inline int
759788
pack_lineno(ProfilerObject *self, int lineno)
760789
{
761-
if (MPISIZE + self->index >= BUFFERSIZE)
762-
(void) flush_data(self);
763-
pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
790+
if (MPISIZE + self->index >= BUFFERSIZE) {
791+
if (flush_data(self) < 0)
792+
return -1;
793+
}
794+
return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
764795
}
765796

766-
static inline void
797+
static inline int
767798
pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
768799
{
769-
if (MPISIZE + PISIZE + self->index >= BUFFERSIZE)
770-
(void) flush_data(self);
771-
pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
772-
pack_packed_int(self, tdelta);
800+
if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
801+
if (flush_data(self) < 0)
802+
return 0;
803+
}
804+
if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
805+
return -1;
806+
return pack_packed_int(self, tdelta);
773807
}
774808

775809
static inline int
@@ -799,7 +833,9 @@ get_fileno(ProfilerObject *self, PyCodeObject *fcode)
799833
}
800834
self->next_fileno++;
801835
Py_DECREF(obj);
802-
pack_define_file(self, fileno, PyString_AS_STRING(fcode->co_filename));
836+
if (pack_define_file(self, fileno,
837+
PyString_AS_STRING(fcode->co_filename)) < 0)
838+
return -1;
803839
}
804840
else {
805841
/* already know this ID */
@@ -815,8 +851,9 @@ get_fileno(ProfilerObject *self, PyCodeObject *fcode)
815851
else {
816852
PyObject *name = PyDict_GetItem(dict, obj);
817853
if (name == NULL) {
818-
pack_define_func(self, fileno, fcode->co_firstlineno,
819-
PyString_AS_STRING(fcode->co_name));
854+
if (pack_define_func(self, fileno, fcode->co_firstlineno,
855+
PyString_AS_STRING(fcode->co_name)) < 0)
856+
return -1;
820857
if (PyDict_SetItem(dict, obj, fcode->co_name))
821858
return -1;
822859
}
@@ -867,11 +904,13 @@ profiler_callback(ProfilerObject *self, PyFrameObject *frame, int what,
867904
fileno = get_fileno(self, frame->f_code);
868905
if (fileno < 0)
869906
return -1;
870-
pack_enter(self, fileno, tdelta,
871-
frame->f_code->co_firstlineno);
907+
if (pack_enter(self, fileno, tdelta,
908+
frame->f_code->co_firstlineno) < 0)
909+
return -1;
872910
break;
873911
case PyTrace_RETURN:
874-
pack_exit(self, tdelta);
912+
if (pack_exit(self, tdelta) < 0)
913+
return -1;
875914
break;
876915
default:
877916
/* should never get here */
@@ -894,18 +933,19 @@ tracer_callback(ProfilerObject *self, PyFrameObject *frame, int what,
894933
fileno = get_fileno(self, frame->f_code);
895934
if (fileno < 0)
896935
return -1;
897-
pack_enter(self, fileno, self->frametimings ? get_tdelta(self) : -1,
898-
frame->f_code->co_firstlineno);
899-
break;
936+
return pack_enter(self, fileno,
937+
self->frametimings ? get_tdelta(self) : -1,
938+
frame->f_code->co_firstlineno);
939+
900940
case PyTrace_RETURN:
901-
pack_exit(self, get_tdelta(self));
902-
break;
941+
return pack_exit(self, get_tdelta(self));
942+
903943
case PyTrace_LINE:
904944
if (self->linetimings)
905-
pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
945+
return pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
906946
else
907-
pack_lineno(self, frame->f_lineno);
908-
break;
947+
return pack_lineno(self, frame->f_lineno);
948+
909949
default:
910950
/* ignore PyTrace_EXCEPTION */
911951
break;
@@ -1042,9 +1082,10 @@ profiler_addinfo(ProfilerObject *self, PyObject *args)
10421082
if (self->logfp == NULL)
10431083
PyErr_SetString(ProfilerError, "profiler already closed");
10441084
else {
1045-
pack_add_info(self, key, value);
1046-
result = Py_None;
1047-
Py_INCREF(result);
1085+
if (pack_add_info(self, key, value) == 0) {
1086+
result = Py_None;
1087+
Py_INCREF(result);
1088+
}
10481089
}
10491090
}
10501091
return result;

0 commit comments

Comments
 (0)