Ignore:
Timestamp:
Mar 19, 2014, 11:11:30 AM (11 years ago)
Author:
dmik
Message:

python: Update vendor to 2.7.6.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • python/vendor/current/Objects/fileobject.c

    r2 r388  
    1717#endif
    1818
    19 #ifdef _MSC_VER
    20 /* Need GetVersion to see if on NT so safe to use _wfopen */
    21 #define WIN32_LEAN_AND_MEAN
    22 #include <windows.h>
    23 #endif /* _MSC_VER */
    24 
    2519#if defined(PYOS_OS2) && defined(PYCC_GCC)
    2620#include <io.h>
     
    2923#define BUF(v) PyString_AS_STRING((PyStringObject *)v)
    3024
    31 #ifndef DONT_HAVE_ERRNO_H
     25#ifdef HAVE_ERRNO_H
    3226#include <errno.h>
    3327#endif
     
    4438
    4539/* Bits in f_newlinetypes */
    46 #define NEWLINE_UNKNOWN 0       /* No newline seen, yet */
    47 #define NEWLINE_CR 1            /* \r newline seen */
    48 #define NEWLINE_LF 2            /* \n newline seen */
    49 #define NEWLINE_CRLF 4          /* \r\n newline seen */
     40#define NEWLINE_UNKNOWN 0       /* No newline seen, yet */
     41#define NEWLINE_CR 1            /* \r newline seen */
     42#define NEWLINE_LF 2            /* \n newline seen */
     43#define NEWLINE_CRLF 4          /* \r\n newline seen */
    5044
    5145/*
     
    5953#define FILE_BEGIN_ALLOW_THREADS(fobj) \
    6054{ \
    61         fobj->unlocked_count++; \
    62         Py_BEGIN_ALLOW_THREADS
     55    fobj->unlocked_count++; \
     56    Py_BEGIN_ALLOW_THREADS
    6357
    6458#define FILE_END_ALLOW_THREADS(fobj) \
    65         Py_END_ALLOW_THREADS \
    66         fobj->unlocked_count--; \
    67         assert(fobj->unlocked_count >= 0); \
     59    Py_END_ALLOW_THREADS \
     60    fobj->unlocked_count--; \
     61    assert(fobj->unlocked_count >= 0); \
    6862}
    6963
    7064#define FILE_ABORT_ALLOW_THREADS(fobj) \
    71         Py_BLOCK_THREADS \
    72         fobj->unlocked_count--; \
    73         assert(fobj->unlocked_count >= 0);
     65    Py_BLOCK_THREADS \
     66    fobj->unlocked_count--; \
     67    assert(fobj->unlocked_count >= 0);
    7468
    7569#ifdef __cplusplus
     
    8074PyFile_AsFile(PyObject *f)
    8175{
    82         if (f == NULL || !PyFile_Check(f))
    83                 return NULL;
    84         else
    85                 return ((PyFileObject *)f)->f_fp;
     76    if (f == NULL || !PyFile_Check(f))
     77        return NULL;
     78    else
     79        return ((PyFileObject *)f)->f_fp;
    8680}
    8781
    8882void PyFile_IncUseCount(PyFileObject *fobj)
    8983{
    90         fobj->unlocked_count++;
     84    fobj->unlocked_count++;
    9185}
    9286
    9387void PyFile_DecUseCount(PyFileObject *fobj)
    9488{
    95         fobj->unlocked_count--;
    96         assert(fobj->unlocked_count >= 0);
     89    fobj->unlocked_count--;
     90    assert(fobj->unlocked_count >= 0);
    9791}
    9892
     
    10094PyFile_Name(PyObject *f)
    10195{
    102         if (f == NULL || !PyFile_Check(f))
    103                 return NULL;
    104         else
    105                 return ((PyFileObject *)f)->f_name;
     96    if (f == NULL || !PyFile_Check(f))
     97        return NULL;
     98    else
     99        return ((PyFileObject *)f)->f_name;
    106100}
    107101
     
    112106file_PyObject_Print(PyObject *op, PyFileObject *f, int flags)
    113107{
    114         int result;
    115         PyFile_IncUseCount(f);
    116         result = PyObject_Print(op, f->f_fp, flags);
    117         PyFile_DecUseCount(f);
    118         return result;
     108    int result;
     109    PyFile_IncUseCount(f);
     110    result = PyObject_Print(op, f->f_fp, flags);
     111    PyFile_DecUseCount(f);
     112    return result;
    119113}
    120114
     
    127121{
    128122#if defined(HAVE_FSTAT) && defined(S_IFDIR) && defined(EISDIR)
    129         struct stat buf;
    130         if (f->f_fp == NULL)
    131                 return f;
    132         if (fstat(fileno(f->f_fp), &buf) == 0 &&
    133             S_ISDIR(buf.st_mode)) {
    134                 char *msg = strerror(EISDIR);
    135                 PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
    136                                                       EISDIR, msg, f->f_name);
    137                 PyErr_SetObject(PyExc_IOError, exc);
    138                 Py_XDECREF(exc);
    139                 return NULL;
    140         }
    141 #endif
    142         return f;
     123    struct stat buf;
     124    if (f->f_fp == NULL)
     125        return f;
     126    if (fstat(fileno(f->f_fp), &buf) == 0 &&
     127        S_ISDIR(buf.st_mode)) {
     128        char *msg = strerror(EISDIR);
     129        PyObject *exc = PyObject_CallFunction(PyExc_IOError, "(isO)",
     130                                              EISDIR, msg, f->f_name);
     131        PyErr_SetObject(PyExc_IOError, exc);
     132        Py_XDECREF(exc);
     133        return NULL;
     134    }
     135#endif
     136    return f;
    143137}
    144138
     
    146140static PyObject *
    147141fill_file_fields(PyFileObject *f, FILE *fp, PyObject *name, char *mode,
    148                  int (*close)(FILE *))
    149 {
    150         assert(name != NULL);
    151         assert(f != NULL);
    152         assert(PyFile_Check(f));
    153         assert(f->f_fp == NULL);
    154 
    155         Py_DECREF(f->f_name);
    156         Py_DECREF(f->f_mode);
    157         Py_DECREF(f->f_encoding);
    158         Py_DECREF(f->f_errors);
    159 
    160         Py_INCREF(name);
    161         f->f_name = name;
    162 
    163         f->f_mode = PyString_FromString(mode);
    164 
    165         f->f_close = close;
    166         f->f_softspace = 0;
    167         f->f_binary = strchr(mode,'b') != NULL;
    168         f->f_buf = NULL;
    169         f->f_univ_newline = (strchr(mode, 'U') != NULL);
    170         f->f_newlinetypes = NEWLINE_UNKNOWN;
    171         f->f_skipnextlf = 0;
    172         Py_INCREF(Py_None);
    173         f->f_encoding = Py_None;
    174         Py_INCREF(Py_None);
    175         f->f_errors = Py_None;
    176         f->readable = f->writable = 0;
    177         if (strchr(mode, 'r') != NULL || f->f_univ_newline)
    178                 f->readable = 1;
    179         if (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL)
    180                 f->writable = 1;
    181         if (strchr(mode, '+') != NULL)
    182                 f->readable = f->writable = 1;
    183 
    184         if (f->f_mode == NULL)
    185                 return NULL;
    186         f->f_fp = fp;
    187         f = dircheck(f);
    188         return (PyObject *) f;
    189 }
     142                 int (*close)(FILE *))
     143{
     144    assert(name != NULL);
     145    assert(f != NULL);
     146    assert(PyFile_Check(f));
     147    assert(f->f_fp == NULL);
     148
     149    Py_DECREF(f->f_name);
     150    Py_DECREF(f->f_mode);
     151    Py_DECREF(f->f_encoding);
     152    Py_DECREF(f->f_errors);
     153
     154    Py_INCREF(name);
     155    f->f_name = name;
     156
     157    f->f_mode = PyString_FromString(mode);
     158
     159    f->f_close = close;
     160    f->f_softspace = 0;
     161    f->f_binary = strchr(mode,'b') != NULL;
     162    f->f_buf = NULL;
     163    f->f_univ_newline = (strchr(mode, 'U') != NULL);
     164    f->f_newlinetypes = NEWLINE_UNKNOWN;
     165    f->f_skipnextlf = 0;
     166    Py_INCREF(Py_None);
     167    f->f_encoding = Py_None;
     168    Py_INCREF(Py_None);
     169    f->f_errors = Py_None;
     170    f->readable = f->writable = 0;
     171    if (strchr(mode, 'r') != NULL || f->f_univ_newline)
     172        f->readable = 1;
     173    if (strchr(mode, 'w') != NULL || strchr(mode, 'a') != NULL)
     174        f->writable = 1;
     175    if (strchr(mode, '+') != NULL)
     176        f->readable = f->writable = 1;
     177
     178    if (f->f_mode == NULL)
     179        return NULL;
     180    f->f_fp = fp;
     181    f = dircheck(f);
     182    return (PyObject *) f;
     183}
     184
     185#if defined _MSC_VER && _MSC_VER >= 1400 && defined(__STDC_SECURE_LIB__)
     186#define Py_VERIFY_WINNT
     187/* The CRT on windows compiled with Visual Studio 2005 and higher may
     188 * assert if given invalid mode strings.  This is all fine and well
     189 * in static languages like C where the mode string is typcially hard
     190 * coded.  But in Python, were we pass in the mode string from the user,
     191 * we need to verify it first manually
     192 */
     193static int _PyVerify_Mode_WINNT(const char *mode)
     194{
     195    /* See if mode string is valid on Windows to avoid hard assertions */
     196    /* remove leading spacese */
     197    int singles = 0;
     198    int pairs = 0;
     199    int encoding = 0;
     200    const char *s, *c;
     201
     202    while(*mode == ' ') /* strip initial spaces */
     203        ++mode;
     204    if (!strchr("rwa", *mode)) /* must start with one of these */
     205        return 0;
     206    while (*++mode) {
     207        if (*mode == ' ' || *mode == 'N') /* ignore spaces and N */
     208            continue;
     209        s = "+TD"; /* each of this can appear only once */
     210        c = strchr(s, *mode);
     211        if (c) {
     212            ptrdiff_t idx = s-c;
     213            if (singles & (1<<idx))
     214                return 0;
     215            singles |= (1<<idx);
     216            continue;
     217        }
     218        s = "btcnSR"; /* only one of each letter in the pairs allowed */
     219        c = strchr(s, *mode);
     220        if (c) {
     221            ptrdiff_t idx = (s-c)/2;
     222            if (pairs & (1<<idx))
     223                return 0;
     224            pairs |= (1<<idx);
     225            continue;
     226        }
     227        if (*mode == ',') {
     228            encoding = 1;
     229            break;
     230        }
     231        return 0; /* found an invalid char */
     232    }
     233
     234    if (encoding) {
     235        char *e[] = {"UTF-8", "UTF-16LE", "UNICODE"};
     236        while (*mode == ' ')
     237            ++mode;
     238        /* find 'ccs =' */
     239        if (strncmp(mode, "ccs", 3))
     240            return 0;
     241        mode += 3;
     242        while (*mode == ' ')
     243            ++mode;
     244        if (*mode != '=')
     245            return 0;
     246        while (*mode == ' ')
     247            ++mode;
     248        for(encoding = 0; encoding<_countof(e); ++encoding) {
     249            size_t l = strlen(e[encoding]);
     250            if (!strncmp(mode, e[encoding], l)) {
     251                mode += l; /* found a valid encoding */
     252                break;
     253            }
     254        }
     255        if (encoding == _countof(e))
     256            return 0;
     257    }
     258    /* skip trailing spaces */
     259    while (*mode == ' ')
     260        ++mode;
     261
     262    return *mode == '\0'; /* must be at the end of the string */
     263}
     264#endif
    190265
    191266/* check for known incorrect mode strings - problem is, platforms are
     
    198273_PyFile_SanitizeMode(char *mode)
    199274{
    200         char *upos;
    201         size_t len = strlen(mode);
    202 
    203         if (!len) {
    204                 PyErr_SetString(PyExc_ValueError, "empty mode string");
    205                 return -1;
    206         }
    207 
    208         upos = strchr(mode, 'U');
    209         if (upos) {
    210                 memmove(upos, upos+1, len-(upos-mode)); /* incl null char */
    211 
    212                 if (mode[0] == 'w' || mode[0] == 'a') {
    213                         PyErr_Format(PyExc_ValueError, "universal newline "
    214                                      "mode can only be used with modes "
    215                                      "starting with 'r'");
    216                         return -1;
    217                 }
    218 
    219                 if (mode[0] != 'r') {
    220                         memmove(mode+1, mode, strlen(mode)+1);
    221                         mode[0] = 'r';
    222                 }
    223 
    224                 if (!strchr(mode, 'b')) {
    225                         memmove(mode+2, mode+1, strlen(mode));
    226                         mode[1] = 'b';
    227                 }
    228         } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
    229                 PyErr_Format(PyExc_ValueError, "mode string must begin with "
    230                             "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode);
    231                 return -1;
    232         }
    233 
    234         return 0;
     275    char *upos;
     276    size_t len = strlen(mode);
     277
     278    if (!len) {
     279        PyErr_SetString(PyExc_ValueError, "empty mode string");
     280        return -1;
     281    }
     282
     283    upos = strchr(mode, 'U');
     284    if (upos) {
     285        memmove(upos, upos+1, len-(upos-mode)); /* incl null char */
     286
     287        if (mode[0] == 'w' || mode[0] == 'a') {
     288            PyErr_Format(PyExc_ValueError, "universal newline "
     289                         "mode can only be used with modes "
     290                         "starting with 'r'");
     291            return -1;
     292        }
     293
     294        if (mode[0] != 'r') {
     295            memmove(mode+1, mode, strlen(mode)+1);
     296            mode[0] = 'r';
     297        }
     298
     299        if (!strchr(mode, 'b')) {
     300            memmove(mode+2, mode+1, strlen(mode));
     301            mode[1] = 'b';
     302        }
     303    } else if (mode[0] != 'r' && mode[0] != 'w' && mode[0] != 'a') {
     304        PyErr_Format(PyExc_ValueError, "mode string must begin with "
     305                    "one of 'r', 'w', 'a' or 'U', not '%.200s'", mode);
     306        return -1;
     307    }
     308#ifdef Py_VERIFY_WINNT
     309    /* additional checks on NT with visual studio 2005 and higher */
     310    if (!_PyVerify_Mode_WINNT(mode)) {
     311        PyErr_Format(PyExc_ValueError, "Invalid mode ('%.50s')", mode);
     312        return -1;
     313    }
     314#endif
     315    return 0;
    235316}
    236317
     
    238319open_the_file(PyFileObject *f, char *name, char *mode)
    239320{
    240         char *newmode;
    241         assert(f != NULL);
    242         assert(PyFile_Check(f));
     321    char *newmode;
     322    assert(f != NULL);
     323    assert(PyFile_Check(f));
    243324#ifdef MS_WINDOWS
    244         /* windows ignores the passed name in order to support Unicode */
    245         assert(f->f_name != NULL);
     325    /* windows ignores the passed name in order to support Unicode */
     326    assert(f->f_name != NULL);
    246327#else
    247         assert(name != NULL);
    248 #endif
    249         assert(mode != NULL);
    250         assert(f->f_fp == NULL);
    251 
    252         /* probably need to replace 'U' by 'rb' */
    253         newmode = PyMem_MALLOC(strlen(mode) + 3);
    254         if (!newmode) {
    255                 PyErr_NoMemory();
    256                 return NULL;
    257         }
    258         strcpy(newmode, mode);
    259 
    260         if (_PyFile_SanitizeMode(newmode)) {
    261                 f = NULL;
    262                 goto cleanup;
    263         }
    264 
    265         /* rexec.py can't stop a user from getting the file() constructor --
    266            all they have to do is get *any* file object f, and then do
    267            type(f).  Here we prevent them from doing damage with it. */
    268         if (PyEval_GetRestricted()) {
    269                 PyErr_SetString(PyExc_IOError,
    270                 "file() constructor not accessible in restricted mode");
    271                 f = NULL;
    272                 goto cleanup;
    273         }
    274         errno = 0;
     328    assert(name != NULL);
     329#endif
     330    assert(mode != NULL);
     331    assert(f->f_fp == NULL);
     332
     333    /* probably need to replace 'U' by 'rb' */
     334    newmode = PyMem_MALLOC(strlen(mode) + 3);
     335    if (!newmode) {
     336        PyErr_NoMemory();
     337        return NULL;
     338    }
     339    strcpy(newmode, mode);
     340
     341    if (_PyFile_SanitizeMode(newmode)) {
     342        f = NULL;
     343        goto cleanup;
     344    }
     345
     346    /* rexec.py can't stop a user from getting the file() constructor --
     347       all they have to do is get *any* file object f, and then do
     348       type(f).  Here we prevent them from doing damage with it. */
     349    if (PyEval_GetRestricted()) {
     350        PyErr_SetString(PyExc_IOError,
     351        "file() constructor not accessible in restricted mode");
     352        f = NULL;
     353        goto cleanup;
     354    }
     355    errno = 0;
    275356
    276357#ifdef MS_WINDOWS
    277         if (PyUnicode_Check(f->f_name)) {
    278                 PyObject *wmode;
    279                 wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL);
    280                 if (f->f_name && wmode) {
    281                         FILE_BEGIN_ALLOW_THREADS(f)
    282                         /* PyUnicode_AS_UNICODE OK without thread
    283                            lock as it is a simple dereference. */
    284                         f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
    285                                           PyUnicode_AS_UNICODE(wmode));
    286                         FILE_END_ALLOW_THREADS(f)
    287                 }
    288                 Py_XDECREF(wmode);
    289         }
    290 #endif
    291         if (NULL == f->f_fp && NULL != name) {
    292                 FILE_BEGIN_ALLOW_THREADS(f)
    293                 f->f_fp = fopen(name, newmode);
    294                 FILE_END_ALLOW_THREADS(f)
    295         }
    296 
    297         if (f->f_fp == NULL) {
     358    if (PyUnicode_Check(f->f_name)) {
     359        PyObject *wmode;
     360        wmode = PyUnicode_DecodeASCII(newmode, strlen(newmode), NULL);
     361        if (f->f_name && wmode) {
     362            FILE_BEGIN_ALLOW_THREADS(f)
     363            /* PyUnicode_AS_UNICODE OK without thread
     364               lock as it is a simple dereference. */
     365            f->f_fp = _wfopen(PyUnicode_AS_UNICODE(f->f_name),
     366                              PyUnicode_AS_UNICODE(wmode));
     367            FILE_END_ALLOW_THREADS(f)
     368        }
     369        Py_XDECREF(wmode);
     370    }
     371#endif
     372    if (NULL == f->f_fp && NULL != name) {
     373        FILE_BEGIN_ALLOW_THREADS(f)
     374        f->f_fp = fopen(name, newmode);
     375        FILE_END_ALLOW_THREADS(f)
     376    }
     377
     378    if (f->f_fp == NULL) {
    298379#if defined  _MSC_VER && (_MSC_VER < 1400 || !defined(__STDC_SECURE_LIB__))
    299                 /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
    300                 * across all Windows flavors.  When it sets EINVAL varies
    301                 * across Windows flavors, the exact conditions aren't
    302                 * documented, and the answer lies in the OS's implementation
    303                 * of Win32's CreateFile function (whose source is secret).
    304                 * Seems the best we can do is map EINVAL to ENOENT.
    305                 * Starting with Visual Studio .NET 2005, EINVAL is correctly
    306                 * set by our CRT error handler (set in exceptions.c.)
    307                 */
    308                 if (errno == 0) /* bad mode string */
    309                         errno = EINVAL;
    310                 else if (errno == EINVAL) /* unknown, but not a mode string */
    311                         errno = ENOENT;
    312 #endif
    313                 /* EINVAL is returned when an invalid filename or
    314                  * an invalid mode is supplied. */
    315                 if (errno == EINVAL) {
    316                         PyObject *v;
    317                         char message[100];
    318                         PyOS_snprintf(message, 100,
    319                             "invalid mode ('%.50s') or filename", mode);
    320                         v = Py_BuildValue("(isO)", errno, message, f->f_name);
    321                         if (v != NULL) {
    322                                 PyErr_SetObject(PyExc_IOError, v);
    323                                 Py_DECREF(v);
    324                         }
    325                 }
    326                 else
    327                         PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
    328                 f = NULL;
    329         }
    330         if (f != NULL)
    331                 f = dircheck(f);
     380        /* MSVC 6 (Microsoft) leaves errno at 0 for bad mode strings,
     381        * across all Windows flavors.  When it sets EINVAL varies
     382        * across Windows flavors, the exact conditions aren't
     383        * documented, and the answer lies in the OS's implementation
     384        * of Win32's CreateFile function (whose source is secret).
     385        * Seems the best we can do is map EINVAL to ENOENT.
     386        * Starting with Visual Studio .NET 2005, EINVAL is correctly
     387        * set by our CRT error handler (set in exceptions.c.)
     388        */
     389        if (errno == 0)         /* bad mode string */
     390            errno = EINVAL;
     391        else if (errno == EINVAL) /* unknown, but not a mode string */
     392            errno = ENOENT;
     393#endif
     394        /* EINVAL is returned when an invalid filename or
     395         * an invalid mode is supplied. */
     396        if (errno == EINVAL) {
     397            PyObject *v;
     398            char message[100];
     399            PyOS_snprintf(message, 100,
     400                "invalid mode ('%.50s') or filename", mode);
     401            v = Py_BuildValue("(isO)", errno, message, f->f_name);
     402            if (v != NULL) {
     403                PyErr_SetObject(PyExc_IOError, v);
     404                Py_DECREF(v);
     405            }
     406        }
     407        else
     408            PyErr_SetFromErrnoWithFilenameObject(PyExc_IOError, f->f_name);
     409        f = NULL;
     410    }
     411    if (f != NULL)
     412        f = dircheck(f);
    332413
    333414cleanup:
    334         PyMem_FREE(newmode);
    335 
    336         return (PyObject *)f;
     415    PyMem_FREE(newmode);
     416
     417    return (PyObject *)f;
    337418}
    338419
     
    340421close_the_file(PyFileObject *f)
    341422{
    342         int sts = 0;
    343         int (*local_close)(FILE *);
    344         FILE *local_fp = f->f_fp;
    345         if (local_fp != NULL) {
    346                 local_close = f->f_close;
    347                 if (local_close != NULL && f->unlocked_count > 0) {
    348                         if (f->ob_refcnt > 0) {
    349                                 PyErr_SetString(PyExc_IOError,
    350                                         "close() called during concurrent "
    351                                         "operation on the same file object.");
    352                         } else {
    353                                 /* This should not happen unless someone is
    354                                  * carelessly playing with the PyFileObject
    355                                  * struct fields and/or its associated FILE
    356                                  * pointer. */
    357                                 PyErr_SetString(PyExc_SystemError,
    358                                         "PyFileObject locking error in "
    359                                         "destructor (refcnt <= 0 at close).");
    360                         }
    361                         return NULL;
    362                 }
    363                 /* NULL out the FILE pointer before releasing the GIL, because
    364                  * it will not be valid anymore after the close() function is
    365                  * called. */
    366                 f->f_fp = NULL;
    367                 if (local_close != NULL) {
    368                         Py_BEGIN_ALLOW_THREADS
    369                         errno = 0;
    370                         sts = (*local_close)(local_fp);
    371                         Py_END_ALLOW_THREADS
    372                         if (sts == EOF)
    373                                 return PyErr_SetFromErrno(PyExc_IOError);
    374                         if (sts != 0)
    375                                 return PyInt_FromLong((long)sts);
    376                 }
    377         }
    378         Py_RETURN_NONE;
     423    int sts = 0;
     424    int (*local_close)(FILE *);
     425    FILE *local_fp = f->f_fp;
     426    char *local_setbuf = f->f_setbuf;
     427    if (local_fp != NULL) {
     428        local_close = f->f_close;
     429        if (local_close != NULL && f->unlocked_count > 0) {
     430            if (f->ob_refcnt > 0) {
     431                PyErr_SetString(PyExc_IOError,
     432                    "close() called during concurrent "
     433                    "operation on the same file object.");
     434            } else {
     435                /* This should not happen unless someone is
     436                 * carelessly playing with the PyFileObject
     437                 * struct fields and/or its associated FILE
     438                 * pointer. */
     439                PyErr_SetString(PyExc_SystemError,
     440                    "PyFileObject locking error in "
     441                    "destructor (refcnt <= 0 at close).");
     442            }
     443            return NULL;
     444        }
     445        /* NULL out the FILE pointer before releasing the GIL, because
     446         * it will not be valid anymore after the close() function is
     447         * called. */
     448        f->f_fp = NULL;
     449        if (local_close != NULL) {
     450            /* Issue #9295: must temporarily reset f_setbuf so that another
     451               thread doesn't free it when running file_close() concurrently.
     452               Otherwise this close() will crash when flushing the buffer. */
     453            f->f_setbuf = NULL;
     454            Py_BEGIN_ALLOW_THREADS
     455            errno = 0;
     456            sts = (*local_close)(local_fp);
     457            Py_END_ALLOW_THREADS
     458            f->f_setbuf = local_setbuf;
     459            if (sts == EOF)
     460                return PyErr_SetFromErrno(PyExc_IOError);
     461            if (sts != 0)
     462                return PyInt_FromLong((long)sts);
     463        }
     464    }
     465    Py_RETURN_NONE;
    379466}
    380467
     
    382469PyFile_FromFile(FILE *fp, char *name, char *mode, int (*close)(FILE *))
    383470{
    384         PyFileObject *f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type,
    385                                                              NULL, NULL);
    386         if (f != NULL) {
    387                 PyObject *o_name = PyString_FromString(name);
    388                 if (o_name == NULL)
    389                         return NULL;
    390                 if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
    391                         Py_DECREF(f);
    392                         f = NULL;
    393                 }
    394                 Py_DECREF(o_name);
    395         }
    396         return (PyObject *) f;
     471    PyFileObject *f;
     472    PyObject *o_name;
     473
     474    f = (PyFileObject *)PyFile_Type.tp_new(&PyFile_Type, NULL, NULL);
     475    if (f == NULL)
     476        return NULL;
     477    o_name = PyString_FromString(name);
     478    if (o_name == NULL) {
     479        if (close != NULL && fp != NULL)
     480            close(fp);
     481        Py_DECREF(f);
     482        return NULL;
     483    }
     484    if (fill_file_fields(f, fp, o_name, mode, close) == NULL) {
     485        Py_DECREF(f);
     486        Py_DECREF(o_name);
     487        return NULL;
     488    }
     489    Py_DECREF(o_name);
     490    return (PyObject *)f;
    397491}
    398492
     
    400494PyFile_FromString(char *name, char *mode)
    401495{
    402         extern int fclose(FILE *);
    403         PyFileObject *f;
    404 
    405         f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
    406         if (f != NULL) {
    407                 if (open_the_file(f, name, mode) == NULL) {
    408                         Py_DECREF(f);
    409                         f = NULL;
    410                 }
    411         }
    412         return (PyObject *)f;
     496    extern int fclose(FILE *);
     497    PyFileObject *f;
     498
     499    f = (PyFileObject *)PyFile_FromFile((FILE *)NULL, name, mode, fclose);
     500    if (f != NULL) {
     501        if (open_the_file(f, name, mode) == NULL) {
     502            Py_DECREF(f);
     503            f = NULL;
     504        }
     505    }
     506    return (PyObject *)f;
    413507}
    414508
     
    416510PyFile_SetBufSize(PyObject *f, int bufsize)
    417511{
    418         PyFileObject *file = (PyFileObject *)f;
    419         if (bufsize >= 0) {
    420                 int type;
    421                 switch (bufsize) {
    422                 case 0:
    423                         type = _IONBF;
    424                         break;
     512    PyFileObject *file = (PyFileObject *)f;
     513    if (bufsize >= 0) {
     514        int type;
     515        switch (bufsize) {
     516        case 0:
     517            type = _IONBF;
     518            break;
    425519#ifdef HAVE_SETVBUF
    426                 case 1:
    427                         type = _IOLBF;
    428                         bufsize = BUFSIZ;
    429                         break;
    430 #endif
    431                 default:
    432                         type = _IOFBF;
     520        case 1:
     521            type = _IOLBF;
     522            bufsize = BUFSIZ;
     523            break;
     524#endif
     525        default:
     526            type = _IOFBF;
    433527#ifndef HAVE_SETVBUF
    434                         bufsize = BUFSIZ;
    435 #endif
    436                         break;
    437                 }
    438                 fflush(file->f_fp);
    439                 if (type == _IONBF) {
    440                         PyMem_Free(file->f_setbuf);
    441                         file->f_setbuf = NULL;
    442                 } else {
    443                         file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
    444                                                                 bufsize);
    445                 }
     528            bufsize = BUFSIZ;
     529#endif
     530            break;
     531        }
     532        fflush(file->f_fp);
     533        if (type == _IONBF) {
     534            PyMem_Free(file->f_setbuf);
     535            file->f_setbuf = NULL;
     536        } else {
     537            file->f_setbuf = (char *)PyMem_Realloc(file->f_setbuf,
     538                                                    bufsize);
     539        }
    446540#ifdef HAVE_SETVBUF
    447                 setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
     541        setvbuf(file->f_fp, file->f_setbuf, type, bufsize);
    448542#else /* !HAVE_SETVBUF */
    449                 setbuf(file->f_fp, file->f_setbuf);
     543        setbuf(file->f_fp, file->f_setbuf);
    450544#endif /* !HAVE_SETVBUF */
    451         }
     545    }
    452546}
    453547
     
    458552PyFile_SetEncoding(PyObject *f, const char *enc)
    459553{
    460         return PyFile_SetEncodingAndErrors(f, enc, NULL);
     554    return PyFile_SetEncodingAndErrors(f, enc, NULL);
    461555}
    462556
     
    464558PyFile_SetEncodingAndErrors(PyObject *f, const char *enc, char* errors)
    465559{
    466         PyFileObject *file = (PyFileObject*)f;
    467         PyObject *str, *oerrors;
    468 
    469         assert(PyFile_Check(f));
    470         str = PyString_FromString(enc);
    471         if (!str)
    472                 return 0;
    473         if (errors) {
    474                 oerrors = PyString_FromString(errors);
    475                 if (!oerrors) {
    476                         Py_DECREF(str);
    477                         return 0;
    478                 }
    479         } else {
    480                 oerrors = Py_None;
    481                 Py_INCREF(Py_None);
    482         }
    483         Py_DECREF(file->f_encoding);
    484         file->f_encoding = str;
    485         Py_DECREF(file->f_errors);
    486         file->f_errors = oerrors;
    487         return 1;
     560    PyFileObject *file = (PyFileObject*)f;
     561    PyObject *str, *oerrors;
     562
     563    assert(PyFile_Check(f));
     564    str = PyString_FromString(enc);
     565    if (!str)
     566        return 0;
     567    if (errors) {
     568        oerrors = PyString_FromString(errors);
     569        if (!oerrors) {
     570            Py_DECREF(str);
     571            return 0;
     572        }
     573    } else {
     574        oerrors = Py_None;
     575        Py_INCREF(Py_None);
     576    }
     577    Py_DECREF(file->f_encoding);
     578    file->f_encoding = str;
     579    Py_DECREF(file->f_errors);
     580    file->f_errors = oerrors;
     581    return 1;
    488582}
    489583
     
    491585err_closed(void)
    492586{
    493         PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
    494         return NULL;
     587    PyErr_SetString(PyExc_ValueError, "I/O operation on closed file");
     588    return NULL;
    495589}
    496590
     
    498592err_mode(char *action)
    499593{
    500         PyErr_Format(PyExc_IOError, "File not open for %s", action);
    501         return NULL;
     594    PyErr_Format(PyExc_IOError, "File not open for %s", action);
     595    return NULL;
    502596}
    503597
     
    508602err_iterbuffered(void)
    509603{
    510         PyErr_SetString(PyExc_ValueError,
    511                 "Mixing iteration and read methods would lose data");
    512         return NULL;
     604    PyErr_SetString(PyExc_ValueError,
     605        "Mixing iteration and read methods would lose data");
     606    return NULL;
    513607}
    514608
     
    520614file_dealloc(PyFileObject *f)
    521615{
    522         PyObject *ret;
    523         if (f->weakreflist != NULL)
    524                 PyObject_ClearWeakRefs((PyObject *) f);
    525         ret = close_the_file(f);
    526         if (!ret) {
    527                 PySys_WriteStderr("close failed in file object destructor:\n");
    528                 PyErr_Print();
    529         }
    530         else {
    531                 Py_DECREF(ret);
    532         }
    533         PyMem_Free(f->f_setbuf);
    534         Py_XDECREF(f->f_name);
    535         Py_XDECREF(f->f_mode);
    536         Py_XDECREF(f->f_encoding);
    537         Py_XDECREF(f->f_errors);
    538         drop_readahead(f);
    539         Py_TYPE(f)->tp_free((PyObject *)f);
     616    PyObject *ret;
     617    if (f->weakreflist != NULL)
     618        PyObject_ClearWeakRefs((PyObject *) f);
     619    ret = close_the_file(f);
     620    if (!ret) {
     621        PySys_WriteStderr("close failed in file object destructor:\n");
     622        PyErr_Print();
     623    }
     624    else {
     625        Py_DECREF(ret);
     626    }
     627    PyMem_Free(f->f_setbuf);
     628    Py_XDECREF(f->f_name);
     629    Py_XDECREF(f->f_mode);
     630    Py_XDECREF(f->f_encoding);
     631    Py_XDECREF(f->f_errors);
     632    drop_readahead(f);
     633    Py_TYPE(f)->tp_free((PyObject *)f);
    540634}
    541635
     
    543637file_repr(PyFileObject *f)
    544638{
    545         if (PyUnicode_Check(f->f_name)) {
     639    PyObject *ret = NULL;
     640    PyObject *name = NULL;
     641    if (PyUnicode_Check(f->f_name)) {
    546642#ifdef Py_USING_UNICODE
    547                 PyObject *ret = NULL;
    548                 PyObject *name = PyUnicode_AsUnicodeEscapeString(f->f_name);
    549                 const char *name_str = name ? PyString_AsString(name) : "?";
    550                 ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
    551                                    f->f_fp == NULL ? "closed" : "open",
    552                                    name_str,
    553                                    PyString_AsString(f->f_mode),
    554                                    f);
    555                 Py_XDECREF(name);
    556                 return ret;
    557 #endif
    558         } else {
    559                 return PyString_FromFormat("<%s file '%s', mode '%s' at %p>",
    560                                    f->f_fp == NULL ? "closed" : "open",
    561                                    PyString_AsString(f->f_name),
    562                                    PyString_AsString(f->f_mode),
    563                                    f);
    564         }
     643        const char *name_str;
     644        name = PyUnicode_AsUnicodeEscapeString(f->f_name);
     645        name_str = name ? PyString_AsString(name) : "?";
     646        ret = PyString_FromFormat("<%s file u'%s', mode '%s' at %p>",
     647                           f->f_fp == NULL ? "closed" : "open",
     648                           name_str,
     649                           PyString_AsString(f->f_mode),
     650                           f);
     651        Py_XDECREF(name);
     652        return ret;
     653#endif
     654    } else {
     655        name = PyObject_Repr(f->f_name);
     656        if (name == NULL)
     657            return NULL;
     658        ret = PyString_FromFormat("<%s file %s, mode '%s' at %p>",
     659                           f->f_fp == NULL ? "closed" : "open",
     660                           PyString_AsString(name),
     661                           PyString_AsString(f->f_mode),
     662                           f);
     663        Py_XDECREF(name);
     664        return ret;
     665    }
    565666}
    566667
     
    568669file_close(PyFileObject *f)
    569670{
    570         PyObject *sts = close_the_file(f);
    571         PyMem_Free(f->f_setbuf);
    572         f->f_setbuf = NULL;
    573         return sts;
     671    PyObject *sts = close_the_file(f);
     672    if (sts) {
     673        PyMem_Free(f->f_setbuf);
     674        f->f_setbuf = NULL;
     675    }
     676    return sts;
    574677}
    575678
     
    593696{
    594697#if !defined(HAVE_LARGEFILE_SUPPORT)
    595         return fseek(fp, offset, whence);
     698    return fseek(fp, offset, whence);
    596699#elif defined(HAVE_FSEEKO) && SIZEOF_OFF_T >= 8
    597         return fseeko(fp, offset, whence);
     700    return fseeko(fp, offset, whence);
    598701#elif defined(HAVE_FSEEK64)
    599         return fseek64(fp, offset, whence);
     702    return fseek64(fp, offset, whence);
    600703#elif defined(__BEOS__)
    601         return _fseek(fp, offset, whence);
     704    return _fseek(fp, offset, whence);
    602705#elif SIZEOF_FPOS_T >= 8
    603         /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
    604            and fgetpos() to implement fseek()*/
    605         fpos_t pos;
    606         switch (whence) {
    607         case SEEK_END:
     706    /* lacking a 64-bit capable fseek(), use a 64-bit capable fsetpos()
     707       and fgetpos() to implement fseek()*/
     708    fpos_t pos;
     709    switch (whence) {
     710    case SEEK_END:
    608711#ifdef MS_WINDOWS
    609                 fflush(fp);
    610                 if (_lseeki64(fileno(fp), 0, 2) == -1)
    611                         return -1;
     712        fflush(fp);
     713        if (_lseeki64(fileno(fp), 0, 2) == -1)
     714            return -1;
    612715#else
    613                 if (fseek(fp, 0, SEEK_END) != 0)
    614                         return -1;
    615 #endif
    616                 /* fall through */
    617         case SEEK_CUR:
    618                 if (fgetpos(fp, &pos) != 0)
    619                         return -1;
    620                 offset += pos;
    621                 break;
    622         /* case SEEK_SET: break; */
    623         }
    624         return fsetpos(fp, &offset);
     716        if (fseek(fp, 0, SEEK_END) != 0)
     717            return -1;
     718#endif
     719        /* fall through */
     720    case SEEK_CUR:
     721        if (fgetpos(fp, &pos) != 0)
     722            return -1;
     723        offset += pos;
     724        break;
     725    /* case SEEK_SET: break; */
     726    }
     727    return fsetpos(fp, &offset);
    625728#else
    626729#error "Large file support, but no way to fseek."
     
    636739{
    637740#if !defined(HAVE_LARGEFILE_SUPPORT)
    638         return ftell(fp);
     741    return ftell(fp);
    639742#elif defined(HAVE_FTELLO) && SIZEOF_OFF_T >= 8
    640         return ftello(fp);
     743    return ftello(fp);
    641744#elif defined(HAVE_FTELL64)
    642         return ftell64(fp);
     745    return ftell64(fp);
    643746#elif SIZEOF_FPOS_T >= 8
    644         fpos_t pos;
    645         if (fgetpos(fp, &pos) != 0)
    646                 return -1;
    647         return pos;
     747    fpos_t pos;
     748    if (fgetpos(fp, &pos) != 0)
     749        return -1;
     750    return pos;
    648751#else
    649752#error "Large file support, but no way to ftell."
     
    655758file_seek(PyFileObject *f, PyObject *args)
    656759{
    657         int whence;
    658         int ret;
    659         Py_off_t offset;
    660         PyObject *offobj, *off_index;
    661 
    662         if (f->f_fp == NULL)
    663                 return err_closed();
    664         drop_readahead(f);
    665         whence = 0;
    666         if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
    667                 return NULL;
    668         off_index = PyNumber_Index(offobj);
    669         if (!off_index) {
    670                 if (!PyFloat_Check(offobj))
    671                         return NULL;
    672                 /* Deprecated in 2.6 */
    673                 PyErr_Clear();
    674                 if (PyErr_WarnEx(PyExc_DeprecationWarning,
    675                                 "integer argument expected, got float",
    676                                 1) < 0)
    677                         return NULL;
    678                 off_index = offobj;
    679                 Py_INCREF(offobj);
    680         }
     760    int whence;
     761    int ret;
     762    Py_off_t offset;
     763    PyObject *offobj, *off_index;
     764
     765    if (f->f_fp == NULL)
     766        return err_closed();
     767    drop_readahead(f);
     768    whence = 0;
     769    if (!PyArg_ParseTuple(args, "O|i:seek", &offobj, &whence))
     770        return NULL;
     771    off_index = PyNumber_Index(offobj);
     772    if (!off_index) {
     773        if (!PyFloat_Check(offobj))
     774            return NULL;
     775        /* Deprecated in 2.6 */
     776        PyErr_Clear();
     777        if (PyErr_WarnEx(PyExc_DeprecationWarning,
     778                        "integer argument expected, got float",
     779                        1) < 0)
     780            return NULL;
     781        off_index = offobj;
     782        Py_INCREF(offobj);
     783    }
    681784#if !defined(HAVE_LARGEFILE_SUPPORT)
    682         offset = PyInt_AsLong(off_index);
     785    offset = PyInt_AsLong(off_index);
    683786#else
    684         offset = PyLong_Check(off_index) ?
    685                 PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index);
    686 #endif
    687         Py_DECREF(off_index);
    688         if (PyErr_Occurred())
    689                 return NULL;
    690 
    691         FILE_BEGIN_ALLOW_THREADS(f)
    692         errno = 0;
    693         ret = _portable_fseek(f->f_fp, offset, whence);
    694         FILE_END_ALLOW_THREADS(f)
    695 
    696         if (ret != 0) {
    697                 PyErr_SetFromErrno(PyExc_IOError);
    698                 clearerr(f->f_fp);
    699                 return NULL;
    700         }
    701         f->f_skipnextlf = 0;
    702         Py_INCREF(Py_None);
    703         return Py_None;
     787    offset = PyLong_Check(off_index) ?
     788        PyLong_AsLongLong(off_index) : PyInt_AsLong(off_index);
     789#endif
     790    Py_DECREF(off_index);
     791    if (PyErr_Occurred())
     792        return NULL;
     793
     794    FILE_BEGIN_ALLOW_THREADS(f)
     795    errno = 0;
     796    ret = _portable_fseek(f->f_fp, offset, whence);
     797    FILE_END_ALLOW_THREADS(f)
     798
     799    if (ret != 0) {
     800        PyErr_SetFromErrno(PyExc_IOError);
     801        clearerr(f->f_fp);
     802        return NULL;
     803    }
     804    f->f_skipnextlf = 0;
     805    Py_INCREF(Py_None);
     806    return Py_None;
    704807}
    705808
     
    709812file_truncate(PyFileObject *f, PyObject *args)
    710813{
    711         Py_off_t newsize;
    712         PyObject *newsizeobj = NULL;
    713         Py_off_t initialpos;
    714         int ret;
    715 
    716         if (f->f_fp == NULL)
    717                 return err_closed();
    718         if (!f->writable)
    719                 return err_mode("writing");
    720         if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
    721                 return NULL;
    722 
    723         /* Get current file position.  If the file happens to be open for
    724         * update and the last operation was an input operation, C doesn't
    725         * define what the later fflush() will do, but we promise truncate()
    726         * won't change the current position (and fflush() *does* change it
    727         * then at least on Windows).  The easiest thing is to capture
    728         * current pos now and seek back to it at the end.
    729         */
    730         FILE_BEGIN_ALLOW_THREADS(f)
    731         errno = 0;
    732         initialpos = _portable_ftell(f->f_fp);
    733         FILE_END_ALLOW_THREADS(f)
    734         if (initialpos == -1)
    735                 goto onioerror;
    736 
    737         /* Set newsize to current postion if newsizeobj NULL, else to the
    738         * specified value.
    739         */
    740         if (newsizeobj != NULL) {
     814    Py_off_t newsize;
     815    PyObject *newsizeobj = NULL;
     816    Py_off_t initialpos;
     817    int ret;
     818
     819    if (f->f_fp == NULL)
     820        return err_closed();
     821    if (!f->writable)
     822        return err_mode("writing");
     823    if (!PyArg_UnpackTuple(args, "truncate", 0, 1, &newsizeobj))
     824        return NULL;
     825
     826    /* Get current file position.  If the file happens to be open for
     827    * update and the last operation was an input operation, C doesn't
     828    * define what the later fflush() will do, but we promise truncate()
     829    * won't change the current position (and fflush() *does* change it
     830    * then at least on Windows).  The easiest thing is to capture
     831    * current pos now and seek back to it at the end.
     832    */
     833    FILE_BEGIN_ALLOW_THREADS(f)
     834    errno = 0;
     835    initialpos = _portable_ftell(f->f_fp);
     836    FILE_END_ALLOW_THREADS(f)
     837    if (initialpos == -1)
     838        goto onioerror;
     839
     840    /* Set newsize to current postion if newsizeobj NULL, else to the
     841    * specified value.
     842    */
     843    if (newsizeobj != NULL) {
    741844#if !defined(HAVE_LARGEFILE_SUPPORT)
    742                 newsize = PyInt_AsLong(newsizeobj);
     845        newsize = PyInt_AsLong(newsizeobj);
    743846#else
    744                 newsize = PyLong_Check(newsizeobj) ?
    745                                 PyLong_AsLongLong(newsizeobj) :
    746                                 PyInt_AsLong(newsizeobj);
    747 #endif
    748                 if (PyErr_Occurred())
    749                         return NULL;
    750         }
    751         else /* default to current position */
    752                 newsize = initialpos;
    753 
    754         /* Flush the stream.  We're mixing stream-level I/O with lower-level
    755         * I/O, and a flush may be necessary to synch both platform views
    756         * of the current file state.
    757         */
    758         FILE_BEGIN_ALLOW_THREADS(f)
    759         errno = 0;
    760         ret = fflush(f->f_fp);
    761         FILE_END_ALLOW_THREADS(f)
    762         if (ret != 0)
    763                 goto onioerror;
     847        newsize = PyLong_Check(newsizeobj) ?
     848                        PyLong_AsLongLong(newsizeobj) :
     849                PyInt_AsLong(newsizeobj);
     850#endif
     851        if (PyErr_Occurred())
     852            return NULL;
     853    }
     854    else /* default to current position */
     855        newsize = initialpos;
     856
     857    /* Flush the stream.  We're mixing stream-level I/O with lower-level
     858    * I/O, and a flush may be necessary to synch both platform views
     859    * of the current file state.
     860    */
     861    FILE_BEGIN_ALLOW_THREADS(f)
     862    errno = 0;
     863    ret = fflush(f->f_fp);
     864    FILE_END_ALLOW_THREADS(f)
     865    if (ret != 0)
     866        goto onioerror;
    764867
    765868#ifdef MS_WINDOWS
    766         /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
    767            so don't even try using it. */
    768         {
    769                 HANDLE hFile;
    770 
    771                 /* Have to move current pos to desired endpoint on Windows. */
    772                 FILE_BEGIN_ALLOW_THREADS(f)
    773                 errno = 0;
    774                 ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
    775                 FILE_END_ALLOW_THREADS(f)
    776                 if (ret)
    777                         goto onioerror;
    778 
    779                 /* Truncate.  Note that this may grow the file! */
    780                 FILE_BEGIN_ALLOW_THREADS(f)
    781                 errno = 0;
    782                 hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
    783                 ret = hFile == (HANDLE)-1;
    784                 if (ret == 0) {
    785                         ret = SetEndOfFile(hFile) == 0;
    786                         if (ret)
    787                                 errno = EACCES;
    788                 }
    789                 FILE_END_ALLOW_THREADS(f)
    790                 if (ret)
    791                         goto onioerror;
    792         }
     869    /* MS _chsize doesn't work if newsize doesn't fit in 32 bits,
     870       so don't even try using it. */
     871    {
     872        HANDLE hFile;
     873
     874        /* Have to move current pos to desired endpoint on Windows. */
     875        FILE_BEGIN_ALLOW_THREADS(f)
     876        errno = 0;
     877        ret = _portable_fseek(f->f_fp, newsize, SEEK_SET) != 0;
     878        FILE_END_ALLOW_THREADS(f)
     879        if (ret)
     880            goto onioerror;
     881
     882        /* Truncate.  Note that this may grow the file! */
     883        FILE_BEGIN_ALLOW_THREADS(f)
     884        errno = 0;
     885        hFile = (HANDLE)_get_osfhandle(fileno(f->f_fp));
     886        ret = hFile == (HANDLE)-1;
     887        if (ret == 0) {
     888            ret = SetEndOfFile(hFile) == 0;
     889            if (ret)
     890                errno = EACCES;
     891        }
     892        FILE_END_ALLOW_THREADS(f)
     893        if (ret)
     894            goto onioerror;
     895    }
    793896#else
    794         FILE_BEGIN_ALLOW_THREADS(f)
    795         errno = 0;
    796         ret = ftruncate(fileno(f->f_fp), newsize);
    797         FILE_END_ALLOW_THREADS(f)
    798         if (ret != 0)
    799                 goto onioerror;
     897    FILE_BEGIN_ALLOW_THREADS(f)
     898    errno = 0;
     899    ret = ftruncate(fileno(f->f_fp), newsize);
     900    FILE_END_ALLOW_THREADS(f)
     901    if (ret != 0)
     902        goto onioerror;
    800903#endif /* !MS_WINDOWS */
    801904
    802         /* Restore original file position. */
    803         FILE_BEGIN_ALLOW_THREADS(f)
    804         errno = 0;
    805         ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
    806         FILE_END_ALLOW_THREADS(f)
    807         if (ret)
    808                 goto onioerror;
    809 
    810         Py_INCREF(Py_None);
    811         return Py_None;
     905    /* Restore original file position. */
     906    FILE_BEGIN_ALLOW_THREADS(f)
     907    errno = 0;
     908    ret = _portable_fseek(f->f_fp, initialpos, SEEK_SET) != 0;
     909    FILE_END_ALLOW_THREADS(f)
     910    if (ret)
     911        goto onioerror;
     912
     913    Py_INCREF(Py_None);
     914    return Py_None;
    812915
    813916onioerror:
    814         PyErr_SetFromErrno(PyExc_IOError);
    815         clearerr(f->f_fp);
    816         return NULL;
     917    PyErr_SetFromErrno(PyExc_IOError);
     918    clearerr(f->f_fp);
     919    return NULL;
    817920}
    818921#endif /* HAVE_FTRUNCATE */
     
    821924file_tell(PyFileObject *f)
    822925{
    823         Py_off_t pos;
    824 
    825         if (f->f_fp == NULL)
    826                 return err_closed();
    827         FILE_BEGIN_ALLOW_THREADS(f)
    828         errno = 0;
    829         pos = _portable_ftell(f->f_fp);
    830         FILE_END_ALLOW_THREADS(f)
    831 
    832         if (pos == -1) {
    833                 PyErr_SetFromErrno(PyExc_IOError);
    834                 clearerr(f->f_fp);
    835                 return NULL;
    836         }
    837         if (f->f_skipnextlf) {
    838                 int c;
    839                 c = GETC(f->f_fp);
    840                 if (c == '\n') {
    841                         f->f_newlinetypes |= NEWLINE_CRLF;
    842                         pos++;
    843                         f->f_skipnextlf = 0;
    844                 } else if (c != EOF) ungetc(c, f->f_fp);
    845         }
     926    Py_off_t pos;
     927
     928    if (f->f_fp == NULL)
     929        return err_closed();
     930    FILE_BEGIN_ALLOW_THREADS(f)
     931    errno = 0;
     932    pos = _portable_ftell(f->f_fp);
     933    FILE_END_ALLOW_THREADS(f)
     934
     935    if (pos == -1) {
     936        PyErr_SetFromErrno(PyExc_IOError);
     937        clearerr(f->f_fp);
     938        return NULL;
     939    }
     940    if (f->f_skipnextlf) {
     941        int c;
     942        c = GETC(f->f_fp);
     943        if (c == '\n') {
     944            f->f_newlinetypes |= NEWLINE_CRLF;
     945            pos++;
     946            f->f_skipnextlf = 0;
     947        } else if (c != EOF) ungetc(c, f->f_fp);
     948    }
    846949#if !defined(HAVE_LARGEFILE_SUPPORT)
    847         return PyInt_FromLong(pos);
     950    return PyInt_FromLong(pos);
    848951#else
    849         return PyLong_FromLongLong(pos);
     952    return PyLong_FromLongLong(pos);
    850953#endif
    851954}
     
    854957file_fileno(PyFileObject *f)
    855958{
    856         if (f->f_fp == NULL)
    857                 return err_closed();
    858         return PyInt_FromLong((long) fileno(f->f_fp));
     959    if (f->f_fp == NULL)
     960        return err_closed();
     961    return PyInt_FromLong((long) fileno(f->f_fp));
    859962}
    860963
     
    862965file_flush(PyFileObject *f)
    863966{
    864         int res;
    865 
    866         if (f->f_fp == NULL)
    867                 return err_closed();
    868         FILE_BEGIN_ALLOW_THREADS(f)
    869         errno = 0;
    870         res = fflush(f->f_fp);
    871         FILE_END_ALLOW_THREADS(f)
    872         if (res != 0) {
    873                 PyErr_SetFromErrno(PyExc_IOError);
    874                 clearerr(f->f_fp);
    875                 return NULL;
    876         }
    877         Py_INCREF(Py_None);
    878         return Py_None;
     967    int res;
     968
     969    if (f->f_fp == NULL)
     970        return err_closed();
     971    FILE_BEGIN_ALLOW_THREADS(f)
     972    errno = 0;
     973    res = fflush(f->f_fp);
     974    FILE_END_ALLOW_THREADS(f)
     975    if (res != 0) {
     976        PyErr_SetFromErrno(PyExc_IOError);
     977        clearerr(f->f_fp);
     978        return NULL;
     979    }
     980    Py_INCREF(Py_None);
     981    return Py_None;
    879982}
    880983
     
    882985file_isatty(PyFileObject *f)
    883986{
    884         long res;
    885         if (f->f_fp == NULL)
    886                 return err_closed();
    887         FILE_BEGIN_ALLOW_THREADS(f)
    888         res = isatty((int)fileno(f->f_fp));
    889         FILE_END_ALLOW_THREADS(f)
    890         return PyBool_FromLong(res);
     987    long res;
     988    if (f->f_fp == NULL)
     989        return err_closed();
     990    FILE_BEGIN_ALLOW_THREADS(f)
     991    res = isatty((int)fileno(f->f_fp));
     992    FILE_END_ALLOW_THREADS(f)
     993    return PyBool_FromLong(res);
    891994}
    892995
     
    8981001#endif
    8991002
    900 #if SIZEOF_INT < 4
    901 #define BIGCHUNK  (512 * 32)
    902 #else
    903 #define BIGCHUNK  (512 * 1024)
    904 #endif
    905 
    9061003static size_t
    9071004new_buffersize(PyFileObject *f, size_t currentsize)
    9081005{
    9091006#ifdef HAVE_FSTAT
    910         off_t pos, end;
    911         struct stat st;
    912         if (fstat(fileno(f->f_fp), &st) == 0) {
    913                 end = st.st_size;
    914                 /* The following is not a bug: we really need to call lseek()
    915                    *and* ftell().  The reason is that some stdio libraries
    916                    mistakenly flush their buffer when ftell() is called and
    917                    the lseek() call it makes fails, thereby throwing away
    918                    data that cannot be recovered in any way.  To avoid this,
    919                    we first test lseek(), and only call ftell() if lseek()
    920                    works.  We can't use the lseek() value either, because we
    921                    need to take the amount of buffered data into account.
    922                    (Yet another reason why stdio stinks. :-) */
    923                 pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
    924                 if (pos >= 0) {
    925                         pos = ftell(f->f_fp);
    926                 }
    927                 if (pos < 0)
    928                         clearerr(f->f_fp);
    929                 if (end > pos && pos >= 0)
    930                         return currentsize + end - pos + 1;
    931                 /* Add 1 so if the file were to grow we'd notice. */
    932         }
    933 #endif
    934         if (currentsize > SMALLCHUNK) {
    935                 /* Keep doubling until we reach BIGCHUNK;
    936                    then keep adding BIGCHUNK. */
    937                 if (currentsize <= BIGCHUNK)
    938                         return currentsize + currentsize;
    939                 else
    940                         return currentsize + BIGCHUNK;
    941         }
    942         return currentsize + SMALLCHUNK;
     1007    off_t pos, end;
     1008    struct stat st;
     1009    if (fstat(fileno(f->f_fp), &st) == 0) {
     1010        end = st.st_size;
     1011        /* The following is not a bug: we really need to call lseek()
     1012           *and* ftell().  The reason is that some stdio libraries
     1013           mistakenly flush their buffer when ftell() is called and
     1014           the lseek() call it makes fails, thereby throwing away
     1015           data that cannot be recovered in any way.  To avoid this,
     1016           we first test lseek(), and only call ftell() if lseek()
     1017           works.  We can't use the lseek() value either, because we
     1018           need to take the amount of buffered data into account.
     1019           (Yet another reason why stdio stinks. :-) */
     1020        pos = lseek(fileno(f->f_fp), 0L, SEEK_CUR);
     1021        if (pos >= 0) {
     1022            pos = ftell(f->f_fp);
     1023        }
     1024        if (pos < 0)
     1025            clearerr(f->f_fp);
     1026        if (end > pos && pos >= 0)
     1027            return currentsize + end - pos + 1;
     1028        /* Add 1 so if the file were to grow we'd notice. */
     1029    }
     1030#endif
     1031    /* Expand the buffer by an amount proportional to the current size,
     1032       giving us amortized linear-time behavior. Use a less-than-double
     1033       growth factor to avoid excessive allocation. */
     1034    return currentsize + (currentsize >> 3) + 6;
    9431035}
    9441036
     
    9601052file_read(PyFileObject *f, PyObject *args)
    9611053{
    962         long bytesrequested = -1;
    963         size_t bytesread, buffersize, chunksize;
    964         PyObject *v;
    965 
    966         if (f->f_fp == NULL)
    967                 return err_closed();
    968         if (!f->readable)
    969                 return err_mode("reading");
    970         /* refuse to mix with f.next() */
    971         if (f->f_buf != NULL &&
    972             (f->f_bufend - f->f_bufptr) > 0 &&
    973             f->f_buf[0] != '\0')
    974                 return err_iterbuffered();
    975         if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
    976                 return NULL;
    977         if (bytesrequested < 0)
    978                 buffersize = new_buffersize(f, (size_t)0);
    979         else
    980                 buffersize = bytesrequested;
    981         if (buffersize > PY_SSIZE_T_MAX) {
    982                 PyErr_SetString(PyExc_OverflowError,
    983         "requested number of bytes is more than a Python string can hold");
    984                 return NULL;
    985         }
    986         v = PyString_FromStringAndSize((char *)NULL, buffersize);
    987         if (v == NULL)
    988                 return NULL;
    989         bytesread = 0;
    990         for (;;) {
    991                 FILE_BEGIN_ALLOW_THREADS(f)
    992                 errno = 0;
    993                 chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
    994                           buffersize - bytesread, f->f_fp, (PyObject *)f);
    995                 FILE_END_ALLOW_THREADS(f)
    996                 if (chunksize == 0) {
    997                         if (!ferror(f->f_fp))
    998                                 break;
    999                         clearerr(f->f_fp);
    1000                         /* When in non-blocking mode, data shouldn't
    1001                          * be discarded if a blocking signal was
    1002                          * received. That will also happen if
    1003                          * chunksize != 0, but bytesread < buffersize. */
    1004                         if (bytesread > 0 && BLOCKED_ERRNO(errno))
    1005                                 break;
    1006                         PyErr_SetFromErrno(PyExc_IOError);
    1007                         Py_DECREF(v);
    1008                         return NULL;
    1009                 }
    1010                 bytesread += chunksize;
    1011                 if (bytesread < buffersize) {
    1012                         clearerr(f->f_fp);
    1013                         break;
    1014                 }
    1015                 if (bytesrequested < 0) {
    1016                         buffersize = new_buffersize(f, buffersize);
    1017                         if (_PyString_Resize(&v, buffersize) < 0)
    1018                                 return NULL;
    1019                 } else {
    1020                         /* Got what was requested. */
    1021                         break;
    1022                 }
    1023         }
    1024         if (bytesread != buffersize)
    1025                 _PyString_Resize(&v, bytesread);
    1026         return v;
     1054    long bytesrequested = -1;
     1055    size_t bytesread, buffersize, chunksize;
     1056    PyObject *v;
     1057
     1058    if (f->f_fp == NULL)
     1059        return err_closed();
     1060    if (!f->readable)
     1061        return err_mode("reading");
     1062    /* refuse to mix with f.next() */
     1063    if (f->f_buf != NULL &&
     1064        (f->f_bufend - f->f_bufptr) > 0 &&
     1065        f->f_buf[0] != '\0')
     1066        return err_iterbuffered();
     1067    if (!PyArg_ParseTuple(args, "|l:read", &bytesrequested))
     1068        return NULL;
     1069    if (bytesrequested < 0)
     1070        buffersize = new_buffersize(f, (size_t)0);
     1071    else
     1072        buffersize = bytesrequested;
     1073    if (buffersize > PY_SSIZE_T_MAX) {
     1074        PyErr_SetString(PyExc_OverflowError,
     1075    "requested number of bytes is more than a Python string can hold");
     1076        return NULL;
     1077    }
     1078    v = PyString_FromStringAndSize((char *)NULL, buffersize);
     1079    if (v == NULL)
     1080        return NULL;
     1081    bytesread = 0;
     1082    for (;;) {
     1083        int interrupted;
     1084        FILE_BEGIN_ALLOW_THREADS(f)
     1085        errno = 0;
     1086        chunksize = Py_UniversalNewlineFread(BUF(v) + bytesread,
     1087                  buffersize - bytesread, f->f_fp, (PyObject *)f);
     1088        interrupted = ferror(f->f_fp) && errno == EINTR;
     1089        FILE_END_ALLOW_THREADS(f)
     1090        if (interrupted) {
     1091            clearerr(f->f_fp);
     1092            if (PyErr_CheckSignals()) {
     1093                Py_DECREF(v);
     1094                return NULL;
     1095            }
     1096        }
     1097        if (chunksize == 0) {
     1098            if (interrupted)
     1099                continue;
     1100            if (!ferror(f->f_fp))
     1101                break;
     1102            clearerr(f->f_fp);
     1103            /* When in non-blocking mode, data shouldn't
     1104             * be discarded if a blocking signal was
     1105             * received. That will also happen if
     1106             * chunksize != 0, but bytesread < buffersize. */
     1107            if (bytesread > 0 && BLOCKED_ERRNO(errno))
     1108                break;
     1109            PyErr_SetFromErrno(PyExc_IOError);
     1110            Py_DECREF(v);
     1111            return NULL;
     1112        }
     1113        bytesread += chunksize;
     1114        if (bytesread < buffersize && !interrupted) {
     1115            clearerr(f->f_fp);
     1116            break;
     1117        }
     1118        if (bytesrequested < 0) {
     1119            buffersize = new_buffersize(f, buffersize);
     1120            if (_PyString_Resize(&v, buffersize) < 0)
     1121                return NULL;
     1122        } else {
     1123            /* Got what was requested. */
     1124            break;
     1125        }
     1126    }
     1127    if (bytesread != buffersize && _PyString_Resize(&v, bytesread))
     1128        return NULL;
     1129    return v;
    10271130}
    10281131
     
    10301133file_readinto(PyFileObject *f, PyObject *args)
    10311134{
    1032         char *ptr;
    1033         Py_ssize_t ntodo;
    1034         Py_ssize_t ndone, nnow;
    1035         Py_buffer pbuf;
    1036 
    1037         if (f->f_fp == NULL)
    1038                 return err_closed();
    1039         if (!f->readable)
    1040                 return err_mode("reading");
    1041         /* refuse to mix with f.next() */
    1042         if (f->f_buf != NULL &&
    1043             (f->f_bufend - f->f_bufptr) > 0 &&
    1044             f->f_buf[0] != '\0')
    1045                 return err_iterbuffered();
    1046         if (!PyArg_ParseTuple(args, "w*", &pbuf))
    1047                 return NULL;
    1048         ptr = pbuf.buf;
    1049         ntodo = pbuf.len;
    1050         ndone = 0;
    1051         while (ntodo > 0) {
    1052                 FILE_BEGIN_ALLOW_THREADS(f)
    1053                 errno = 0;
    1054                 nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
    1055                                                 (PyObject *)f);
    1056                 FILE_END_ALLOW_THREADS(f)
    1057                 if (nnow == 0) {
    1058                         if (!ferror(f->f_fp))
    1059                                 break;
    1060                         PyErr_SetFromErrno(PyExc_IOError);
    1061                         clearerr(f->f_fp);
    1062                         PyBuffer_Release(&pbuf);
    1063                         return NULL;
    1064                 }
    1065                 ndone += nnow;
    1066                 ntodo -= nnow;
    1067         }
    1068         PyBuffer_Release(&pbuf);
    1069         return PyInt_FromSsize_t(ndone);
     1135    char *ptr;
     1136    Py_ssize_t ntodo;
     1137    Py_ssize_t ndone, nnow;
     1138    Py_buffer pbuf;
     1139
     1140    if (f->f_fp == NULL)
     1141        return err_closed();
     1142    if (!f->readable)
     1143        return err_mode("reading");
     1144    /* refuse to mix with f.next() */
     1145    if (f->f_buf != NULL &&
     1146        (f->f_bufend - f->f_bufptr) > 0 &&
     1147        f->f_buf[0] != '\0')
     1148        return err_iterbuffered();
     1149    if (!PyArg_ParseTuple(args, "w*", &pbuf))
     1150        return NULL;
     1151    ptr = pbuf.buf;
     1152    ntodo = pbuf.len;
     1153    ndone = 0;
     1154    while (ntodo > 0) {
     1155        int interrupted;
     1156        FILE_BEGIN_ALLOW_THREADS(f)
     1157        errno = 0;
     1158        nnow = Py_UniversalNewlineFread(ptr+ndone, ntodo, f->f_fp,
     1159                                        (PyObject *)f);
     1160        interrupted = ferror(f->f_fp) && errno == EINTR;
     1161        FILE_END_ALLOW_THREADS(f)
     1162        if (interrupted) {
     1163            clearerr(f->f_fp);
     1164            if (PyErr_CheckSignals()) {
     1165                PyBuffer_Release(&pbuf);
     1166                return NULL;
     1167            }
     1168        }
     1169        if (nnow == 0) {
     1170            if (interrupted)
     1171                continue;
     1172            if (!ferror(f->f_fp))
     1173                break;
     1174            PyErr_SetFromErrno(PyExc_IOError);
     1175            clearerr(f->f_fp);
     1176            PyBuffer_Release(&pbuf);
     1177            return NULL;
     1178        }
     1179        ndone += nnow;
     1180        ntodo -= nnow;
     1181    }
     1182    PyBuffer_Release(&pbuf);
     1183    return PyInt_FromSsize_t(ndone);
    10701184}
    10711185
     
    10891203Reports from other platforms on this method vs getc_unlocked (which MS doesn't
    10901204have):
    1091         Linux           a wash
    1092         Solaris         a wash
    1093         Tru64 Unix      getline_via_fgets significantly faster
     1205    Linux               a wash
     1206    Solaris             a wash
     1207    Tru64 Unix          getline_via_fgets significantly faster
    10941208
    10951209CAUTION:  The C std isn't clear about this:  in those cases where fgets
     
    11381252#define INITBUFSIZE 100
    11391253#define MAXBUFSIZE 300
    1140         char* p;        /* temp */
    1141         char buf[MAXBUFSIZE];
    1142         PyObject* v;    /* the string object result */
    1143         char* pvfree;   /* address of next free slot */
    1144         char* pvend;    /* address one beyond last free slot */
    1145         size_t nfree;   /* # of free buffer slots; pvend-pvfree */
    1146         size_t total_v_size;  /* total # of slots in buffer */
    1147         size_t increment;       /* amount to increment the buffer */
    1148         size_t prev_v_size;
    1149 
    1150         /* Optimize for normal case:  avoid _PyString_Resize if at all
    1151         * possible via first reading into stack buffer "buf".
    1152         */
    1153         total_v_size = INITBUFSIZE;     /* start small and pray */
    1154         pvfree = buf;
    1155         for (;;) {
    1156                 FILE_BEGIN_ALLOW_THREADS(f)
    1157                 pvend = buf + total_v_size;
    1158                 nfree = pvend - pvfree;
    1159                 memset(pvfree, '\n', nfree);
    1160                 assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */
    1161                 p = fgets(pvfree, (int)nfree, fp);
    1162                 FILE_END_ALLOW_THREADS(f)
    1163 
    1164                 if (p == NULL) {
    1165                         clearerr(fp);
    1166                         if (PyErr_CheckSignals())
    1167                                 return NULL;
    1168                         v = PyString_FromStringAndSize(buf, pvfree - buf);
    1169                         return v;
    1170                 }
    1171                 /* fgets read *something* */
    1172                 p = memchr(pvfree, '\n', nfree);
    1173                 if (p != NULL) {
    1174                         /* Did the \n come from fgets or from us?
    1175                         * Since fgets stops at the first \n, and then writes
    1176                         * \0, if it's from fgets a \0 must be next.  But if
    1177                         * that's so, it could not have come from us, since
    1178                         * the \n's we filled the buffer with have only more
    1179                         * \n's to the right.
    1180                         */
    1181                         if (p+1 < pvend && *(p+1) == '\0') {
    1182                                 /* It's from fgets:  we win!  In particular,
    1183                                 * we haven't done any mallocs yet, and can
    1184                                 * build the final result on the first try.
    1185                                 */
    1186                                 ++p;    /* include \n from fgets */
    1187                         }
    1188                         else {
    1189                                 /* Must be from us:  fgets didn't fill the
    1190                                 * buffer and didn't find a newline, so it
    1191                                 * must be the last and newline-free line of
    1192                                 * the file.
    1193                                 */
    1194                                 assert(p > pvfree && *(p-1) == '\0');
    1195                                 --p;    /* don't include \0 from fgets */
    1196                         }
    1197                         v = PyString_FromStringAndSize(buf, p - buf);
    1198                         return v;
    1199                 }
    1200                 /* yuck:  fgets overwrote all the newlines, i.e. the entire
    1201                 * buffer.  So this line isn't over yet, or maybe it is but
    1202                 * we're exactly at EOF.  If we haven't already, try using the
    1203                 * rest of the stack buffer.
    1204                 */
    1205                 assert(*(pvend-1) == '\0');
    1206                 if (pvfree == buf) {
    1207                         pvfree = pvend - 1;     /* overwrite trailing null */
    1208                         total_v_size = MAXBUFSIZE;
    1209                 }
    1210                 else
    1211                         break;
    1212         }
    1213 
    1214         /* The stack buffer isn't big enough; malloc a string object and read
    1215         * into its buffer.
    1216         */
    1217         total_v_size = MAXBUFSIZE << 1;
    1218         v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
    1219         if (v == NULL)
    1220                 return v;
    1221         /* copy over everything except the last null byte */
    1222         memcpy(BUF(v), buf, MAXBUFSIZE-1);
    1223         pvfree = BUF(v) + MAXBUFSIZE - 1;
    1224 
    1225         /* Keep reading stuff into v; if it ever ends successfully, break
    1226         * after setting p one beyond the end of the line.  The code here is
    1227         * very much like the code above, except reads into v's buffer; see
    1228         * the code above for detailed comments about the logic.
    1229         */
    1230         for (;;) {
    1231                 FILE_BEGIN_ALLOW_THREADS(f)
    1232                 pvend = BUF(v) + total_v_size;
    1233                 nfree = pvend - pvfree;
    1234                 memset(pvfree, '\n', nfree);
    1235                 assert(nfree < INT_MAX);
    1236                 p = fgets(pvfree, (int)nfree, fp);
    1237                 FILE_END_ALLOW_THREADS(f)
    1238 
    1239                 if (p == NULL) {
    1240                         clearerr(fp);
    1241                         if (PyErr_CheckSignals()) {
    1242                                 Py_DECREF(v);
    1243                                 return NULL;
    1244                         }
    1245                         p = pvfree;
    1246                         break;
    1247                 }
    1248                 p = memchr(pvfree, '\n', nfree);
    1249                 if (p != NULL) {
    1250                         if (p+1 < pvend && *(p+1) == '\0') {
    1251                                 /* \n came from fgets */
    1252                                 ++p;
    1253                                 break;
    1254                         }
    1255                         /* \n came from us; last line of file, no newline */
    1256                         assert(p > pvfree && *(p-1) == '\0');
    1257                         --p;
    1258                         break;
    1259                 }
    1260                 /* expand buffer and try again */
    1261                 assert(*(pvend-1) == '\0');
    1262                 increment = total_v_size >> 2;  /* mild exponential growth */
    1263                 prev_v_size = total_v_size;
    1264                 total_v_size += increment;
    1265                 /* check for overflow */
    1266                 if (total_v_size <= prev_v_size ||
    1267                     total_v_size > PY_SSIZE_T_MAX) {
    1268                         PyErr_SetString(PyExc_OverflowError,
    1269                             "line is longer than a Python string can hold");
    1270                         Py_DECREF(v);
    1271                         return NULL;
    1272                 }
    1273                 if (_PyString_Resize(&v, (int)total_v_size) < 0)
    1274                         return NULL;
    1275                 /* overwrite the trailing null byte */
    1276                 pvfree = BUF(v) + (prev_v_size - 1);
    1277         }
    1278         if (BUF(v) + total_v_size != p)
    1279                 _PyString_Resize(&v, p - BUF(v));
    1280         return v;
     1254    char* p;            /* temp */
     1255    char buf[MAXBUFSIZE];
     1256    PyObject* v;        /* the string object result */
     1257    char* pvfree;       /* address of next free slot */
     1258    char* pvend;    /* address one beyond last free slot */
     1259    size_t nfree;       /* # of free buffer slots; pvend-pvfree */
     1260    size_t total_v_size;  /* total # of slots in buffer */
     1261    size_t increment;           /* amount to increment the buffer */
     1262    size_t prev_v_size;
     1263
     1264    /* Optimize for normal case:  avoid _PyString_Resize if at all
     1265    * possible via first reading into stack buffer "buf".
     1266    */
     1267    total_v_size = INITBUFSIZE;         /* start small and pray */
     1268    pvfree = buf;
     1269    for (;;) {
     1270        FILE_BEGIN_ALLOW_THREADS(f)
     1271        pvend = buf + total_v_size;
     1272        nfree = pvend - pvfree;
     1273        memset(pvfree, '\n', nfree);
     1274        assert(nfree < INT_MAX); /* Should be atmost MAXBUFSIZE */
     1275        p = fgets(pvfree, (int)nfree, fp);
     1276        FILE_END_ALLOW_THREADS(f)
     1277
     1278        if (p == NULL) {
     1279            clearerr(fp);
     1280            if (PyErr_CheckSignals())
     1281                return NULL;
     1282            v = PyString_FromStringAndSize(buf, pvfree - buf);
     1283            return v;
     1284        }
     1285        /* fgets read *something* */
     1286        p = memchr(pvfree, '\n', nfree);
     1287        if (p != NULL) {
     1288            /* Did the \n come from fgets or from us?
     1289            * Since fgets stops at the first \n, and then writes
     1290            * \0, if it's from fgets a \0 must be next.  But if
     1291            * that's so, it could not have come from us, since
     1292            * the \n's we filled the buffer with have only more
     1293            * \n's to the right.
     1294            */
     1295            if (p+1 < pvend && *(p+1) == '\0') {
     1296                /* It's from fgets:  we win!  In particular,
     1297                * we haven't done any mallocs yet, and can
     1298                * build the final result on the first try.
     1299                */
     1300                ++p;                    /* include \n from fgets */
     1301            }
     1302            else {
     1303                /* Must be from us:  fgets didn't fill the
     1304                * buffer and didn't find a newline, so it
     1305                * must be the last and newline-free line of
     1306                * the file.
     1307                */
     1308                assert(p > pvfree && *(p-1) == '\0');
     1309                --p;                    /* don't include \0 from fgets */
     1310            }
     1311            v = PyString_FromStringAndSize(buf, p - buf);
     1312            return v;
     1313        }
     1314        /* yuck:  fgets overwrote all the newlines, i.e. the entire
     1315        * buffer.  So this line isn't over yet, or maybe it is but
     1316        * we're exactly at EOF.  If we haven't already, try using the
     1317        * rest of the stack buffer.
     1318        */
     1319        assert(*(pvend-1) == '\0');
     1320        if (pvfree == buf) {
     1321            pvfree = pvend - 1;                 /* overwrite trailing null */
     1322            total_v_size = MAXBUFSIZE;
     1323        }
     1324        else
     1325            break;
     1326    }
     1327
     1328    /* The stack buffer isn't big enough; malloc a string object and read
     1329    * into its buffer.
     1330    */
     1331    total_v_size = MAXBUFSIZE << 1;
     1332    v = PyString_FromStringAndSize((char*)NULL, (int)total_v_size);
     1333    if (v == NULL)
     1334        return v;
     1335    /* copy over everything except the last null byte */
     1336    memcpy(BUF(v), buf, MAXBUFSIZE-1);
     1337    pvfree = BUF(v) + MAXBUFSIZE - 1;
     1338
     1339    /* Keep reading stuff into v; if it ever ends successfully, break
     1340    * after setting p one beyond the end of the line.  The code here is
     1341    * very much like the code above, except reads into v's buffer; see
     1342    * the code above for detailed comments about the logic.
     1343    */
     1344    for (;;) {
     1345        FILE_BEGIN_ALLOW_THREADS(f)
     1346        pvend = BUF(v) + total_v_size;
     1347        nfree = pvend - pvfree;
     1348        memset(pvfree, '\n', nfree);
     1349        assert(nfree < INT_MAX);
     1350        p = fgets(pvfree, (int)nfree, fp);
     1351        FILE_END_ALLOW_THREADS(f)
     1352
     1353        if (p == NULL) {
     1354            clearerr(fp);
     1355            if (PyErr_CheckSignals()) {
     1356                Py_DECREF(v);
     1357                return NULL;
     1358            }
     1359            p = pvfree;
     1360            break;
     1361        }
     1362        p = memchr(pvfree, '\n', nfree);
     1363        if (p != NULL) {
     1364            if (p+1 < pvend && *(p+1) == '\0') {
     1365                /* \n came from fgets */
     1366                ++p;
     1367                break;
     1368            }
     1369            /* \n came from us; last line of file, no newline */
     1370            assert(p > pvfree && *(p-1) == '\0');
     1371            --p;
     1372            break;
     1373        }
     1374        /* expand buffer and try again */
     1375        assert(*(pvend-1) == '\0');
     1376        increment = total_v_size >> 2;          /* mild exponential growth */
     1377        prev_v_size = total_v_size;
     1378        total_v_size += increment;
     1379        /* check for overflow */
     1380        if (total_v_size <= prev_v_size ||
     1381            total_v_size > PY_SSIZE_T_MAX) {
     1382            PyErr_SetString(PyExc_OverflowError,
     1383                "line is longer than a Python string can hold");
     1384            Py_DECREF(v);
     1385            return NULL;
     1386        }
     1387        if (_PyString_Resize(&v, (int)total_v_size) < 0)
     1388            return NULL;
     1389        /* overwrite the trailing null byte */
     1390        pvfree = BUF(v) + (prev_v_size - 1);
     1391    }
     1392    if (BUF(v) + total_v_size != p && _PyString_Resize(&v, p - BUF(v)))
     1393        return NULL;
     1394    return v;
    12811395#undef INITBUFSIZE
    12821396#undef MAXBUFSIZE
    12831397}
    1284 #endif  /* ifdef USE_FGETS_IN_GETLINE */
     1398#endif  /* ifdef USE_FGETS_IN_GETLINE */
    12851399
    12861400/* Internal routine to get a line.
     
    12931407get_line(PyFileObject *f, int n)
    12941408{
    1295         FILE *fp = f->f_fp;
    1296         int c;
    1297         char *buf, *end;
    1298         size_t total_v_size;    /* total # of slots in buffer */
    1299         size_t used_v_size;     /* # used slots in buffer */
    1300         size_t increment;       /* amount to increment the buffer */
    1301         PyObject *v;
    1302         int newlinetypes = f->f_newlinetypes;
    1303         int skipnextlf = f->f_skipnextlf;
    1304         int univ_newline = f->f_univ_newline;
     1409    FILE *fp = f->f_fp;
     1410    int c;
     1411    char *buf, *end;
     1412    size_t total_v_size;        /* total # of slots in buffer */
     1413    size_t used_v_size;         /* # used slots in buffer */
     1414    size_t increment;       /* amount to increment the buffer */
     1415    PyObject *v;
     1416    int newlinetypes = f->f_newlinetypes;
     1417    int skipnextlf = f->f_skipnextlf;
     1418    int univ_newline = f->f_univ_newline;
    13051419
    13061420#if defined(USE_FGETS_IN_GETLINE)
    1307         if (n <= 0 && !univ_newline )
    1308                 return getline_via_fgets(f, fp);
    1309 #endif
    1310         total_v_size = n > 0 ? n : 100;
    1311         v = PyString_FromStringAndSize((char *)NULL, total_v_size);
    1312         if (v == NULL)
    1313                 return NULL;
    1314         buf = BUF(v);
    1315         end = buf + total_v_size;
    1316 
    1317         for (;;) {
    1318                 FILE_BEGIN_ALLOW_THREADS(f)
    1319                 FLOCKFILE(fp);
    1320                 if (univ_newline) {
    1321                         c = 'x'; /* Shut up gcc warning */
    1322                         while ( buf != end && (c = GETC(fp)) != EOF ) {
    1323                                 if (skipnextlf ) {
    1324                                         skipnextlf = 0;
    1325                                         if (c == '\n') {
    1326                                                 /* Seeing a \n here with
    1327                                                  * skipnextlf true means we
    1328                                                  * saw a \r before.
    1329                                                  */
    1330                                                 newlinetypes |= NEWLINE_CRLF;
    1331                                                 c = GETC(fp);
    1332                                                 if (c == EOF) break;
    1333                                         } else {
    1334                                                 newlinetypes |= NEWLINE_CR;
    1335                                         }
    1336                                 }
    1337                                 if (c == '\r') {
    1338                                         skipnextlf = 1;
    1339                                         c = '\n';
    1340                                 } else if ( c == '\n')
    1341                                         newlinetypes |= NEWLINE_LF;
    1342                                 *buf++ = c;
    1343                                 if (c == '\n') break;
    1344                         }
    1345                         if ( c == EOF && skipnextlf )
    1346                                 newlinetypes |= NEWLINE_CR;
    1347                 } else /* If not universal newlines use the normal loop */
    1348                 while ((c = GETC(fp)) != EOF &&
    1349                        (*buf++ = c) != '\n' &&
    1350                         buf != end)
    1351                         ;
    1352                 FUNLOCKFILE(fp);
    1353                 FILE_END_ALLOW_THREADS(f)
    1354                 f->f_newlinetypes = newlinetypes;
    1355                 f->f_skipnextlf = skipnextlf;
    1356                 if (c == '\n')
    1357                         break;
    1358                 if (c == EOF) {
    1359                         if (ferror(fp)) {
    1360                                 PyErr_SetFromErrno(PyExc_IOError);
    1361                                 clearerr(fp);
    1362                                 Py_DECREF(v);
    1363                                 return NULL;
    1364                         }
    1365                         clearerr(fp);
    1366                         if (PyErr_CheckSignals()) {
    1367                                 Py_DECREF(v);
    1368                                 return NULL;
    1369                         }
    1370                         break;
    1371                 }
    1372                 /* Must be because buf == end */
    1373                 if (n > 0)
    1374                         break;
    1375                 used_v_size = total_v_size;
    1376                 increment = total_v_size >> 2; /* mild exponential growth */
    1377                 total_v_size += increment;
    1378                 if (total_v_size > PY_SSIZE_T_MAX) {
    1379                         PyErr_SetString(PyExc_OverflowError,
    1380                             "line is longer than a Python string can hold");
    1381                         Py_DECREF(v);
    1382                         return NULL;
    1383                 }
    1384                 if (_PyString_Resize(&v, total_v_size) < 0)
    1385                         return NULL;
    1386                 buf = BUF(v) + used_v_size;
    1387                 end = BUF(v) + total_v_size;
    1388         }
    1389 
    1390         used_v_size = buf - BUF(v);
    1391         if (used_v_size != total_v_size)
    1392                 _PyString_Resize(&v, used_v_size);
    1393         return v;
     1421    if (n <= 0 && !univ_newline )
     1422        return getline_via_fgets(f, fp);
     1423#endif
     1424    total_v_size = n > 0 ? n : 100;
     1425    v = PyString_FromStringAndSize((char *)NULL, total_v_size);
     1426    if (v == NULL)
     1427        return NULL;
     1428    buf = BUF(v);
     1429    end = buf + total_v_size;
     1430
     1431    for (;;) {
     1432        FILE_BEGIN_ALLOW_THREADS(f)
     1433        FLOCKFILE(fp);
     1434        if (univ_newline) {
     1435            c = 'x'; /* Shut up gcc warning */
     1436            while ( buf != end && (c = GETC(fp)) != EOF ) {
     1437                if (skipnextlf ) {
     1438                    skipnextlf = 0;
     1439                    if (c == '\n') {
     1440                        /* Seeing a \n here with
     1441                         * skipnextlf true means we
     1442                         * saw a \r before.
     1443                         */
     1444                        newlinetypes |= NEWLINE_CRLF;
     1445                        c = GETC(fp);
     1446                        if (c == EOF) break;
     1447                    } else {
     1448                        newlinetypes |= NEWLINE_CR;
     1449                    }
     1450                }
     1451                if (c == '\r') {
     1452                    skipnextlf = 1;
     1453                    c = '\n';
     1454                } else if ( c == '\n')
     1455                    newlinetypes |= NEWLINE_LF;
     1456                *buf++ = c;
     1457                if (c == '\n') break;
     1458            }
     1459            if (c == EOF) {
     1460                if (ferror(fp) && errno == EINTR) {
     1461                    FUNLOCKFILE(fp);
     1462                    FILE_ABORT_ALLOW_THREADS(f)
     1463                    f->f_newlinetypes = newlinetypes;
     1464                    f->f_skipnextlf = skipnextlf;
     1465
     1466                    if (PyErr_CheckSignals()) {
     1467                        Py_DECREF(v);
     1468                        return NULL;
     1469                    }
     1470                    /* We executed Python signal handlers and got no exception.
     1471                     * Now back to reading the line where we left off. */
     1472                    clearerr(fp);
     1473                    continue;
     1474                }
     1475                if (skipnextlf)
     1476                    newlinetypes |= NEWLINE_CR;
     1477            }
     1478        } else /* If not universal newlines use the normal loop */
     1479        while ((c = GETC(fp)) != EOF &&
     1480               (*buf++ = c) != '\n' &&
     1481            buf != end)
     1482            ;
     1483        FUNLOCKFILE(fp);
     1484        FILE_END_ALLOW_THREADS(f)
     1485        f->f_newlinetypes = newlinetypes;
     1486        f->f_skipnextlf = skipnextlf;
     1487        if (c == '\n')
     1488            break;
     1489        if (c == EOF) {
     1490            if (ferror(fp)) {
     1491                if (errno == EINTR) {
     1492                    if (PyErr_CheckSignals()) {
     1493                        Py_DECREF(v);
     1494                        return NULL;
     1495                    }
     1496                    /* We executed Python signal handlers and got no exception.
     1497                     * Now back to reading the line where we left off. */
     1498                    clearerr(fp);
     1499                    continue;
     1500                }
     1501                PyErr_SetFromErrno(PyExc_IOError);
     1502                clearerr(fp);
     1503                Py_DECREF(v);
     1504                return NULL;
     1505            }
     1506            clearerr(fp);
     1507            if (PyErr_CheckSignals()) {
     1508                Py_DECREF(v);
     1509                return NULL;
     1510            }
     1511            break;
     1512        }
     1513        /* Must be because buf == end */
     1514        if (n > 0)
     1515            break;
     1516        used_v_size = total_v_size;
     1517        increment = total_v_size >> 2; /* mild exponential growth */
     1518        total_v_size += increment;
     1519        if (total_v_size > PY_SSIZE_T_MAX) {
     1520            PyErr_SetString(PyExc_OverflowError,
     1521                "line is longer than a Python string can hold");
     1522            Py_DECREF(v);
     1523            return NULL;
     1524        }
     1525        if (_PyString_Resize(&v, total_v_size) < 0)
     1526            return NULL;
     1527        buf = BUF(v) + used_v_size;
     1528        end = BUF(v) + total_v_size;
     1529    }
     1530
     1531    used_v_size = buf - BUF(v);
     1532    if (used_v_size != total_v_size && _PyString_Resize(&v, used_v_size))
     1533        return NULL;
     1534    return v;
    13941535}
    13951536
     
    13991540PyFile_GetLine(PyObject *f, int n)
    14001541{
    1401         PyObject *result;
    1402 
    1403         if (f == NULL) {
    1404                 PyErr_BadInternalCall();
    1405                 return NULL;
    1406         }
    1407 
    1408         if (PyFile_Check(f)) {
    1409                 PyFileObject *fo = (PyFileObject *)f;
    1410                 if (fo->f_fp == NULL)
    1411                         return err_closed();
    1412                 if (!fo->readable)
    1413                         return err_mode("reading");
    1414                 /* refuse to mix with f.next() */
    1415                 if (fo->f_buf != NULL &&
    1416                     (fo->f_bufend - fo->f_bufptr) > 0 &&
    1417                     fo->f_buf[0] != '\0')
    1418                         return err_iterbuffered();
    1419                 result = get_line(fo, n);
    1420         }
    1421         else {
    1422                 PyObject *reader;
    1423                 PyObject *args;
    1424 
    1425                 reader = PyObject_GetAttrString(f, "readline");
    1426                 if (reader == NULL)
    1427                         return NULL;
    1428                 if (n <= 0)
    1429                         args = PyTuple_New(0);
    1430                 else
    1431                         args = Py_BuildValue("(i)", n);
    1432                 if (args == NULL) {
    1433                         Py_DECREF(reader);
    1434                         return NULL;
    1435                 }
    1436                 result = PyEval_CallObject(reader, args);
    1437                 Py_DECREF(reader);
    1438                 Py_DECREF(args);
    1439                 if (result != NULL && !PyString_Check(result) &&
    1440                     !PyUnicode_Check(result)) {
    1441                         Py_DECREF(result);
    1442                         result = NULL;
    1443                         PyErr_SetString(PyExc_TypeError,
    1444                                    "object.readline() returned non-string");
    1445                 }
    1446         }
    1447 
    1448         if (n < 0 && result != NULL && PyString_Check(result)) {
    1449                 char *s = PyString_AS_STRING(result);
    1450                 Py_ssize_t len = PyString_GET_SIZE(result);
    1451                 if (len == 0) {
    1452                         Py_DECREF(result);
    1453                         result = NULL;
    1454                         PyErr_SetString(PyExc_EOFError,
    1455                                         "EOF when reading a line");
    1456                 }
    1457                 else if (s[len-1] == '\n') {
    1458                         if (result->ob_refcnt == 1)
    1459                                 _PyString_Resize(&result, len-1);
    1460                         else {
    1461                                 PyObject *v;
    1462                                 v = PyString_FromStringAndSize(s, len-1);
    1463                                 Py_DECREF(result);
    1464                                 result = v;
    1465                         }
    1466                 }
    1467         }
     1542    PyObject *result;
     1543
     1544    if (f == NULL) {
     1545        PyErr_BadInternalCall();
     1546        return NULL;
     1547    }
     1548
     1549    if (PyFile_Check(f)) {
     1550        PyFileObject *fo = (PyFileObject *)f;
     1551        if (fo->f_fp == NULL)
     1552            return err_closed();
     1553        if (!fo->readable)
     1554            return err_mode("reading");
     1555        /* refuse to mix with f.next() */
     1556        if (fo->f_buf != NULL &&
     1557            (fo->f_bufend - fo->f_bufptr) > 0 &&
     1558            fo->f_buf[0] != '\0')
     1559            return err_iterbuffered();
     1560        result = get_line(fo, n);
     1561    }
     1562    else {
     1563        PyObject *reader;
     1564        PyObject *args;
     1565
     1566        reader = PyObject_GetAttrString(f, "readline");
     1567        if (reader == NULL)
     1568            return NULL;
     1569        if (n <= 0)
     1570            args = PyTuple_New(0);
     1571        else
     1572            args = Py_BuildValue("(i)", n);
     1573        if (args == NULL) {
     1574            Py_DECREF(reader);
     1575            return NULL;
     1576        }
     1577        result = PyEval_CallObject(reader, args);
     1578        Py_DECREF(reader);
     1579        Py_DECREF(args);
     1580        if (result != NULL && !PyString_Check(result) &&
     1581            !PyUnicode_Check(result)) {
     1582            Py_DECREF(result);
     1583            result = NULL;
     1584            PyErr_SetString(PyExc_TypeError,
     1585                       "object.readline() returned non-string");
     1586        }
     1587    }
     1588
     1589    if (n < 0 && result != NULL && PyString_Check(result)) {
     1590        char *s = PyString_AS_STRING(result);
     1591        Py_ssize_t len = PyString_GET_SIZE(result);
     1592        if (len == 0) {
     1593            Py_DECREF(result);
     1594            result = NULL;
     1595            PyErr_SetString(PyExc_EOFError,
     1596                            "EOF when reading a line");
     1597        }
     1598        else if (s[len-1] == '\n') {
     1599            if (result->ob_refcnt == 1) {
     1600                if (_PyString_Resize(&result, len-1))
     1601                    return NULL;
     1602            }
     1603            else {
     1604                PyObject *v;
     1605                v = PyString_FromStringAndSize(s, len-1);
     1606                Py_DECREF(result);
     1607                result = v;
     1608            }
     1609        }
     1610    }
    14681611#ifdef Py_USING_UNICODE
    1469         if (n < 0 && result != NULL && PyUnicode_Check(result)) {
    1470                 Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
    1471                 Py_ssize_t len = PyUnicode_GET_SIZE(result);
    1472                 if (len == 0) {
    1473                         Py_DECREF(result);
    1474                         result = NULL;
    1475                         PyErr_SetString(PyExc_EOFError,
    1476                                         "EOF when reading a line");
    1477                 }
    1478                 else if (s[len-1] == '\n') {
    1479                         if (result->ob_refcnt == 1)
    1480                                 PyUnicode_Resize(&result, len-1);
    1481                         else {
    1482                                 PyObject *v;
    1483                                 v = PyUnicode_FromUnicode(s, len-1);
    1484                                 Py_DECREF(result);
    1485                                 result = v;
    1486                         }
    1487                 }
    1488         }
    1489 #endif
    1490         return result;
     1612    if (n < 0 && result != NULL && PyUnicode_Check(result)) {
     1613        Py_UNICODE *s = PyUnicode_AS_UNICODE(result);
     1614        Py_ssize_t len = PyUnicode_GET_SIZE(result);
     1615        if (len == 0) {
     1616            Py_DECREF(result);
     1617            result = NULL;
     1618            PyErr_SetString(PyExc_EOFError,
     1619                            "EOF when reading a line");
     1620        }
     1621        else if (s[len-1] == '\n') {
     1622            if (result->ob_refcnt == 1)
     1623                PyUnicode_Resize(&result, len-1);
     1624            else {
     1625                PyObject *v;
     1626                v = PyUnicode_FromUnicode(s, len-1);
     1627                Py_DECREF(result);
     1628                result = v;
     1629            }
     1630        }
     1631    }
     1632#endif
     1633    return result;
    14911634}
    14921635
     
    14961639file_readline(PyFileObject *f, PyObject *args)
    14971640{
    1498         int n = -1;
    1499 
    1500         if (f->f_fp == NULL)
    1501                 return err_closed();
    1502         if (!f->readable)
    1503                 return err_mode("reading");
    1504         /* refuse to mix with f.next() */
    1505         if (f->f_buf != NULL &&
    1506             (f->f_bufend - f->f_bufptr) > 0 &&
    1507             f->f_buf[0] != '\0')
    1508                 return err_iterbuffered();
    1509         if (!PyArg_ParseTuple(args, "|i:readline", &n))
    1510                 return NULL;
    1511         if (n == 0)
    1512                 return PyString_FromString("");
    1513         if (n < 0)
    1514                 n = 0;
    1515         return get_line(f, n);
     1641    int n = -1;
     1642
     1643    if (f->f_fp == NULL)
     1644        return err_closed();
     1645    if (!f->readable)
     1646        return err_mode("reading");
     1647    /* refuse to mix with f.next() */
     1648    if (f->f_buf != NULL &&
     1649        (f->f_bufend - f->f_bufptr) > 0 &&
     1650        f->f_buf[0] != '\0')
     1651        return err_iterbuffered();
     1652    if (!PyArg_ParseTuple(args, "|i:readline", &n))
     1653        return NULL;
     1654    if (n == 0)
     1655        return PyString_FromString("");
     1656    if (n < 0)
     1657        n = 0;
     1658    return get_line(f, n);
    15161659}
    15171660
     
    15191662file_readlines(PyFileObject *f, PyObject *args)
    15201663{
    1521         long sizehint = 0;
    1522         PyObject *list = NULL;
    1523         PyObject *line;
    1524         char small_buffer[SMALLCHUNK];
    1525         char *buffer = small_buffer;
    1526         size_t buffersize = SMALLCHUNK;
    1527         PyObject *big_buffer = NULL;
    1528         size_t nfilled = 0;
    1529         size_t nread;
    1530         size_t totalread = 0;
    1531         char *p, *q, *end;
    1532         int err;
    1533         int shortread = 0;
    1534 
    1535         if (f->f_fp == NULL)
    1536                 return err_closed();
    1537         if (!f->readable)
    1538                 return err_mode("reading");
    1539         /* refuse to mix with f.next() */
    1540         if (f->f_buf != NULL &&
    1541             (f->f_bufend - f->f_bufptr) > 0 &&
    1542             f->f_buf[0] != '\0')
    1543                 return err_iterbuffered();
    1544         if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
    1545                 return NULL;
    1546         if ((list = PyList_New(0)) == NULL)
    1547                 return NULL;
    1548         for (;;) {
    1549                 if (shortread)
    1550                         nread = 0;
    1551                 else {
    1552                         FILE_BEGIN_ALLOW_THREADS(f)
    1553                         errno = 0;
    1554                         nread = Py_UniversalNewlineFread(buffer+nfilled,
    1555                                 buffersize-nfilled, f->f_fp, (PyObject *)f);
    1556                         FILE_END_ALLOW_THREADS(f)
    1557                         shortread = (nread < buffersize-nfilled);
    1558                 }
    1559                 if (nread == 0) {
    1560                         sizehint = 0;
    1561                         if (!ferror(f->f_fp))
    1562                                 break;
    1563                         PyErr_SetFromErrno(PyExc_IOError);
    1564                         clearerr(f->f_fp);
    1565                         goto error;
    1566                 }
    1567                 totalread += nread;
    1568                 p = (char *)memchr(buffer+nfilled, '\n', nread);
    1569                 if (p == NULL) {
    1570                         /* Need a larger buffer to fit this line */
    1571                         nfilled += nread;
    1572                         buffersize *= 2;
    1573                         if (buffersize > PY_SSIZE_T_MAX) {
    1574                                 PyErr_SetString(PyExc_OverflowError,
    1575                             "line is longer than a Python string can hold");
    1576                                 goto error;
    1577                         }
    1578                         if (big_buffer == NULL) {
    1579                                 /* Create the big buffer */
    1580                                 big_buffer = PyString_FromStringAndSize(
    1581                                         NULL, buffersize);
    1582                                 if (big_buffer == NULL)
    1583                                         goto error;
    1584                                 buffer = PyString_AS_STRING(big_buffer);
    1585                                 memcpy(buffer, small_buffer, nfilled);
    1586                         }
    1587                         else {
    1588                                 /* Grow the big buffer */
    1589                                 if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
    1590                                         goto error;
    1591                                 buffer = PyString_AS_STRING(big_buffer);
    1592                         }
    1593                         continue;
    1594                 }
    1595                 end = buffer+nfilled+nread;
    1596                 q = buffer;
    1597                 do {
    1598                         /* Process complete lines */
    1599                         p++;
    1600                         line = PyString_FromStringAndSize(q, p-q);
    1601                         if (line == NULL)
    1602                                 goto error;
    1603                         err = PyList_Append(list, line);
    1604                         Py_DECREF(line);
    1605                         if (err != 0)
    1606                                 goto error;
    1607                         q = p;
    1608                         p = (char *)memchr(q, '\n', end-q);
    1609                 } while (p != NULL);
    1610                 /* Move the remaining incomplete line to the start */
    1611                 nfilled = end-q;
    1612                 memmove(buffer, q, nfilled);
    1613                 if (sizehint > 0)
    1614                         if (totalread >= (size_t)sizehint)
    1615                                 break;
    1616         }
    1617         if (nfilled != 0) {
    1618                 /* Partial last line */
    1619                 line = PyString_FromStringAndSize(buffer, nfilled);
    1620                 if (line == NULL)
    1621                         goto error;
    1622                 if (sizehint > 0) {
    1623                         /* Need to complete the last line */
    1624                         PyObject *rest = get_line(f, 0);
    1625                         if (rest == NULL) {
    1626                                 Py_DECREF(line);
    1627                                 goto error;
    1628                         }
    1629                         PyString_Concat(&line, rest);
    1630                         Py_DECREF(rest);
    1631                         if (line == NULL)
    1632                                 goto error;
    1633                 }
    1634                 err = PyList_Append(list, line);
    1635                 Py_DECREF(line);
    1636                 if (err != 0)
    1637                         goto error;
    1638         }
     1664    long sizehint = 0;
     1665    PyObject *list = NULL;
     1666    PyObject *line;
     1667    char small_buffer[SMALLCHUNK];
     1668    char *buffer = small_buffer;
     1669    size_t buffersize = SMALLCHUNK;
     1670    PyObject *big_buffer = NULL;
     1671    size_t nfilled = 0;
     1672    size_t nread;
     1673    size_t totalread = 0;
     1674    char *p, *q, *end;
     1675    int err;
     1676    int shortread = 0;  /* bool, did the previous read come up short? */
     1677
     1678    if (f->f_fp == NULL)
     1679        return err_closed();
     1680    if (!f->readable)
     1681        return err_mode("reading");
     1682    /* refuse to mix with f.next() */
     1683    if (f->f_buf != NULL &&
     1684        (f->f_bufend - f->f_bufptr) > 0 &&
     1685        f->f_buf[0] != '\0')
     1686        return err_iterbuffered();
     1687    if (!PyArg_ParseTuple(args, "|l:readlines", &sizehint))
     1688        return NULL;
     1689    if ((list = PyList_New(0)) == NULL)
     1690        return NULL;
     1691    for (;;) {
     1692        if (shortread)
     1693            nread = 0;
     1694        else {
     1695            FILE_BEGIN_ALLOW_THREADS(f)
     1696            errno = 0;
     1697            nread = Py_UniversalNewlineFread(buffer+nfilled,
     1698                buffersize-nfilled, f->f_fp, (PyObject *)f);
     1699            FILE_END_ALLOW_THREADS(f)
     1700            shortread = (nread < buffersize-nfilled);
     1701        }
     1702        if (nread == 0) {
     1703            sizehint = 0;
     1704            if (!ferror(f->f_fp))
     1705                break;
     1706            if (errno == EINTR) {
     1707                if (PyErr_CheckSignals()) {
     1708                    goto error;
     1709                }
     1710                clearerr(f->f_fp);
     1711                shortread = 0;
     1712                continue;
     1713            }
     1714            PyErr_SetFromErrno(PyExc_IOError);
     1715            clearerr(f->f_fp);
     1716            goto error;
     1717        }
     1718        totalread += nread;
     1719        p = (char *)memchr(buffer+nfilled, '\n', nread);
     1720        if (p == NULL) {
     1721            /* Need a larger buffer to fit this line */
     1722            nfilled += nread;
     1723            buffersize *= 2;
     1724            if (buffersize > PY_SSIZE_T_MAX) {
     1725                PyErr_SetString(PyExc_OverflowError,
     1726                "line is longer than a Python string can hold");
     1727                goto error;
     1728            }
     1729            if (big_buffer == NULL) {
     1730                /* Create the big buffer */
     1731                big_buffer = PyString_FromStringAndSize(
     1732                    NULL, buffersize);
     1733                if (big_buffer == NULL)
     1734                    goto error;
     1735                buffer = PyString_AS_STRING(big_buffer);
     1736                memcpy(buffer, small_buffer, nfilled);
     1737            }
     1738            else {
     1739                /* Grow the big buffer */
     1740                if ( _PyString_Resize(&big_buffer, buffersize) < 0 )
     1741                    goto error;
     1742                buffer = PyString_AS_STRING(big_buffer);
     1743            }
     1744            continue;
     1745        }
     1746        end = buffer+nfilled+nread;
     1747        q = buffer;
     1748        do {
     1749            /* Process complete lines */
     1750            p++;
     1751            line = PyString_FromStringAndSize(q, p-q);
     1752            if (line == NULL)
     1753                goto error;
     1754            err = PyList_Append(list, line);
     1755            Py_DECREF(line);
     1756            if (err != 0)
     1757                goto error;
     1758            q = p;
     1759            p = (char *)memchr(q, '\n', end-q);
     1760        } while (p != NULL);
     1761        /* Move the remaining incomplete line to the start */
     1762        nfilled = end-q;
     1763        memmove(buffer, q, nfilled);
     1764        if (sizehint > 0)
     1765            if (totalread >= (size_t)sizehint)
     1766                break;
     1767    }
     1768    if (nfilled != 0) {
     1769        /* Partial last line */
     1770        line = PyString_FromStringAndSize(buffer, nfilled);
     1771        if (line == NULL)
     1772            goto error;
     1773        if (sizehint > 0) {
     1774            /* Need to complete the last line */
     1775            PyObject *rest = get_line(f, 0);
     1776            if (rest == NULL) {
     1777                Py_DECREF(line);
     1778                goto error;
     1779            }
     1780            PyString_Concat(&line, rest);
     1781            Py_DECREF(rest);
     1782            if (line == NULL)
     1783                goto error;
     1784        }
     1785        err = PyList_Append(list, line);
     1786        Py_DECREF(line);
     1787        if (err != 0)
     1788            goto error;
     1789    }
    16391790
    16401791cleanup:
    1641         Py_XDECREF(big_buffer);
    1642         return list;
     1792    Py_XDECREF(big_buffer);
     1793    return list;
    16431794
    16441795error:
    1645         Py_CLEAR(list);
    1646         goto cleanup;
     1796    Py_CLEAR(list);
     1797    goto cleanup;
    16471798}
    16481799
     
    16501801file_write(PyFileObject *f, PyObject *args)
    16511802{
    1652         Py_buffer pbuf;
    1653         char *s;
    1654         Py_ssize_t n, n2;
    1655         if (f->f_fp == NULL)
    1656                 return err_closed();
    1657         if (!f->writable)
    1658                 return err_mode("writing");
    1659         if (f->f_binary) {
    1660                 if (!PyArg_ParseTuple(args, "s*", &pbuf))
    1661                         return NULL;
    1662                 s = pbuf.buf;
    1663                 n = pbuf.len;
    1664         } else
    1665                 if (!PyArg_ParseTuple(args, "t#", &s, &n))
    1666                 return NULL;
    1667         f->f_softspace = 0;
    1668         FILE_BEGIN_ALLOW_THREADS(f)
    1669         errno = 0;
    1670         n2 = fwrite(s, 1, n, f->f_fp);
    1671         FILE_END_ALLOW_THREADS(f)
    1672         if (f->f_binary)
    1673                 PyBuffer_Release(&pbuf);
    1674         if (n2 != n) {
    1675                 PyErr_SetFromErrno(PyExc_IOError);
    1676                 clearerr(f->f_fp);
    1677                 return NULL;
    1678         }
    1679         Py_INCREF(Py_None);
    1680         return Py_None;
     1803    Py_buffer pbuf;
     1804    const char *s;
     1805    Py_ssize_t n, n2;
     1806    PyObject *encoded = NULL;
     1807
     1808    if (f->f_fp == NULL)
     1809        return err_closed();
     1810    if (!f->writable)
     1811        return err_mode("writing");
     1812    if (f->f_binary) {
     1813        if (!PyArg_ParseTuple(args, "s*", &pbuf))
     1814            return NULL;
     1815        s = pbuf.buf;
     1816        n = pbuf.len;
     1817    }
     1818    else {
     1819        PyObject *text;
     1820        if (!PyArg_ParseTuple(args, "O", &text))
     1821            return NULL;
     1822
     1823        if (PyString_Check(text)) {
     1824            s = PyString_AS_STRING(text);
     1825            n = PyString_GET_SIZE(text);
     1826#ifdef Py_USING_UNICODE
     1827        } else if (PyUnicode_Check(text)) {
     1828            const char *encoding, *errors;
     1829            if (f->f_encoding != Py_None)
     1830                encoding = PyString_AS_STRING(f->f_encoding);
     1831            else
     1832                encoding = PyUnicode_GetDefaultEncoding();
     1833            if (f->f_errors != Py_None)
     1834                errors = PyString_AS_STRING(f->f_errors);
     1835            else
     1836                errors = "strict";
     1837            encoded = PyUnicode_AsEncodedString(text, encoding, errors);
     1838            if (encoded == NULL)
     1839                return NULL;
     1840            s = PyString_AS_STRING(encoded);
     1841            n = PyString_GET_SIZE(encoded);
     1842#endif
     1843        } else {
     1844            if (PyObject_AsCharBuffer(text, &s, &n))
     1845                return NULL;
     1846        }
     1847    }
     1848    f->f_softspace = 0;
     1849    FILE_BEGIN_ALLOW_THREADS(f)
     1850    errno = 0;
     1851    n2 = fwrite(s, 1, n, f->f_fp);
     1852    FILE_END_ALLOW_THREADS(f)
     1853    Py_XDECREF(encoded);
     1854    if (f->f_binary)
     1855        PyBuffer_Release(&pbuf);
     1856    if (n2 != n) {
     1857        PyErr_SetFromErrno(PyExc_IOError);
     1858        clearerr(f->f_fp);
     1859        return NULL;
     1860    }
     1861    Py_INCREF(Py_None);
     1862    return Py_None;
    16811863}
    16821864
     
    16851867{
    16861868#define CHUNKSIZE 1000
    1687         PyObject *list, *line;
    1688         PyObject *it;   /* iter(seq) */
    1689         PyObject *result;
    1690         int index, islist;
    1691         Py_ssize_t i, j, nwritten, len;
    1692 
    1693         assert(seq != NULL);
    1694         if (f->f_fp == NULL)
    1695                 return err_closed();
    1696         if (!f->writable)
    1697                 return err_mode("writing");
    1698 
    1699         result = NULL;
    1700         list = NULL;
    1701         islist = PyList_Check(seq);
    1702         if  (islist)
    1703                 it = NULL;
    1704         else {
    1705                 it = PyObject_GetIter(seq);
    1706                 if (it == NULL) {
    1707                         PyErr_SetString(PyExc_TypeError,
    1708                                 "writelines() requires an iterable argument");
    1709                         return NULL;
    1710                 }
    1711                 /* From here on, fail by going to error, to reclaim "it". */
    1712                 list = PyList_New(CHUNKSIZE);
    1713                 if (list == NULL)
    1714                         goto error;
    1715         }
    1716 
    1717         /* Strategy: slurp CHUNKSIZE lines into a private list,
    1718            checking that they are all strings, then write that list
    1719            without holding the interpreter lock, then come back for more. */
    1720         for (index = 0; ; index += CHUNKSIZE) {
    1721                 if (islist) {
    1722                         Py_XDECREF(list);
    1723                         list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
    1724                         if (list == NULL)
    1725                                 goto error;
    1726                         j = PyList_GET_SIZE(list);
    1727                 }
    1728                 else {
    1729                         for (j = 0; j < CHUNKSIZE; j++) {
    1730                                 line = PyIter_Next(it);
    1731                                 if (line == NULL) {
    1732                                         if (PyErr_Occurred())
    1733                                                 goto error;
    1734                                         break;
    1735                                 }
    1736                                 PyList_SetItem(list, j, line);
    1737                         }
    1738                 }
    1739                 if (j == 0)
    1740                         break;
    1741 
    1742                 /* Check that all entries are indeed strings. If not,
    1743                    apply the same rules as for file.write() and
    1744                    convert the results to strings. This is slow, but
    1745                    seems to be the only way since all conversion APIs
    1746                    could potentially execute Python code. */
    1747                 for (i = 0; i < j; i++) {
    1748                         PyObject *v = PyList_GET_ITEM(list, i);
    1749                         if (!PyString_Check(v)) {
    1750                                 const char *buffer;
    1751                                 if (((f->f_binary &&
    1752                                       PyObject_AsReadBuffer(v,
    1753                                               (const void**)&buffer,
    1754                                                             &len)) ||
    1755                                      PyObject_AsCharBuffer(v,
    1756                                                            &buffer,
    1757                                                            &len))) {
    1758                                         PyErr_SetString(PyExc_TypeError,
    1759                         "writelines() argument must be a sequence of strings");
    1760                                         goto error;
    1761                                 }
    1762                                 line = PyString_FromStringAndSize(buffer,
    1763                                                                   len);
    1764                                 if (line == NULL)
    1765                                         goto error;
    1766                                 Py_DECREF(v);
    1767                                 PyList_SET_ITEM(list, i, line);
    1768                         }
    1769                 }
    1770 
    1771                 /* Since we are releasing the global lock, the
    1772                    following code may *not* execute Python code. */
    1773                 f->f_softspace = 0;
    1774                 FILE_BEGIN_ALLOW_THREADS(f)
    1775                 errno = 0;
    1776                 for (i = 0; i < j; i++) {
    1777                         line = PyList_GET_ITEM(list, i);
    1778                         len = PyString_GET_SIZE(line);
    1779                         nwritten = fwrite(PyString_AS_STRING(line),
    1780                                           1, len, f->f_fp);
    1781                         if (nwritten != len) {
    1782                                 FILE_ABORT_ALLOW_THREADS(f)
    1783                                 PyErr_SetFromErrno(PyExc_IOError);
    1784                                 clearerr(f->f_fp);
    1785                                 goto error;
    1786                         }
    1787                 }
    1788                 FILE_END_ALLOW_THREADS(f)
    1789 
    1790                 if (j < CHUNKSIZE)
    1791                         break;
    1792         }
    1793 
    1794         Py_INCREF(Py_None);
    1795         result = Py_None;
     1869    PyObject *list, *line;
     1870    PyObject *it;       /* iter(seq) */
     1871    PyObject *result;
     1872    int index, islist;
     1873    Py_ssize_t i, j, nwritten, len;
     1874
     1875    assert(seq != NULL);
     1876    if (f->f_fp == NULL)
     1877        return err_closed();
     1878    if (!f->writable)
     1879        return err_mode("writing");
     1880
     1881    result = NULL;
     1882    list = NULL;
     1883    islist = PyList_Check(seq);
     1884    if  (islist)
     1885        it = NULL;
     1886    else {
     1887        it = PyObject_GetIter(seq);
     1888        if (it == NULL) {
     1889            PyErr_SetString(PyExc_TypeError,
     1890                "writelines() requires an iterable argument");
     1891            return NULL;
     1892        }
     1893        /* From here on, fail by going to error, to reclaim "it". */
     1894        list = PyList_New(CHUNKSIZE);
     1895        if (list == NULL)
     1896            goto error;
     1897    }
     1898
     1899    /* Strategy: slurp CHUNKSIZE lines into a private list,
     1900       checking that they are all strings, then write that list
     1901       without holding the interpreter lock, then come back for more. */
     1902    for (index = 0; ; index += CHUNKSIZE) {
     1903        if (islist) {
     1904            Py_XDECREF(list);
     1905            list = PyList_GetSlice(seq, index, index+CHUNKSIZE);
     1906            if (list == NULL)
     1907                goto error;
     1908            j = PyList_GET_SIZE(list);
     1909        }
     1910        else {
     1911            for (j = 0; j < CHUNKSIZE; j++) {
     1912                line = PyIter_Next(it);
     1913                if (line == NULL) {
     1914                    if (PyErr_Occurred())
     1915                        goto error;
     1916                    break;
     1917                }
     1918                PyList_SetItem(list, j, line);
     1919            }
     1920            /* The iterator might have closed the file on us. */
     1921            if (f->f_fp == NULL) {
     1922                err_closed();
     1923                goto error;
     1924            }
     1925        }
     1926        if (j == 0)
     1927            break;
     1928
     1929        /* Check that all entries are indeed strings. If not,
     1930           apply the same rules as for file.write() and
     1931           convert the results to strings. This is slow, but
     1932           seems to be the only way since all conversion APIs
     1933           could potentially execute Python code. */
     1934        for (i = 0; i < j; i++) {
     1935            PyObject *v = PyList_GET_ITEM(list, i);
     1936            if (!PyString_Check(v)) {
     1937                const char *buffer;
     1938                if (((f->f_binary &&
     1939                      PyObject_AsReadBuffer(v,
     1940                          (const void**)&buffer,
     1941                                        &len)) ||
     1942                     PyObject_AsCharBuffer(v,
     1943                                           &buffer,
     1944                                           &len))) {
     1945                    PyErr_SetString(PyExc_TypeError,
     1946            "writelines() argument must be a sequence of strings");
     1947                            goto error;
     1948                }
     1949                line = PyString_FromStringAndSize(buffer,
     1950                                                  len);
     1951                if (line == NULL)
     1952                    goto error;
     1953                Py_DECREF(v);
     1954                PyList_SET_ITEM(list, i, line);
     1955            }
     1956        }
     1957
     1958        /* Since we are releasing the global lock, the
     1959           following code may *not* execute Python code. */
     1960        f->f_softspace = 0;
     1961        FILE_BEGIN_ALLOW_THREADS(f)
     1962        errno = 0;
     1963        for (i = 0; i < j; i++) {
     1964            line = PyList_GET_ITEM(list, i);
     1965            len = PyString_GET_SIZE(line);
     1966            nwritten = fwrite(PyString_AS_STRING(line),
     1967                              1, len, f->f_fp);
     1968            if (nwritten != len) {
     1969                FILE_ABORT_ALLOW_THREADS(f)
     1970                PyErr_SetFromErrno(PyExc_IOError);
     1971                clearerr(f->f_fp);
     1972                goto error;
     1973            }
     1974        }
     1975        FILE_END_ALLOW_THREADS(f)
     1976
     1977        if (j < CHUNKSIZE)
     1978            break;
     1979    }
     1980
     1981    Py_INCREF(Py_None);
     1982    result = Py_None;
    17961983  error:
    1797         Py_XDECREF(list);
    1798         Py_XDECREF(it);
    1799         return result;
     1984    Py_XDECREF(list);
     1985    Py_XDECREF(it);
     1986    return result;
    18001987#undef CHUNKSIZE
    18011988}
     
    18041991file_self(PyFileObject *f)
    18051992{
    1806         if (f->f_fp == NULL)
    1807                 return err_closed();
    1808         Py_INCREF(f);
    1809         return (PyObject *)f;
     1993    if (f->f_fp == NULL)
     1994        return err_closed();
     1995    Py_INCREF(f);
     1996    return (PyObject *)f;
    18101997}
    18111998
     
    18132000file_xreadlines(PyFileObject *f)
    18142001{
    1815         if (PyErr_WarnPy3k("f.xreadlines() not supported in 3.x, "
    1816                            "try 'for line in f' instead", 1) < 0)
    1817                return NULL;
    1818         return file_self(f);
     2002    if (PyErr_WarnPy3k("f.xreadlines() not supported in 3.x, "
     2003                       "try 'for line in f' instead", 1) < 0)
     2004           return NULL;
     2005    return file_self(f);
    18192006}
    18202007
     
    18222009file_exit(PyObject *f, PyObject *args)
    18232010{
    1824         PyObject *ret = PyObject_CallMethod(f, "close", NULL);
    1825         if (!ret)
    1826                 /* If error occurred, pass through */
    1827                 return NULL;
    1828         Py_DECREF(ret);
    1829         /* We cannot return the result of close since a true
    1830         * value will be interpreted as "yes, swallow the
    1831         * exception if one was raised inside the with block". */
    1832         Py_RETURN_NONE;
     2011    PyObject *ret = PyObject_CallMethod(f, "close", NULL);
     2012    if (!ret)
     2013        /* If error occurred, pass through */
     2014        return NULL;
     2015    Py_DECREF(ret);
     2016    /* We cannot return the result of close since a true
     2017    * value will be interpreted as "yes, swallow the
     2018    * exception if one was raised inside the with block". */
     2019    Py_RETURN_NONE;
    18332020}
    18342021
     
    19182105
    19192106PyDoc_STRVAR(enter_doc,
    1920              "__enter__() -> self.");
     2107             "__enter__() -> self.");
    19212108
    19222109PyDoc_STRVAR(exit_doc,
    1923              "__exit__(*excinfo) -> None.  Closes the file.");
     2110             "__exit__(*excinfo) -> None.  Closes the file.");
    19242111
    19252112static PyMethodDef file_methods[] = {
    1926         {"readline",  (PyCFunction)file_readline, METH_VARARGS, readline_doc},
    1927         {"read",      (PyCFunction)file_read,     METH_VARARGS, read_doc},
    1928         {"write",     (PyCFunction)file_write,    METH_VARARGS, write_doc},
    1929         {"fileno",    (PyCFunction)file_fileno,   METH_NOARGS,  fileno_doc},
    1930         {"seek",      (PyCFunction)file_seek,     METH_VARARGS, seek_doc},
     2113    {"readline",  (PyCFunction)file_readline, METH_VARARGS, readline_doc},
     2114    {"read",      (PyCFunction)file_read,     METH_VARARGS, read_doc},
     2115    {"write",     (PyCFunction)file_write,    METH_VARARGS, write_doc},
     2116    {"fileno",    (PyCFunction)file_fileno,   METH_NOARGS,  fileno_doc},
     2117    {"seek",      (PyCFunction)file_seek,     METH_VARARGS, seek_doc},
    19312118#ifdef HAVE_FTRUNCATE
    1932         {"truncate",  (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
    1933 #endif
    1934         {"tell",      (PyCFunction)file_tell,     METH_NOARGS,  tell_doc},
    1935         {"readinto",  (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
    1936         {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
    1937         {"xreadlines",(PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
    1938         {"writelines",(PyCFunction)file_writelines, METH_O,     writelines_doc},
    1939         {"flush",     (PyCFunction)file_flush,    METH_NOARGS,  flush_doc},
    1940         {"close",     (PyCFunction)file_close,    METH_NOARGS,  close_doc},
    1941         {"isatty",    (PyCFunction)file_isatty,   METH_NOARGS,  isatty_doc},
    1942         {"__enter__", (PyCFunction)file_self,     METH_NOARGS,  enter_doc},
    1943         {"__exit__",  (PyCFunction)file_exit,     METH_VARARGS, exit_doc},
    1944         {NULL,        NULL}             /* sentinel */
     2119    {"truncate",  (PyCFunction)file_truncate, METH_VARARGS, truncate_doc},
     2120#endif
     2121    {"tell",      (PyCFunction)file_tell,     METH_NOARGS,  tell_doc},
     2122    {"readinto",  (PyCFunction)file_readinto, METH_VARARGS, readinto_doc},
     2123    {"readlines", (PyCFunction)file_readlines, METH_VARARGS, readlines_doc},
     2124    {"xreadlines",(PyCFunction)file_xreadlines, METH_NOARGS, xreadlines_doc},
     2125    {"writelines",(PyCFunction)file_writelines, METH_O,     writelines_doc},
     2126    {"flush",     (PyCFunction)file_flush,    METH_NOARGS,  flush_doc},
     2127    {"close",     (PyCFunction)file_close,    METH_NOARGS,  close_doc},
     2128    {"isatty",    (PyCFunction)file_isatty,   METH_NOARGS,  isatty_doc},
     2129    {"__enter__", (PyCFunction)file_self,     METH_NOARGS,  enter_doc},
     2130    {"__exit__",  (PyCFunction)file_exit,     METH_VARARGS, exit_doc},
     2131    {NULL,            NULL}             /* sentinel */
    19452132};
    19462133
     
    19482135
    19492136static PyMemberDef file_memberlist[] = {
    1950         {"mode",        T_OBJECT,       OFF(f_mode),    RO,
    1951         "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
    1952         {"name",        T_OBJECT,       OFF(f_name),    RO,
    1953         "file name"},
    1954         {"encoding",    T_OBJECT,       OFF(f_encoding),        RO,
    1955         "file encoding"},
    1956         {"errors",      T_OBJECT,       OFF(f_errors),  RO,
    1957         "Unicode error handler"},
    1958         /* getattr(f, "closed") is implemented without this table */
    1959         {NULL}  /* Sentinel */
     2137    {"mode",            T_OBJECT,       OFF(f_mode),    RO,
     2138    "file mode ('r', 'U', 'w', 'a', possibly with 'b' or '+' added)"},
     2139    {"name",            T_OBJECT,       OFF(f_name),    RO,
     2140    "file name"},
     2141    {"encoding",        T_OBJECT,       OFF(f_encoding),        RO,
     2142    "file encoding"},
     2143    {"errors",          T_OBJECT,       OFF(f_errors),  RO,
     2144    "Unicode error handler"},
     2145    /* getattr(f, "closed") is implemented without this table */
     2146    {NULL}      /* Sentinel */
    19602147};
    19612148
     
    19632150get_closed(PyFileObject *f, void *closure)
    19642151{
    1965         return PyBool_FromLong((long)(f->f_fp == 0));
     2152    return PyBool_FromLong((long)(f->f_fp == 0));
    19662153}
    19672154static PyObject *
    19682155get_newlines(PyFileObject *f, void *closure)
    19692156{
    1970         switch (f->f_newlinetypes) {
    1971         case NEWLINE_UNKNOWN:
    1972                 Py_INCREF(Py_None);
    1973                 return Py_None;
    1974         case NEWLINE_CR:
    1975                 return PyString_FromString("\r");
    1976         case NEWLINE_LF:
    1977                 return PyString_FromString("\n");
    1978         case NEWLINE_CR|NEWLINE_LF:
    1979                 return Py_BuildValue("(ss)", "\r", "\n");
    1980         case NEWLINE_CRLF:
    1981                 return PyString_FromString("\r\n");
    1982         case NEWLINE_CR|NEWLINE_CRLF:
    1983                 return Py_BuildValue("(ss)", "\r", "\r\n");
    1984         case NEWLINE_LF|NEWLINE_CRLF:
    1985                 return Py_BuildValue("(ss)", "\n", "\r\n");
    1986         case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
    1987                 return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
    1988         default:
    1989                 PyErr_Format(PyExc_SystemError,
    1990                              "Unknown newlines value 0x%x\n",
    1991                              f->f_newlinetypes);
    1992                 return NULL;
    1993         }
     2157    switch (f->f_newlinetypes) {
     2158    case NEWLINE_UNKNOWN:
     2159        Py_INCREF(Py_None);
     2160        return Py_None;
     2161    case NEWLINE_CR:
     2162        return PyString_FromString("\r");
     2163    case NEWLINE_LF:
     2164        return PyString_FromString("\n");
     2165    case NEWLINE_CR|NEWLINE_LF:
     2166        return Py_BuildValue("(ss)", "\r", "\n");
     2167    case NEWLINE_CRLF:
     2168        return PyString_FromString("\r\n");
     2169    case NEWLINE_CR|NEWLINE_CRLF:
     2170        return Py_BuildValue("(ss)", "\r", "\r\n");
     2171    case NEWLINE_LF|NEWLINE_CRLF:
     2172        return Py_BuildValue("(ss)", "\n", "\r\n");
     2173    case NEWLINE_CR|NEWLINE_LF|NEWLINE_CRLF:
     2174        return Py_BuildValue("(sss)", "\r", "\n", "\r\n");
     2175    default:
     2176        PyErr_Format(PyExc_SystemError,
     2177                     "Unknown newlines value 0x%x\n",
     2178                     f->f_newlinetypes);
     2179        return NULL;
     2180    }
    19942181}
    19952182
     
    19972184get_softspace(PyFileObject *f, void *closure)
    19982185{
    1999         if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
    2000                 return NULL;
    2001         return PyInt_FromLong(f->f_softspace);
     2186    if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
     2187        return NULL;
     2188    return PyInt_FromLong(f->f_softspace);
    20022189}
    20032190
     
    20052192set_softspace(PyFileObject *f, PyObject *value)
    20062193{
    2007         int new;
    2008         if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
    2009                 return -1;
    2010 
    2011         if (value == NULL) {
    2012                 PyErr_SetString(PyExc_TypeError,
    2013                                 "can't delete softspace attribute");
    2014                 return -1;
    2015         }
    2016 
    2017         new = PyInt_AsLong(value);
    2018         if (new == -1 && PyErr_Occurred())
    2019                 return -1;
    2020         f->f_softspace = new;
    2021         return 0;
     2194    int new;
     2195    if (PyErr_WarnPy3k("file.softspace not supported in 3.x", 1) < 0)
     2196        return -1;
     2197
     2198    if (value == NULL) {
     2199        PyErr_SetString(PyExc_TypeError,
     2200                        "can't delete softspace attribute");
     2201        return -1;
     2202    }
     2203
     2204    new = PyInt_AsLong(value);
     2205    if (new == -1 && PyErr_Occurred())
     2206        return -1;
     2207    f->f_softspace = new;
     2208    return 0;
    20222209}
    20232210
    20242211static PyGetSetDef file_getsetlist[] = {
    2025         {"closed", (getter)get_closed, NULL, "True if the file is closed"},
    2026         {"newlines", (getter)get_newlines, NULL,
    2027         "end-of-line convention used in this file"},
    2028         {"softspace", (getter)get_softspace, (setter)set_softspace,
    2029         "flag indicating that a space needs to be printed; used by print"},
    2030         {0},
     2212    {"closed", (getter)get_closed, NULL, "True if the file is closed"},
     2213    {"newlines", (getter)get_newlines, NULL,
     2214    "end-of-line convention used in this file"},
     2215    {"softspace", (getter)get_softspace, (setter)set_softspace,
     2216    "flag indicating that a space needs to be printed; used by print"},
     2217    {0},
    20312218};
    20322219
     
    20342221drop_readahead(PyFileObject *f)
    20352222{
    2036         if (f->f_buf != NULL) {
    2037                 PyMem_Free(f->f_buf);
    2038                 f->f_buf = NULL;
    2039         }
     2223    if (f->f_buf != NULL) {
     2224        PyMem_Free(f->f_buf);
     2225        f->f_buf = NULL;
     2226    }
    20402227}
    20412228
     
    20462233readahead(PyFileObject *f, int bufsize)
    20472234{
    2048         Py_ssize_t chunksize;
    2049 
    2050         if (f->f_buf != NULL) {
    2051                 if( (f->f_bufend - f->f_bufptr) >= 1)
    2052                         return 0;
    2053                 else
    2054                         drop_readahead(f);
    2055         }
    2056         if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
    2057                 PyErr_NoMemory();
    2058                 return -1;
    2059         }
    2060         FILE_BEGIN_ALLOW_THREADS(f)
    2061         errno = 0;
    2062         chunksize = Py_UniversalNewlineFread(
    2063                 f->f_buf, bufsize, f->f_fp, (PyObject *)f);
    2064         FILE_END_ALLOW_THREADS(f)
    2065         if (chunksize == 0) {
    2066                 if (ferror(f->f_fp)) {
    2067                         PyErr_SetFromErrno(PyExc_IOError);
    2068                         clearerr(f->f_fp);
    2069                         drop_readahead(f);
    2070                         return -1;
    2071                 }
    2072         }
    2073         f->f_bufptr = f->f_buf;
    2074         f->f_bufend = f->f_buf + chunksize;
    2075         return 0;
     2235    Py_ssize_t chunksize;
     2236
     2237    if (f->f_buf != NULL) {
     2238        if( (f->f_bufend - f->f_bufptr) >= 1)
     2239            return 0;
     2240        else
     2241            drop_readahead(f);
     2242    }
     2243    if ((f->f_buf = (char *)PyMem_Malloc(bufsize)) == NULL) {
     2244        PyErr_NoMemory();
     2245        return -1;
     2246    }
     2247    FILE_BEGIN_ALLOW_THREADS(f)
     2248    errno = 0;
     2249    chunksize = Py_UniversalNewlineFread(
     2250        f->f_buf, bufsize, f->f_fp, (PyObject *)f);
     2251    FILE_END_ALLOW_THREADS(f)
     2252    if (chunksize == 0) {
     2253        if (ferror(f->f_fp)) {
     2254            PyErr_SetFromErrno(PyExc_IOError);
     2255            clearerr(f->f_fp);
     2256            drop_readahead(f);
     2257            return -1;
     2258        }
     2259    }
     2260    f->f_bufptr = f->f_buf;
     2261    f->f_bufend = f->f_buf + chunksize;
     2262    return 0;
    20762263}
    20772264
     
    20842271readahead_get_line_skip(PyFileObject *f, int skip, int bufsize)
    20852272{
    2086         PyStringObject* s;
    2087         char *bufptr;
    2088         char *buf;
    2089         Py_ssize_t len;
    2090 
    2091         if (f->f_buf == NULL)
    2092                 if (readahead(f, bufsize) < 0)
    2093                         return NULL;
    2094 
    2095         len = f->f_bufend - f->f_bufptr;
    2096         if (len == 0)
    2097                 return (PyStringObject *)
    2098                         PyString_FromStringAndSize(NULL, skip);
    2099         bufptr = (char *)memchr(f->f_bufptr, '\n', len);
    2100         if (bufptr != NULL) {
    2101                 bufptr++;                       /* Count the '\n' */
    2102                 len = bufptr - f->f_bufptr;
    2103                 s = (PyStringObject *)
    2104                         PyString_FromStringAndSize(NULL, skip+len);
    2105                 if (s == NULL)
    2106                         return NULL;
    2107                 memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
    2108                 f->f_bufptr = bufptr;
    2109                 if (bufptr == f->f_bufend)
    2110                         drop_readahead(f);
    2111         } else {
    2112                 bufptr = f->f_bufptr;
    2113                 buf = f->f_buf;
    2114                 f->f_buf = NULL;        /* Force new readahead buffer */
    2115                 assert(skip+len < INT_MAX);
    2116                 s = readahead_get_line_skip(
    2117                         f, (int)(skip+len), bufsize + (bufsize>>2) );
    2118                 if (s == NULL) {
    2119                         PyMem_Free(buf);
    2120                         return NULL;
    2121                 }
    2122                 memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
    2123                 PyMem_Free(buf);
    2124         }
    2125         return s;
     2273    PyStringObject* s;
     2274    char *bufptr;
     2275    char *buf;
     2276    Py_ssize_t len;
     2277
     2278    if (f->f_buf == NULL)
     2279        if (readahead(f, bufsize) < 0)
     2280            return NULL;
     2281
     2282    len = f->f_bufend - f->f_bufptr;
     2283    if (len == 0)
     2284        return (PyStringObject *)
     2285            PyString_FromStringAndSize(NULL, skip);
     2286    bufptr = (char *)memchr(f->f_bufptr, '\n', len);
     2287    if (bufptr != NULL) {
     2288        bufptr++;                               /* Count the '\n' */
     2289        len = bufptr - f->f_bufptr;
     2290        s = (PyStringObject *)
     2291            PyString_FromStringAndSize(NULL, skip+len);
     2292        if (s == NULL)
     2293            return NULL;
     2294        memcpy(PyString_AS_STRING(s)+skip, f->f_bufptr, len);
     2295        f->f_bufptr = bufptr;
     2296        if (bufptr == f->f_bufend)
     2297            drop_readahead(f);
     2298    } else {
     2299        bufptr = f->f_bufptr;
     2300        buf = f->f_buf;
     2301        f->f_buf = NULL;                /* Force new readahead buffer */
     2302        assert(skip+len < INT_MAX);
     2303        s = readahead_get_line_skip(
     2304            f, (int)(skip+len), bufsize + (bufsize>>2) );
     2305        if (s == NULL) {
     2306            PyMem_Free(buf);
     2307            return NULL;
     2308        }
     2309        memcpy(PyString_AS_STRING(s)+skip, bufptr, len);
     2310        PyMem_Free(buf);
     2311    }
     2312    return s;
    21262313}
    21272314
     
    21322319file_iternext(PyFileObject *f)
    21332320{
    2134         PyStringObject* l;
    2135 
    2136         if (f->f_fp == NULL)
    2137                 return err_closed();
    2138         if (!f->readable)
    2139                 return err_mode("reading");
    2140 
    2141         l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
    2142         if (l == NULL || PyString_GET_SIZE(l) == 0) {
    2143                 Py_XDECREF(l);
    2144                 return NULL;
    2145         }
    2146         return (PyObject *)l;
     2321    PyStringObject* l;
     2322
     2323    if (f->f_fp == NULL)
     2324        return err_closed();
     2325    if (!f->readable)
     2326        return err_mode("reading");
     2327
     2328    l = readahead_get_line_skip(f, 0, READAHEAD_BUFSIZE);
     2329    if (l == NULL || PyString_GET_SIZE(l) == 0) {
     2330        Py_XDECREF(l);
     2331        return NULL;
     2332    }
     2333    return (PyObject *)l;
    21472334}
    21482335
     
    21512338file_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    21522339{
    2153         PyObject *self;
    2154         static PyObject *not_yet_string;
    2155 
    2156         assert(type != NULL && type->tp_alloc != NULL);
    2157 
    2158         if (not_yet_string == NULL) {
    2159                 not_yet_string = PyString_InternFromString("<uninitialized file>");
    2160                 if (not_yet_string == NULL)
    2161                         return NULL;
    2162         }
    2163 
    2164         self = type->tp_alloc(type, 0);
    2165         if (self != NULL) {
    2166                 /* Always fill in the name and mode, so that nobody else
    2167                    needs to special-case NULLs there. */
    2168                 Py_INCREF(not_yet_string);
    2169                 ((PyFileObject *)self)->f_name = not_yet_string;
    2170                 Py_INCREF(not_yet_string);
    2171                 ((PyFileObject *)self)->f_mode = not_yet_string;
    2172                 Py_INCREF(Py_None);
    2173                 ((PyFileObject *)self)->f_encoding = Py_None;
    2174                 Py_INCREF(Py_None);
    2175                 ((PyFileObject *)self)->f_errors = Py_None;
    2176                 ((PyFileObject *)self)->weakreflist = NULL;
    2177                 ((PyFileObject *)self)->unlocked_count = 0;
    2178         }
    2179         return self;
     2340    PyObject *self;
     2341    static PyObject *not_yet_string;
     2342
     2343    assert(type != NULL && type->tp_alloc != NULL);
     2344
     2345    if (not_yet_string == NULL) {
     2346        not_yet_string = PyString_InternFromString("<uninitialized file>");
     2347        if (not_yet_string == NULL)
     2348            return NULL;
     2349    }
     2350
     2351    self = type->tp_alloc(type, 0);
     2352    if (self != NULL) {
     2353        /* Always fill in the name and mode, so that nobody else
     2354           needs to special-case NULLs there. */
     2355        Py_INCREF(not_yet_string);
     2356        ((PyFileObject *)self)->f_name = not_yet_string;
     2357        Py_INCREF(not_yet_string);
     2358        ((PyFileObject *)self)->f_mode = not_yet_string;
     2359        Py_INCREF(Py_None);
     2360        ((PyFileObject *)self)->f_encoding = Py_None;
     2361        Py_INCREF(Py_None);
     2362        ((PyFileObject *)self)->f_errors = Py_None;
     2363        ((PyFileObject *)self)->weakreflist = NULL;
     2364        ((PyFileObject *)self)->unlocked_count = 0;
     2365    }
     2366    return self;
    21802367}
    21812368
     
    21832370file_init(PyObject *self, PyObject *args, PyObject *kwds)
    21842371{
    2185         PyFileObject *foself = (PyFileObject *)self;
    2186         int ret = 0;
    2187         static char *kwlist[] = {"name", "mode", "buffering", 0};
    2188         char *name = NULL;
    2189         char *mode = "r";
    2190         int bufsize = -1;
    2191         int wideargument = 0;
    2192 
    2193         assert(PyFile_Check(self));
    2194         if (foself->f_fp != NULL) {
    2195                 /* Have to close the existing file first. */
    2196                 PyObject *closeresult = file_close(foself);
    2197                 if (closeresult == NULL)
    2198                         return -1;
    2199                 Py_DECREF(closeresult);
    2200         }
    2201 
    2202 #ifdef Py_WIN_WIDE_FILENAMES
    2203         if (GetVersion() < 0x80000000) {    /* On NT, so wide API available */
    2204                 PyObject *po;
    2205                 if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
    2206                                                 kwlist, &po, &mode, &bufsize)) {
    2207                         wideargument = 1;
    2208                         if (fill_file_fields(foself, NULL, po, mode,
    2209                                              fclose) == NULL)
    2210                                 goto Error;
    2211                 } else {
    2212                         /* Drop the argument parsing error as narrow
    2213                            strings are also valid. */
    2214                         PyErr_Clear();
    2215                 }
    2216         }
    2217 #endif
    2218 
    2219         if (!wideargument) {
    2220                 PyObject *o_name;
    2221 
    2222                 if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
    2223                                                 Py_FileSystemDefaultEncoding,
    2224                                                 &name,
    2225                                                 &mode, &bufsize))
    2226                         return -1;
    2227 
    2228                 /* We parse again to get the name as a PyObject */
    2229                 if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file",
    2230                                                  kwlist, &o_name, &mode,
    2231                                                  &bufsize))
    2232                         goto Error;
    2233 
    2234                 if (fill_file_fields(foself, NULL, o_name, mode,
    2235                                      fclose) == NULL)
    2236                         goto Error;
    2237         }
    2238         if (open_the_file(foself, name, mode) == NULL)
    2239                 goto Error;
    2240         foself->f_setbuf = NULL;
    2241         PyFile_SetBufSize(self, bufsize);
    2242         goto Done;
     2372    PyFileObject *foself = (PyFileObject *)self;
     2373    int ret = 0;
     2374    static char *kwlist[] = {"name", "mode", "buffering", 0};
     2375    char *name = NULL;
     2376    char *mode = "r";
     2377    int bufsize = -1;
     2378    int wideargument = 0;
     2379#ifdef MS_WINDOWS
     2380    PyObject *po;
     2381#endif
     2382
     2383    assert(PyFile_Check(self));
     2384    if (foself->f_fp != NULL) {
     2385        /* Have to close the existing file first. */
     2386        PyObject *closeresult = file_close(foself);
     2387        if (closeresult == NULL)
     2388            return -1;
     2389        Py_DECREF(closeresult);
     2390    }
     2391
     2392#ifdef MS_WINDOWS
     2393    if (PyArg_ParseTupleAndKeywords(args, kwds, "U|si:file",
     2394                                    kwlist, &po, &mode, &bufsize)) {
     2395        wideargument = 1;
     2396        if (fill_file_fields(foself, NULL, po, mode,
     2397                             fclose) == NULL)
     2398            goto Error;
     2399    } else {
     2400        /* Drop the argument parsing error as narrow
     2401           strings are also valid. */
     2402        PyErr_Clear();
     2403    }
     2404#endif
     2405
     2406    if (!wideargument) {
     2407        PyObject *o_name;
     2408
     2409        if (!PyArg_ParseTupleAndKeywords(args, kwds, "et|si:file", kwlist,
     2410                                        Py_FileSystemDefaultEncoding,
     2411                                        &name,
     2412                                        &mode, &bufsize))
     2413            return -1;
     2414
     2415        /* We parse again to get the name as a PyObject */
     2416        if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|si:file",
     2417                                         kwlist, &o_name, &mode,
     2418                                         &bufsize))
     2419            goto Error;
     2420
     2421        if (fill_file_fields(foself, NULL, o_name, mode,
     2422                             fclose) == NULL)
     2423            goto Error;
     2424    }
     2425    if (open_the_file(foself, name, mode) == NULL)
     2426        goto Error;
     2427    foself->f_setbuf = NULL;
     2428    PyFile_SetBufSize(self, bufsize);
     2429    goto Done;
    22432430
    22442431Error:
    2245         ret = -1;
    2246         /* fall through */
     2432    ret = -1;
     2433    /* fall through */
    22472434Done:
    2248         PyMem_Free(name); /* free the encoded string */
    2249         return ret;
     2435    PyMem_Free(name); /* free the encoded string */
     2436    return ret;
    22502437}
    22512438
     
    22742461
    22752462PyTypeObject PyFile_Type = {
    2276         PyVarObject_HEAD_INIT(&PyType_Type, 0)
    2277         "file",
    2278         sizeof(PyFileObject),
    2279         0,
    2280         (destructor)file_dealloc,               /* tp_dealloc */
    2281         0,                                      /* tp_print */
    2282         0,                                      /* tp_getattr */
    2283         0,                                      /* tp_setattr */
    2284         0,                                      /* tp_compare */
    2285         (reprfunc)file_repr,                    /* tp_repr */
    2286         0,                                      /* tp_as_number */
    2287         0,                                      /* tp_as_sequence */
    2288         0,                                      /* tp_as_mapping */
    2289         0,                                      /* tp_hash */
    2290         0,                                      /* tp_call */
    2291         0,                                      /* tp_str */
    2292         PyObject_GenericGetAttr,                /* tp_getattro */
    2293         /* softspace is writable:  we must supply tp_setattro */
    2294         PyObject_GenericSetAttr,                /* tp_setattro */
    2295         0,                                      /* tp_as_buffer */
    2296         Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
    2297         file_doc,                               /* tp_doc */
    2298         0,                                      /* tp_traverse */
    2299         0,                                      /* tp_clear */
    2300         0,                                      /* tp_richcompare */
    2301         offsetof(PyFileObject, weakreflist),    /* tp_weaklistoffset */
    2302         (getiterfunc)file_self,                 /* tp_iter */
    2303         (iternextfunc)file_iternext,            /* tp_iternext */
    2304         file_methods,                           /* tp_methods */
    2305         file_memberlist,                        /* tp_members */
    2306         file_getsetlist,                        /* tp_getset */
    2307         0,                                      /* tp_base */
    2308         0,                                      /* tp_dict */
    2309         0,                                      /* tp_descr_get */
    2310         0,                                      /* tp_descr_set */
    2311         0,                                      /* tp_dictoffset */
    2312         file_init,                              /* tp_init */
    2313         PyType_GenericAlloc,                    /* tp_alloc */
    2314         file_new,                               /* tp_new */
    2315         PyObject_Del,                           /* tp_free */
     2463    PyVarObject_HEAD_INIT(&PyType_Type, 0)
     2464    "file",
     2465    sizeof(PyFileObject),
     2466    0,
     2467    (destructor)file_dealloc,                   /* tp_dealloc */
     2468    0,                                          /* tp_print */
     2469    0,                                          /* tp_getattr */
     2470    0,                                          /* tp_setattr */
     2471    0,                                          /* tp_compare */
     2472    (reprfunc)file_repr,                        /* tp_repr */
     2473    0,                                          /* tp_as_number */
     2474    0,                                          /* tp_as_sequence */
     2475    0,                                          /* tp_as_mapping */
     2476    0,                                          /* tp_hash */
     2477    0,                                          /* tp_call */
     2478    0,                                          /* tp_str */
     2479    PyObject_GenericGetAttr,                    /* tp_getattro */
     2480    /* softspace is writable:  we must supply tp_setattro */
     2481    PyObject_GenericSetAttr,                    /* tp_setattro */
     2482    0,                                          /* tp_as_buffer */
     2483    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
     2484    file_doc,                                   /* tp_doc */
     2485    0,                                          /* tp_traverse */
     2486    0,                                          /* tp_clear */
     2487    0,                                          /* tp_richcompare */
     2488    offsetof(PyFileObject, weakreflist),        /* tp_weaklistoffset */
     2489    (getiterfunc)file_self,                     /* tp_iter */
     2490    (iternextfunc)file_iternext,                /* tp_iternext */
     2491    file_methods,                               /* tp_methods */
     2492    file_memberlist,                            /* tp_members */
     2493    file_getsetlist,                            /* tp_getset */
     2494    0,                                          /* tp_base */
     2495    0,                                          /* tp_dict */
     2496    0,                                          /* tp_descr_get */
     2497    0,                                          /* tp_descr_set */
     2498    0,                                          /* tp_dictoffset */
     2499    file_init,                                  /* tp_init */
     2500    PyType_GenericAlloc,                        /* tp_alloc */
     2501    file_new,                                   /* tp_new */
     2502    PyObject_Del,                           /* tp_free */
    23162503};
    23172504
     
    23212508PyFile_SoftSpace(PyObject *f, int newflag)
    23222509{
    2323         long oldflag = 0;
    2324         if (f == NULL) {
    2325                 /* Do nothing */
    2326         }
    2327         else if (PyFile_Check(f)) {
    2328                 oldflag = ((PyFileObject *)f)->f_softspace;
    2329                 ((PyFileObject *)f)->f_softspace = newflag;
    2330         }
    2331         else {
    2332                 PyObject *v;
    2333                 v = PyObject_GetAttrString(f, "softspace");
    2334                 if (v == NULL)
    2335                         PyErr_Clear();
    2336                 else {
    2337                         if (PyInt_Check(v))
    2338                                 oldflag = PyInt_AsLong(v);
    2339                         assert(oldflag < INT_MAX);
    2340                         Py_DECREF(v);
    2341                 }
    2342                 v = PyInt_FromLong((long)newflag);
    2343                 if (v == NULL)
    2344                         PyErr_Clear();
    2345                 else {
    2346                         if (PyObject_SetAttrString(f, "softspace", v) != 0)
    2347                                 PyErr_Clear();
    2348                         Py_DECREF(v);
    2349                 }
    2350         }
    2351         return (int)oldflag;
     2510    long oldflag = 0;
     2511    if (f == NULL) {
     2512        /* Do nothing */
     2513    }
     2514    else if (PyFile_Check(f)) {
     2515        oldflag = ((PyFileObject *)f)->f_softspace;
     2516        ((PyFileObject *)f)->f_softspace = newflag;
     2517    }
     2518    else {
     2519        PyObject *v;
     2520        v = PyObject_GetAttrString(f, "softspace");
     2521        if (v == NULL)
     2522            PyErr_Clear();
     2523        else {
     2524            if (PyInt_Check(v))
     2525                oldflag = PyInt_AsLong(v);
     2526            assert(oldflag < INT_MAX);
     2527            Py_DECREF(v);
     2528        }
     2529        v = PyInt_FromLong((long)newflag);
     2530        if (v == NULL)
     2531            PyErr_Clear();
     2532        else {
     2533            if (PyObject_SetAttrString(f, "softspace", v) != 0)
     2534                PyErr_Clear();
     2535            Py_DECREF(v);
     2536        }
     2537    }
     2538    return (int)oldflag;
    23522539}
    23532540
     
    23572544PyFile_WriteObject(PyObject *v, PyObject *f, int flags)
    23582545{
    2359         PyObject *writer, *value, *args, *result;
    2360         if (f == NULL) {
    2361                 PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
    2362                 return -1;
    2363         }
    2364         else if (PyFile_Check(f)) {
    2365                 PyFileObject *fobj = (PyFileObject *) f;
     2546    PyObject *writer, *value, *args, *result;
     2547    if (f == NULL) {
     2548        PyErr_SetString(PyExc_TypeError, "writeobject with NULL file");
     2549        return -1;
     2550    }
     2551    else if (PyFile_Check(f)) {
     2552        PyFileObject *fobj = (PyFileObject *) f;
    23662553#ifdef Py_USING_UNICODE
    2367                 PyObject *enc = fobj->f_encoding;
    2368                 int result;
    2369 #endif
    2370                 if (fobj->f_fp == NULL) {
    2371                         err_closed();
    2372                         return -1;
    2373                 }
     2554        PyObject *enc = fobj->f_encoding;
     2555        int result;
     2556#endif
     2557        if (fobj->f_fp == NULL) {
     2558            err_closed();
     2559            return -1;
     2560        }
    23742561#ifdef Py_USING_UNICODE
    2375                 if ((flags & Py_PRINT_RAW) &&
    2376                     PyUnicode_Check(v) && enc != Py_None) {
    2377                         char *cenc = PyString_AS_STRING(enc);
    2378                         char *errors = fobj->f_errors == Py_None ?
    2379                           "strict" : PyString_AS_STRING(fobj->f_errors);
    2380                         value = PyUnicode_AsEncodedString(v, cenc, errors);
    2381                         if (value == NULL)
    2382                                 return -1;
    2383                 } else {
    2384                         value = v;
    2385                         Py_INCREF(value);
    2386                 }
    2387                 result = file_PyObject_Print(value, fobj, flags);
    2388                 Py_DECREF(value);
    2389                 return result;
     2562        if ((flags & Py_PRINT_RAW) &&
     2563            PyUnicode_Check(v) && enc != Py_None) {
     2564            char *cenc = PyString_AS_STRING(enc);
     2565            char *errors = fobj->f_errors == Py_None ?
     2566              "strict" : PyString_AS_STRING(fobj->f_errors);
     2567            value = PyUnicode_AsEncodedString(v, cenc, errors);
     2568            if (value == NULL)
     2569                return -1;
     2570        } else {
     2571            value = v;
     2572            Py_INCREF(value);
     2573        }
     2574        result = file_PyObject_Print(value, fobj, flags);
     2575        Py_DECREF(value);
     2576        return result;
    23902577#else
    2391                 return file_PyObject_Print(v, fobj, flags);
    2392 #endif
    2393         }
    2394         writer = PyObject_GetAttrString(f, "write");
    2395         if (writer == NULL)
    2396                 return -1;
    2397         if (flags & Py_PRINT_RAW) {
    2398                 if (PyUnicode_Check(v)) {
    2399                         value = v;
    2400                         Py_INCREF(value);
    2401                 } else
    2402                         value = PyObject_Str(v);
    2403         }
    2404         else
    2405                 value = PyObject_Repr(v);
    2406         if (value == NULL) {
    2407                 Py_DECREF(writer);
    2408                 return -1;
    2409         }
    2410         args = PyTuple_Pack(1, value);
    2411         if (args == NULL) {
    2412                 Py_DECREF(value);
    2413                 Py_DECREF(writer);
    2414                 return -1;
    2415         }
    2416         result = PyEval_CallObject(writer, args);
    2417         Py_DECREF(args);
    2418         Py_DECREF(value);
    2419         Py_DECREF(writer);
    2420         if (result == NULL)
    2421                 return -1;
    2422         Py_DECREF(result);
    2423         return 0;
     2578        return file_PyObject_Print(v, fobj, flags);
     2579#endif
     2580    }
     2581    writer = PyObject_GetAttrString(f, "write");
     2582    if (writer == NULL)
     2583        return -1;
     2584    if (flags & Py_PRINT_RAW) {
     2585        if (PyUnicode_Check(v)) {
     2586            value = v;
     2587            Py_INCREF(value);
     2588        } else
     2589            value = PyObject_Str(v);
     2590    }
     2591    else
     2592        value = PyObject_Repr(v);
     2593    if (value == NULL) {
     2594        Py_DECREF(writer);
     2595        return -1;
     2596    }
     2597    args = PyTuple_Pack(1, value);
     2598    if (args == NULL) {
     2599        Py_DECREF(value);
     2600        Py_DECREF(writer);
     2601        return -1;
     2602    }
     2603    result = PyEval_CallObject(writer, args);
     2604    Py_DECREF(args);
     2605    Py_DECREF(value);
     2606    Py_DECREF(writer);
     2607    if (result == NULL)
     2608        return -1;
     2609    Py_DECREF(result);
     2610    return 0;
    24242611}
    24252612
     
    24282615{
    24292616
    2430         if (f == NULL) {
    2431                 /* Should be caused by a pre-existing error */
    2432                 if (!PyErr_Occurred())
    2433                         PyErr_SetString(PyExc_SystemError,
    2434                                         "null file for PyFile_WriteString");
    2435                 return -1;
    2436         }
    2437         else if (PyFile_Check(f)) {
    2438                 PyFileObject *fobj = (PyFileObject *) f;
    2439                 FILE *fp = PyFile_AsFile(f);
    2440                 if (fp == NULL) {
    2441                         err_closed();
    2442                         return -1;
    2443                 }
    2444                 FILE_BEGIN_ALLOW_THREADS(fobj)
    2445                 fputs(s, fp);
    2446                 FILE_END_ALLOW_THREADS(fobj)
    2447                 return 0;
    2448         }
    2449         else if (!PyErr_Occurred()) {
    2450                 PyObject *v = PyString_FromString(s);
    2451                 int err;
    2452                 if (v == NULL)
    2453                         return -1;
    2454                 err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
    2455                 Py_DECREF(v);
    2456                 return err;
    2457         }
    2458         else
    2459                 return -1;
     2617    if (f == NULL) {
     2618        /* Should be caused by a pre-existing error */
     2619        if (!PyErr_Occurred())
     2620            PyErr_SetString(PyExc_SystemError,
     2621                            "null file for PyFile_WriteString");
     2622        return -1;
     2623    }
     2624    else if (PyFile_Check(f)) {
     2625        PyFileObject *fobj = (PyFileObject *) f;
     2626        FILE *fp = PyFile_AsFile(f);
     2627        if (fp == NULL) {
     2628            err_closed();
     2629            return -1;
     2630        }
     2631        FILE_BEGIN_ALLOW_THREADS(fobj)
     2632        fputs(s, fp);
     2633        FILE_END_ALLOW_THREADS(fobj)
     2634        return 0;
     2635    }
     2636    else if (!PyErr_Occurred()) {
     2637        PyObject *v = PyString_FromString(s);
     2638        int err;
     2639        if (v == NULL)
     2640            return -1;
     2641        err = PyFile_WriteObject(v, f, Py_PRINT_RAW);
     2642        Py_DECREF(v);
     2643        return err;
     2644    }
     2645    else
     2646        return -1;
    24602647}
    24612648
     
    24692656int PyObject_AsFileDescriptor(PyObject *o)
    24702657{
    2471         int fd;
    2472         PyObject *meth;
    2473 
    2474         if (PyInt_Check(o)) {
    2475                 fd = PyInt_AsLong(o);
    2476         }
    2477         else if (PyLong_Check(o)) {
    2478                 fd = PyLong_AsLong(o);
    2479         }
    2480         else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
    2481         {
    2482                 PyObject *fno = PyEval_CallObject(meth, NULL);
    2483                 Py_DECREF(meth);
    2484                 if (fno == NULL)
    2485                         return -1;
    2486 
    2487                 if (PyInt_Check(fno)) {
    2488                         fd = PyInt_AsLong(fno);
    2489                         Py_DECREF(fno);
    2490                 }
    2491                 else if (PyLong_Check(fno)) {
    2492                         fd = PyLong_AsLong(fno);
    2493                         Py_DECREF(fno);
    2494                 }
    2495                 else {
    2496                         PyErr_SetString(PyExc_TypeError,
    2497                                         "fileno() returned a non-integer");
    2498                         Py_DECREF(fno);
    2499                         return -1;
    2500                 }
    2501         }
    2502         else {
    2503                 PyErr_SetString(PyExc_TypeError,
    2504                                 "argument must be an int, or have a fileno() method.");
    2505                 return -1;
    2506         }
    2507 
    2508         if (fd < 0) {
    2509                 PyErr_Format(PyExc_ValueError,
    2510                              "file descriptor cannot be a negative integer (%i)",
    2511                              fd);
    2512                 return -1;
    2513         }
    2514         return fd;
     2658    int fd;
     2659    PyObject *meth;
     2660
     2661    if (PyInt_Check(o)) {
     2662        fd = _PyInt_AsInt(o);
     2663    }
     2664    else if (PyLong_Check(o)) {
     2665        fd = _PyLong_AsInt(o);
     2666    }
     2667    else if ((meth = PyObject_GetAttrString(o, "fileno")) != NULL)
     2668    {
     2669        PyObject *fno = PyEval_CallObject(meth, NULL);
     2670        Py_DECREF(meth);
     2671        if (fno == NULL)
     2672            return -1;
     2673
     2674        if (PyInt_Check(fno)) {
     2675            fd = _PyInt_AsInt(fno);
     2676            Py_DECREF(fno);
     2677        }
     2678        else if (PyLong_Check(fno)) {
     2679            fd = _PyLong_AsInt(fno);
     2680            Py_DECREF(fno);
     2681        }
     2682        else {
     2683            PyErr_SetString(PyExc_TypeError,
     2684                            "fileno() returned a non-integer");
     2685            Py_DECREF(fno);
     2686            return -1;
     2687        }
     2688    }
     2689    else {
     2690        PyErr_SetString(PyExc_TypeError,
     2691                        "argument must be an int, or have a fileno() method.");
     2692        return -1;
     2693    }
     2694
     2695    if (fd < 0) {
     2696        PyErr_Format(PyExc_ValueError,
     2697                     "file descriptor cannot be a negative integer (%i)",
     2698                     fd);
     2699        return -1;
     2700    }
     2701    return fd;
    25152702}
    25162703
     
    25362723Py_UniversalNewlineFgets(char *buf, int n, FILE *stream, PyObject *fobj)
    25372724{
    2538         char *p = buf;
    2539         int c;
    2540         int newlinetypes = 0;
    2541         int skipnextlf = 0;
    2542         int univ_newline = 1;
    2543 
    2544         if (fobj) {
    2545                 if (!PyFile_Check(fobj)) {
    2546                         errno = ENXIO;  /* What can you do... */
    2547                         return NULL;
    2548                 }
    2549                 univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
    2550                 if ( !univ_newline )
    2551                         return fgets(buf, n, stream);
    2552                 newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
    2553                 skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
    2554         }
    2555         FLOCKFILE(stream);
    2556         c = 'x'; /* Shut up gcc warning */
    2557         while (--n > 0 && (c = GETC(stream)) != EOF ) {
    2558                 if (skipnextlf ) {
    2559                         skipnextlf = 0;
    2560                         if (c == '\n') {
    2561                                 /* Seeing a \n here with skipnextlf true
    2562                                 ** means we saw a \r before.
    2563                                 */
    2564                                 newlinetypes |= NEWLINE_CRLF;
    2565                                 c = GETC(stream);
    2566                                 if (c == EOF) break;
    2567                         } else {
    2568                                 /*
    2569                                 ** Note that c == EOF also brings us here,
    2570                                 ** so we're okay if the last char in the file
    2571                                 ** is a CR.
    2572                                 */
    2573                                 newlinetypes |= NEWLINE_CR;
    2574                         }
    2575                 }
    2576                 if (c == '\r') {
    2577                         /* A \r is translated into a \n, and we skip
    2578                         ** an adjacent \n, if any. We don't set the
    2579                         ** newlinetypes flag until we've seen the next char.
    2580                         */
    2581                         skipnextlf = 1;
    2582                         c = '\n';
    2583                 } else if ( c == '\n') {
    2584                         newlinetypes |= NEWLINE_LF;
    2585                 }
    2586                 *p++ = c;
    2587                 if (c == '\n') break;
    2588         }
    2589         if ( c == EOF && skipnextlf )
    2590                 newlinetypes |= NEWLINE_CR;
    2591         FUNLOCKFILE(stream);
    2592         *p = '\0';
    2593         if (fobj) {
    2594                 ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
    2595                 ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
    2596         } else if ( skipnextlf ) {
    2597                 /* If we have no file object we cannot save the
    2598                 ** skipnextlf flag. We have to readahead, which
    2599                 ** will cause a pause if we're reading from an
    2600                 ** interactive stream, but that is very unlikely
    2601                 ** unless we're doing something silly like
    2602                 ** execfile("/dev/tty").
    2603                 */
    2604                 c = GETC(stream);
    2605                 if ( c != '\n' )
    2606                         ungetc(c, stream);
    2607         }
    2608         if (p == buf)
    2609                 return NULL;
    2610         return buf;
     2725    char *p = buf;
     2726    int c;
     2727    int newlinetypes = 0;
     2728    int skipnextlf = 0;
     2729    int univ_newline = 1;
     2730
     2731    if (fobj) {
     2732        if (!PyFile_Check(fobj)) {
     2733            errno = ENXIO;              /* What can you do... */
     2734            return NULL;
     2735        }
     2736        univ_newline = ((PyFileObject *)fobj)->f_univ_newline;
     2737        if ( !univ_newline )
     2738            return fgets(buf, n, stream);
     2739        newlinetypes = ((PyFileObject *)fobj)->f_newlinetypes;
     2740        skipnextlf = ((PyFileObject *)fobj)->f_skipnextlf;
     2741    }
     2742    FLOCKFILE(stream);
     2743    c = 'x'; /* Shut up gcc warning */
     2744    while (--n > 0 && (c = GETC(stream)) != EOF ) {
     2745        if (skipnextlf ) {
     2746            skipnextlf = 0;
     2747            if (c == '\n') {
     2748                /* Seeing a \n here with skipnextlf true
     2749                ** means we saw a \r before.
     2750                */
     2751                newlinetypes |= NEWLINE_CRLF;
     2752                c = GETC(stream);
     2753                if (c == EOF) break;
     2754            } else {
     2755                /*
     2756                ** Note that c == EOF also brings us here,
     2757                ** so we're okay if the last char in the file
     2758                ** is a CR.
     2759                */
     2760                newlinetypes |= NEWLINE_CR;
     2761            }
     2762        }
     2763        if (c == '\r') {
     2764            /* A \r is translated into a \n, and we skip
     2765            ** an adjacent \n, if any. We don't set the
     2766            ** newlinetypes flag until we've seen the next char.
     2767            */
     2768            skipnextlf = 1;
     2769            c = '\n';
     2770        } else if ( c == '\n') {
     2771            newlinetypes |= NEWLINE_LF;
     2772        }
     2773        *p++ = c;
     2774        if (c == '\n') break;
     2775    }
     2776    if ( c == EOF && skipnextlf )
     2777        newlinetypes |= NEWLINE_CR;
     2778    FUNLOCKFILE(stream);
     2779    *p = '\0';
     2780    if (fobj) {
     2781        ((PyFileObject *)fobj)->f_newlinetypes = newlinetypes;
     2782        ((PyFileObject *)fobj)->f_skipnextlf = skipnextlf;
     2783    } else if ( skipnextlf ) {
     2784        /* If we have no file object we cannot save the
     2785        ** skipnextlf flag. We have to readahead, which
     2786        ** will cause a pause if we're reading from an
     2787        ** interactive stream, but that is very unlikely
     2788        ** unless we're doing something silly like
     2789        ** execfile("/dev/tty").
     2790        */
     2791        c = GETC(stream);
     2792        if ( c != '\n' )
     2793            ungetc(c, stream);
     2794    }
     2795    if (p == buf)
     2796        return NULL;
     2797    return buf;
    26112798}
    26122799
     
    26232810size_t
    26242811Py_UniversalNewlineFread(char *buf, size_t n,
    2625                         FILE *stream, PyObject *fobj)
    2626 {
    2627         char *dst = buf;
    2628         PyFileObject *f = (PyFileObject *)fobj;
    2629         int newlinetypes, skipnextlf;
    2630 
    2631         assert(buf != NULL);
    2632         assert(stream != NULL);
    2633 
    2634         if (!fobj || !PyFile_Check(fobj)) {
    2635                 errno = ENXIO;  /* What can you do... */
    2636                 return 0;
    2637         }
    2638         if (!f->f_univ_newline)
    2639                 return fread(buf, 1, n, stream);
    2640         newlinetypes = f->f_newlinetypes;
    2641         skipnextlf = f->f_skipnextlf;
    2642         /* Invariant:  n is the number of bytes remaining to be filled
    2643         * in the buffer.
    2644         */
    2645         while (n) {
    2646                 size_t nread;
    2647                 int shortread;
    2648                 char *src = dst;
    2649 
    2650                 nread = fread(dst, 1, n, stream);
    2651                 assert(nread <= n);
    2652                 if (nread == 0)
    2653                         break;
    2654 
    2655                 n -= nread; /* assuming 1 byte out for each in; will adjust */
    2656                 shortread = n != 0;     /* true iff EOF or error */
    2657                 while (nread--) {
    2658                         char c = *src++;
    2659                         if (c == '\r') {
    2660                                 /* Save as LF and set flag to skip next LF. */
    2661                                 *dst++ = '\n';
    2662                                 skipnextlf = 1;
    2663                         }
    2664                         else if (skipnextlf && c == '\n') {
    2665                                 /* Skip LF, and remember we saw CR LF. */
    2666                                 skipnextlf = 0;
    2667                                 newlinetypes |= NEWLINE_CRLF;
    2668                                 ++n;
    2669                         }
    2670                         else {
    2671                                 /* Normal char to be stored in buffer.  Also
    2672                                 * update the newlinetypes flag if either this
    2673                                 * is an LF or the previous char was a CR.
    2674                                 */
    2675                                 if (c == '\n')
    2676                                         newlinetypes |= NEWLINE_LF;
    2677                                 else if (skipnextlf)
    2678                                         newlinetypes |= NEWLINE_CR;
    2679                                 *dst++ = c;
    2680                                 skipnextlf = 0;
    2681                         }
    2682                 }
    2683                 if (shortread) {
    2684                         /* If this is EOF, update type flags. */
    2685                         if (skipnextlf && feof(stream))
    2686                                 newlinetypes |= NEWLINE_CR;
    2687                         break;
    2688                 }
    2689         }
    2690         f->f_newlinetypes = newlinetypes;
    2691         f->f_skipnextlf = skipnextlf;
    2692         return dst - buf;
     2812                        FILE *stream, PyObject *fobj)
     2813{
     2814    char *dst = buf;
     2815    PyFileObject *f = (PyFileObject *)fobj;
     2816    int newlinetypes, skipnextlf;
     2817
     2818    assert(buf != NULL);
     2819    assert(stream != NULL);
     2820
     2821    if (!fobj || !PyFile_Check(fobj)) {
     2822        errno = ENXIO;          /* What can you do... */
     2823        return 0;
     2824    }
     2825    if (!f->f_univ_newline)
     2826        return fread(buf, 1, n, stream);
     2827    newlinetypes = f->f_newlinetypes;
     2828    skipnextlf = f->f_skipnextlf;
     2829    /* Invariant:  n is the number of bytes remaining to be filled
     2830    * in the buffer.
     2831    */
     2832    while (n) {
     2833        size_t nread;
     2834        int shortread;
     2835        char *src = dst;
     2836
     2837        nread = fread(dst, 1, n, stream);
     2838        assert(nread <= n);
     2839        if (nread == 0)
     2840            break;
     2841
     2842        n -= nread; /* assuming 1 byte out for each in; will adjust */
     2843        shortread = n != 0;             /* true iff EOF or error */
     2844        while (nread--) {
     2845            char c = *src++;
     2846            if (c == '\r') {
     2847                /* Save as LF and set flag to skip next LF. */
     2848                *dst++ = '\n';
     2849                skipnextlf = 1;
     2850            }
     2851            else if (skipnextlf && c == '\n') {
     2852                /* Skip LF, and remember we saw CR LF. */
     2853                skipnextlf = 0;
     2854                newlinetypes |= NEWLINE_CRLF;
     2855                ++n;
     2856            }
     2857            else {
     2858                /* Normal char to be stored in buffer.  Also
     2859                * update the newlinetypes flag if either this
     2860                * is an LF or the previous char was a CR.
     2861                */
     2862                if (c == '\n')
     2863                    newlinetypes |= NEWLINE_LF;
     2864                else if (skipnextlf)
     2865                    newlinetypes |= NEWLINE_CR;
     2866                *dst++ = c;
     2867                skipnextlf = 0;
     2868            }
     2869        }
     2870        if (shortread) {
     2871            /* If this is EOF, update type flags. */
     2872            if (skipnextlf && feof(stream))
     2873                newlinetypes |= NEWLINE_CR;
     2874            break;
     2875        }
     2876    }
     2877    f->f_newlinetypes = newlinetypes;
     2878    f->f_skipnextlf = skipnextlf;
     2879    return dst - buf;
    26932880}
    26942881
Note: See TracChangeset for help on using the changeset viewer.