[Python-checkins] python/dist/src/Modules _hotshot.c,1.22,1.23

fdrake@users.sourceforge.net fdrake@users.sourceforge.net
Thu, 18 Jul 2002 12:11:46 -0700


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

Modified Files:
	_hotshot.c 
Log Message:
- When the log reader detects end-of-file, close the file.
- The log reader now provides a "closed" attribute similar to the
  profiler.
- Both the profiler and log reader now provide a fileno() method.
- Use METH_NOARGS where possible, allowing simpler code in the method
  implementations.


Index: _hotshot.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_hotshot.c,v
retrieving revision 1.22
retrieving revision 1.23
diff -C2 -d -r1.22 -r1.23
*** _hotshot.c	17 Jul 2002 19:38:05 -0000	1.22
--- _hotshot.c	18 Jul 2002 19:11:44 -0000	1.23
***************
*** 109,122 ****
  logreader_close(LogReaderObject *self, PyObject *args)
  {
!     PyObject *result = NULL;
!     if (PyArg_ParseTuple(args, ":close")) {
!         if (self->logfp != NULL) {
!             fclose(self->logfp);
!             self->logfp = NULL;
!         }
!         result = Py_None;
!         Py_INCREF(result);
      }
!     return result;
  }
  
--- 109,135 ----
  logreader_close(LogReaderObject *self, PyObject *args)
  {
!     if (self->logfp != NULL) {
!         fclose(self->logfp);
!         self->logfp = NULL;
      }
!     Py_INCREF(Py_None);
! 
!     return Py_None;
! }
! 
! PyDoc_STRVAR(logreader_fileno__doc__,
! "fileno() -> file descriptor\n"
! "Returns the file descriptor for the log file, if open.\n"
! "Raises ValueError if the log file is closed.");
! 
! static PyObject *
! logreader_fileno(LogReaderObject *self)
! {
!     if (self->logfp == NULL) {
!         PyErr_SetString(PyExc_ValueError,
!                         "logreader's file object already closed");
!         return NULL;
!     }
!     return PyInt_FromLong(fileno(self->logfp));
  }
  
***************
*** 351,356 ****
  
  static void
