Debian Codegraph @

Doc/library/msvcrt.rst

raw
1 :mod:`!msvcrt` --- Useful routines from the MS VC++ runtime
2 ===========================================================
3
4 .. module:: msvcrt
5 :platform: Windows
6 :synopsis: Miscellaneous useful routines from the MS VC++ runtime.
7
8 .. sectionauthor:: Fred L. Drake, Jr. <[email protected]>
9
10 --------------
11
12 These functions provide access to some useful capabilities on Windows platforms.
13 Some higher-level modules use these functions to build the Windows
14 implementations of their services. For example, the :mod:`getpass` module uses
15 this in the implementation of the :func:`getpass` function.
16
17 Further documentation on these functions can be found in the Platform API
18 documentation.
19
20 The module implements both the normal and wide char variants of the console I/O
21 api. The normal API deals only with ASCII characters and is of limited use
22 for internationalized applications. The wide char API should be used where
23 ever possible.
24
25 .. availability:: Windows.
26
27 .. versionchanged:: 3.3
28 Operations in this module now raise :exc:`OSError` where :exc:`IOError`
29 was raised.
30
31
32 .. _msvcrt-files:
33
34 File Operations
35 ---------------
36
37
38 .. function:: locking(fd, mode, nbytes)
39
40 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
41 :exc:`OSError` on failure. The locked region of the file extends from the
42 current file position for *nbytes* bytes, and may continue beyond the end of the
43 file. *mode* must be one of the :const:`!LK_\*` constants listed below. Multiple
44 regions in a file may be locked at the same time, but may not overlap. Adjacent
45 regions are not merged; they must be unlocked individually.
46
47 .. audit-event:: msvcrt.locking fd,mode,nbytes msvcrt.locking
48
49
50 .. data:: LK_LOCK
51 LK_RLCK
52
53 Locks the specified bytes. If the bytes cannot be locked, the program
54 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
55 be locked, :exc:`OSError` is raised.
56
57
58 .. data:: LK_NBLCK
59 LK_NBRLCK
60
61 Locks the specified bytes. If the bytes cannot be locked, :exc:`OSError` is
62 raised.
63
64
65 .. data:: LK_UNLCK
66
67 Unlocks the specified bytes, which must have been previously locked.
68
69
70 .. function:: setmode(fd, flags)
71
72 Set the line-end translation mode for the file descriptor *fd*. To set it to
73 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
74 :const:`os.O_BINARY`.
75
76
77 .. function:: open_osfhandle(handle, flags)
78
79 Create a C runtime file descriptor from the file handle *handle*. The *flags*
80 parameter should be a bitwise OR of :const:`os.O_APPEND`,
81 :const:`os.O_RDONLY`, :const:`os.O_TEXT` and :const:`os.O_NOINHERIT`.
82 The returned file descriptor may be used as a parameter
83 to :func:`os.fdopen` to create a file object.
84
85 The file descriptor is inheritable by default. Pass :const:`os.O_NOINHERIT`
86 flag to make it non inheritable.
87
88 .. audit-event:: msvcrt.open_osfhandle handle,flags msvcrt.open_osfhandle
89
90
91 .. function:: get_osfhandle(fd)
92
93 Return the file handle for the file descriptor *fd*. Raises :exc:`OSError` if
94 *fd* is not recognized.
95
96 .. audit-event:: msvcrt.get_osfhandle fd msvcrt.get_osfhandle
97
98
99 .. _msvcrt-console:
100
101 Console I/O
102 -----------
103
104
105 .. function:: kbhit()
106
107 Returns a nonzero value if a keypress is waiting to be read. Otherwise,
108 return 0.
109
110
111 .. function:: getch()
112
113 Read a keypress and return the resulting character as a byte string.
114 Nothing is echoed to the console. This call will block if a keypress
115 is not already available, but will not wait for :kbd:`Enter` to be
116 pressed. If the pressed key was a special function key, this will
117 return ``'\000'`` or ``'\xe0'``; the next call will return the keycode.
118 The :kbd:`Control-C` keypress cannot be read with this function.
119
120
121 .. function:: getwch()
122
123 Wide char variant of :func:`getch`, returning a Unicode value.
124
125
126 .. function:: getche()
127
128 Similar to :func:`getch`, but the keypress will be echoed if it represents a
129 printable character.
130
131
132 .. function:: getwche()
133
134 Wide char variant of :func:`getche`, returning a Unicode value.
135
136
137 .. function:: putch(char)
138
139 Print the byte string *char* to the console without buffering.
140
141
142 .. function:: putwch(unicode_char)
143
144 Wide char variant of :func:`putch`, accepting a Unicode value.
145
146
147 .. function:: ungetch(char)
148
149 Cause the byte string *char* to be "pushed back" into the console buffer;
150 it will be the next character read by :func:`getch` or :func:`getche`.
151
152
153 .. function:: ungetwch(unicode_char)
154
155 Wide char variant of :func:`ungetch`, accepting a Unicode value.
156
157
158 .. _msvcrt-other:
159
160 Other Functions
161 ---------------
162
163
164 .. function:: heapmin()
165
166 Force the :c:func:`malloc` heap to clean itself up and return unused blocks to
167 the operating system. On failure, this raises :exc:`OSError`.
168
169
170 .. function:: set_error_mode(mode)
171
172 Changes the location where the C runtime writes an error message for an error
173 that might end the program. *mode* must be one of the :const:`!OUT_\*`
174 constants listed below or :const:`REPORT_ERRMODE`. Returns the old setting
175 or -1 if an error occurs. Only available in
176 :ref:`debug build of Python <debug-build>`.
177
178
179 .. data:: OUT_TO_DEFAULT
180
181 Error sink is determined by the app's type. Only available in
182 :ref:`debug build of Python <debug-build>`.
183
184
185 .. data:: OUT_TO_STDERR
186
187 Error sink is a standard error. Only available in
188 :ref:`debug build of Python <debug-build>`.
189
190
191 .. data:: OUT_TO_MSGBOX
192
193 Error sink is a message box. Only available in
194 :ref:`debug build of Python <debug-build>`.
195
196
197 .. data:: REPORT_ERRMODE
198
199 Report the current error mode value. Only available in
200 :ref:`debug build of Python <debug-build>`.
201
202
203 .. function:: CrtSetReportMode(type, mode)
204
205 Specifies the destination or destinations for a specific report type
206 generated by :c:func:`!_CrtDbgReport` in the MS VC++ runtime. *type* must be
207 one of the :const:`!CRT_\*` constants listed below. *mode* must be one of the
208 :const:`!CRTDBG_\*` constants listed below. Only available in
209 :ref:`debug build of Python <debug-build>`.
210
211
212 .. function:: CrtSetReportFile(type, file)
213
214 After you use :func:`CrtSetReportMode` to specify :const:`CRTDBG_MODE_FILE`,
215 you can specify the file handle to receive the message text. *type* must be
216 one of the :const:`!CRT_\*` constants listed below. *file* should be the file
217 handle your want specified. Only available in
218 :ref:`debug build of Python <debug-build>`.
219
220
221 .. data:: CRT_WARN
222
223 Warnings, messages, and information that doesn't need immediate attention.
224
225
226 .. data:: CRT_ERROR
227
228 Errors, unrecoverable problems, and issues that require immediate attention.
229
230
231 .. data:: CRT_ASSERT
232
233 Assertion failures.
234
235
236 .. data:: CRTDBG_MODE_DEBUG
237
238 Writes the message to the debugger's output window.
239
240
241 .. data:: CRTDBG_MODE_FILE
242
243 Writes the message to a user-supplied file handle. :func:`CrtSetReportFile`
244 should be called to define the specific file or stream to use as
245 the destination.
246
247
248 .. data:: CRTDBG_MODE_WNDW
249
250 Creates a message box to display the message along with the ``Abort``,
251 ``Retry``, and ``Ignore`` buttons.
252
253
254 .. data:: CRTDBG_REPORT_MODE
255
256 Returns current *mode* for the specified *type*.
257
258
259 .. data:: CRT_ASSEMBLY_VERSION
260
261 The CRT Assembly version, from the :file:`crtassem.h` header file.
262
263
264 .. data:: VC_ASSEMBLY_PUBLICKEYTOKEN
265
266 The VC Assembly public key token, from the :file:`crtassem.h` header file.
267
268
269 .. data:: LIBRARIES_ASSEMBLY_NAME_PREFIX
270
271 The Libraries Assembly name prefix, from the :file:`crtassem.h` header file.