blob: 9be792d00b31bb5f2998b7d0b88cd45f0b476fca [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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 Heimes2f1019e2007-12-10 16:18:49 +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 Brandl48310cd2009-01-03 21:18:54 +000021for internationalized applications. The wide char API should be used where
Christian Heimes2f1019e2007-12-10 16:18:49 +000022ever possible
Georg Brandl116aa622007-08-15 14:28:22 +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*
Christian Heimesfaf2f632008-01-06 16:59:19 +000070 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl116aa622007-08-15 14:28:22 +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 Brandl48310cd2009-01-03 21:18:54 +0000101
Christian Heimes2f1019e2007-12-10 16:18:49 +0000102.. function:: getwch()
103
Christian Heimesa34706f2008-01-04 03:06:10 +0000104 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000105
Georg Brandl116aa622007-08-15 14:28:22 +0000106
107.. function:: getche()
108
109 Similar to :func:`getch`, but the keypress will be echoed if it represents a
110 printable character.
111
112
Christian Heimes2f1019e2007-12-10 16:18:49 +0000113.. function:: getwche()
114
Christian Heimesa34706f2008-01-04 03:06:10 +0000115 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000116
Christian Heimes2f1019e2007-12-10 16:18:49 +0000117
Georg Brandl116aa622007-08-15 14:28:22 +0000118.. function:: putch(char)
119
120 Print the character *char* to the console without buffering.
121
Georg Brandl48310cd2009-01-03 21:18:54 +0000122
Christian Heimes2f1019e2007-12-10 16:18:49 +0000123.. function:: putwch(unicode_char)
124
Christian Heimesa34706f2008-01-04 03:06:10 +0000125 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000126
Georg Brandl116aa622007-08-15 14:28:22 +0000127
128.. function:: ungetch(char)
129
130 Cause the character *char* to be "pushed back" into the console buffer; it will
131 be the next character read by :func:`getch` or :func:`getche`.
132
Georg Brandl48310cd2009-01-03 21:18:54 +0000133
Christian Heimes2f1019e2007-12-10 16:18:49 +0000134.. function:: ungetwch(unicode_char)
135
Christian Heimesa34706f2008-01-04 03:06:10 +0000136 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000137
Georg Brandl116aa622007-08-15 14:28:22 +0000138
139.. _msvcrt-other:
140
141Other Functions
142---------------
143
144
145.. function:: heapmin()
146
147 Force the :cfunc:`malloc` heap to clean itself up and return unused blocks to
148 the operating system. This only works on Windows NT. On failure, this raises
149 :exc:`IOError`.
150