Debian Codegraph @

lib_pypy/msvcrt.py

raw symbols SCIP
1 """
2 Python interface to the Microsoft Visual C Runtime
3 Library, providing access to those non-portable, but
4 still useful routines.
5 """
6
7 # XXX incomplete: implemented only functions needed by subprocess.py
8 # PAC: 2010/08 added MS locking for Whoosh
9
10 # 07/2016: rewrote in CFFI
11
12 import sys
13 if sys.platform != 'win32':
14 raise ModuleNotFoundError("The 'msvcrt' module is only available on Windows", name="msvcrt")
15
16 import _rawffi
17 if sys.maxsize > 2 ** 31:
18 from _pypy_winbase_cffi64 import ffi as _ffi
19 else:
20 from _pypy_winbase_cffi import ffi as _ffi
21 _lib = _ffi.dlopen(_rawffi.get_libc().name)
22 _kernel32 = _ffi.dlopen('kernel32')
23
24 import errno
25
26 from __pypy__ import builtinify, get_osfhandle as _get_osfhandle
27
28 def _ioerr():
29 e = _ffi.errno
30 raise IOError(e, errno.errorcode[e])
31
32
33 @builtinify
34 def open_osfhandle(fd, flags):
35 """"open_osfhandle(handle, flags) -> file descriptor
36
37 Create a C runtime file descriptor from the file handle handle. The
38 flags parameter should be a bitwise OR of os.O_APPEND, os.O_RDONLY,
39 and os.O_TEXT. The returned file descriptor may be used as a parameter
40 to os.fdopen() to create a file object."""
41 fd = _lib._open_osfhandle(fd, flags)
42 if fd == -1:
43 _ioerr()
44 return fd
45
46 @builtinify
47 def get_osfhandle(fd):
48 """"get_osfhandle(fd) -> file handle
49
50 Return the file handle for the file descriptor fd. Raises IOError if
51 fd is not recognized."""
52 result = _get_osfhandle(fd)
53 if result == -1:
54 _ioerr()
55 return result
56
57 @builtinify
58 def setmode(fd, flags):
59 """setmode(fd, mode) -> Previous mode
60
61 Set the line-end translation mode for the file descriptor fd. To set
62 it to text mode, flags should be os.O_TEXT; for binary, it should be
63 os.O_BINARY."""
64 flags = _lib._setmode(fd, flags)
65 if flags == -1:
66 _ioerr()
67 return flags
68
69 LK_UNLCK, LK_LOCK, LK_NBLCK, LK_RLCK, LK_NBRLCK = range(5)
70
71 @builtinify
72 def locking(fd, mode, nbytes):
73 """"locking(fd, mode, nbytes) -> None
74
75 Lock part of a file based on file descriptor fd from the C runtime.
76 Raises IOError on failure. The locked region of the file extends from
77 the current file position for nbytes bytes, and may continue beyond
78 the end of the file. mode must be one of the LK_* constants listed
79 below. Multiple regions in a file may be locked at the same time, but
80 may not overlap. Adjacent regions are not merged; they must be unlocked
81 individually."""
82 rv = _lib._locking(fd, mode, nbytes)
83 if rv != 0:
84 _ioerr()
85
86 # Console I/O routines
87
88 kbhit = _lib._kbhit
89
90 @builtinify
91 def getch():
92 return bytes([_lib._getch()])
93
94 @builtinify
95 def getwch():
96 return chr(_lib._getwch())
97
98 @builtinify
99 def getche():
100 return bytes([_lib._getche()])
101
102 @builtinify
103 def getwche():
104 return chr(_lib._getwche())
105
106 @builtinify
107 def putch(ch):
108 _lib._putch(ord(ch))
109
110 @builtinify
111 def putwch(ch):
112 _lib._putwch(ch)
113
114 @builtinify
115 def ungetch(ch):
116 msg = 'ungetch() argument must be a byte string of length 1, not '
117 if not isinstance(ch, bytes):
118 raise(TypeError, msg + type(ch))
119 if len(ch) != 1:
120 raise(TypeError, msg + 'not bytes')
121 if _lib._ungetch(ord(ch)) == -1: # EOF
122 _ioerr()
123
124 @builtinify
125 def ungetwch(ch):
126 msg = 'ungetwch() argument must be a unicode character, not '
127 if not isinstance(ch, str):
128 raise(TypeError, msg + type(ch))
129 if len(ch) != 1:
130 raise(TypeError, msg + 'not str')
131 if _lib._ungetwch(ord(ch)) == -1: # EOF
132 _ioerr()
133
134 SetErrorMode = _kernel32.SetErrorMode
135 GetErrorMode = _kernel32.GetErrorMode
136 SEM_FAILCRITICALERRORS = _lib.SEM_FAILCRITICALERRORS
137 SEM_NOGPFAULTERRORBOX = _lib.SEM_NOGPFAULTERRORBOX
138 SEM_NOALIGNMENTFAULTEXCEPT = _lib.SEM_NOALIGNMENTFAULTEXCEPT
139 SEM_NOOPENFILEERRORBOX = _lib.SEM_NOOPENFILEERRORBOX