diff options
Diffstat (limited to 'PC/msvcrtmodule.c')
-rw-r--r-- | PC/msvcrtmodule.c | 604 |
1 files changed, 324 insertions, 280 deletions
diff --git a/PC/msvcrtmodule.c b/PC/msvcrtmodule.c index 18dec6d..52d4100 100644 --- a/PC/msvcrtmodule.c +++ b/PC/msvcrtmodule.c @@ -32,421 +32,451 @@ #endif #endif -// Force the malloc heap to clean itself up, and free unused blocks -// back to the OS. (According to the docs, only works on NT.) +/*[python input] +class Py_intptr_t_converter(CConverter): + type = 'Py_intptr_t' + format_unit = '"_Py_PARSE_INTPTR"' + +class handle_return_converter(long_return_converter): + type = 'Py_intptr_t' + cast = '(void *)' + conversion_fn = 'PyLong_FromVoidPtr' + +class byte_char_return_converter(CReturnConverter): + type = 'int' + + def render(self, function, data): + data.declarations.append('char s[1];') + data.return_value = 's[0]' + data.return_conversion.append( + 'return_value = PyBytes_FromStringAndSize(s, 1);\n') + +class wchar_t_return_converter(CReturnConverter): + type = 'wchar_t' + + def render(self, function, data): + self.declare(data) + data.return_conversion.append( + 'return_value = PyUnicode_FromOrdinal(_return_value);\n') +[python start generated code]*/ +/*[python end generated code: output=da39a3ee5e6b4b0d input=6a54fc4e73d0b367]*/ + +/*[clinic input] +module msvcrt +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f31a87a783d036cd]*/ + +#include "clinic/msvcrtmodule.c.h" + +/*[clinic input] +msvcrt.heapmin + +Minimize the malloc() heap. + +Force the malloc() heap to clean itself up and return unused blocks +to the operating system. On failure, this raises OSError. +[clinic start generated code]*/ + static PyObject * -msvcrt_heapmin(PyObject *self, PyObject *args) +msvcrt_heapmin_impl(PyModuleDef *module) +/*[clinic end generated code: output=464f866feb57c436 input=82e1771d21bde2d8]*/ { - if (!PyArg_ParseTuple(args, ":heapmin")) - return NULL; - if (_heapmin() != 0) return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +msvcrt.locking + + fd: int + mode: int + nbytes: long + / -PyDoc_STRVAR(heapmin_doc, -"heapmin() -> None\n\ -\n\ -Force the malloc() heap to clean itself up and return unused blocks\n\ -to the operating system. On failure, this raises IOError."); +Lock part of a file based on file descriptor fd from the C runtime. + +Raises IOError on failure. The locked region of the file extends from +the current file position for nbytes bytes, and may continue beyond +the end of the file. mode must be one of the LK_* constants listed +below. Multiple regions in a file may be locked at the same time, but +may not overlap. Adjacent regions are not merged; they must be unlocked +individually. +[clinic start generated code]*/ -// Perform locking operations on a C runtime file descriptor. static PyObject * -msvcrt_locking(PyObject *self, PyObject *args) +msvcrt_locking_impl(PyModuleDef *module, int fd, int mode, long nbytes) +/*[clinic end generated code: output=dff41e5e76d544de input=d9f13f0f6a713ba7]*/ { - int fd; - int mode; - long nbytes; int err; - if (!PyArg_ParseTuple(args, "iil:locking", &fd, &mode, &nbytes)) - return NULL; - Py_BEGIN_ALLOW_THREADS err = _locking(fd, mode, nbytes); Py_END_ALLOW_THREADS if (err != 0) return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(locking_doc, -"locking(fd, mode, nbytes) -> None\n\ -\n\ -Lock part of a file based on file descriptor fd from the C runtime.\n\ -Raises IOError on failure. The locked region of the file extends from\n\ -the current file position for nbytes bytes, and may continue beyond\n\ -the end of the file. mode must be one of the LK_* constants listed\n\ -below. Multiple regions in a file may be locked at the same time, but\n\ -may not overlap. Adjacent regions are not merged; they must be unlocked\n\ -individually."); - -// Set the file translation mode for a C runtime file descriptor. -static PyObject * -msvcrt_setmode(PyObject *self, PyObject *args) -{ - int fd; - int flags; - if (!PyArg_ParseTuple(args,"ii:setmode", &fd, &flags)) - return NULL; +/*[clinic input] +msvcrt.setmode -> long + + fd: int + mode as flags: int + / + +Set the line-end translation mode for the file descriptor fd. + +To set it to text mode, flags should be os.O_TEXT; for binary, it +should be os.O_BINARY. + +Return value is the previous mode. +[clinic start generated code]*/ +static long +msvcrt_setmode_impl(PyModuleDef *module, int fd, int flags) +/*[clinic end generated code: output=8c84e5b37c586d0d input=76e7c01f6b137f75]*/ +{ flags = _setmode(fd, flags); if (flags == -1) - return PyErr_SetFromErrno(PyExc_IOError); + PyErr_SetFromErrno(PyExc_IOError); - return PyLong_FromLong(flags); + return flags; } -PyDoc_STRVAR(setmode_doc, -"setmode(fd, mode) -> Previous mode\n\ -\n\ -Set the line-end translation mode for the file descriptor fd. To set\n\ -it to text mode, flags should be os.O_TEXT; for binary, it should be\n\ -os.O_BINARY."); +/*[clinic input] +msvcrt.open_osfhandle -> long -// Convert an OS file handle to a C runtime file descriptor. -static PyObject * -msvcrt_open_osfhandle(PyObject *self, PyObject *args) + handle: Py_intptr_t + flags: int + / + +Create a C runtime file descriptor from the file handle handle. + +The flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY, +and os.O_TEXT. The returned file descriptor may be used as a parameter +to os.fdopen() to create a file object. +[clinic start generated code]*/ + +static long +msvcrt_open_osfhandle_impl(PyModuleDef *module, Py_intptr_t handle, + int flags) +/*[clinic end generated code: output=86bce32582c49c06 input=4d8516ed32db8f65]*/ { - Py_intptr_t handle; - int flags; int fd; - if (!PyArg_ParseTuple(args, _Py_PARSE_INTPTR "i:open_osfhandle", - &handle, &flags)) - return NULL; - fd = _open_osfhandle(handle, flags); if (fd == -1) - return PyErr_SetFromErrno(PyExc_IOError); + PyErr_SetFromErrno(PyExc_IOError); - return PyLong_FromLong(fd); + return fd; } -PyDoc_STRVAR(open_osfhandle_doc, -"open_osfhandle(handle, flags) -> file descriptor\n\ -\n\ -Create a C runtime file descriptor from the file handle handle. The\n\ -flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,\n\ -and os.O_TEXT. The returned file descriptor may be used as a parameter\n\ -to os.fdopen() to create a file object."); +/*[clinic input] +msvcrt.get_osfhandle -> handle -// Convert a C runtime file descriptor to an OS file handle. -static PyObject * -msvcrt_get_osfhandle(PyObject *self, PyObject *args) -{ - int fd; - Py_intptr_t handle; + fd: int + / - if (!PyArg_ParseTuple(args,"i:get_osfhandle", &fd)) - return NULL; +Return the file handle for the file descriptor fd. - if (!_PyVerify_fd(fd)) - return PyErr_SetFromErrno(PyExc_IOError); +Raises IOError if fd is not recognized. +[clinic start generated code]*/ - handle = _get_osfhandle(fd); - if (handle == -1) - return PyErr_SetFromErrno(PyExc_IOError); +static Py_intptr_t +msvcrt_get_osfhandle_impl(PyModuleDef *module, int fd) +/*[clinic end generated code: output=376bff52586b55a6 input=c7d18d02c8017ec1]*/ +{ + Py_intptr_t handle = -1; - /* technically 'handle' is not a pointer, but a integer as - large as a pointer, Python's *VoidPtr interface is the - most appropriate here */ - return PyLong_FromVoidPtr((void*)handle); -} + if (!_PyVerify_fd(fd)) { + PyErr_SetFromErrno(PyExc_IOError); + } + else { + _Py_BEGIN_SUPPRESS_IPH + handle = _get_osfhandle(fd); + _Py_END_SUPPRESS_IPH + if (handle == -1) + PyErr_SetFromErrno(PyExc_IOError); + } -PyDoc_STRVAR(get_osfhandle_doc, -"get_osfhandle(fd) -> file handle\n\ -\n\ -Return the file handle for the file descriptor fd. Raises IOError\n\ -if fd is not recognized."); + return handle; +} /* Console I/O */ +/*[clinic input] +msvcrt.kbhit -> long -static PyObject * -msvcrt_kbhit(PyObject *self, PyObject *args) +Return true if a keypress is waiting to be read. +[clinic start generated code]*/ + +static long +msvcrt_kbhit_impl(PyModuleDef *module) +/*[clinic end generated code: output=2b7293fcbe5cb24e input=e70d678a5c2f6acc]*/ { - int ok; + return _kbhit(); +} - if (!PyArg_ParseTuple(args, ":kbhit")) - return NULL; +/*[clinic input] +msvcrt.getch -> byte_char - ok = _kbhit(); - return PyLong_FromLong(ok); -} +Read a keypress and return the resulting character as a byte string. -PyDoc_STRVAR(kbhit_doc, -"kbhit() -> bool\n\ -\n\ -Return true if a keypress is waiting to be read."); +Nothing is echoed to the console. This call will block if a keypress is +not already available, but will not wait for Enter to be pressed. If the +pressed key was a special function key, this will return '\000' or +'\xe0'; the next call will return the keycode. The Control-C keypress +cannot be read with this function. +[clinic start generated code]*/ -static PyObject * -msvcrt_getch(PyObject *self, PyObject *args) +static int +msvcrt_getch_impl(PyModuleDef *module) +/*[clinic end generated code: output=199e3d89f49c166a input=37a40cf0ed0d1153]*/ { int ch; - char s[1]; - - if (!PyArg_ParseTuple(args, ":getch")) - return NULL; Py_BEGIN_ALLOW_THREADS ch = _getch(); Py_END_ALLOW_THREADS - s[0] = ch; - return PyBytes_FromStringAndSize(s, 1); + return ch; } -PyDoc_STRVAR(getch_doc, -"getch() -> key character\n\ -\n\ -Read a keypress and return the resulting character as a byte string.\n\ -Nothing is echoed to the console. This call will block if a keypress is\n\ -not already available, but will not wait for Enter to be pressed. If the\n\ -pressed key was a special function key, this will return '\\000' or\n\ -'\\xe0'; the next call will return the keycode. The Control-C keypress\n\ -cannot be read with this function."); - -#ifdef _WCONIO_DEFINED -static PyObject * -msvcrt_getwch(PyObject *self, PyObject *args) +/*[clinic input] +msvcrt.getwch -> wchar_t + +Wide char variant of getch(), returning a Unicode value. +[clinic start generated code]*/ + +static wchar_t +msvcrt_getwch_impl(PyModuleDef *module) +/*[clinic end generated code: output=9d3762861328b1fe input=27b3dec8ad823d7c]*/ { wchar_t ch; - if (!PyArg_ParseTuple(args, ":getwch")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getwch(); Py_END_ALLOW_THREADS - return PyUnicode_FromOrdinal(ch); + return ch; } -PyDoc_STRVAR(getwch_doc, -"getwch() -> Unicode key character\n\ -\n\ -Wide char variant of getch(), returning a Unicode value."); -#endif +/*[clinic input] +msvcrt.getche -> byte_char -static PyObject * -msvcrt_getche(PyObject *self, PyObject *args) +Similar to getch(), but the keypress will be echoed if possible. +[clinic start generated code]*/ + +static int +msvcrt_getche_impl(PyModuleDef *module) +/*[clinic end generated code: output=8aa369be6550068e input=43311ade9ed4a9c0]*/ { int ch; - char s[1]; - - if (!PyArg_ParseTuple(args, ":getche")) - return NULL; Py_BEGIN_ALLOW_THREADS ch = _getche(); Py_END_ALLOW_THREADS - s[0] = ch; - return PyBytes_FromStringAndSize(s, 1); + return ch; } -PyDoc_STRVAR(getche_doc, -"getche() -> key character\n\ -\n\ -Similar to getch(), but the keypress will be echoed if it represents\n\ -a printable character."); +/*[clinic input] +msvcrt.getwche -> wchar_t -#ifdef _WCONIO_DEFINED -static PyObject * -msvcrt_getwche(PyObject *self, PyObject *args) +Wide char variant of getche(), returning a Unicode value. +[clinic start generated code]*/ + +static wchar_t +msvcrt_getwche_impl(PyModuleDef *module) +/*[clinic end generated code: output=3693cf78e3ea0cf6 input=49337d59d1a591f8]*/ { wchar_t ch; - if (!PyArg_ParseTuple(args, ":getwche")) - return NULL; - Py_BEGIN_ALLOW_THREADS ch = _getwche(); Py_END_ALLOW_THREADS - return PyUnicode_FromOrdinal(ch); + return ch; } -PyDoc_STRVAR(getwche_doc, -"getwche() -> Unicode key character\n\ -\n\ -Wide char variant of getche(), returning a Unicode value."); -#endif +/*[clinic input] +msvcrt.putch + + char: char + / + +Print the byte string char to the console without buffering. +[clinic start generated code]*/ static PyObject * -msvcrt_putch(PyObject *self, PyObject *args) +msvcrt_putch_impl(PyModuleDef *module, char char_value) +/*[clinic end generated code: output=c05548b11554f36f input=ec078dd10cb054d6]*/ { - char ch; + _putch(char_value); + Py_RETURN_NONE; +} - if (!PyArg_ParseTuple(args, "c:putch", &ch)) - return NULL; +/*[clinic input] +msvcrt.putwch - _putch(ch); - Py_INCREF(Py_None); - return Py_None; -} + unicode_char: int(accept={str}) + / -PyDoc_STRVAR(putch_doc, -"putch(char) -> None\n\ -\n\ -Print the byte string char to the console without buffering."); +Wide char variant of putch(), accepting a Unicode value. +[clinic start generated code]*/ -#ifdef _WCONIO_DEFINED static PyObject * -msvcrt_putwch(PyObject *self, PyObject *args) +msvcrt_putwch_impl(PyModuleDef *module, int unicode_char) +/*[clinic end generated code: output=c216a73694ca73dd input=996ccd0bbcbac4c3]*/ { - int ch; - - if (!PyArg_ParseTuple(args, "C:putwch", &ch)) - return NULL; - - _putwch(ch); + _putwch(unicode_char); Py_RETURN_NONE; } -PyDoc_STRVAR(putwch_doc, -"putwch(unicode_char) -> None\n\ -\n\ -Wide char variant of putch(), accepting a Unicode value."); -#endif +/*[clinic input] +msvcrt.ungetch -static PyObject * -msvcrt_ungetch(PyObject *self, PyObject *args) -{ - char ch; + char: char + / - if (!PyArg_ParseTuple(args, "c:ungetch", &ch)) - return NULL; +Opposite of getch. + +Cause the byte string char to be "pushed back" into the +console buffer; it will be the next character read by +getch() or getche(). +[clinic start generated code]*/ - if (_ungetch(ch) == EOF) +static PyObject * +msvcrt_ungetch_impl(PyModuleDef *module, char char_value) +/*[clinic end generated code: output=19a4cd3249709ec9 input=22f07ee9001bbf0f]*/ +{ + if (_ungetch(char_value) == EOF) return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(ungetch_doc, -"ungetch(char) -> None\n\ -\n\ -Cause the byte string char to be \"pushed back\" into the\n\ -console buffer; it will be the next character read by\n\ -getch() or getche()."); +/*[clinic input] +msvcrt.ungetwch -#ifdef _WCONIO_DEFINED -static PyObject * -msvcrt_ungetwch(PyObject *self, PyObject *args) -{ - int ch; + unicode_char: int(accept={str}) + / - if (!PyArg_ParseTuple(args, "C:ungetwch", &ch)) - return NULL; +Wide char variant of ungetch(), accepting a Unicode value. +[clinic start generated code]*/ - if (_ungetwch(ch) == WEOF) +static PyObject * +msvcrt_ungetwch_impl(PyModuleDef *module, int unicode_char) +/*[clinic end generated code: output=1ee7674710322bd1 input=83ec0492be04d564]*/ +{ + if (_ungetwch(unicode_char) == WEOF) return PyErr_SetFromErrno(PyExc_IOError); - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(ungetwch_doc, -"ungetwch(unicode_char) -> None\n\ -\n\ -Wide char variant of ungetch(), accepting a Unicode value."); -#endif +#ifdef _DEBUG +/*[clinic input] +msvcrt.CrtSetReportFile -> long -static void -insertint(PyObject *d, char *name, int value) + type: int + file: int + / + +Wrapper around _CrtSetReportFile. + +Only available on Debug builds. +[clinic start generated code]*/ + +static long +msvcrt_CrtSetReportFile_impl(PyModuleDef *module, int type, int file) +/*[clinic end generated code: output=8c3644fb2edfa808 input=bb8f721a604fcc45]*/ { - PyObject *v = PyLong_FromLong((long) value); - if (v == NULL) { - /* Don't bother reporting this error */ - PyErr_Clear(); - } - else { - PyDict_SetItemString(d, name, v); - Py_DECREF(v); - } + return (long)_CrtSetReportFile(type, (_HFILE)file); } -#ifdef _DEBUG +/*[clinic input] +msvcrt.CrtSetReportMode -> long -static PyObject* -msvcrt_setreportfile(PyObject *self, PyObject *args) -{ - int type, file; - _HFILE res; + type: int + mode: int + / - if (!PyArg_ParseTuple(args, "ii", &type, &file)) - return NULL; - res = _CrtSetReportFile(type, (_HFILE)file); - return PyLong_FromLong((long)res); - Py_INCREF(Py_None); - return Py_None; -} +Wrapper around _CrtSetReportMode. + +Only available on Debug builds. +[clinic start generated code]*/ -static PyObject* -msvcrt_setreportmode(PyObject *self, PyObject *args) +static long +msvcrt_CrtSetReportMode_impl(PyModuleDef *module, int type, int mode) +/*[clinic end generated code: output=b407fbf8716a52b9 input=9319d29b4319426b]*/ { - int type, mode; int res; - if (!PyArg_ParseTuple(args, "ii", &type, &mode)) - return NULL; res = _CrtSetReportMode(type, mode); if (res == -1) - return PyErr_SetFromErrno(PyExc_IOError); - return PyLong_FromLong(res); + PyErr_SetFromErrno(PyExc_IOError); + return res; } -static PyObject* -msvcrt_seterrormode(PyObject *self, PyObject *args) -{ - int mode, res; +/*[clinic input] +msvcrt.set_error_mode -> long - if (!PyArg_ParseTuple(args, "i", &mode)) - return NULL; - res = _set_error_mode(mode); - return PyLong_FromLong(res); + mode: int + / + +Wrapper around _set_error_mode. + +Only available on Debug builds. +[clinic start generated code]*/ + +static long +msvcrt_set_error_mode_impl(PyModuleDef *module, int mode) +/*[clinic end generated code: output=62148adffa90867d input=046fca59c0f20872]*/ +{ + return _set_error_mode(mode); } +#endif /* _DEBUG */ -#endif +/*[clinic input] +msvcrt.SetErrorMode + + mode: unsigned_int(bitwise=True) + / -static PyObject* -seterrormode(PyObject *self, PyObject *args) +Wrapper around SetErrorMode. +[clinic start generated code]*/ + +static PyObject * +msvcrt_SetErrorMode_impl(PyModuleDef *module, unsigned int mode) +/*[clinic end generated code: output=544c60b085be79c6 input=d8b167258d32d907]*/ { - unsigned int mode, res; + unsigned int res; - if (!PyArg_ParseTuple(args, "I", &mode)) - return NULL; res = SetErrorMode(mode); return PyLong_FromUnsignedLong(res); } +/*[clinic input] +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/ /* List of functions exported by this module */ static struct PyMethodDef msvcrt_functions[] = { - {"heapmin", msvcrt_heapmin, METH_VARARGS, heapmin_doc}, - {"locking", msvcrt_locking, METH_VARARGS, locking_doc}, - {"setmode", msvcrt_setmode, METH_VARARGS, setmode_doc}, - {"open_osfhandle", msvcrt_open_osfhandle, METH_VARARGS, open_osfhandle_doc}, - {"get_osfhandle", msvcrt_get_osfhandle, METH_VARARGS, get_osfhandle_doc}, - {"kbhit", msvcrt_kbhit, METH_VARARGS, kbhit_doc}, - {"getch", msvcrt_getch, METH_VARARGS, getch_doc}, - {"getche", msvcrt_getche, METH_VARARGS, getche_doc}, - {"putch", msvcrt_putch, METH_VARARGS, putch_doc}, - {"ungetch", msvcrt_ungetch, METH_VARARGS, ungetch_doc}, - {"SetErrorMode", seterrormode, METH_VARARGS}, -#ifdef _DEBUG - {"CrtSetReportFile", msvcrt_setreportfile, METH_VARARGS}, - {"CrtSetReportMode", msvcrt_setreportmode, METH_VARARGS}, - {"set_error_mode", msvcrt_seterrormode, METH_VARARGS}, -#endif -#ifdef _WCONIO_DEFINED - {"getwch", msvcrt_getwch, METH_VARARGS, getwch_doc}, - {"getwche", msvcrt_getwche, METH_VARARGS, getwche_doc}, - {"putwch", msvcrt_putwch, METH_VARARGS, putwch_doc}, - {"ungetwch", msvcrt_ungetwch, METH_VARARGS, ungetwch_doc}, -#endif + MSVCRT_HEAPMIN_METHODDEF + MSVCRT_LOCKING_METHODDEF + MSVCRT_SETMODE_METHODDEF + MSVCRT_OPEN_OSFHANDLE_METHODDEF + MSVCRT_GET_OSFHANDLE_METHODDEF + MSVCRT_KBHIT_METHODDEF + MSVCRT_GETCH_METHODDEF + MSVCRT_GETCHE_METHODDEF + MSVCRT_PUTCH_METHODDEF + MSVCRT_UNGETCH_METHODDEF + MSVCRT_SETERRORMODE_METHODDEF + MSVCRT_CRTSETREPORTFILE_METHODDEF + MSVCRT_CRTSETREPORTMODE_METHODDEF + MSVCRT_SET_ERROR_MODE_METHODDEF + MSVCRT_GETWCH_METHODDEF + MSVCRT_GETWCHE_METHODDEF + MSVCRT_PUTWCH_METHODDEF + MSVCRT_UNGETWCH_METHODDEF {NULL, NULL} }; @@ -463,6 +493,20 @@ static struct PyModuleDef msvcrtmodule = { NULL }; +static void +insertint(PyObject *d, char *name, int value) +{ + PyObject *v = PyLong_FromLong((long) value); + if (v == NULL) { + /* Don't bother reporting this error */ + PyErr_Clear(); + } + else { + PyDict_SetItemString(d, name, v); + Py_DECREF(v); + } +} + PyMODINIT_FUNC PyInit_msvcrt(void) { |