| 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 |  | 
| Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 23 | .. versionchanged:: 3.3 | 
 | 24 |    Operations in this module now raise :exc:`OSError` where :exc:`IOError` | 
 | 25 |    was raised. | 
 | 26 |  | 
 | 27 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 28 | .. _msvcrt-files: | 
 | 29 |  | 
 | 30 | File Operations | 
 | 31 | --------------- | 
 | 32 |  | 
 | 33 |  | 
 | 34 | .. function:: locking(fd, mode, nbytes) | 
 | 35 |  | 
 | 36 |    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] | 37 |    :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] | 38 |    current file position for *nbytes* bytes, and may continue beyond the end of the | 
 | 39 |    file.  *mode* must be one of the :const:`LK_\*` constants listed below. Multiple | 
 | 40 |    regions in a file may be locked at the same time, but may not overlap.  Adjacent | 
 | 41 |    regions are not merged; they must be unlocked individually. | 
 | 42 |  | 
 | 43 |  | 
 | 44 | .. data:: LK_LOCK | 
 | 45 |           LK_RLCK | 
 | 46 |  | 
 | 47 |    Locks the specified bytes. If the bytes cannot be locked, the program | 
 | 48 |    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] | 49 |    be locked, :exc:`OSError` is raised. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 50 |  | 
 | 51 |  | 
 | 52 | .. data:: LK_NBLCK | 
 | 53 |           LK_NBRLCK | 
 | 54 |  | 
| Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 55 |    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] | 56 |    raised. | 
 | 57 |  | 
 | 58 |  | 
 | 59 | .. data:: LK_UNLCK | 
 | 60 |  | 
 | 61 |    Unlocks the specified bytes, which must have been previously locked. | 
 | 62 |  | 
 | 63 |  | 
 | 64 | .. function:: setmode(fd, flags) | 
 | 65 |  | 
 | 66 |    Set the line-end translation mode for the file descriptor *fd*. To set it to | 
 | 67 |    text mode, *flags* should be :const:`os.O_TEXT`; for binary, it should be | 
 | 68 |    :const:`os.O_BINARY`. | 
 | 69 |  | 
 | 70 |  | 
 | 71 | .. function:: open_osfhandle(handle, flags) | 
 | 72 |  | 
 | 73 |    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] | 74 |    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] | 75 |    and :const:`os.O_TEXT`.  The returned file descriptor may be used as a parameter | 
 | 76 |    to :func:`os.fdopen` to create a file object. | 
 | 77 |  | 
 | 78 |  | 
 | 79 | .. function:: get_osfhandle(fd) | 
 | 80 |  | 
| Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 81 |    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] | 82 |    *fd* is not recognized. | 
 | 83 |  | 
 | 84 |  | 
 | 85 | .. _msvcrt-console: | 
 | 86 |  | 
 | 87 | Console I/O | 
 | 88 | ----------- | 
 | 89 |  | 
 | 90 |  | 
 | 91 | .. function:: kbhit() | 
 | 92 |  | 
 | 93 |    Return true if a keypress is waiting to be read. | 
 | 94 |  | 
 | 95 |  | 
 | 96 | .. function:: getch() | 
 | 97 |  | 
| Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 98 |    Read a keypress and return the resulting character as a byte string. | 
 | 99 |    Nothing is echoed to the console.  This call will block if a keypress | 
 | 100 |    is not already available, but will not wait for :kbd:`Enter` to be | 
 | 101 |    pressed. If the pressed key was a special function key, this will | 
 | 102 |    return ``'\000'`` or ``'\xe0'``; the next call will return the keycode. | 
 | 103 |    The :kbd:`Control-C` keypress cannot be read with this function. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 104 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 105 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 106 | .. function:: getwch() | 
 | 107 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 108 |    Wide char variant of :func:`getch`, returning a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 109 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 110 |  | 
 | 111 | .. function:: getche() | 
 | 112 |  | 
 | 113 |    Similar to :func:`getch`, but the keypress will be echoed if it  represents a | 
 | 114 |    printable character. | 
 | 115 |  | 
 | 116 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 117 | .. function:: getwche() | 
 | 118 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 119 |    Wide char variant of :func:`getche`, returning a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 120 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 121 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 122 | .. function:: putch(char) | 
 | 123 |  | 
| Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 124 |    Print the byte string *char* to the console without buffering. | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 125 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 126 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 127 | .. function:: putwch(unicode_char) | 
 | 128 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 129 |    Wide char variant of :func:`putch`, accepting a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 130 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 131 |  | 
 | 132 | .. function:: ungetch(char) | 
 | 133 |  | 
| Brian Curtin | 8790a07 | 2010-08-24 05:20:30 +0000 | [diff] [blame] | 134 |    Cause the byte string *char* to be "pushed back" into the console buffer; | 
 | 135 |    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] | 136 |  | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 137 |  | 
| Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 138 | .. function:: ungetwch(unicode_char) | 
 | 139 |  | 
| Christian Heimes | a34706f | 2008-01-04 03:06:10 +0000 | [diff] [blame] | 140 |    Wide char variant of :func:`ungetch`, accepting a Unicode value. | 
| Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 141 |  | 
| Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 142 |  | 
 | 143 | .. _msvcrt-other: | 
 | 144 |  | 
 | 145 | Other Functions | 
 | 146 | --------------- | 
 | 147 |  | 
 | 148 |  | 
 | 149 | .. function:: heapmin() | 
 | 150 |  | 
| Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 151 |    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] | 152 |    the operating system.  On failure, this raises :exc:`OSError`. |