blob: b334eeb314dd7c289ec282484cc765abc21d0bb7 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007
Georg Brandl116aa622007-08-15 14:28:22 +00008.. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org>
9
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040010--------------
Georg Brandl116aa622007-08-15 14:28:22 +000011
12These functions provide access to some useful capabilities on Windows platforms.
13Some higher-level modules use these functions to build the Windows
14implementations of their services. For example, the :mod:`getpass` module uses
15this in the implementation of the :func:`getpass` function.
16
17Further documentation on these functions can be found in the Platform API
18documentation.
19
Christian Heimes2f1019e2007-12-10 16:18:49 +000020The module implements both the normal and wide char variants of the console I/O
21api. The normal API deals only with ASCII characters and is of limited use
Georg Brandl48310cd2009-01-03 21:18:54 +000022for internationalized applications. The wide char API should be used where
Martin Panterd21e0b52015-10-10 10:36:22 +000023ever possible.
Georg Brandl116aa622007-08-15 14:28:22 +000024
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020025.. versionchanged:: 3.3
26 Operations in this module now raise :exc:`OSError` where :exc:`IOError`
27 was raised.
28
29
Georg Brandl116aa622007-08-15 14:28:22 +000030.. _msvcrt-files:
31
32File Operations
33---------------
34
35
36.. function:: locking(fd, mode, nbytes)
37
38 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020039 :exc:`OSError` on failure. The locked region of the file extends from the
Georg Brandl116aa622007-08-15 14:28:22 +000040 current file position for *nbytes* bytes, and may continue beyond the end of the
41 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
42 regions in a file may be locked at the same time, but may not overlap. Adjacent
43 regions are not merged; they must be unlocked individually.
44
45
46.. data:: LK_LOCK
47 LK_RLCK
48
49 Locks the specified bytes. If the bytes cannot be locked, the program
50 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020051 be locked, :exc:`OSError` is raised.
Georg Brandl116aa622007-08-15 14:28:22 +000052
53
54.. data:: LK_NBLCK
55 LK_NBRLCK
56
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020057 Locks the specified bytes. If the bytes cannot be locked, :exc:`OSError` is
Georg Brandl116aa622007-08-15 14:28:22 +000058 raised.
59
60
61.. data:: LK_UNLCK
62
63 Unlocks the specified bytes, which must have been previously locked.
64
65
66.. function:: setmode(fd, flags)
67
68 Set the line-end translation mode for the file descriptor *fd*. To set it to
69 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
70 :const:`os.O_BINARY`.
71
72
73.. function:: open_osfhandle(handle, flags)
74
75 Create a C runtime file descriptor from the file handle *handle*. The *flags*
Christian Heimesfaf2f632008-01-06 16:59:19 +000076 parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
Georg Brandl116aa622007-08-15 14:28:22 +000077 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
78 to :func:`os.fdopen` to create a file object.
79
80
81.. function:: get_osfhandle(fd)
82
Antoine Pitrou62ab10a02011-10-12 20:10:51 +020083 Return the file handle for the file descriptor *fd*. Raises :exc:`OSError` if
Georg Brandl116aa622007-08-15 14:28:22 +000084 *fd* is not recognized.
85
86
87.. _msvcrt-console:
88
89Console I/O
90-----------
91
92
93.. function:: kbhit()
94
95 Return true if a keypress is waiting to be read.
96
97
98.. function:: getch()
99
Brian Curtin8790a072010-08-24 05:20:30 +0000100 Read a keypress and return the resulting character as a byte string.
101 Nothing is echoed to the console. This call will block if a keypress
102 is not already available, but will not wait for :kbd:`Enter` to be
103 pressed. If the pressed key was a special function key, this will
104 return ``'\000'`` or ``'\xe0'``; the next call will return the keycode.
105 The :kbd:`Control-C` keypress cannot be read with this function.
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Georg Brandl48310cd2009-01-03 21:18:54 +0000107
Christian Heimes2f1019e2007-12-10 16:18:49 +0000108.. function:: getwch()
109
Christian Heimesa34706f2008-01-04 03:06:10 +0000110 Wide char variant of :func:`getch`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000111
Georg Brandl116aa622007-08-15 14:28:22 +0000112
113.. function:: getche()
114
115 Similar to :func:`getch`, but the keypress will be echoed if it represents a
116 printable character.
117
118
Christian Heimes2f1019e2007-12-10 16:18:49 +0000119.. function:: getwche()
120
Christian Heimesa34706f2008-01-04 03:06:10 +0000121 Wide char variant of :func:`getche`, returning a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000122
Christian Heimes2f1019e2007-12-10 16:18:49 +0000123
Georg Brandl116aa622007-08-15 14:28:22 +0000124.. function:: putch(char)
125
Brian Curtin8790a072010-08-24 05:20:30 +0000126 Print the byte string *char* to the console without buffering.
Georg Brandl116aa622007-08-15 14:28:22 +0000127
Georg Brandl48310cd2009-01-03 21:18:54 +0000128
Christian Heimes2f1019e2007-12-10 16:18:49 +0000129.. function:: putwch(unicode_char)
130
Christian Heimesa34706f2008-01-04 03:06:10 +0000131 Wide char variant of :func:`putch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000132
Georg Brandl116aa622007-08-15 14:28:22 +0000133
134.. function:: ungetch(char)
135
Brian Curtin8790a072010-08-24 05:20:30 +0000136 Cause the byte string *char* to be "pushed back" into the console buffer;
137 it will be the next character read by :func:`getch` or :func:`getche`.
Georg Brandl116aa622007-08-15 14:28:22 +0000138
Georg Brandl48310cd2009-01-03 21:18:54 +0000139
Christian Heimes2f1019e2007-12-10 16:18:49 +0000140.. function:: ungetwch(unicode_char)
141
Christian Heimesa34706f2008-01-04 03:06:10 +0000142 Wide char variant of :func:`ungetch`, accepting a Unicode value.
Georg Brandl48310cd2009-01-03 21:18:54 +0000143
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145.. _msvcrt-other:
146
147Other Functions
148---------------
149
150
151.. function:: heapmin()
152
Georg Brandl60203b42010-10-06 10:11:56 +0000153 Force the :c:func:`malloc` heap to clean itself up and return unused blocks to
Antoine Pitrou62ab10a02011-10-12 20:10:51 +0200154 the operating system. On failure, this raises :exc:`OSError`.