blob: 4537be899974de3a3f2cc7e01c6da408620a0727 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`msvcrt` -- Useful routines from the MS VC++ runtime
3=========================================================
4
5.. module:: msvcrt
6 :platform: Windows
7 :synopsis: Miscellaneous useful routines from the MS VC++ runtime.
8.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
10
11These functions provide access to some useful capabilities on Windows platforms.
12Some higher-level modules use these functions to build the Windows
13implementations of their services. For example, the :mod:`getpass` module uses
14this in the implementation of the :func:`getpass` function.
15
16Further documentation on these functions can be found in the Platform API
17documentation.
18
Christian Heimes7c7f6af2007-12-10 15:12:41 +000019The module implements both the normal and wide char variants of the console I/O
20api. The normal API deals only with ASCII characters and is of limited use
Georg Brandlc62ef8b2009-01-03 20:55:06 +000021for internationalized applications. The wide char API should be used where
Christian Heimes7c7f6af2007-12-10 15:12:41 +000022ever possible
Georg Brandl8ec7f652007-08-15 14:28:01 +000023
24.. _msvcrt-files:
25
26File Operations
27---------------
28
29
30.. function:: locking(fd, mode, nbytes)
31
32 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
33 :exc:`IOError` on failure. The locked region of the file extends from the
34 current file position for *nbytes* bytes, and may continue beyond the end of the
35 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
36 regions in a file may be locked at the same time, but may not overlap. Adjacent
37 regions are not merged; they must be unlocked individually.
38
39
40.. data:: LK_LOCK
41 LK_RLCK
42
43 Locks the specified bytes. If the bytes cannot be locked, the program
44 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
45 be locked, :exc:`IOError` is raised.
46
47
48.. data:: LK_NBLCK
49 LK_NBRLCK
50
51 Locks the specified bytes. If the bytes cannot be locked, :exc:`IOError` is
52 raised.
53
54
55.. data:: LK_UNLCK
56
57 Unlocks the specified bytes, which must have been previously locked.
58
59
60.. function:: setmode(fd, flags)
61
62 Set the line-end translation mode for the file descriptor *fd*. To set it to
63 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
64 :const:`os.O_BINARY`.
65
66
67.. function:: open_osfhandle(handle, flags)
68
69 Create a C runtime file descriptor from the file handle *handle*. The *flags*
Georg Brandlf725b952008-01-05 19:44:22 +000070 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl8ec7f652007-08-15 14:28:01 +000071 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
72 to :func:`os.fdopen` to create a file object.
73
74
75.. function:: get_osfhandle(fd)
76
77 Return the file handle for the file descriptor *fd*. Raises :exc:`IOError` if
78 *fd* is not recognized.
79
80
81.. _msvcrt-console:
82
83Console I/O
84-----------
85
86
87.. function:: kbhit()
88
89 Return true if a keypress is waiting to be read.
90
91
92.. function:: getch()
93
94 Read a keypress and return the resulting character. Nothing is echoed to the
95 console. This call will block if a keypress is not already available, but will
96 not wait for :kbd:`Enter` to be pressed. If the pressed key was a special
97 function key, this will return ``'\000'`` or ``'\xe0'``; the next call will
98 return the keycode. The :kbd:`Control-C` keypress cannot be read with this
99 function.
100
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000101
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000102.. function:: getwch()
103
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000104 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000105
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000106 .. versionadded:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000107
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
109.. function:: getche()
110
111 Similar to :func:`getch`, but the keypress will be echoed if it represents a
112 printable character.
113
114
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000115.. function:: getwche()
116
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000117 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000118
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000119 .. versionadded:: 2.6
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000120
121
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122.. function:: putch(char)
123
124 Print the character *char* to the console without buffering.
125
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000126
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000127.. function:: putwch(unicode_char)
128
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000129 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000130
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000131 .. versionadded:: 2.6
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000132
Georg Brandl8ec7f652007-08-15 14:28:01 +0000133
134.. function:: ungetch(char)
135
136 Cause the character *char* to be "pushed back" into the console buffer; it will
137 be the next character read by :func:`getch` or :func:`getche`.
138
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000139
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000140.. function:: ungetwch(unicode_char)
141
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000142 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandlc62ef8b2009-01-03 20:55:06 +0000143
Andrew M. Kuchling7ef1ce62008-01-04 01:15:50 +0000144 .. versionadded:: 2.6
Christian Heimes7c7f6af2007-12-10 15:12:41 +0000145
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
147.. _msvcrt-other:
148
149Other Functions
150---------------
151
152
153.. function:: heapmin()
154
155 Force the :cfunc:`malloc` heap to clean itself up and return unused blocks to
156 the operating system. This only works on Windows NT. On failure, this raises
157 :exc:`IOError`.
158