blob: 9d23720e322566381b966919e4b2d6516c5fe582 [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
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020023.. versionchanged:: 3.3
24 Operations in this module now raise :exc:`OSError` where :exc:`IOError`
25 was raised.
26
27
Georg Brandl116aa622007-08-15 14:28:22 +000028.. _msvcrt-files:
29
30File Operations
31---------------
32
33
34.. function:: locking(fd, mode, nbytes)
35
36 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020037 :exc:`OSError` on failure. The locked region of the file extends from the
Georg Brandl116aa622007-08-15 14:28:22 +000038 current file position for *nbytes* bytes, and may continue beyond the end of the
39 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
40 regions in a file may be locked at the same time, but may not overlap. Adjacent
41 regions are not merged; they must be unlocked individually.
42
43
44.. data:: LK_LOCK
45 LK_RLCK
46
47 Locks the specified bytes. If the bytes cannot be locked, the program
48 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020049 be locked, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000050
51
52.. data:: LK_NBLCK
53 LK_NBRLCK
54
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020055 Locks the specified bytes. If the bytes cannot be locked, :exc:`OSError` is
Georg Brandl116aa622007-08-15 14:28:22 +000056 raised.
57
58
59.. data:: LK_UNLCK
60
61 Unlocks the specified bytes, which must have been previously locked.
62
63
64.. function:: setmode(fd, flags)
65
66 Set the line-end translation mode for the file descriptor *fd*. To set it to
67 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
68 :const:`os.O_BINARY`.
69
70
71.. function:: open_osfhandle(handle, flags)
72
73 Create a C runtime file descriptor from the file handle *handle*. The *flags*
Christian Heimesfaf2f632008-01-06 16:59:19 +000074 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl116aa622007-08-15 14:28:22 +000075 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
76 to :func:`os.fdopen` to create a file object.
77
78
79.. function:: get_osfhandle(fd)
80
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020081 Return the file handle for the file descriptor *fd*. Raises :exc:`OSError` if
Georg Brandl116aa622007-08-15 14:28:22 +000082 *fd* is not recognized.
83
84
85.. _msvcrt-console:
86
87Console I/O
88-----------
89
90
91.. function:: kbhit()
92
93 Return true if a keypress is waiting to be read.
94
95
96.. function:: getch()
97
Brian Curtin8790a072010-08-24 05:20:30 +000098 Read a keypress and return the resulting character as a byte string.
99 Nothing is echoed to the console. This call will block if a keypress
100 is not already available, but will not wait for :kbd:`Enter` to be
101 pressed. If the pressed key was a special function key, this will
102 return ``'\000'`` or ``'\xe0'``; the next call will return the keycode.
103 The :kbd:`Control-C` keypress cannot be read with this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Georg Brandl48310cd2009-01-03 21:18:54 +0000105
Christian Heimes2f1019e2007-12-10 16:18:49 +0000106.. function:: getwch()
107
Christian Heimesa34706f2008-01-04 03:06:10 +0000108 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000109
Georg Brandl116aa622007-08-15 14:28:22 +0000110
111.. function:: getche()
112
113 Similar to :func:`getch`, but the keypress will be echoed if it represents a
114 printable character.
115
116
Christian Heimes2f1019e2007-12-10 16:18:49 +0000117.. function:: getwche()
118
Christian Heimesa34706f2008-01-04 03:06:10 +0000119 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000120
Christian Heimes2f1019e2007-12-10 16:18:49 +0000121
Georg Brandl116aa622007-08-15 14:28:22 +0000122.. function:: putch(char)
123
Brian Curtin8790a072010-08-24 05:20:30 +0000124 Print the byte string *char* to the console without buffering.
Georg Brandl116aa622007-08-15 14:28:22 +0000125
Georg Brandl48310cd2009-01-03 21:18:54 +0000126
Christian Heimes2f1019e2007-12-10 16:18:49 +0000127.. function:: putwch(unicode_char)
128
Christian Heimesa34706f2008-01-04 03:06:10 +0000129 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000130
Georg Brandl116aa622007-08-15 14:28:22 +0000131
132.. function:: ungetch(char)
133
Brian Curtin8790a072010-08-24 05:20:30 +0000134 Cause the byte string *char* to be "pushed back" into the console buffer;
135 it will be the next character read by :func:`getch` or :func:`getche`.
Georg Brandl116aa622007-08-15 14:28:22 +0000136
Georg Brandl48310cd2009-01-03 21:18:54 +0000137
Christian Heimes2f1019e2007-12-10 16:18:49 +0000138.. function:: ungetwch(unicode_char)
139
Christian Heimesa34706f2008-01-04 03:06:10 +0000140 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000141
Georg Brandl116aa622007-08-15 14:28:22 +0000142
143.. _msvcrt-other:
144
145Other Functions
146---------------
147
148
149.. function:: heapmin()
150
Georg Brandl60203b42010-10-06 10:11:56 +0000151 Force the :c:func:`malloc` heap to clean itself up and return unused blocks to
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200152 the operating system. On failure, this raises :exc:`OSError`.