Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | |
| 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 | |
| 11 | These functions provide access to some useful capabilities on Windows platforms. |
| 12 | Some higher-level modules use these functions to build the Windows |
| 13 | implementations of their services. For example, the :mod:`getpass` module uses |
| 14 | this in the implementation of the :func:`getpass` function. |
| 15 | |
| 16 | Further documentation on these functions can be found in the Platform API |
| 17 | documentation. |
| 18 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 19 | The module implements both the normal and wide char variants of the console I/O |
| 20 | api. The normal API deals only with ASCII characters and is of limited use |
| 21 | for internationalized applications. The wide char API should be used where |
| 22 | ever possible |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 23 | |
| 24 | .. _msvcrt-files: |
| 25 | |
| 26 | File 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 Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame^] | 70 | parameter should be a bitwise OR of :const:`os.O_APPEND`, :const:`os.O_RDONLY`, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 71 | 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 | |
| 83 | Console 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 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 101 | |
| 102 | .. function:: getwch() |
| 103 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 104 | Wide char variant of :func:`getch`, returning a Unicode value. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 105 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
| 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 Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 113 | .. function:: getwche() |
| 114 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 115 | Wide char variant of :func:`getche`, returning a Unicode value. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 116 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 117 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 118 | .. function:: putch(char) |
| 119 | |
| 120 | Print the character *char* to the console without buffering. |
| 121 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 122 | |
| 123 | .. function:: putwch(unicode_char) |
| 124 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 125 | Wide char variant of :func:`putch`, accepting a Unicode value. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 126 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
| 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 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 133 | |
| 134 | .. function:: ungetwch(unicode_char) |
| 135 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 136 | Wide char variant of :func:`ungetch`, accepting a Unicode value. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 137 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
| 139 | .. _msvcrt-other: |
| 140 | |
| 141 | Other 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 | |