Serhiy Storchaka | 29b0a26 | 2016-12-04 10:20:55 +0200 | [diff] [blame] | 1 | :mod:`msvcrt` --- Useful routines from the MS VC++ runtime |
| 2 | ========================================================== |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: msvcrt |
| 5 | :platform: Windows |
| 6 | :synopsis: Miscellaneous useful routines from the MS VC++ runtime. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 7 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 8 | .. sectionauthor:: Fred L. Drake, Jr. <fdrake@acm.org> |
| 9 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 10 | -------------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 11 | |
| 12 | These functions provide access to some useful capabilities on Windows platforms. |
| 13 | Some higher-level modules use these functions to build the Windows |
| 14 | implementations of their services. For example, the :mod:`getpass` module uses |
| 15 | this in the implementation of the :func:`getpass` function. |
| 16 | |
| 17 | Further documentation on these functions can be found in the Platform API |
| 18 | documentation. |
| 19 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 20 | The module implements both the normal and wide char variants of the console I/O |
| 21 | api. The normal API deals only with ASCII characters and is of limited use |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 22 | for internationalized applications. The wide char API should be used where |
Martin Panter | d21e0b5 | 2015-10-10 10:36:22 +0000 | [diff] [blame] | 23 | ever possible. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 24 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 25 | .. versionchanged:: 3.3 |
| 26 | Operations in this module now raise :exc:`OSError` where :exc:`IOError` |
| 27 | was raised. |
| 28 | |
| 29 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 30 | .. _msvcrt-files: |
| 31 | |
| 32 | File 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 Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 39 | :exc:`OSError` on failure. The locked region of the file extends from the |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 40 | 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 Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 51 | be locked, :exc:`OSError` is raised. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | .. data:: LK_NBLCK |
| 55 | LK_NBRLCK |
| 56 | |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 57 | Locks the specified bytes. If the bytes cannot be locked, :exc:`OSError` is |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 58 | 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 Heimes | faf2f63 | 2008-01-06 16:59:19 +0000 | [diff] [blame] | 76 | 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] | 77 | 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 Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 83 | Return the file handle for the file descriptor *fd*. Raises :exc:`OSError` if |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 84 | *fd* is not recognized. |
| 85 | |
| 86 | |
| 87 | .. _msvcrt-console: |
| 88 | |
| 89 | Console 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 Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 100 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 106 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 107 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 108 | .. function:: getwch() |
| 109 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 110 | Wide char variant of :func:`getch`, returning a Unicode value. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 111 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 112 | |
| 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 Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 119 | .. function:: getwche() |
| 120 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 121 | Wide char variant of :func:`getche`, returning a Unicode value. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 122 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 123 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 124 | .. function:: putch(char) |
| 125 | |
Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 126 | Print the byte string *char* to the console without buffering. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 127 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 128 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 129 | .. function:: putwch(unicode_char) |
| 130 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 131 | Wide char variant of :func:`putch`, accepting a Unicode value. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 132 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 133 | |
| 134 | .. function:: ungetch(char) |
| 135 | |
Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 136 | 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 Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 138 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 139 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 140 | .. function:: ungetwch(unicode_char) |
| 141 | |
Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 142 | Wide char variant of :func:`ungetch`, accepting a Unicode value. |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 143 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 144 | |
| 145 | .. _msvcrt-other: |
| 146 | |
| 147 | Other Functions |
| 148 | --------------- |
| 149 | |
| 150 | |
| 151 | .. function:: heapmin() |
| 152 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 153 | Force the :c:func:`malloc` heap to clean itself up and return unused blocks to |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 154 | the operating system. On failure, this raises :exc:`OSError`. |