ยปCore Development>Code coverage>PC/msvcrtmodule.c

Python code coverage for PC/msvcrtmodule.c

#countcontent
1n/a/*********************************************************
2n/a
3n/a msvcrtmodule.c
4n/a
5n/a A Python interface to the Microsoft Visual C Runtime
6n/a Library, providing access to those non-portable, but
7n/a still useful routines.
8n/a
9n/a Only ever compiled with an MS compiler, so no attempt
10n/a has been made to avoid MS language extensions, etc...
11n/a
12n/a This may only work on NT or 95...
13n/a
14n/a Author: Mark Hammond and Guido van Rossum.
15n/a Maintenance: Guido van Rossum.
16n/a
17n/a***********************************************************/
18n/a
19n/a#include "Python.h"
20n/a#include "malloc.h"
21n/a#include <io.h>
22n/a#include <conio.h>
23n/a#include <sys/locking.h>
24n/a#include <crtdbg.h>
25n/a#include <windows.h>
26n/a
27n/a#ifdef _MSC_VER
28n/a#if _MSC_VER >= 1500 && _MSC_VER < 1600
29n/a#include <crtassem.h>
30n/a#elif _MSC_VER >= 1600
31n/a#include <crtversion.h>
32n/a#endif
33n/a#endif
34n/a
35n/a/*[python input]
36n/aclass intptr_t_converter(CConverter):
37n/a type = 'intptr_t'
38n/a format_unit = '"_Py_PARSE_INTPTR"'
39n/a
40n/aclass handle_return_converter(long_return_converter):
41n/a type = 'intptr_t'
42n/a cast = '(void *)'
43n/a conversion_fn = 'PyLong_FromVoidPtr'
44n/a
45n/aclass byte_char_return_converter(CReturnConverter):
46n/a type = 'int'
47n/a
48n/a def render(self, function, data):
49n/a data.declarations.append('char s[1];')
50n/a data.return_value = 's[0]'
51n/a data.return_conversion.append(
52n/a 'return_value = PyBytes_FromStringAndSize(s, 1);\n')
53n/a
54n/aclass wchar_t_return_converter(CReturnConverter):
55n/a type = 'wchar_t'
56n/a
57n/a def render(self, function, data):
58n/a self.declare(data)
59n/a data.return_conversion.append(
60n/a 'return_value = PyUnicode_FromOrdinal(_return_value);\n')
61n/a[python start generated code]*/
62n/a/*[python end generated code: output=da39a3ee5e6b4b0d input=b59f1663dba11997]*/
63n/a
64n/a/*[clinic input]
65n/amodule msvcrt
66n/a[clinic start generated code]*/
67n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=f31a87a783d036cd]*/
68n/a
69n/a#include "clinic/msvcrtmodule.c.h"
70n/a
71n/a/*[clinic input]
72n/amsvcrt.heapmin
73n/a
74n/aMinimize the malloc() heap.
75n/a
76n/aForce the malloc() heap to clean itself up and return unused blocks
77n/ato the operating system. On failure, this raises OSError.
78n/a[clinic start generated code]*/
79n/a
80n/astatic PyObject *
81n/amsvcrt_heapmin_impl(PyObject *module)
82n/a/*[clinic end generated code: output=1ba00f344782dc19 input=82e1771d21bde2d8]*/
83n/a{
84n/a if (_heapmin() != 0)
85n/a return PyErr_SetFromErrno(PyExc_IOError);
86n/a
87n/a Py_RETURN_NONE;
88n/a}
89n/a/*[clinic input]
90n/amsvcrt.locking
91n/a
92n/a fd: int
93n/a mode: int
94n/a nbytes: long
95n/a /
96n/a
97n/aLock part of a file based on file descriptor fd from the C runtime.
98n/a
99n/aRaises IOError on failure. The locked region of the file extends from
100n/athe current file position for nbytes bytes, and may continue beyond
101n/athe end of the file. mode must be one of the LK_* constants listed
102n/abelow. Multiple regions in a file may be locked at the same time, but
103n/amay not overlap. Adjacent regions are not merged; they must be unlocked
104n/aindividually.
105n/a[clinic start generated code]*/
106n/a
107n/astatic PyObject *
108n/amsvcrt_locking_impl(PyObject *module, int fd, int mode, long nbytes)
109n/a/*[clinic end generated code: output=a4a90deca9785a03 input=d9f13f0f6a713ba7]*/
110n/a{
111n/a int err;
112n/a
113n/a Py_BEGIN_ALLOW_THREADS
114n/a _Py_BEGIN_SUPPRESS_IPH
115n/a err = _locking(fd, mode, nbytes);
116n/a _Py_END_SUPPRESS_IPH
117n/a Py_END_ALLOW_THREADS
118n/a if (err != 0)
119n/a return PyErr_SetFromErrno(PyExc_IOError);
120n/a
121n/a Py_RETURN_NONE;
122n/a}
123n/a
124n/a/*[clinic input]
125n/amsvcrt.setmode -> long
126n/a
127n/a fd: int
128n/a mode as flags: int
129n/a /
130n/a
131n/aSet the line-end translation mode for the file descriptor fd.
132n/a
133n/aTo set it to text mode, flags should be os.O_TEXT; for binary, it
134n/ashould be os.O_BINARY.
135n/a
136n/aReturn value is the previous mode.
137n/a[clinic start generated code]*/
138n/a
139n/astatic long
140n/amsvcrt_setmode_impl(PyObject *module, int fd, int flags)
141n/a/*[clinic end generated code: output=24a9be5ea07ccb9b input=76e7c01f6b137f75]*/
142n/a{
143n/a _Py_BEGIN_SUPPRESS_IPH
144n/a flags = _setmode(fd, flags);
145n/a _Py_END_SUPPRESS_IPH
146n/a if (flags == -1)
147n/a PyErr_SetFromErrno(PyExc_IOError);
148n/a
149n/a return flags;
150n/a}
151n/a
152n/a/*[clinic input]
153n/amsvcrt.open_osfhandle -> long
154n/a
155n/a handle: intptr_t
156n/a flags: int
157n/a /
158n/a
159n/aCreate a C runtime file descriptor from the file handle handle.
160n/a
161n/aThe flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
162n/aand os.O_TEXT. The returned file descriptor may be used as a parameter
163n/ato os.fdopen() to create a file object.
164n/a[clinic start generated code]*/
165n/a
166n/astatic long
167n/amsvcrt_open_osfhandle_impl(PyObject *module, intptr_t handle, int flags)
168n/a/*[clinic end generated code: output=cede871bf939d6e3 input=cb2108bbea84514e]*/
169n/a{
170n/a int fd;
171n/a
172n/a _Py_BEGIN_SUPPRESS_IPH
173n/a fd = _open_osfhandle(handle, flags);
174n/a _Py_END_SUPPRESS_IPH
175n/a if (fd == -1)
176n/a PyErr_SetFromErrno(PyExc_IOError);
177n/a
178n/a return fd;
179n/a}
180n/a
181n/a/*[clinic input]
182n/amsvcrt.get_osfhandle -> handle
183n/a
184n/a fd: int
185n/a /
186n/a
187n/aReturn the file handle for the file descriptor fd.
188n/a
189n/aRaises IOError if fd is not recognized.
190n/a[clinic start generated code]*/
191n/a
192n/astatic intptr_t
193n/amsvcrt_get_osfhandle_impl(PyObject *module, int fd)
194n/a/*[clinic end generated code: output=7ce761dd0de2b503 input=c7d18d02c8017ec1]*/
195n/a{
196n/a intptr_t handle = -1;
197n/a
198n/a _Py_BEGIN_SUPPRESS_IPH
199n/a handle = _get_osfhandle(fd);
200n/a _Py_END_SUPPRESS_IPH
201n/a if (handle == -1)
202n/a PyErr_SetFromErrno(PyExc_IOError);
203n/a
204n/a return handle;
205n/a}
206n/a
207n/a/* Console I/O */
208n/a/*[clinic input]
209n/amsvcrt.kbhit -> long
210n/a
211n/aReturn true if a keypress is waiting to be read.
212n/a[clinic start generated code]*/
213n/a
214n/astatic long
215n/amsvcrt_kbhit_impl(PyObject *module)
216n/a/*[clinic end generated code: output=940dfce6587c1890 input=e70d678a5c2f6acc]*/
217n/a{
218n/a return _kbhit();
219n/a}
220n/a
221n/a/*[clinic input]
222n/amsvcrt.getch -> byte_char
223n/a
224n/aRead a keypress and return the resulting character as a byte string.
225n/a
226n/aNothing is echoed to the console. This call will block if a keypress is
227n/anot already available, but will not wait for Enter to be pressed. If the
228n/apressed key was a special function key, this will return '\000' or
229n/a'\xe0'; the next call will return the keycode. The Control-C keypress
230n/acannot be read with this function.
231n/a[clinic start generated code]*/
232n/a
233n/astatic int
234n/amsvcrt_getch_impl(PyObject *module)
235n/a/*[clinic end generated code: output=a4e51f0565064a7d input=37a40cf0ed0d1153]*/
236n/a{
237n/a int ch;
238n/a
239n/a Py_BEGIN_ALLOW_THREADS
240n/a ch = _getch();
241n/a Py_END_ALLOW_THREADS
242n/a return ch;
243n/a}
244n/a
245n/a/*[clinic input]
246n/amsvcrt.getwch -> wchar_t
247n/a
248n/aWide char variant of getch(), returning a Unicode value.
249n/a[clinic start generated code]*/
250n/a
251n/astatic wchar_t
252n/amsvcrt_getwch_impl(PyObject *module)
253n/a/*[clinic end generated code: output=be9937494e22f007 input=27b3dec8ad823d7c]*/
254n/a{
255n/a wchar_t ch;
256n/a
257n/a Py_BEGIN_ALLOW_THREADS
258n/a ch = _getwch();
259n/a Py_END_ALLOW_THREADS
260n/a return ch;
261n/a}
262n/a
263n/a/*[clinic input]
264n/amsvcrt.getche -> byte_char
265n/a
266n/aSimilar to getch(), but the keypress will be echoed if possible.
267n/a[clinic start generated code]*/
268n/a
269n/astatic int
270n/amsvcrt_getche_impl(PyObject *module)
271n/a/*[clinic end generated code: output=d8f7db4fd2990401 input=43311ade9ed4a9c0]*/
272n/a{
273n/a int ch;
274n/a
275n/a Py_BEGIN_ALLOW_THREADS
276n/a ch = _getche();
277n/a Py_END_ALLOW_THREADS
278n/a return ch;
279n/a}
280n/a
281n/a/*[clinic input]
282n/amsvcrt.getwche -> wchar_t
283n/a
284n/aWide char variant of getche(), returning a Unicode value.
285n/a[clinic start generated code]*/
286n/a
287n/astatic wchar_t
288n/amsvcrt_getwche_impl(PyObject *module)
289n/a/*[clinic end generated code: output=d0dae5ba3829d596 input=49337d59d1a591f8]*/
290n/a{
291n/a wchar_t ch;
292n/a
293n/a Py_BEGIN_ALLOW_THREADS
294n/a ch = _getwche();
295n/a Py_END_ALLOW_THREADS
296n/a return ch;
297n/a}
298n/a
299n/a/*[clinic input]
300n/amsvcrt.putch
301n/a
302n/a char: char
303n/a /
304n/a
305n/aPrint the byte string char to the console without buffering.
306n/a[clinic start generated code]*/
307n/a
308n/astatic PyObject *
309n/amsvcrt_putch_impl(PyObject *module, char char_value)
310n/a/*[clinic end generated code: output=92ec9b81012d8f60 input=ec078dd10cb054d6]*/
311n/a{
312n/a _Py_BEGIN_SUPPRESS_IPH
313n/a _putch(char_value);
314n/a _Py_END_SUPPRESS_IPH
315n/a Py_RETURN_NONE;
316n/a}
317n/a
318n/a/*[clinic input]
319n/amsvcrt.putwch
320n/a
321n/a unicode_char: int(accept={str})
322n/a /
323n/a
324n/aWide char variant of putch(), accepting a Unicode value.
325n/a[clinic start generated code]*/
326n/a
327n/astatic PyObject *
328n/amsvcrt_putwch_impl(PyObject *module, int unicode_char)
329n/a/*[clinic end generated code: output=a3bd1a8951d28eee input=996ccd0bbcbac4c3]*/
330n/a{
331n/a _Py_BEGIN_SUPPRESS_IPH
332n/a _putwch(unicode_char);
333n/a _Py_END_SUPPRESS_IPH
334n/a Py_RETURN_NONE;
335n/a
336n/a}
337n/a
338n/a/*[clinic input]
339n/amsvcrt.ungetch
340n/a
341n/a char: char
342n/a /
343n/a
344n/aOpposite of getch.
345n/a
346n/aCause the byte string char to be "pushed back" into the
347n/aconsole buffer; it will be the next character read by
348n/agetch() or getche().
349n/a[clinic start generated code]*/
350n/a
351n/astatic PyObject *
352n/amsvcrt_ungetch_impl(PyObject *module, char char_value)
353n/a/*[clinic end generated code: output=c6942a0efa119000 input=22f07ee9001bbf0f]*/
354n/a{
355n/a int res;
356n/a
357n/a _Py_BEGIN_SUPPRESS_IPH
358n/a res = _ungetch(char_value);
359n/a _Py_END_SUPPRESS_IPH
360n/a
361n/a if (res == EOF)
362n/a return PyErr_SetFromErrno(PyExc_IOError);
363n/a Py_RETURN_NONE;
364n/a}
365n/a
366n/a/*[clinic input]
367n/amsvcrt.ungetwch
368n/a
369n/a unicode_char: int(accept={str})
370n/a /
371n/a
372n/aWide char variant of ungetch(), accepting a Unicode value.
373n/a[clinic start generated code]*/
374n/a
375n/astatic PyObject *
376n/amsvcrt_ungetwch_impl(PyObject *module, int unicode_char)
377n/a/*[clinic end generated code: output=e63af05438b8ba3d input=83ec0492be04d564]*/
378n/a{
379n/a int res;
380n/a
381n/a _Py_BEGIN_SUPPRESS_IPH
382n/a res = _ungetwch(unicode_char);
383n/a _Py_END_SUPPRESS_IPH
384n/a
385n/a if (res == WEOF)
386n/a return PyErr_SetFromErrno(PyExc_IOError);
387n/a Py_RETURN_NONE;
388n/a}
389n/a
390n/a#ifdef _DEBUG
391n/a/*[clinic input]
392n/amsvcrt.CrtSetReportFile -> long
393n/a
394n/a type: int
395n/a file: int
396n/a /
397n/a
398n/aWrapper around _CrtSetReportFile.
399n/a
400n/aOnly available on Debug builds.
401n/a[clinic start generated code]*/
402n/a
403n/astatic long
404n/amsvcrt_CrtSetReportFile_impl(PyObject *module, int type, int file)
405n/a/*[clinic end generated code: output=df291c7fe032eb68 input=bb8f721a604fcc45]*/
406n/a{
407n/a long res;
408n/a
409n/a _Py_BEGIN_SUPPRESS_IPH
410n/a res = (long)_CrtSetReportFile(type, (_HFILE)file);
411n/a _Py_END_SUPPRESS_IPH
412n/a
413n/a return res;
414n/a}
415n/a
416n/a/*[clinic input]
417n/amsvcrt.CrtSetReportMode -> long
418n/a
419n/a type: int
420n/a mode: int
421n/a /
422n/a
423n/aWrapper around _CrtSetReportMode.
424n/a
425n/aOnly available on Debug builds.
426n/a[clinic start generated code]*/
427n/a
428n/astatic long
429n/amsvcrt_CrtSetReportMode_impl(PyObject *module, int type, int mode)
430n/a/*[clinic end generated code: output=b2863761523de317 input=9319d29b4319426b]*/
431n/a{
432n/a int res;
433n/a
434n/a _Py_BEGIN_SUPPRESS_IPH
435n/a res = _CrtSetReportMode(type, mode);
436n/a _Py_END_SUPPRESS_IPH
437n/a if (res == -1)
438n/a PyErr_SetFromErrno(PyExc_IOError);
439n/a return res;
440n/a}
441n/a
442n/a/*[clinic input]
443n/amsvcrt.set_error_mode -> long
444n/a
445n/a mode: int
446n/a /
447n/a
448n/aWrapper around _set_error_mode.
449n/a
450n/aOnly available on Debug builds.
451n/a[clinic start generated code]*/
452n/a
453n/astatic long
454n/amsvcrt_set_error_mode_impl(PyObject *module, int mode)
455n/a/*[clinic end generated code: output=ac4a09040d8ac4e3 input=046fca59c0f20872]*/
456n/a{
457n/a long res;
458n/a
459n/a _Py_BEGIN_SUPPRESS_IPH
460n/a res = _set_error_mode(mode);
461n/a _Py_END_SUPPRESS_IPH
462n/a
463n/a return res;
464n/a}
465n/a#endif /* _DEBUG */
466n/a
467n/a/*[clinic input]
468n/amsvcrt.SetErrorMode
469n/a
470n/a mode: unsigned_int(bitwise=True)
471n/a /
472n/a
473n/aWrapper around SetErrorMode.
474n/a[clinic start generated code]*/
475n/a
476n/astatic PyObject *
477n/amsvcrt_SetErrorMode_impl(PyObject *module, unsigned int mode)
478n/a/*[clinic end generated code: output=01d529293f00da8f input=d8b167258d32d907]*/
479n/a{
480n/a unsigned int res;
481n/a
482n/a _Py_BEGIN_SUPPRESS_IPH
483n/a res = SetErrorMode(mode);
484n/a _Py_END_SUPPRESS_IPH
485n/a
486n/a return PyLong_FromUnsignedLong(res);
487n/a}
488n/a
489n/a/*[clinic input]
490n/a[clinic start generated code]*/
491n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=da39a3ee5e6b4b0d]*/
492n/a
493n/a/* List of functions exported by this module */
494n/astatic struct PyMethodDef msvcrt_functions[] = {
495n/a MSVCRT_HEAPMIN_METHODDEF
496n/a MSVCRT_LOCKING_METHODDEF
497n/a MSVCRT_SETMODE_METHODDEF
498n/a MSVCRT_OPEN_OSFHANDLE_METHODDEF
499n/a MSVCRT_GET_OSFHANDLE_METHODDEF
500n/a MSVCRT_KBHIT_METHODDEF
501n/a MSVCRT_GETCH_METHODDEF
502n/a MSVCRT_GETCHE_METHODDEF
503n/a MSVCRT_PUTCH_METHODDEF
504n/a MSVCRT_UNGETCH_METHODDEF
505n/a MSVCRT_SETERRORMODE_METHODDEF
506n/a MSVCRT_CRTSETREPORTFILE_METHODDEF
507n/a MSVCRT_CRTSETREPORTMODE_METHODDEF
508n/a MSVCRT_SET_ERROR_MODE_METHODDEF
509n/a MSVCRT_GETWCH_METHODDEF
510n/a MSVCRT_GETWCHE_METHODDEF
511n/a MSVCRT_PUTWCH_METHODDEF
512n/a MSVCRT_UNGETWCH_METHODDEF
513n/a {NULL, NULL}
514n/a};
515n/a
516n/a
517n/astatic struct PyModuleDef msvcrtmodule = {
518n/a PyModuleDef_HEAD_INIT,
519n/a "msvcrt",
520n/a NULL,
521n/a -1,
522n/a msvcrt_functions,
523n/a NULL,
524n/a NULL,
525n/a NULL,
526n/a NULL
527n/a};
528n/a
529n/astatic void
530n/ainsertint(PyObject *d, char *name, int value)
531n/a{
532n/a PyObject *v = PyLong_FromLong((long) value);
533n/a if (v == NULL) {
534n/a /* Don't bother reporting this error */
535n/a PyErr_Clear();
536n/a }
537n/a else {
538n/a PyDict_SetItemString(d, name, v);
539n/a Py_DECREF(v);
540n/a }
541n/a}
542n/a
543n/aPyMODINIT_FUNC
544n/aPyInit_msvcrt(void)
545n/a{
546n/a int st;
547n/a PyObject *d, *version;
548n/a PyObject *m = PyModule_Create(&msvcrtmodule);
549n/a if (m == NULL)
550n/a return NULL;
551n/a d = PyModule_GetDict(m);
552n/a
553n/a /* constants for the locking() function's mode argument */
554n/a insertint(d, "LK_LOCK", _LK_LOCK);
555n/a insertint(d, "LK_NBLCK", _LK_NBLCK);
556n/a insertint(d, "LK_NBRLCK", _LK_NBRLCK);
557n/a insertint(d, "LK_RLCK", _LK_RLCK);
558n/a insertint(d, "LK_UNLCK", _LK_UNLCK);
559n/a insertint(d, "SEM_FAILCRITICALERRORS", SEM_FAILCRITICALERRORS);
560n/a insertint(d, "SEM_NOALIGNMENTFAULTEXCEPT", SEM_NOALIGNMENTFAULTEXCEPT);
561n/a insertint(d, "SEM_NOGPFAULTERRORBOX", SEM_NOGPFAULTERRORBOX);
562n/a insertint(d, "SEM_NOOPENFILEERRORBOX", SEM_NOOPENFILEERRORBOX);
563n/a#ifdef _DEBUG
564n/a insertint(d, "CRT_WARN", _CRT_WARN);
565n/a insertint(d, "CRT_ERROR", _CRT_ERROR);
566n/a insertint(d, "CRT_ASSERT", _CRT_ASSERT);
567n/a insertint(d, "CRTDBG_MODE_DEBUG", _CRTDBG_MODE_DEBUG);
568n/a insertint(d, "CRTDBG_MODE_FILE", _CRTDBG_MODE_FILE);
569n/a insertint(d, "CRTDBG_MODE_WNDW", _CRTDBG_MODE_WNDW);
570n/a insertint(d, "CRTDBG_REPORT_MODE", _CRTDBG_REPORT_MODE);
571n/a insertint(d, "CRTDBG_FILE_STDERR", (int)_CRTDBG_FILE_STDERR);
572n/a insertint(d, "CRTDBG_FILE_STDOUT", (int)_CRTDBG_FILE_STDOUT);
573n/a insertint(d, "CRTDBG_REPORT_FILE", (int)_CRTDBG_REPORT_FILE);
574n/a#endif
575n/a
576n/a /* constants for the crt versions */
577n/a#ifdef _VC_ASSEMBLY_PUBLICKEYTOKEN
578n/a st = PyModule_AddStringConstant(m, "VC_ASSEMBLY_PUBLICKEYTOKEN",
579n/a _VC_ASSEMBLY_PUBLICKEYTOKEN);
580n/a if (st < 0) return NULL;
581n/a#endif
582n/a#ifdef _CRT_ASSEMBLY_VERSION
583n/a st = PyModule_AddStringConstant(m, "CRT_ASSEMBLY_VERSION",
584n/a _CRT_ASSEMBLY_VERSION);
585n/a if (st < 0) return NULL;
586n/a#endif
587n/a#ifdef __LIBRARIES_ASSEMBLY_NAME_PREFIX
588n/a st = PyModule_AddStringConstant(m, "LIBRARIES_ASSEMBLY_NAME_PREFIX",
589n/a __LIBRARIES_ASSEMBLY_NAME_PREFIX);
590n/a if (st < 0) return NULL;
591n/a#endif
592n/a
593n/a /* constants for the 2010 crt versions */
594n/a#if defined(_VC_CRT_MAJOR_VERSION) && defined (_VC_CRT_MINOR_VERSION) && defined(_VC_CRT_BUILD_VERSION) && defined(_VC_CRT_RBUILD_VERSION)
595n/a version = PyUnicode_FromFormat("%d.%d.%d.%d", _VC_CRT_MAJOR_VERSION,
596n/a _VC_CRT_MINOR_VERSION,
597n/a _VC_CRT_BUILD_VERSION,
598n/a _VC_CRT_RBUILD_VERSION);
599n/a st = PyModule_AddObject(m, "CRT_ASSEMBLY_VERSION", version);
600n/a if (st < 0) return NULL;
601n/a#endif
602n/a /* make compiler warning quiet if st is unused */
603n/a (void)st;
604n/a
605n/a return m;
606n/a}