blob: 42fffee6a0f449244178b5dd022f7f99d0120be9 [file] [log] [blame]
Serhiy Storchaka29b0a262016-12-04 10:20:55 +02001:mod:`msvcrt` --- Useful routines from the MS VC++ runtime
2==========================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: msvcrt
5 :platform: Windows
6 :synopsis: Miscellaneous useful routines from the MS VC++ runtime.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
12These functions provide access to some useful capabilities on Windows platforms.
13Some higher-level modules use these functions to build the Windows
14implementations of their services. For example, the :mod:`getpass` module uses
15this in the implementation of the :func:`getpass` function.
16
17Further documentation on these functions can be found in the Platform API
18documentation.
19
Christian Heimes2f1019e2007-12-10 16:18:49 +000020The module implements both the normal and wide char variants of the console I/O
21api. The normal API deals only with ASCII characters and is of limited use
Georg Brandl48310cd2009-01-03 21:18:54 +000022for internationalized applications. The wide char API should be used where
Martin Panterd21e0b52015-10-10 10:36:22 +000023ever possible.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020025.. versionchanged:: 3.3
26 Operations in this module now raise :exc:`OSError` where :exc:`IOError`
27 was raised.
28
29
Georg Brandl116aa622007-08-15 14:28:22 +000030.. _msvcrt-files:
31
32File Operations
33---------------
34
35
36.. function:: locking(fd, mode, nbytes)
37
38 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020039 :exc:`OSError` on failure. The locked region of the file extends from the
Georg Brandl116aa622007-08-15 14:28:22 +000040 current file position for *nbytes* bytes, and may continue beyond the end of the
41 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
42 regions in a file may be locked at the same time, but may not overlap. Adjacent
43 regions are not merged; they must be unlocked individually.
44
Saiyang Gou7514f4f2020-02-12 23:47:42 -080045 .. audit-event:: msvcrt.locking fd,mode,nbytes msvcrt.locking
46
Georg Brandl116aa622007-08-15 14:28:22 +000047
48.. data:: LK_LOCK
49 LK_RLCK
50
51 Locks the specified bytes. If the bytes cannot be locked, the program
52 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020053 be locked, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000054
55
56.. data:: LK_NBLCK
57 LK_NBRLCK
58
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020059 Locks the specified bytes. If the bytes cannot be locked, :exc:`OSError` is
Georg Brandl116aa622007-08-15 14:28:22 +000060 raised.
61
62
63.. data:: LK_UNLCK
64
65 Unlocks the specified bytes, which must have been previously locked.
66
67
68.. function:: setmode(fd, flags)
69
70 Set the line-end translation mode for the file descriptor *fd*. To set it to
71 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
72 :const:`os.O_BINARY`.
73
74
75.. function:: open_osfhandle(handle, flags)
76
77 Create a C runtime file descriptor from the file handle *handle*. The *flags*
Christian Heimesfaf2f632008-01-06 16:59:19 +000078 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl116aa622007-08-15 14:28:22 +000079 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
80 to :func:`os.fdopen` to create a file object.
81
Saiyang Gou7514f4f2020-02-12 23:47:42 -080082 .. audit-event:: msvcrt.open_osfhandle handle,flags msvcrt.open_osfhandle
83
Georg Brandl116aa622007-08-15 14:28:22 +000084
85.. function:: get_osfhandle(fd)
86
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020087 Return the file handle for the file descriptor *fd*. Raises :exc:`OSError` if
Georg Brandl116aa622007-08-15 14:28:22 +000088 *fd* is not recognized.
89
Saiyang Gou7514f4f2020-02-12 23:47:42 -080090 .. audit-event:: msvcrt.get_osfhandle fd msvcrt.get_osfhandle
91
Georg Brandl116aa622007-08-15 14:28:22 +000092
93.. _msvcrt-console:
94
95Console I/O
96-----------
97
98
99.. function:: kbhit()
100
Serhiy Storchaka138ccbb2019-11-12 16:57:03 +0200101 Return ``True`` if a keypress is waiting to be read.
Georg Brandl116aa622007-08-15 14:28:22 +0000102
103
104.. function:: getch()
105
Brian Curtin8790a072010-08-24 05:20:30 +0000106 Read a keypress and return the resulting character as a byte string.
107 Nothing is echoed to the console. This call will block if a keypress
108 is not already available, but will not wait for :kbd:`Enter` to be
109 pressed. If the pressed key was a special function key, this will
110 return ``'\000'`` or ``'\xe0'``; the next call will return the keycode.
111 The :kbd:`Control-C` keypress cannot be read with this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000112
Georg Brandl48310cd2009-01-03 21:18:54 +0000113
Christian Heimes2f1019e2007-12-10 16:18:49 +0000114.. function:: getwch()
115
Christian Heimesa34706f2008-01-04 03:06:10 +0000116 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
119.. function:: getche()
120
121 Similar to :func:`getch`, but the keypress will be echoed if it represents a
122 printable character.
123
124
Christian Heimes2f1019e2007-12-10 16:18:49 +0000125.. function:: getwche()
126
Christian Heimesa34706f2008-01-04 03:06:10 +0000127 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000128
Christian Heimes2f1019e2007-12-10 16:18:49 +0000129
Georg Brandl116aa622007-08-15 14:28:22 +0000130.. function:: putch(char)
131
Brian Curtin8790a072010-08-24 05:20:30 +0000132 Print the byte string *char* to the console without buffering.
Georg Brandl116aa622007-08-15 14:28:22 +0000133
Georg Brandl48310cd2009-01-03 21:18:54 +0000134
Christian Heimes2f1019e2007-12-10 16:18:49 +0000135.. function:: putwch(unicode_char)
136
Christian Heimesa34706f2008-01-04 03:06:10 +0000137 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000138
Georg Brandl116aa622007-08-15 14:28:22 +0000139
140.. function:: ungetch(char)
141
Brian Curtin8790a072010-08-24 05:20:30 +0000142 Cause the byte string *char* to be "pushed back" into the console buffer;
143 it will be the next character read by :func:`getch` or :func:`getche`.
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Georg Brandl48310cd2009-01-03 21:18:54 +0000145
Christian Heimes2f1019e2007-12-10 16:18:49 +0000146.. function:: ungetwch(unicode_char)
147
Christian Heimesa34706f2008-01-04 03:06:10 +0000148 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151.. _msvcrt-other:
152
153Other Functions
154---------------
155
156
157.. function:: heapmin()
158
Georg Brandl60203b42010-10-06 10:11:56 +0000159 Force the :c:func:`malloc` heap to clean itself up and return unused blocks to
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200160 the operating system. On failure, this raises :exc:`OSError`.