blob: 2b311809bcb36899f453f2a5430b0dd7a1d10a33 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001: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.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
8
9
10These functions provide access to some useful capabilities on Windows platforms.
11Some higher-level modules use these functions to build the Windows
12implementations of their services. For example, the :mod:`getpass` module uses
13this in the implementation of the :func:`getpass` function.
14
15Further documentation on these functions can be found in the Platform API
16documentation.
17
Christian Heimes2f1019e2007-12-10 16:18:49 +000018The module implements both the normal and wide char variants of the console I/O
19api. The normal API deals only with ASCII characters and is of limited use
Georg Brandl48310cd2009-01-03 21:18:54 +000020for internationalized applications. The wide char API should be used where
Christian Heimes2f1019e2007-12-10 16:18:49 +000021ever possible
Georg Brandl116aa622007-08-15 14:28:22 +000022
23.. _msvcrt-files:
24
25File Operations
26---------------
27
28
29.. function:: locking(fd, mode, nbytes)
30
31 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
32 :exc:`IOError` on failure. The locked region of the file extends from the
33 current file position for *nbytes* bytes, and may continue beyond the end of the
34 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
35 regions in a file may be locked at the same time, but may not overlap. Adjacent
36 regions are not merged; they must be unlocked individually.
37
38
39.. data:: LK_LOCK
40 LK_RLCK
41
42 Locks the specified bytes. If the bytes cannot be locked, the program
43 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
44 be locked, :exc:`IOError` is raised.
45
46
47.. data:: LK_NBLCK
48 LK_NBRLCK
49
50 Locks the specified bytes. If the bytes cannot be locked, :exc:`IOError` is
51 raised.
52
53
54.. data:: LK_UNLCK
55
56 Unlocks the specified bytes, which must have been previously locked.
57
58
59.. function:: setmode(fd, flags)
60
61 Set the line-end translation mode for the file descriptor *fd*. To set it to
62 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
63 :const:`os.O_BINARY`.
64
65
66.. function:: open_osfhandle(handle, flags)
67
68 Create a C runtime file descriptor from the file handle *handle*. The *flags*
Christian Heimesfaf2f632008-01-06 16:59:19 +000069 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl116aa622007-08-15 14:28:22 +000070 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
71 to :func:`os.fdopen` to create a file object.
72
73
74.. function:: get_osfhandle(fd)
75
76 Return the file handle for the file descriptor *fd*. Raises :exc:`IOError` if
77 *fd* is not recognized.
78
79
80.. _msvcrt-console:
81
82Console I/O
83-----------
84
85
86.. function:: kbhit()
87
88 Return true if a keypress is waiting to be read.
89
90
91.. function:: getch()
92
93 Read a keypress and return the resulting character. Nothing is echoed to the
94 console. This call will block if a keypress is not already available, but will
95 not wait for :kbd:`Enter` to be pressed. If the pressed key was a special
96 function key, this will return ``'\000'`` or ``'\xe0'``; the next call will
97 return the keycode. The :kbd:`Control-C` keypress cannot be read with this
98 function.
99
Georg Brandl48310cd2009-01-03 21:18:54 +0000100
Christian Heimes2f1019e2007-12-10 16:18:49 +0000101.. function:: getwch()
102
Christian Heimesa34706f2008-01-04 03:06:10 +0000103 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000104
Georg Brandl116aa622007-08-15 14:28:22 +0000105
106.. function:: getche()
107
108 Similar to :func:`getch`, but the keypress will be echoed if it represents a
109 printable character.
110
111
Christian Heimes2f1019e2007-12-10 16:18:49 +0000112.. function:: getwche()
113
Christian Heimesa34706f2008-01-04 03:06:10 +0000114 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000115
Christian Heimes2f1019e2007-12-10 16:18:49 +0000116
Georg Brandl116aa622007-08-15 14:28:22 +0000117.. function:: putch(char)
118
119 Print the character *char* to the console without buffering.
120
Georg Brandl48310cd2009-01-03 21:18:54 +0000121
Christian Heimes2f1019e2007-12-10 16:18:49 +0000122.. function:: putwch(unicode_char)
123
Christian Heimesa34706f2008-01-04 03:06:10 +0000124 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000125
Georg Brandl116aa622007-08-15 14:28:22 +0000126
127.. function:: ungetch(char)
128
129 Cause the character *char* to be "pushed back" into the console buffer; it will
130 be the next character read by :func:`getch` or :func:`getche`.
131
Georg Brandl48310cd2009-01-03 21:18:54 +0000132
Christian Heimes2f1019e2007-12-10 16:18:49 +0000133.. function:: ungetwch(unicode_char)
134
Christian Heimesa34706f2008-01-04 03:06:10 +0000135 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000136
Georg Brandl116aa622007-08-15 14:28:22 +0000137
138.. _msvcrt-other:
139
140Other Functions
141---------------
142
143
144.. function:: heapmin()
145
146 Force the :cfunc:`malloc` heap to clean itself up and return unused blocks to
147 the operating system. This only works on Windows NT. On failure, this raises
148 :exc:`IOError`.
149