| 1 |
|
| 2 |
from _structseq import structseqtype, structseqfield, structseq_new |
| 3 |
|
| 4 |
|
| 5 |
import errno |
| 6 |
import sys |
| 7 |
if 'posix' in sys.builtin_module_names: |
| 8 |
import posix |
| 9 |
osname = 'posix' |
| 10 |
elif 'nt' in sys.builtin_module_names: |
| 11 |
import nt as posix |
| 12 |
osname = 'nt' |
| 13 |
else: |
| 14 |
raise ImportError("XXX") |
| 15 |
|
| 16 |
error = OSError |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class stat_result(metaclass=structseqtype): |
| 21 |
|
| 22 |
name = "os.stat_result" |
| 23 |
__module__ = "os" |
| 24 |
|
| 25 |
st_mode = structseqfield(0, "protection bits") |
| 26 |
st_ino = structseqfield(1, "inode") |
| 27 |
st_dev = structseqfield(2, "device") |
| 28 |
st_nlink = structseqfield(3, "number of hard links") |
| 29 |
st_uid = structseqfield(4, "user ID of owner") |
| 30 |
st_gid = structseqfield(5, "group ID of owner") |
| 31 |
st_size = structseqfield(6, "total size, in bytes") |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
_integer_atime = structseqfield(7) |
| 36 |
_integer_mtime = structseqfield(8) |
| 37 |
_integer_ctime = structseqfield(9) |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
st_atime = structseqfield(11, "time of last access") |
| 42 |
st_mtime = structseqfield(12, "time of last modification") |
| 43 |
st_ctime = structseqfield(13, "time of last change") |
| 44 |
|
| 45 |
if "st_blksize" in posix._statfields: |
| 46 |
st_blksize = structseqfield(20, "blocksize for filesystem I/O") |
| 47 |
if "st_blocks" in posix._statfields: |
| 48 |
st_blocks = structseqfield(21, "number of blocks allocated") |
| 49 |
if "st_rdev" in posix._statfields: |
| 50 |
st_rdev = structseqfield(22, "device ID (if special file)") |
| 51 |
if "st_flags" in posix._statfields: |
| 52 |
st_flags = structseqfield(23, "user defined flags for file") |
| 53 |
|
| 54 |
if osname == 'nt': |
| 55 |
st_file_attributes = structseqfield(30, "Windows file attribute bits") |
| 56 |
st_reparse_tag = structseqfield(31, "Windows reparse tag") |
| 57 |
|
| 58 |
|
| 59 |
if "nsec_atime" in posix._statfields: |
| 60 |
nsec_atime = structseqfield(40, "nanoseconds part of time of last access") |
| 61 |
if "nsec_mtime" in posix._statfields: |
| 62 |
nsec_mtime = structseqfield(41, "nanoseconds part of time of last modification") |
| 63 |
if "nsec_ctime" in posix._statfields: |
| 64 |
nsec_ctime = structseqfield(42, "nanoseconds part of time of last change") |
| 65 |
|
| 66 |
|
| 67 |
st_atime_ns = structseqfield(50, "time of last access in nanoseconds") |
| 68 |
st_mtime_ns = structseqfield(51, "time of last modification in nanoseconds") |
| 69 |
st_ctime_ns = structseqfield(52, "time of last change in nanoseconds") |
| 70 |
|
| 71 |
def __init__(self, *args, **kw): |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
if self.st_atime is None: |
| 76 |
self.__dict__['st_atime'] = self[7] |
| 77 |
if self.st_mtime is None: |
| 78 |
self.__dict__['st_mtime'] = self[8] |
| 79 |
if self.st_ctime is None: |
| 80 |
self.__dict__['st_ctime'] = self[9] |
| 81 |
|
| 82 |
|
| 83 |
class statvfs_result(metaclass=structseqtype): |
| 84 |
""" |
| 85 |
Result from statvfs or fstatvfs. |
| 86 |
|
| 87 |
This object may be accessed either as a tuple of |
| 88 |
(bsize, frsize, blocks, bfree, bavail, files, ffree, favail, flag, namemax), |
| 89 |
or via the attributes f_bsize, f_frsize, f_blocks, f_bfree, and so on. |
| 90 |
""" |
| 91 |
|
| 92 |
name = "os.statvfs_result" |
| 93 |
__module__ = "os" |
| 94 |
|
| 95 |
f_bsize = structseqfield(0) |
| 96 |
f_frsize = structseqfield(1) |
| 97 |
f_blocks = structseqfield(2) |
| 98 |
f_bfree = structseqfield(3) |
| 99 |
f_bavail = structseqfield(4) |
| 100 |
f_files = structseqfield(5) |
| 101 |
f_ffree = structseqfield(6) |
| 102 |
f_favail = structseqfield(7) |
| 103 |
f_flag = structseqfield(8) |
| 104 |
f_namemax = structseqfield(9) |
| 105 |
f_fsid = structseqfield(20) |
| 106 |
|
| 107 |
|
| 108 |
class uname_result(metaclass=structseqtype): |
| 109 |
|
| 110 |
name = osname + ".uname_result" |
| 111 |
|
| 112 |
sysname = structseqfield(0, "operating system name") |
| 113 |
nodename = structseqfield(1, "name of machine on network " |
| 114 |
"(implementation-defined") |
| 115 |
release = structseqfield(2, "operating system release") |
| 116 |
version = structseqfield(3, "operating system version") |
| 117 |
machine = structseqfield(4, "hardware identifier") |
| 118 |
|
| 119 |
class terminal_size(metaclass=structseqtype): |
| 120 |
|
| 121 |
name = "os.terminal_size" |
| 122 |
__module__ = "os" |
| 123 |
|
| 124 |
columns = structseqfield(0, "width of the terminal window in characters") |
| 125 |
lines = structseqfield(1, "height of the terminal window in characters") |
| 126 |
|
| 127 |
|
| 128 |
class times_result(metaclass=structseqtype): |
| 129 |
|
| 130 |
name = "posix.times_result" |
| 131 |
__module__ = "posix" |
| 132 |
|
| 133 |
user = structseqfield(0, "user time") |
| 134 |
system = structseqfield(1, "system time") |
| 135 |
children_user = structseqfield(2, "user time of children") |
| 136 |
children_system = structseqfield(3, "system time of children") |
| 137 |
elapsed = structseqfield(4, "elapsed time since an arbitrary point in the past") |
| 138 |
|
| 139 |
|
| 140 |
class sched_param(metaclass=structseqtype): |
| 141 |
name = "posix.sched_param" |
| 142 |
__module__ = "posix" |
| 143 |
|
| 144 |
sched_priority = structseqfield(0, "sched_priority") |
| 145 |
|
| 146 |
def __new__(cls, sched_priority): |
| 147 |
return structseq_new(cls, sched_priority) |
| 148 |
|
| 149 |
def waitstatus_to_exitcode(status): |
| 150 |
""" |
| 151 |
Convert a wait status to an exit code. |
| 152 |
|
| 153 |
On Unix: |
| 154 |
|
| 155 |
* If WIFEXITED(status) is true, return WEXITSTATUS(status). |
| 156 |
* If WIFSIGNALED(status) is true, return -WTERMSIG(status). |
| 157 |
* Otherwise, raise a ValueError. |
| 158 |
|
| 159 |
On Windows, return status shifted right by 8 bits. |
| 160 |
|
| 161 |
On Unix, if the process is being traced or if waitpid() was called with |
| 162 |
WUNTRACED option, the caller must first check if WIFSTOPPED(status) is true. |
| 163 |
This function must not be called if WIFSTOPPED(status) is true. |
| 164 |
""" |
| 165 |
if not isinstance(status, int): |
| 166 |
raise TypeError("integer argument expected, got float") |
| 167 |
if sys.platform == "win32": |
| 168 |
return status >> 8 |
| 169 |
if posix.WIFEXITED(status): |
| 170 |
exitcode = posix.WEXITSTATUS(status) |
| 171 |
if exitcode < 0: |
| 172 |
raise ValueError("invalid WEXITSTATUS: %s" % (exitcode, )) |
| 173 |
return exitcode |
| 174 |
elif posix.WIFSIGNALED(status): |
| 175 |
signum = posix.WTERMSIG(status) |
| 176 |
if signum < 0: |
| 177 |
raise ValueError("invalid WTERMSIG: %s" % (exitcode, )) |
| 178 |
return -signum |
| 179 |
elif posix.WIFSTOPPED(status): |
| 180 |
signum = posix.WSTOPSIG(status) |
| 181 |
raise ValueError("process stopped by delivery of signal %s" % (signum, )) |
| 182 |
raise ValueError("invalid wait status %s" % (status, )) |
| 183 |
|
| 184 |
|
| 185 |
if osname == 'posix': |
| 186 |
def wait(): |
| 187 |
""" wait() -> (pid, status) |
| 188 |
|
| 189 |
Wait for completion of a child process. |
| 190 |
""" |
| 191 |
return posix.waitpid(-1, 0) |
| 192 |
|
| 193 |
def wait3(options): |
| 194 |
""" wait3(options) -> (pid, status, rusage) |
| 195 |
|
| 196 |
Wait for completion of a child process and provides resource usage information |
| 197 |
""" |
| 198 |
from _pypy_wait import wait3 |
| 199 |
return wait3(options) |
| 200 |
|
| 201 |
def wait4(pid, options): |
| 202 |
""" wait4(pid, options) -> (pid, status, rusage) |
| 203 |
|
| 204 |
Wait for completion of the child process "pid" and provides resource usage information |
| 205 |
""" |
| 206 |
from _pypy_wait import wait4 |
| 207 |
return wait4(pid, options) |
| 208 |
|
| 209 |
def urandom(n): |
| 210 |
"""urandom(n) -> str |
| 211 |
|
| 212 |
Return a string of n random bytes suitable for cryptographic use. |
| 213 |
|
| 214 |
""" |
| 215 |
if n < 0: |
| 216 |
raise ValueError("negative argument not allowed") |
| 217 |
try: |
| 218 |
with open('/dev/urandom', 'rb', buffering=0) as fd: |
| 219 |
return fd.read(n) |
| 220 |
except OSError as e: |
| 221 |
if e.errno in (errno.ENOENT, errno.ENXIO, errno.ENODEV, errno.EACCES): |
| 222 |
raise NotImplementedError("/dev/urandom (or equivalent) not found") |
| 223 |
raise |