| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 1 | :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 |  | 
 | 10 | These functions provide access to some useful capabilities on Windows platforms. | 
 | 11 | Some higher-level modules use these functions to build the  Windows | 
 | 12 | implementations of their services.  For example, the :mod:`getpass` module uses | 
 | 13 | this in the implementation of the :func:`getpass` function. | 
 | 14 |  | 
 | 15 | Further documentation on these functions can be found in the Platform API | 
 | 16 | documentation. | 
 | 17 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 18 | The module implements both the normal and wide char variants of the console I/O | 
 | 19 | 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] | 20 | for internationalized applications. The wide char API should be used where | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 21 | ever possible | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 22 |  | 
 | 23 | .. _msvcrt-files: | 
 | 24 |  | 
 | 25 | File Operations | 
 | 26 | --------------- | 
 | 27 |  | 
 | 28 |  | 
 | 29 | .. function:: locking(fd, mode, nbytes) | 
 | 30 |  | 
 | 31 |    Lock part of a file based on file descriptor *fd* from the C runtime.  Raises | 
 | 32 |    :exc:`IOError` on failure.  The locked region of the file extends from the | 
 | 33 |    current file position for *nbytes* bytes, and may continue beyond the end of the | 
 | 34 |    file.  *mode* must be one of the :const:`LK_\*` constants listed below. Multiple | 
 | 35 |    regions in a file may be locked at the same time, but may not overlap.  Adjacent | 
 | 36 |    regions are not merged; they must be unlocked individually. | 
 | 37 |  | 
 | 38 |  | 
 | 39 | .. data:: LK_LOCK | 
 | 40 |           LK_RLCK | 
 | 41 |  | 
 | 42 |    Locks the specified bytes. If the bytes cannot be locked, the program | 
 | 43 |    immediately tries again after 1 second.  If, after 10 attempts, the bytes cannot | 
 | 44 |    be locked, :exc:`IOError` is raised. | 
 | 45 |  | 
 | 46 |  | 
 | 47 | .. data:: LK_NBLCK | 
 | 48 |           LK_NBRLCK | 
 | 49 |  | 
 | 50 |    Locks the specified bytes. If the bytes cannot be locked, :exc:`IOError` is | 
 | 51 |    raised. | 
 | 52 |  | 
 | 53 |  | 
 | 54 | .. data:: LK_UNLCK | 
 | 55 |  | 
 | 56 |    Unlocks the specified bytes, which must have been previously locked. | 
 | 57 |  | 
 | 58 |  | 
 | 59 | .. function:: setmode(fd, flags) | 
 | 60 |  | 
 | 61 |    Set the line-end translation mode for the file descriptor *fd*. To set it to | 
 | 62 |    text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be | 
 | 63 |    :const:`os.O_BINARY`. | 
 | 64 |  | 
 | 65 |  | 
 | 66 | .. function:: open_osfhandle(handle, flags) | 
 | 67 |  | 
 | 68 |    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] | 69 |    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] | 70 |    and :const:`os.O_TEXT`.  The returned file descriptor may be used as a parameter | 
 | 71 |    to :func:`os.fdopen` to create a file object. | 
 | 72 |  | 
 | 73 |  | 
 | 74 | .. function:: get_osfhandle(fd) | 
 | 75 |  | 
 | 76 |    Return the file handle for the file descriptor *fd*.  Raises :exc:`IOError` if | 
 | 77 |    *fd* is not recognized. | 
 | 78 |  | 
 | 79 |  | 
 | 80 | .. _msvcrt-console: | 
 | 81 |  | 
 | 82 | Console I/O | 
 | 83 | ----------- | 
 | 84 |  | 
 | 85 |  | 
 | 86 | .. function:: kbhit() | 
 | 87 |  | 
 | 88 |    Return true if a keypress is waiting to be read. | 
 | 89 |  | 
 | 90 |  | 
 | 91 | .. function:: getch() | 
 | 92 |  | 
 | 93 |    Read a keypress and return the resulting character.  Nothing is echoed to the | 
 | 94 |    console.  This call will block if a keypress is not already available, but will | 
 | 95 |    not wait for :kbd:`Enter` to be pressed. If the pressed key was a special | 
 | 96 |    function key, this will return ``'\000'`` or ``'\xe0'``; the next call will | 
 | 97 |    return the keycode.  The :kbd:`Control-C` keypress cannot be read with this | 
 | 98 |    function. | 
 | 99 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 100 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 101 | .. function:: getwch() | 
 | 102 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 103 |    Wide char variant of :func:`getch`, returning a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 104 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 105 |  | 
 | 106 | .. function:: getche() | 
 | 107 |  | 
 | 108 |    Similar to :func:`getch`, but the keypress will be echoed if it  represents a | 
 | 109 |    printable character. | 
 | 110 |  | 
 | 111 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 112 | .. function:: getwche() | 
 | 113 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 114 |    Wide char variant of :func:`getche`, returning a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 115 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 116 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 117 | .. function:: putch(char) | 
 | 118 |  | 
 | 119 |    Print the character *char* to the console without buffering. | 
 | 120 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 121 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 122 | .. function:: putwch(unicode_char) | 
 | 123 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 124 |    Wide char variant of :func:`putch`, accepting a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 125 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 126 |  | 
 | 127 | .. function:: ungetch(char) | 
 | 128 |  | 
 | 129 |    Cause the character *char* to be "pushed back" into the console buffer; it will | 
 | 130 |    be the next character read by :func:`getch` or :func:`getche`. | 
 | 131 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 132 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 133 | .. function:: ungetwch(unicode_char) | 
 | 134 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 135 |    Wide char variant of :func:`ungetch`, accepting a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 136 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 137 |  | 
 | 138 | .. _msvcrt-other: | 
 | 139 |  | 
 | 140 | Other Functions | 
 | 141 | --------------- | 
 | 142 |  | 
 | 143 |  | 
 | 144 | .. function:: heapmin() | 
 | 145 |  | 
 | 146 |    Force the :cfunc:`malloc` heap to clean itself up and return unused blocks to | 
| Benjamin Peterson | 1baf465 | 2009-12-31 03:11:23 +0000 | [diff] [blame] | 147 |    the operating system.  On failure, this raises :exc:`IOError`. |