[Python-checkins] CVS: python/dist/src/Modules _hotshot.c,1.10,1.11

Fred L. Drake fdrake@users.sourceforge.net
Tue, 04 Dec 2001 13:40:55 -0800


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv9251

Modified Files:
	_hotshot.c 
Log Message:
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.


Index: _hotshot.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_hotshot.c,v
retrieving revision 1.10
retrieving revision 1.11
diff -C2 -d -r1.10 -r1.11
*** _hotshot.c	2001/11/28 20:27:42	1.10
--- _hotshot.c	2001/12/04 21:40:53	1.11
***************
*** 591,594 ****
--- 591,596 ----
  }
  
+ static void
+ do_stop(ProfilerObject *self);
  
  static int
***************
*** 606,609 ****
--- 608,612 ----
              char *s = PyString_AsString(self->logfilename);
              PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
+             do_stop(self);
              return -1;
          }
***************
*** 613,616 ****
--- 616,620 ----
              char *s = PyString_AsString(self->logfilename);
              PyErr_SetFromErrnoWithFilename(PyExc_IOError, s);
+             do_stop(self);
              return -1;
          }
***************
*** 619,623 ****
  }
  
! static inline void
  pack_packed_int(ProfilerObject *self, int value)
  {
--- 623,627 ----
  }
  
! static inline int
  pack_packed_int(ProfilerObject *self, int value)
  {
***************
*** 632,635 ****
--- 636,640 ----
          self->index++;
      } while (value);
+     return 0;
  }
  
***************
*** 638,642 ****
   * checked to ensure it actually fits in modsize bits.
   */
! static inline void
  pack_modified_packed_int(ProfilerObject *self, int value,
                           int modsize, int subfield)
--- 643,647 ----
   * checked to ensure it actually fits in modsize bits.
   */
! static inline int
  pack_modified_packed_int(ProfilerObject *self, int value,
                           int modsize, int subfield)
***************
*** 652,674 ****
          self->buffer[self->index] = b;
          self->index++;
!         pack_packed_int(self, value >> bits);
!     }
!     else {
!         self->buffer[self->index] = b;
!         self->index++;
      }
  }
  
! static void
  pack_string(ProfilerObject *self, const char *s, int len)
  {
!     if (len + PISIZE + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
!     pack_packed_int(self, len);
      memcpy(self->buffer + self->index, s, len);
      self->index += len;
  }
  
! static void
  pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
  {
--- 657,682 ----
          self->buffer[self->index] = b;
          self->index++;
!         return pack_packed_int(self, value >> bits);
      }
+     self->buffer[self->index] = b;
+     self->index++;
+     return 0;
  }
  
