blob: d43bb4c60f1056aed85b5b2dbb01ab2e78d2fabf [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
19
20.. _msvcrt-files:
21
22File Operations
23---------------
24
25
26.. function:: locking(fd, mode, nbytes)
27
28 Lock part of a file based on file descriptor *fd* from the C runtime. Raises
29 :exc:`IOError` on failure. The locked region of the file extends from the
30 current file position for *nbytes* bytes, and may continue beyond the end of the
31 file. *mode* must be one of the :const:`LK_\*` constants listed below. Multiple
32 regions in a file may be locked at the same time, but may not overlap. Adjacent
33 regions are not merged; they must be unlocked individually.
34
35
36.. data:: LK_LOCK
37 LK_RLCK
38
39 Locks the specified bytes. If the bytes cannot be locked, the program
40 immediately tries again after 1 second. If, after 10 attempts, the bytes cannot
41 be locked, :exc:`IOError` is raised.
42
43
44.. data:: LK_NBLCK
45 LK_NBRLCK
46
47 Locks the specified bytes. If the bytes cannot be locked, :exc:`IOError` is
48 raised.
49
50
51.. data:: LK_UNLCK
52
53 Unlocks the specified bytes, which must have been previously locked.
54
55
56.. function:: setmode(fd, flags)
57
58 Set the line-end translation mode for the file descriptor *fd*. To set it to
59 text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be
60 :const:`os.O_BINARY`.
61
62
63.. function:: open_osfhandle(handle, flags)
64
65 Create a C runtime file descriptor from the file handle *handle*. The *flags*
66 parameter should be a bit-wise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`,
67 and :const:`os.O_TEXT`. The returned file descriptor may be used as a parameter
68 to :func:`os.fdopen` to create a file object.
69
70
71.. function:: get_osfhandle(fd)
72
73 Return the file handle for the file descriptor *fd*. Raises :exc:`IOError` if
74 *fd* is not recognized.
75
76
77.. _msvcrt-console:
78
79Console I/O
80-----------
81
82
83.. function:: kbhit()
84
85 Return true if a keypress is waiting to be read.
86
87
88.. function:: getch()
89
90 Read a keypress and return the resulting character. Nothing is echoed to the
91 console. This call will block if a keypress is not already available, but will
92 not wait for :kbd:`Enter` to be pressed. If the pressed key was a special
93 function key, this will return ``'\000'`` or ``'\xe0'``; the next call will
94 return the keycode. The :kbd:`Control-C` keypress cannot be read with this
95 function.
96
97
98.. function:: getche()
99
100 Similar to :func:`getch`, but the keypress will be echoed if it represents a
101 printable character.
102
103
104.. function:: putch(char)
105
106 Print the character *char* to the console without buffering.
107
108
109.. function:: ungetch(char)
110
111 Cause the character *char* to be "pushed back" into the console buffer; it will
112 be the next character read by :func:`getch` or :func:`getche`.
113
114
115.. _msvcrt-other:
116
117Other Functions
118---------------
119
120
121.. function:: heapmin()
122
123 Force the :cfunc:`malloc` heap to clean itself up and return unused blocks to
124 the operating system. This only works on Windows NT. On failure, this raises
125 :exc:`IOError`.
126