! eof_error(void)
  {
      PyErr_SetString(PyExc_EOFError,
                      "end of file with incomplete profile record");
--- 364,371 ----
  
  static void
! eof_error(LogReaderObject *self)
  {
+     fclose(self->logfp);
+     self->logfp = NULL;
      PyErr_SetString(PyExc_EOFError,
                      "end of file with incomplete profile record");
***************
*** 380,386 ****
  restart:
      /* decode the record type */
!     if ((c = fgetc(self->logfp)) == EOF)
          return NULL;
! 
      what = c & WHAT_OTHER;
      if (what == WHAT_OTHER)
--- 395,403 ----
  restart:
      /* decode the record type */
!     if ((c = fgetc(self->logfp)) == EOF) {
!         fclose(self->logfp);
!         self->logfp = NULL;
          return NULL;
!     }
      what = c & WHAT_OTHER;
      if (what == WHAT_OTHER)
***************
*** 451,455 ****
      }
      else if (err == ERR_EOF) {
!         eof_error();
      }
      else if (!err) {
--- 468,472 ----
      }
      else if (err == ERR_EOF) {
!         eof_error(self);
      }
      else if (!err) {
***************
*** 1023,1040 ****
  
  static PyObject *
! profiler_close(ProfilerObject *self, PyObject *args)
  {
!     PyObject *result = NULL;
  
!     if (PyArg_ParseTuple(args, ":close")) {
!         do_stop(self);
!         if (self->logfp != NULL) {
!             fclose(self->logfp);
!             self->logfp = NULL;
!         }
!         Py_INCREF(Py_None);
!         result = Py_None;
      }
!     return result;
  }
  
--- 1040,1065 ----
  
  static PyObject *
! profiler_close(ProfilerObject *self)
  {
!     do_stop(self);
!     if (self->logfp != NULL) {
!         fclose(self->logfp);
!         self->logfp = NULL;
!     }
!     Py_INCREF(Py_None);
!     return Py_None;
! }
  
! #define fileno__doc__ logreader_fileno__doc__
! 
! static PyObject *
! profiler_fileno(ProfilerObject *self)
! {
!     if (self->logfp == NULL) {
!         PyErr_SetString(PyExc_ValueError,
!                         "profiler's file object already closed");
!         return NULL;
      }
!     return PyInt_FromLong(fileno(self->logfp));
  }
  
***************
*** 1110,1119 ****
      PyObject *result = NULL;
  
!     if (PyArg_ParseTuple(args, ":start")) {
!         if (is_available(self)) {
!             do_start(self);
!             result = Py_None;
!             Py_INCREF(result);
!         }
      }
      return result;
--- 1135,1142 ----
      PyObject *result = NULL;
  
!     if (is_available(self)) {
!         do_start(self);
!         result = Py_None;
!         Py_INCREF(result);
      }
      return result;
***************
*** 1129,1140 ****
      PyObject *result = NULL;
  
!     if (PyArg_ParseTuple(args, ":stop")) {
!         if (!self->active)
!             PyErr_SetString(ProfilerError, "profiler not active");
!         else {
!             do_stop(self);
!             result = Py_None;
!             Py_INCREF(result);
!         }
      }
      return result;
--- 1152,1161 ----
      PyObject *result = NULL;
  
!     if (!self->active)
!         PyErr_SetString(ProfilerError, "profiler not active");
!     else {
!         do_stop(self);
!         result = Py_None;
!         Py_INCREF(result);
      }
      return result;
***************
*** 1157,1165 ****
  static PyMethodDef profiler_methods[] = {
      {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
!     {"close",   (PyCFunction)profiler_close,   METH_VARARGS, close__doc__},
      {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
      {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
!     {"start",   (PyCFunction)profiler_start,   METH_VARARGS, start__doc__},
!     {"stop",    (PyCFunction)profiler_stop,    METH_VARARGS, stop__doc__},
      {NULL, NULL}
  };
--- 1178,1187 ----
  static PyMethodDef profiler_methods[] = {
      {"addinfo", (PyCFunction)profiler_addinfo, METH_VARARGS, addinfo__doc__},
!     {"close",   (PyCFunction)profiler_close,   METH_NOARGS,  close__doc__},
!     {"fileno",  (PyCFunction)profiler_fileno,  METH_NOARGS,  fileno__doc__},
      {"runcall", (PyCFunction)profiler_runcall, METH_VARARGS, runcall__doc__},
      {"runcode", (PyCFunction)profiler_runcode, METH_VARARGS, runcode__doc__},
!     {"start",   (PyCFunction)profiler_start,   METH_NOARGS,  start__doc__},
!     {"stop",    (PyCFunction)profiler_stop,    METH_NOARGS,  stop__doc__},
      {NULL, NULL}
  };
***************
*** 1193,1196 ****
--- 1215,1219 ----
  "\n"
  "close():      Stop the profiler and close the log files.\n"
+ "fileno():     Returns the file descriptor of the log file.\n"
  "runcall():    Run a single function call with profiling enabled.\n"
  "runcode():    Execute a code object with profiling enabled.\n"
***************
*** 1245,1250 ****
  
  static PyMethodDef logreader_methods[] = {
!     {"close",   (PyCFunction)logreader_close,  METH_VARARGS,
       logreader_close__doc__},
      {NULL, NULL}
  };
--- 1268,1275 ----
  
  static PyMethodDef logreader_methods[] = {
!     {"close",   (PyCFunction)logreader_close,  METH_NOARGS,
       logreader_close__doc__},
+     {"fileno",  (PyCFunction)logreader_fileno, METH_NOARGS,
+      logreader_fileno__doc__},
      {NULL, NULL}
  };
***************
*** 1274,1277 ****
--- 1299,1316 ----
  };
  
+ static PyObject *
+ logreader_get_closed(LogReaderObject *self, void *closure)
+ {
+     PyObject *result = (self->logfp == NULL) ? Py_True : Py_False;
+     Py_INCREF(result);
+     return result;
+ }
+ 
+ static PyGetSetDef logreader_getsets[] = {
+     {"closed", (getter)logreader_get_closed, NULL,
+      PyDoc_STR("True if the logreader's input file has already been closed.")},
+     {NULL}
+ };
+ 
  static PyTypeObject LogReaderType = {
      PyObject_HEAD_INIT(NULL)
***************
*** 1305,1309 ****
      logreader_methods,			/* tp_methods		*/
      logreader_members,			/* tp_members		*/
!     0,					/* tp_getset		*/
      0,					/* tp_base		*/
      0,					/* tp_dict		*/
--- 1344,1348 ----
      logreader_methods,			/* tp_methods		*/
      logreader_members,			/* tp_members		*/
!     logreader_getsets,			/* tp_getset		*/
      0,					/* tp_base		*/
      0,					/* tp_dict		*/
***************
*** 1341,1345 ****
              for (;;) {
                  if ((c = fgetc(self->logfp)) == EOF) {
!                     eof_error();
                      break;
                  }
--- 1380,1384 ----
              for (;;) {
                  if ((c = fgetc(self->logfp)) == EOF) {
!                     eof_error(self);
                      break;
                  }
***************
*** 1351,1355 ****
                  if (err) {
                      if (err == ERR_EOF)
!                         eof_error();
                      else
                          PyErr_SetString(PyExc_RuntimeError,
--- 1390,1394 ----
                  if (err) {
                      if (err == ERR_EOF)
!                         eof_error(self);
                      else
                          PyErr_SetString(PyExc_RuntimeError,