@@ -22,6 +22,18 @@ static char __author__[] =
2222 Gustavo Niemeyer <[email protected] >\n\ 2323" ;
2424
25+ /* Our very own off_t-like type, 64-bit if possible */
26+ /* copied from Objects/fileobject.c */
27+ #if !defined(HAVE_LARGEFILE_SUPPORT )
28+ typedef off_t Py_off_t ;
29+ #elif SIZEOF_OFF_T >= 8
30+ typedef off_t Py_off_t ;
31+ #elif SIZEOF_FPOS_T >= 8
32+ typedef fpos_t Py_off_t ;
33+ #else
34+ #error "Large file support, but neither off_t nor fpos_t is large enough."
35+ #endif
36+
2537#define BUF (v ) PyString_AS_STRING((PyStringObject *)v)
2638
2739#define MODE_CLOSED 0
@@ -405,7 +417,9 @@ Util_ReadAhead(BZ2FileObject *f, int bufsize)
405417 Util_DropReadAhead (f );
406418 }
407419 if (f -> mode == MODE_READ_EOF ) {
408- return -1 ;
420+ f -> f_bufptr = f -> f_buf ;
421+ f -> f_bufend = f -> f_buf ;
422+ return 0 ;
409423 }
410424 if ((f -> f_buf = PyMem_Malloc (bufsize )) == NULL ) {
411425 return -1 ;
@@ -682,13 +696,13 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args)
682696 }
683697 totalread += nread ;
684698 p = memchr (buffer + nfilled , '\n' , nread );
685- if (p == NULL ) {
699+ if (! shortread && p == NULL ) {
686700 /* Need a larger buffer to fit this line */
687701 nfilled += nread ;
688702 buffersize *= 2 ;
689703 if (buffersize > INT_MAX ) {
690704 PyErr_SetString (PyExc_OverflowError ,
691- "line is longer than a Python string can hold" );
705+ "line is longer than a Python string can hold" );
692706 goto error ;
693707 }
694708 if (big_buffer == NULL ) {
@@ -705,11 +719,11 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args)
705719 _PyString_Resize (& big_buffer , buffersize );
706720 buffer = PyString_AS_STRING (big_buffer );
707721 }
708- continue ;
722+ continue ;
709723 }
710724 end = buffer + nfilled + nread ;
711725 q = buffer ;
712- do {
726+ while ( p != NULL ) {
713727 /* Process complete lines */
714728 p ++ ;
715729 line = PyString_FromStringAndSize (q , p - q );
@@ -721,7 +735,7 @@ BZ2File_readlines(BZ2FileObject *self, PyObject *args)
721735 goto error ;
722736 q = p ;
723737 p = memchr (q , '\n' , end - q );
724- } while ( p != NULL );
738+ }
725739 /* Move the remaining incomplete line to the start */
726740 nfilled = end - q ;
727741 memmove (buffer , q , nfilled );
@@ -962,7 +976,8 @@ static PyObject *
962976BZ2File_seek (BZ2FileObject * self , PyObject * args )
963977{
964978 int where = 0 ;
965- long offset ;
979+ PyObject * offobj ;
980+ Py_off_t offset ;
966981 char small_buffer [SMALLCHUNK ];
967982 char * buffer = small_buffer ;
968983 size_t buffersize = SMALLCHUNK ;
@@ -973,7 +988,15 @@ BZ2File_seek(BZ2FileObject *self, PyObject *args)
973988 int rewind = 0 ;
974989 PyObject * ret = NULL ;
975990
976- if (!PyArg_ParseTuple (args , "l|i:seek" , & offset , & where ))
991+ if (!PyArg_ParseTuple (args , "O|i:seek" , & offobj , & where ))
992+ return NULL ;
993+ #if !defined(HAVE_LARGEFILE_SUPPORT )
994+ offset = PyInt_AsLong (offobj );
995+ #else
996+ offset = PyLong_Check (offobj ) ?
997+ PyLong_AsLongLong (offobj ) : PyInt_AsLong (offobj );
998+ #endif
999+ if (PyErr_Occurred ())
9771000 return NULL ;
9781001
9791002 ACQUIRE_LOCK (self );
0 commit comments