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/PC/msvcrtmodule.c

    r2 r388  
    11/*********************************************************
    22
    3         msvcrtmodule.c
    4 
    5         A Python interface to the Microsoft Visual C Runtime
    6         Library, providing access to those non-portable, but
    7         still useful routines.
    8 
    9         Only ever compiled with an MS compiler, so no attempt
    10         has been made to avoid MS language extensions, etc...
    11 
    12         This may only work on NT or 95...
    13 
    14         Author: Mark Hammond and Guido van Rossum.
    15         Maintenance: Guido van Rossum.
     3    msvcrtmodule.c
     4
     5    A Python interface to the Microsoft Visual C Runtime
     6    Library, providing access to those non-portable, but
     7    still useful routines.
     8
     9    Only ever compiled with an MS compiler, so no attempt
     10    has been made to avoid MS language extensions, etc...
     11
     12    This may only work on NT or 95...
     13
     14    Author: Mark Hammond and Guido van Rossum.
     15    Maintenance: Guido van Rossum.
    1616
    1717***********************************************************/
     
    3434msvcrt_heapmin(PyObject *self, PyObject *args)
    3535{
    36         if (!PyArg_ParseTuple(args, ":heapmin"))
    37                 return NULL;
    38 
    39         if (_heapmin() != 0)
    40                 return PyErr_SetFromErrno(PyExc_IOError);
    41 
    42         Py_INCREF(Py_None);
    43         return Py_None;
    44 }
     36    if (!PyArg_ParseTuple(args, ":heapmin"))
     37        return NULL;
     38
     39    if (_heapmin() != 0)
     40        return PyErr_SetFromErrno(PyExc_IOError);
     41
     42    Py_INCREF(Py_None);
     43    return Py_None;
     44}
     45
     46PyDoc_STRVAR(heapmin_doc,
     47"heapmin() -> None\n\
     48\n\
     49Force the malloc() heap to clean itself up and return unused blocks\n\
     50to the operating system. On failure, this raises IOError.");
    4551
    4652// Perform locking operations on a C runtime file descriptor.
     
    4854msvcrt_locking(PyObject *self, PyObject *args)
    4955{
    50         int fd;
    51         int mode;
    52         long nbytes;
    53         int err;
    54 
    55         if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
    56                 return NULL;
    57 
    58         Py_BEGIN_ALLOW_THREADS
    59         err = _locking(fd, mode, nbytes);
    60         Py_END_ALLOW_THREADS
    61         if (err != 0)
    62                 return PyErr_SetFromErrno(PyExc_IOError);
    63 
    64         Py_INCREF(Py_None);
    65         return Py_None;
    66 }
     56    int fd;
     57    int mode;
     58    long nbytes;
     59    int err;
     60
     61    if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes))
     62        return NULL;
     63
     64    Py_BEGIN_ALLOW_THREADS
     65    err = _locking(fd, mode, nbytes);
     66    Py_END_ALLOW_THREADS
     67    if (err != 0)
     68        return PyErr_SetFromErrno(PyExc_IOError);
     69
     70    Py_INCREF(Py_None);
     71    return Py_None;
     72}
     73
     74PyDoc_STRVAR(locking_doc,
     75"locking(fd, mode, nbytes) -> None\n\
     76\n\
     77Lock part of a file based on file descriptor fd from the C runtime.\n\
     78Raises IOError on failure. The locked region of the file extends from\n\
     79the current file position for nbytes bytes, and may continue beyond\n\
     80the end of the file. mode must be one of the LK_* constants listed\n\
     81below. Multiple regions in a file may be locked at the same time, but\n\
     82may not overlap. Adjacent regions are not merged; they must be unlocked\n\
     83individually.");
    6784
    6885// Set the file translation mode for a C runtime file descriptor.
     
    7087msvcrt_setmode(PyObject *self, PyObject *args)
    7188{
    72         int fd;
    73         int flags;
    74         if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
    75                 return NULL;
    76 
    77         flags = _setmode(fd, flags);
    78         if (flags == -1)
    79                 return PyErr_SetFromErrno(PyExc_IOError);
    80 
    81         return PyInt_FromLong(flags);
    82 }
     89    int fd;
     90    int flags;
     91    if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags))
     92        return NULL;
     93
     94    flags = _setmode(fd, flags);
     95    if (flags == -1)
     96        return PyErr_SetFromErrno(PyExc_IOError);
     97
     98    return PyInt_FromLong(flags);
     99}
     100
     101PyDoc_STRVAR(setmode_doc,
     102"setmode(fd, mode) -> Previous mode\n\
     103\n\
     104Set the line-end translation mode for the file descriptor fd. To set\n\
     105it to text mode, flags should be os.O_TEXT; for binary, it should be\n\
     106os.O_BINARY.");
    83107
    84108// Convert an OS file handle to a C runtime file descriptor.
     
    86110msvcrt_open_osfhandle(PyObject *self, PyObject *args)
    87111{
    88         long handle;
    89         int flags;
    90         int fd;
    91 
    92         if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
    93                 return NULL;
    94 
    95         fd = _open_osfhandle(handle, flags);
    96         if (fd == -1)
    97                 return PyErr_SetFromErrno(PyExc_IOError);
    98 
    99         return PyInt_FromLong(fd);
    100 }
     112    long handle;
     113    int flags;
     114    int fd;
     115
     116    if (!PyArg_ParseTuple(args, "li:open_osfhandle", &handle, &flags))
     117        return NULL;
     118
     119    fd = _open_osfhandle(handle, flags);
     120    if (fd == -1)
     121        return PyErr_SetFromErrno(PyExc_IOError);
     122
     123    return PyInt_FromLong(fd);
     124}
     125
     126PyDoc_STRVAR(open_osfhandle_doc,
     127"open_osfhandle(handle, flags) -> file descriptor\n\
     128\n\
     129Create a C runtime file descriptor from the file handle handle. The\n\
     130flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\
     131and os.O_TEXT. The returned file descriptor may be used as a parameter\n\
     132to os.fdopen() to create a file object.");
    101133
    102134// Convert a C runtime file descriptor to an OS file handle.
     
    104136msvcrt_get_osfhandle(PyObject *self, PyObject *args)
    105137{
    106         int fd;
    107         Py_intptr_t handle;
    108 
    109         if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
    110                 return NULL;
    111 
    112         handle = _get_osfhandle(fd);
    113         if (handle == -1)
    114                 return PyErr_SetFromErrno(PyExc_IOError);
    115 
    116         /* technically 'handle' is not a pointer, but a integer as
    117            large as a pointer, Python's *VoidPtr interface is the
    118            most appropriate here */
    119         return PyLong_FromVoidPtr((void*)handle);
    120 }
     138    int fd;
     139    Py_intptr_t handle;
     140
     141    if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd))
     142        return NULL;
     143
     144    if (!_PyVerify_fd(fd))
     145        return PyErr_SetFromErrno(PyExc_IOError);
     146
     147    handle = _get_osfhandle(fd);
     148    if (handle == -1)
     149        return PyErr_SetFromErrno(PyExc_IOError);
     150
     151    /* technically 'handle' is not a pointer, but a integer as
     152       large as a pointer, Python's *VoidPtr interface is the
     153       most appropriate here */
     154    return PyLong_FromVoidPtr((void*)handle);
     155}
     156
     157PyDoc_STRVAR(get_osfhandle_doc,
     158"get_osfhandle(fd) -> file handle\n\
     159\n\
     160Return the file handle for the file descriptor fd. Raises IOError\n\
     161if fd is not recognized.");
    121162
    122163/* Console I/O */
     
    125166msvcrt_kbhit(PyObject *self, PyObject *args)
    126167{
    127         int ok;
    128 
    129         if (!PyArg_ParseTuple(args, ":kbhit"))
    130                 return NULL;
    131 
    132         ok = _kbhit();
    133         return PyInt_FromLong(ok);
    134 }
     168    int ok;
     169
     170    if (!PyArg_ParseTuple(args, ":kbhit"))
     171        return NULL;
     172
     173    ok = _kbhit();
     174    return PyInt_FromLong(ok);
     175}
     176
     177PyDoc_STRVAR(kbhit_doc,
     178"kbhit() -> bool\n\
     179\n\
     180Return true if a keypress is waiting to be read.");
    135181
    136182static PyObject *
    137183msvcrt_getch(PyObject *self, PyObject *args)
    138184{
    139         int ch;
    140         char s[1];
    141 
    142         if (!PyArg_ParseTuple(args, ":getch"))
    143                 return NULL;
    144 
    145         Py_BEGIN_ALLOW_THREADS
    146         ch = _getch();
    147         Py_END_ALLOW_THREADS
    148         s[0] = ch;
    149         return PyString_FromStringAndSize(s, 1);
    150 }
     185    int ch;
     186    char s[1];
     187
     188    if (!PyArg_ParseTuple(args, ":getch"))
     189        return NULL;
     190
     191    Py_BEGIN_ALLOW_THREADS
     192    ch = _getch();
     193    Py_END_ALLOW_THREADS
     194    s[0] = ch;
     195    return PyString_FromStringAndSize(s, 1);
     196}
     197
     198PyDoc_STRVAR(getch_doc,
     199"getch() -> key character\n\
     200\n\
     201Read a keypress and return the resulting character. Nothing is echoed to\n\
     202the console. This call will block if a keypress is not already\n\
     203available, but will not wait for Enter to be pressed. If the pressed key\n\
     204was a special function key, this will return '\\000' or '\\xe0'; the next\n\
     205call will return the keycode. The Control-C keypress cannot be read with\n\
     206this function.");
    151207
    152208#ifdef _WCONIO_DEFINED
     
    154210msvcrt_getwch(PyObject *self, PyObject *args)
    155211{
    156         Py_UNICODE ch;
    157         Py_UNICODE u[1];
    158 
    159         if (!PyArg_ParseTuple(args, ":getwch"))
    160                 return NULL;
    161 
    162         Py_BEGIN_ALLOW_THREADS
    163         ch = _getwch();
    164         Py_END_ALLOW_THREADS
    165         u[0] = ch;
    166         return PyUnicode_FromUnicode(u, 1);
    167 }
     212    Py_UNICODE ch;
     213    Py_UNICODE u[1];
     214
     215    if (!PyArg_ParseTuple(args, ":getwch"))
     216        return NULL;
     217
     218    Py_BEGIN_ALLOW_THREADS
     219    ch = _getwch();
     220    Py_END_ALLOW_THREADS
     221    u[0] = ch;
     222    return PyUnicode_FromUnicode(u, 1);
     223}
     224
     225PyDoc_STRVAR(getwch_doc,
     226"getwch() -> Unicode key character\n\
     227\n\
     228Wide char variant of getch(), returning a Unicode value.");
    168229#endif
    169230
     
    171232msvcrt_getche(PyObject *self, PyObject *args)
    172233{
    173         int ch;
    174         char s[1];
    175 
    176         if (!PyArg_ParseTuple(args, ":getche"))
    177                 return NULL;
    178 
    179         Py_BEGIN_ALLOW_THREADS
    180         ch = _getche();
    181         Py_END_ALLOW_THREADS
    182         s[0] = ch;
    183         return PyString_FromStringAndSize(s, 1);
    184 }
     234    int ch;
     235    char s[1];
     236
     237    if (!PyArg_ParseTuple(args, ":getche"))
     238        return NULL;
     239
     240    Py_BEGIN_ALLOW_THREADS
     241    ch = _getche();
     242    Py_END_ALLOW_THREADS
     243    s[0] = ch;
     244    return PyString_FromStringAndSize(s, 1);
     245}
     246
     247PyDoc_STRVAR(getche_doc,
     248"getche() -> key character\n\
     249\n\
     250Similar to getch(), but the keypress will be echoed if it represents\n\
     251a printable character.");
    185252
    186253#ifdef _WCONIO_DEFINED
     
    188255msvcrt_getwche(PyObject *self, PyObject *args)
    189256{
    190         Py_UNICODE ch;
    191         Py_UNICODE s[1];
    192 
    193         if (!PyArg_ParseTuple(args, ":getwche"))
    194                 return NULL;
    195 
    196         Py_BEGIN_ALLOW_THREADS
    197         ch = _getwche();
    198         Py_END_ALLOW_THREADS
    199         s[0] = ch;
    200         return PyUnicode_FromUnicode(s, 1);
    201 }
     257    Py_UNICODE ch;
     258    Py_UNICODE s[1];
     259
     260    if (!PyArg_ParseTuple(args, ":getwche"))
     261        return NULL;
     262
     263    Py_BEGIN_ALLOW_THREADS
     264    ch = _getwche();
     265    Py_END_ALLOW_THREADS
     266    s[0] = ch;
     267    return PyUnicode_FromUnicode(s, 1);
     268}
     269
     270PyDoc_STRVAR(getwche_doc,
     271"getwche() -> Unicode key character\n\
     272\n\
     273Wide char variant of getche(), returning a Unicode value.");
    202274#endif
    203275
     
    205277msvcrt_putch(PyObject *self, PyObject *args)
    206278{
    207         char ch;
    208 
    209         if (!PyArg_ParseTuple(args, "c:putch", &ch))
    210                 return NULL;
    211 
    212         _putch(ch);
    213         Py_INCREF(Py_None);
    214         return Py_None;
    215 }
     279    char ch;
     280
     281    if (!PyArg_ParseTuple(args, "c:putch", &ch))
     282        return NULL;
     283
     284    _putch(ch);
     285    Py_INCREF(Py_None);
     286    return Py_None;
     287}
     288
     289PyDoc_STRVAR(putch_doc,
     290"putch(char) -> None\n\
     291\n\
     292Print the character char to the console without buffering.");
    216293
    217294#ifdef _WCONIO_DEFINED
     
    219296msvcrt_putwch(PyObject *self, PyObject *args)
    220297{
    221         Py_UNICODE *ch;
    222         int size;
    223 
    224         if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
    225                 return NULL;
    226 
    227         if (size == 0) {
    228                 PyErr_SetString(PyExc_ValueError,
    229                         "Expected unicode string of length 1");
    230                 return NULL;
    231         }
    232         _putwch(*ch);
    233         Py_RETURN_NONE;
    234 
    235 }
     298    Py_UNICODE *ch;
     299    int size;
     300
     301    if (!PyArg_ParseTuple(args, "u#:putwch", &ch, &size))
     302        return NULL;
     303
     304    if (size == 0) {
     305        PyErr_SetString(PyExc_ValueError,
     306            "Expected unicode string of length 1");
     307        return NULL;
     308    }
     309    _putwch(*ch);
     310    Py_RETURN_NONE;
     311
     312}
     313
     314PyDoc_STRVAR(putwch_doc,
     315"putwch(unicode_char) -> None\n\
     316\n\
     317Wide char variant of putch(), accepting a Unicode value.");
    236318#endif
    237319
     
    239321msvcrt_ungetch(PyObject *self, PyObject *args)
    240322{
    241         char ch;
    242 
    243         if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
    244                 return NULL;
    245 
    246         if (_ungetch(ch) == EOF)
    247                 return PyErr_SetFromErrno(PyExc_IOError);
    248         Py_INCREF(Py_None);
    249         return Py_None;
    250 }
     323    char ch;
     324
     325    if (!PyArg_ParseTuple(args, "c:ungetch", &ch))
     326        return NULL;
     327
     328    if (_ungetch(ch) == EOF)
     329        return PyErr_SetFromErrno(PyExc_IOError);
     330    Py_INCREF(Py_None);
     331    return Py_None;
     332}
     333
     334PyDoc_STRVAR(ungetch_doc,
     335"ungetch(char) -> None\n\
     336\n\
     337Cause the character char to be \"pushed back\" into the console buffer;\n\
     338it will be the next character read by getch() or getche().");
    251339
    252340#ifdef _WCONIO_DEFINED
     
    254342msvcrt_ungetwch(PyObject *self, PyObject *args)
    255343{
    256         Py_UNICODE ch;
    257 
    258         if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
    259                 return NULL;
    260 
    261         if (_ungetch(ch) == EOF)
    262                 return PyErr_SetFromErrno(PyExc_IOError);
    263         Py_INCREF(Py_None);
    264         return Py_None;
    265 }
     344    Py_UNICODE ch;
     345
     346    if (!PyArg_ParseTuple(args, "u:ungetwch", &ch))
     347        return NULL;
     348
     349    if (_ungetch(ch) == EOF)
     350        return PyErr_SetFromErrno(PyExc_IOError);
     351    Py_INCREF(Py_None);
     352    return Py_None;
     353}
     354
     355PyDoc_STRVAR(ungetwch_doc,
     356"ungetwch(unicode_char) -> None\n\
     357\n\
     358Wide char variant of ungetch(), accepting a Unicode value.");
    266359#endif
    267360
     
    269362insertint(PyObject *d, char *name, int value)
    270363{
    271         PyObject *v = PyInt_FromLong((long) value);
    272         if (v == NULL) {
    273                 /* Don't bother reporting this error */
    274                 PyErr_Clear();
    275         }
    276         else {
    277                 PyDict_SetItemString(d, name, v);
    278                 Py_DECREF(v);
    279         }
     364    PyObject *v = PyInt_FromLong((long) value);
     365    if (v == NULL) {
     366        /* Don't bother reporting this error */
     367        PyErr_Clear();
     368    }
     369    else {
     370        PyDict_SetItemString(d, name, v);
     371        Py_DECREF(v);
     372    }
    280373}
    281374
     
    283376/* List of functions exported by this module */
    284377static struct PyMethodDef msvcrt_functions[] = {
    285         {"heapmin",             msvcrt_heapmin, METH_VARARGS},
    286         {"locking",             msvcrt_locking, METH_VARARGS},
    287         {"setmode",             msvcrt_setmode, METH_VARARGS},
    288         {"open_osfhandle",      msvcrt_open_osfhandle, METH_VARARGS},
    289         {"get_osfhandle",       msvcrt_get_osfhandle, METH_VARARGS},
    290         {"kbhit",               msvcrt_kbhit, METH_VARARGS},
    291         {"getch",               msvcrt_getch, METH_VARARGS},
    292         {"getche",              msvcrt_getche, METH_VARARGS},
    293         {"putch",               msvcrt_putch, METH_VARARGS},
    294         {"ungetch",             msvcrt_ungetch, METH_VARARGS},
     378    {"heapmin",                 msvcrt_heapmin, METH_VARARGS, heapmin_doc},
     379    {"locking",             msvcrt_locking, METH_VARARGS, locking_doc},
     380    {"setmode",                 msvcrt_setmode, METH_VARARGS, setmode_doc},
     381    {"open_osfhandle",          msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc},
     382    {"get_osfhandle",           msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc},
     383    {"kbhit",                   msvcrt_kbhit, METH_VARARGS, kbhit_doc},
     384    {"getch",                   msvcrt_getch, METH_VARARGS, getch_doc},
     385    {"getche",                  msvcrt_getche, METH_VARARGS, getche_doc},
     386    {"putch",                   msvcrt_putch, METH_VARARGS, putch_doc},
     387    {"ungetch",                 msvcrt_ungetch, METH_VARARGS, ungetch_doc},
    295388#ifdef _WCONIO_DEFINED
    296         {"getwch",              msvcrt_getwch, METH_VARARGS},
    297         {"getwche",             msvcrt_getwche, METH_VARARGS},
    298         {"putwch",              msvcrt_putwch, METH_VARARGS},
    299         {"ungetwch",            msvcrt_ungetwch, METH_VARARGS},
    300 #endif
    301         {NULL,                  NULL}
     389    {"getwch",                  msvcrt_getwch, METH_VARARGS, getwch_doc},
     390    {"getwche",                 msvcrt_getwche, METH_VARARGS, getwche_doc},
     391    {"putwch",                  msvcrt_putwch, METH_VARARGS, putwch_doc},
     392    {"ungetwch",                msvcrt_ungetwch, METH_VARARGS, ungetwch_doc},
     393#endif
     394    {NULL,                      NULL}
    302395};
    303396
     
    305398initmsvcrt(void)
    306399{
    307         int st;
    308         PyObject *d;
    309         PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
    310         if (m == NULL)
    311                 return;
    312         d = PyModule_GetDict(m);
    313 
    314         /* constants for the locking() function's mode argument */
    315         insertint(d, "LK_LOCK", _LK_LOCK);
    316         insertint(d, "LK_NBLCK", _LK_NBLCK);
    317         insertint(d, "LK_NBRLCK", _LK_NBRLCK);
    318         insertint(d, "LK_RLCK", _LK_RLCK);
    319         insertint(d, "LK_UNLCK", _LK_UNLCK);
    320 
    321         /* constants for the crt versions */
     400    int st;
     401    PyObject *d;
     402    PyObject *m = Py_InitModule("msvcrt", msvcrt_functions);
     403    if (m == NULL)
     404        return;
     405    d = PyModule_GetDict(m);
     406
     407    /* constants for the locking() function's mode argument */
     408    insertint(d, "LK_LOCK", _LK_LOCK);
     409    insertint(d, "LK_NBLCK", _LK_NBLCK);
     410    insertint(d, "LK_NBRLCK", _LK_NBRLCK);
     411    insertint(d, "LK_RLCK", _LK_RLCK);
     412    insertint(d, "LK_UNLCK", _LK_UNLCK);
     413
     414    /* constants for the crt versions */
    322415#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
    323         st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
    324                                         _VC_ASSEMBLY_PUBLICKEYTOKEN);
    325         if (st < 0)return;
     416    st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
     417                                    _VC_ASSEMBLY_PUBLICKEYTOKEN);
     418    if (st < 0)return;
    326419#endif
    327420#ifdef _CRT_ASSEMBLY_VERSION
    328         st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
    329                                         _CRT_ASSEMBLY_VERSION);
    330         if (st < 0)return;
     421    st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
     422                                    _CRT_ASSEMBLY_VERSION);
     423    if (st < 0)return;
    331424#endif
    332425#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
    333         st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
    334                                         __LIBRARIES_ASSEMBLY_NAME_PREFIX);
    335         if (st < 0)return;
    336 #endif
    337 }
     426    st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
     427                                    __LIBRARIES_ASSEMBLY_NAME_PREFIX);
     428    if (st < 0)return;
     429#endif
     430}
Note: See TracChangeset for help on using the changeset viewer.