[Python-checkins] python/dist/src/Modules _hotshot.c,1.16,1.17

nascheme@users.sourceforge.net nascheme@users.sourceforge.net
2002年5月29日 11:19:21 -0700


Update of /cvsroot/python/python/dist/src/Modules
In directory usw-pr-cvs1:/tmp/cvs-serv28796/Modules
Modified Files:
	_hotshot.c 
Log Message:
The logreader object did not always refill the input buffer correctly
and got confused by certain log files. Remove logreader_refill and the
associated logic and replace with fgetc.
Index: _hotshot.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/_hotshot.c,v
retrieving revision 1.16
retrieving revision 1.17
diff -C2 -d -r1.16 -r1.17
*** _hotshot.c	20 Mar 2002 21:32:07 -0000	1.16
--- _hotshot.c	29 May 2002 18:19:14 -0000	1.17
***************
*** 84,92 ****
 PyObject *info;
 FILE *logfp;
- int filled;
- int index;
 int linetimings;
 int frametimings;
- unsigned char buffer[BUFFERSIZE];
 } LogReaderObject;
 
--- 84,89 ----
***************
*** 273,295 ****
 unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
 {
 int accum = 0;
 int bits = 0;
- int index = self->index;
 int cont;
 
 do {
- if (index >= self->filled)
- return ERR_EOF;
 /* read byte */
! accum |= ((self->buffer[index] & 0x7F) >> discard) << bits;
 bits += (7 - discard);
! cont = self->buffer[index] & 0x80;
! /* move to next */
 discard = 0;
- index++;
 } while (cont);
 
- /* save state */
- self->index = index;
 *pvalue = accum;
 
--- 270,288 ----
 unpack_packed_int(LogReaderObject *self, int *pvalue, int discard)
 {
+ int c;
 int accum = 0;
 int bits = 0;
 int cont;
 
 do {
 /* read byte */
! 	if ((c = fgetc(self->logfp)) == EOF)
! return ERR_EOF;
! accum |= ((c & 0x7F) >> discard) << bits;
 bits += (7 - discard);
! cont = c & 0x80;
 discard = 0;
 } while (cont);
 
 *pvalue = accum;
 
***************
*** 303,333 ****
 unpack_string(LogReaderObject *self, PyObject **pvalue)
 {
 int len;
! int oldindex = self->index;
! int err = unpack_packed_int(self, &len, 0);
 
! if (!err) {
! /* need at least len bytes in buffer */
! if (len > (self->filled - self->index)) {
! self->index = oldindex;
! err = ERR_EOF;
! }
! else {
! *pvalue = PyString_FromStringAndSize((char *)self->buffer + self->index,
! len);
! if (*pvalue == NULL) {
! self->index = oldindex;
! err = ERR_EXCEPTION;
! }
! else
! self->index += len;
 }
 }
! return err;
 }
 
 
 static int
! unpack_add_info(LogReaderObject *self, int skip_opcode)
 {
 PyObject *key;
--- 296,325 ----
 unpack_string(LogReaderObject *self, PyObject **pvalue)
 {
+ int i;
 int len;
! int err;
! char *buf;
! 
! if ((err = unpack_packed_int(self, &len, 0)))
! return err;
 
! buf = malloc(len);
! for (i=0; i < len; i++) {
! if ((buf[i] = fgetc(self->logfp)) == EOF) {
! free(buf);
! return ERR_EOF;
 }
 }
! *pvalue = PyString_FromStringAndSize(buf, len);
! free(buf);
! if (*pvalue == NULL) {
! return ERR_EXCEPTION;
! }
! return 0;
 }
 
 
 static int
! unpack_add_info(LogReaderObject *self)
 {
 PyObject *key;
***************
*** 335,343 ****
 int err;
 
- if (skip_opcode) {
- if (self->buffer[self->index] != WHAT_ADD_INFO)
- return ERR_BAD_RECTYPE;
- self->index++;
- }
 err = unpack_string(self, &key);
 if (!err) {
--- 327,330 ----
***************
*** 370,392 ****
 
 static void
- logreader_refill(LogReaderObject *self)
- {
- int needed;
- size_t res;
- 
- if (self->index) {
- memmove(self->buffer, &self->buffer[self->index],
- self->filled - self->index);
- self->filled = self->filled - self->index;
- self->index = 0;
- }
- needed = BUFFERSIZE - self->filled;
- if (needed > 0) {
- res = fread(&self->buffer[self->filled], 1, needed, self->logfp);
- self->filled += res;
- }
- }
- 
- static void
 eof_error(void)
 {
--- 357,360 ----
***************
*** 398,402 ****
 logreader_tp_iternext(LogReaderObject *self)
 {
! int what, oldindex;
 int err = ERR_NONE;
 int lineno = -1;
--- 366,371 ----
 logreader_tp_iternext(LogReaderObject *self)
 {
! int c;
! int what;
 int err = ERR_NONE;
 int lineno = -1;
***************
*** 414,433 ****
 return NULL;
 }
- restart:
- if ((self->filled - self->index) < MAXEVENTSIZE)
- logreader_refill(self);
 
! /* end of input */
! if (self->filled == 0)
 return NULL;
 
! oldindex = self->index;
 
- /* decode the record type */
- what = self->buffer[self->index] & WHAT_OTHER;
- if (what == WHAT_OTHER) {
- what = self->buffer[self->index];
- self->index++;
- }
 switch (what) {
 case WHAT_ENTER:
--- 383,398 ----
 return NULL;
 }
 
! restart:
! /* decode the record type */
! if ((c = fgetc(self->logfp)) == EOF)
 return NULL;
 
! what = c & WHAT_OTHER;
! if (what == WHAT_OTHER)
! what = c; /* need all the bits for type */
! else
! ungetc(c, self->logfp); /* type byte includes packed int */
 
 switch (what) {
 case WHAT_ENTER:
***************
*** 448,452 ****
 break;
 case WHAT_ADD_INFO:
! err = unpack_add_info(self, 0);
 break;
 case WHAT_DEFINE_FILE:
--- 413,417 ----
 break;
 case WHAT_ADD_INFO:
! err = unpack_add_info(self);
 break;
 case WHAT_DEFINE_FILE:
***************
*** 469,500 ****
 break;
 case WHAT_LINE_TIMES:
! if (self->index >= self->filled)
 err = ERR_EOF;
 else {
! self->linetimings = self->buffer[self->index] ? 1 : 0;
! self->index++;
! goto restart;
! }
 break;
 case WHAT_FRAME_TIMES:
! if (self->index >= self->filled)
 err = ERR_EOF;
 else {
! self->frametimings = self->buffer[self->index] ? 1 : 0;
! self->index++;
! goto restart;
! }
 break;
 default:
 err = ERR_BAD_RECTYPE;
 }
- if (err == ERR_EOF && oldindex != 0) {
- /* It looks like we ran out of data before we had it all; this
- * could easily happen with large packed integers or string
- * data. Try forcing the buffer to be re-filled before failing.
- */
- err = ERR_NONE;
- logreader_refill(self);
- }
 if (err == ERR_BAD_RECTYPE) {
 PyErr_SetString(PyExc_ValueError,
--- 434,455 ----
 break;
 case WHAT_LINE_TIMES:
! if ((c = fgetc(self->logfp)) == EOF)
 err = ERR_EOF;
 else {
! self->linetimings = c ? 1 : 0;
! 	 goto restart;
! 	}
 break;
 case WHAT_FRAME_TIMES:
! if ((c = fgetc(self->logfp)) == EOF)
 err = ERR_EOF;
 else {
! self->frametimings = c ? 1 : 0;
! 	 goto restart;
! 	}
 break;
 default:
 err = ERR_BAD_RECTYPE;
 }
 if (err == ERR_BAD_RECTYPE) {
 PyErr_SetString(PyExc_ValueError,
***************
*** 502,506 ****
 }
 else if (err == ERR_EOF) {
- /* Could not avoid end-of-buffer error. */
 eof_error();
 }
--- 457,460 ----
***************
*** 1390,1399 ****
 LogReaderObject *self = NULL;
 char *filename;
 
 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
 self = PyObject_New(LogReaderObject, &LogReaderType);
 if (self != NULL) {
- self->filled = 0;
- self->index = 0;
 self->frametimings = 1;
 self->linetimings = 0;
--- 1344,1353 ----
 LogReaderObject *self = NULL;
 char *filename;
+ int c;
+ int err = 0;
 
 if (PyArg_ParseTuple(args, "s:logreader", &filename)) {
 self = PyObject_New(LogReaderObject, &LogReaderType);
 if (self != NULL) {
 self->frametimings = 1;
 self->linetimings = 0;
***************
*** 1411,1422 ****
 goto finally;
 }
! /* Aggressively attempt to load all preliminary ADD_INFO
! * records from the log so the info records are available
! * from a fresh logreader object.
! */
! logreader_refill(self);
! while (self->filled > self->index
! && self->buffer[self->index] == WHAT_ADD_INFO) {
! int err = unpack_add_info(self, 1);
 if (err) {
 if (err == ERR_EOF)
--- 1365,1379 ----
 goto finally;
 }
! /* read initial info */
! for (;;) {
! if ((c = fgetc(self->logfp)) == EOF) {
! eof_error();
! break;
! }
! if (c != WHAT_ADD_INFO) {
! ungetc(c, self->logfp);
! break;
! }
! err = unpack_add_info(self);
 if (err) {
 if (err == ERR_EOF)
***************
*** 1427,1436 ****
 break;
 }
- /* Refill agressively so we can avoid EOF during
- * initialization unless there's a real EOF condition
- * (the tp_iternext handler loops attempts to refill
- * and try again).
- */
- logreader_refill(self);
 }
 }
--- 1384,1387 ----

AltStyle によって変換されたページ (->オリジナル) /