! static int
  pack_string(ProfilerObject *self, const char *s, int len)
  {
!     if (len + PISIZE + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
!     if (pack_packed_int(self, len) < 0)
!         return -1;
      memcpy(self->buffer + self->index, s, len);
      self->index += len;
+     return 0;
  }
  
! static int
  pack_add_info(ProfilerObject *self, const char *s1, const char *s2)
  {
***************
*** 676,701 ****
      int len2 = strlen(s2);
  
!     if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      self->buffer[self->index] = WHAT_ADD_INFO;
      self->index++;
!     pack_string(self, s1, len1);
!     pack_string(self, s2, len2);
  }
  
! static void
  pack_define_file(ProfilerObject *self, int fileno, const char *filename)
  {
      int len = strlen(filename);
  
!     if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      self->buffer[self->index] = WHAT_DEFINE_FILE;
      self->index++;
!     pack_packed_int(self, fileno);
!     pack_string(self, filename, len);
  }
  
! static void
  pack_define_func(ProfilerObject *self, int fileno, int lineno,
                   const char *funcname)
--- 684,715 ----
      int len2 = strlen(s2);
  
!     if (len1 + len2 + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      self->buffer[self->index] = WHAT_ADD_INFO;
      self->index++;
!     if (pack_string(self, s1, len1) < 0)
!         return -1;
!     return pack_string(self, s2, len2);
  }
  
! static int
  pack_define_file(ProfilerObject *self, int fileno, const char *filename)
  {
      int len = strlen(filename);
  
!     if (len + PISIZE*2 + 1 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      self->buffer[self->index] = WHAT_DEFINE_FILE;
      self->index++;
!     if (pack_packed_int(self, fileno) < 0)
!         return -1;
!     return pack_string(self, filename, len);
  }
  
! static int
  pack_define_func(ProfilerObject *self, int fileno, int lineno,
                   const char *funcname)
***************
*** 703,774 ****
      int len = strlen(funcname);
  
!     if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      self->buffer[self->index] = WHAT_DEFINE_FUNC;
      self->index++;
!     pack_packed_int(self, fileno);
!     pack_packed_int(self, lineno);
!     pack_string(self, funcname, len);
  }
  
! static void
  pack_line_times(ProfilerObject *self)
  {
!     if (2 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      self->buffer[self->index] = WHAT_LINE_TIMES;
      self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
      self->index += 2;
  }
  
! static void
  pack_frame_times(ProfilerObject *self)
  {
!     if (2 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      self->buffer[self->index] = WHAT_FRAME_TIMES;
      self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
      self->index += 2;
  }
  
! static inline void
  pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
  {
!     if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
      pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
      pack_packed_int(self, lineno);
      if (self->frametimings)
!         pack_packed_int(self, tdelta);
  }
  
! static inline void
  pack_exit(ProfilerObject *self, int tdelta)
  {
!     if (MPISIZE + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
!     if (self->frametimings)
!         pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
!     else {
!         self->buffer[self->index] = WHAT_EXIT;
!         self->index++;
      }
  }
  
! static inline void
  pack_lineno(ProfilerObject *self, int lineno)
  {
!     if (MPISIZE + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
!     pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
  }
  
! static inline void
  pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
  {
!     if (MPISIZE + PISIZE + self->index >= BUFFERSIZE)
!         (void) flush_data(self);
!     pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
!     pack_packed_int(self, tdelta);
  }
  
--- 717,808 ----
      int len = strlen(funcname);
  
!     if (len + PISIZE*3 + 1 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      self->buffer[self->index] = WHAT_DEFINE_FUNC;
      self->index++;
!     if (pack_packed_int(self, fileno) < 0)
!         return -1;
!     if (pack_packed_int(self, lineno) < 0)
!         return -1;
!     return pack_string(self, funcname, len);
  }
  
! static int
  pack_line_times(ProfilerObject *self)
  {
!     if (2 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      self->buffer[self->index] = WHAT_LINE_TIMES;
      self->buffer[self->index + 1] = self->linetimings ? 1 : 0;
      self->index += 2;
+     return 0;
  }
  
! static int
  pack_frame_times(ProfilerObject *self)
  {
!     if (2 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      self->buffer[self->index] = WHAT_FRAME_TIMES;
      self->buffer[self->index + 1] = self->frametimings ? 1 : 0;
      self->index += 2;
+     return 0;
  }
  
! static inline int
  pack_enter(ProfilerObject *self, int fileno, int tdelta, int lineno)
  {
!     if (MPISIZE + PISIZE*2 + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
      pack_modified_packed_int(self, fileno, 2, WHAT_ENTER);
      pack_packed_int(self, lineno);
      if (self->frametimings)
!         return pack_packed_int(self, tdelta);
!     else
!         return 0;
  }
  
! static inline int
  pack_exit(ProfilerObject *self, int tdelta)
  {
!     if (MPISIZE + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
      }
+     if (self->frametimings)
+         return pack_modified_packed_int(self, tdelta, 2, WHAT_EXIT);
+     self->buffer[self->index] = WHAT_EXIT;
+     self->index++;
+     return 0;
  }
  
! static inline int
  pack_lineno(ProfilerObject *self, int lineno)
  {
!     if (MPISIZE + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return -1;
!     }
!     return pack_modified_packed_int(self, lineno, 2, WHAT_LINENO);
  }
  
! static inline int
  pack_lineno_tdelta(ProfilerObject *self, int lineno, int tdelta)
  {
!     if (MPISIZE + PISIZE + self->index >= BUFFERSIZE) {
!         if (flush_data(self) < 0)
!             return 0;
!     }
!     if (pack_modified_packed_int(self, lineno, 2, WHAT_LINENO) < 0)
!         return -1;
!     return pack_packed_int(self, tdelta);
  }
  
***************
*** 800,804 ****
          self->next_fileno++;
          Py_DECREF(obj);
!         pack_define_file(self, fileno, PyString_AS_STRING(fcode->co_filename));
      }
      else {
--- 834,840 ----
          self->next_fileno++;
          Py_DECREF(obj);
!         if (pack_define_file(self, fileno,
!                              PyString_AS_STRING(fcode->co_filename)) < 0)
!             return -1;
      }
      else {
***************
*** 816,821 ****
          PyObject *name = PyDict_GetItem(dict, obj);
          if (name == NULL) {
!             pack_define_func(self, fileno, fcode->co_firstlineno,
!                              PyString_AS_STRING(fcode->co_name));
              if (PyDict_SetItem(dict, obj, fcode->co_name))
                  return -1;
--- 852,858 ----
          PyObject *name = PyDict_GetItem(dict, obj);
          if (name == NULL) {
!             if (pack_define_func(self, fileno, fcode->co_firstlineno,
!                                  PyString_AS_STRING(fcode->co_name)) < 0)
!                 return -1;
              if (PyDict_SetItem(dict, obj, fcode->co_name))
                  return -1;
***************
*** 868,876 ****
          if (fileno < 0)
              return -1;
!         pack_enter(self, fileno, tdelta,
!                    frame->f_code->co_firstlineno);
          break;
      case PyTrace_RETURN:
!         pack_exit(self, tdelta);
          break;
      default:
--- 905,915 ----
          if (fileno < 0)
              return -1;
!         if (pack_enter(self, fileno, tdelta,
!                        frame->f_code->co_firstlineno) < 0)
!             return -1;
          break;
      case PyTrace_RETURN:
!         if (pack_exit(self, tdelta) < 0)
!             return -1;
          break;
      default:
***************
*** 895,910 ****
          if (fileno < 0)
              return -1;
!         pack_enter(self, fileno, self->frametimings ? get_tdelta(self) : -1,
!                    frame->f_code->co_firstlineno);
!         break;
      case PyTrace_RETURN:
!         pack_exit(self, get_tdelta(self));
!         break;
      case PyTrace_LINE:
          if (self->linetimings)
!             pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
          else
!             pack_lineno(self, frame->f_lineno);
!         break;
      default:
          /* ignore PyTrace_EXCEPTION */
--- 934,950 ----
          if (fileno < 0)
              return -1;
!         return pack_enter(self, fileno,
!                           self->frametimings ? get_tdelta(self) : -1,
!                           frame->f_code->co_firstlineno);
! 
      case PyTrace_RETURN:
!         return pack_exit(self, get_tdelta(self));
! 
      case PyTrace_LINE:
          if (self->linetimings)
!             return pack_lineno_tdelta(self, frame->f_lineno, get_tdelta(self));
          else
!             return pack_lineno(self, frame->f_lineno);
! 
      default:
          /* ignore PyTrace_EXCEPTION */
***************
*** 1043,1049 ****
              PyErr_SetString(ProfilerError, "profiler already closed");
          else {
!             pack_add_info(self, key, value);
!             result = Py_None;
!             Py_INCREF(result);
          }
      }
--- 1083,1090 ----
              PyErr_SetString(ProfilerError, "profiler already closed");
          else {
!             if (pack_add_info(self, key, value) == 0) {
!                 result = Py_None;
!                 Py_INCREF(result);
!             }
          }
      }