| 1 |
import os |
| 2 |
import sys |
| 3 |
from math import modf |
| 4 |
from errno import EOPNOTSUPP |
| 5 |
try: |
| 6 |
from errno import ENOTSUP |
| 7 |
except ImportError: |
| 8 |
|
| 9 |
ENOTSUP = 0 |
| 10 |
|
| 11 |
from rpython.rlib import rposix, rposix_stat, rfile |
| 12 |
from rpython.rlib import objectmodel, rurandom |
| 13 |
from rpython.rlib.objectmodel import specialize, not_rpython |
| 14 |
from rpython.rlib.rarithmetic import (widen, |
| 15 |
r_longlong, intmask, r_uint, r_int, INT_MIN, INT_MAX) |
| 16 |
|
| 17 |
from rpython.rlib.unroll import unrolling_iterable |
| 18 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 19 |
from rpython.tool.sourcetools import func_with_new_name |
| 20 |
from rpython.rlib.rutf8 import codepoints_in_utf8 |
| 21 |
|
| 22 |
from pypy.interpreter.buffer import BufferInterfaceNotFound |
| 23 |
from pypy.interpreter.gateway import unwrap_spec, WrappedDefault, Unwrapper |
| 24 |
from pypy.interpreter.error import ( |
| 25 |
OperationError, oefmt, wrap_oserror, wrap_oserror2, strerror as _strerror, |
| 26 |
exception_from_saved_errno) |
| 27 |
from pypy.interpreter.executioncontext import ExecutionContext |
| 28 |
from pypy.interpreter.baseobjspace import W_Root |
| 29 |
|
| 30 |
|
| 31 |
_WIN32 = sys.platform == 'win32' |
| 32 |
_LINUX = sys.platform.startswith("linux") |
| 33 |
if _WIN32: |
| 34 |
from rpython.rlib import rwin32 |
| 35 |
|
| 36 |
c_int = "c_int" |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
c_uid_t = 'c_uid_t' |
| 45 |
|
| 46 |
|
| 47 |
c_gid_t = 'c_uid_t' |
| 48 |
|
| 49 |
def wrap_uid(space, uid): |
| 50 |
if uid <= r_uint(sys.maxint): |
| 51 |
return space.newint(intmask(uid)) |
| 52 |
else: |
| 53 |
return space.newint(uid) |
| 54 |
wrap_gid = wrap_uid |
| 55 |
|
| 56 |
class FileEncoder(object): |
| 57 |
is_unicode = True |
| 58 |
|
| 59 |
def __init__(self, space, w_obj): |
| 60 |
self.space = space |
| 61 |
self.w_obj = w_obj |
| 62 |
|
| 63 |
def as_bytes(self): |
| 64 |
return self.space.fsencode_w(self.w_obj) |
| 65 |
|
| 66 |
def as_utf8(self): |
| 67 |
ret = self.space.fsencode_w(self.w_obj) |
| 68 |
if '\x00' in ret: |
| 69 |
raise oefmt(self.space.w_ValueError, "embedded null character") |
| 70 |
return ret |
| 71 |
|
| 72 |
class FileDecoder(object): |
| 73 |
is_unicode = False |
| 74 |
|
| 75 |
def __init__(self, space, w_obj): |
| 76 |
self.space = space |
| 77 |
self.w_obj = w_obj |
| 78 |
|
| 79 |
def as_bytes(self): |
| 80 |
return self.space.fsencode_w(self.w_obj) |
| 81 |
|
| 82 |
def as_utf8(self): |
| 83 |
ret = self.space.fsdecode_w(self.w_obj) |
| 84 |
if '\x00' in ret: |
| 85 |
raise oefmt(self.space.w_ValueError, "embedded null character") |
| 86 |
return ret |
| 87 |
|
| 88 |
@specialize.memo() |
| 89 |
def make_dispatch_function(func, tag, allow_fd_fn=None): |
| 90 |
@specialize.argtype(1) |
| 91 |
def dispatch(space, w_fname, *args): |
| 92 |
if allow_fd_fn is not None: |
| 93 |
try: |
| 94 |
fd = space.c_int_w(w_fname) |
| 95 |
except OperationError: |
| 96 |
pass |
| 97 |
else: |
| 98 |
return allow_fd_fn(fd, *args) |
| 99 |
if space.isinstance_w(w_fname, space.w_unicode): |
| 100 |
fname = FileEncoder(space, w_fname) |
| 101 |
return func(fname, *args) |
| 102 |
else: |
| 103 |
fname = FileDecoder(space, w_fname) |
| 104 |
return func(fname, *args) |
| 105 |
return dispatch |
| 106 |
|
| 107 |
@specialize.arg(0, 1) |
| 108 |
def dispatch_filename(func, tag=0, allow_fd_fn=None): |
| 109 |
return make_dispatch_function(func, tag, allow_fd_fn) |
| 110 |
|
| 111 |
@specialize.memo() |
| 112 |
def dispatch_filename_2(func): |
| 113 |
def dispatch(space, w_fname1, w_fname2, *args): |
| 114 |
if space.isinstance_w(w_fname1, space.w_unicode): |
| 115 |
fname1 = FileEncoder(space, w_fname1) |
| 116 |
if space.isinstance_w(w_fname2, space.w_unicode): |
| 117 |
fname2 = FileEncoder(space, w_fname2) |
| 118 |
return func(fname1, fname2, *args) |
| 119 |
else: |
| 120 |
fname2 = FileDecoder(space, w_fname2) |
| 121 |
return func(fname1, fname2, *args) |
| 122 |
else: |
| 123 |
fname1 = FileDecoder(space, w_fname1) |
| 124 |
if space.isinstance_w(w_fname2, space.w_unicode): |
| 125 |
fname2 = FileEncoder(space, w_fname2) |
| 126 |
return func(fname1, fname2, *args) |
| 127 |
else: |
| 128 |
fname2 = FileDecoder(space, w_fname2) |
| 129 |
return func(fname1, fname2, *args) |
| 130 |
return dispatch |
| 131 |
|
| 132 |
@specialize.arg(0) |
| 133 |
def call_rposix(func, path, *args): |
| 134 |
"""Call a function that takes a filesystem path as its first argument""" |
| 135 |
path_b = path.as_bytes |
| 136 |
assert path_b is not None |
| 137 |
return func(path_b, *args) |
| 138 |
|
| 139 |
|
| 140 |
class Path(object): |
| 141 |
_immutable_fields_ = ['as_fd', 'as_bytes', 'as_unicode', 'w_path'] |
| 142 |
|
| 143 |
def __init__(self, fd, bytes, unicode, w_path): |
| 144 |
self.as_fd = fd |
| 145 |
self.as_bytes = bytes |
| 146 |
self.as_unicode = unicode |
| 147 |
self.w_path = w_path |
| 148 |
|
| 149 |
def __repr__(self): |
| 150 |
|
| 151 |
return ''.join(['Path(', str(self.as_fd), ', ', str(self.as_bytes), |
| 152 |
', ', str(self.as_unicode), ', [', str(self.w_path), |
| 153 |
', ', str(getattr(self.w_path, '_length', 'bytes')), '])']) |
| 154 |
|
| 155 |
def _path_from_unicode(space, w_value): |
| 156 |
if _WIN32: |
| 157 |
path_b = space.bytes0_w(space.fsencode(w_value)) |
| 158 |
path_u = space.realunicode_w(w_value) |
| 159 |
return Path(-1, path_b, path_u, w_value) |
| 160 |
else: |
| 161 |
path_b = space.bytes0_w(space.fsencode(w_value)) |
| 162 |
return Path(-1, path_b, None, w_value) |
| 163 |
|
| 164 |
def _path_from_bytes(space, w_value): |
| 165 |
path_b = space.bytes0_w(w_value) |
| 166 |
return Path(-1, path_b, None, w_value) |
| 167 |
|
| 168 |
@specialize.arg(2, 3) |
| 169 |
def _unwrap_path(space, w_value, allow_fd=True, nullable=False): |
| 170 |
|
| 171 |
if nullable: |
| 172 |
if allow_fd: |
| 173 |
allowed_types = "string, bytes, os.PathLike, integer or None" |
| 174 |
else: |
| 175 |
allowed_types = "string, bytes, os.PathLike or None" |
| 176 |
else: |
| 177 |
if allow_fd: |
| 178 |
allowed_types = "string, bytes, os.PathLike or integer" |
| 179 |
else: |
| 180 |
allowed_types = "string, bytes or os.PathLike" |
| 181 |
if nullable and space.is_w(w_value, space.w_None): |
| 182 |
return Path(-1, '.', None, space.w_None) |
| 183 |
if space.isinstance_w(w_value, space.w_unicode): |
| 184 |
return _path_from_unicode(space, w_value) |
| 185 |
elif space.isinstance_w(w_value, space.w_bytes): |
| 186 |
return _path_from_bytes(space, w_value) |
| 187 |
|
| 188 |
|
| 189 |
try: |
| 190 |
space._try_buffer_w(w_value, space.BUF_FULL_RO) |
| 191 |
except BufferInterfaceNotFound: |
| 192 |
pass |
| 193 |
else: |
| 194 |
tp = space.type(w_value).name |
| 195 |
space.warn(space.newtext( |
| 196 |
"path should be %s, not %s" % (allowed_types, tp,)), |
| 197 |
space.w_DeprecationWarning) |
| 198 |
path_b = space.bytesbuf0_w(w_value) |
| 199 |
return Path(-1, path_b, None, w_value) |
| 200 |
|
| 201 |
|
| 202 |
if allow_fd: |
| 203 |
try: |
| 204 |
space.index(w_value) |
| 205 |
except OperationError: |
| 206 |
pass |
| 207 |
else: |
| 208 |
fd = unwrap_fd(space, w_value, allowed_types) |
| 209 |
return Path(fd, None, None, w_value) |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
w_fspath_method = space.lookup(w_value, '__fspath__') |
| 214 |
if w_fspath_method: |
| 215 |
w_result = space.get_and_call_function(w_fspath_method, w_value) |
| 216 |
if space.isinstance_w(w_result, space.w_unicode): |
| 217 |
return _path_from_unicode(space, w_result) |
| 218 |
elif space.isinstance_w(w_result, space.w_bytes): |
| 219 |
return _path_from_bytes(space, w_result) |
| 220 |
raise oefmt(space.w_TypeError, |
| 221 |
"expected %S.__fspath__() to return str or bytes, not %T", |
| 222 |
w_value, w_result) |
| 223 |
|
| 224 |
raise oefmt(space.w_TypeError, |
| 225 |
'expected %T.__fspath__() to return str or bytes, not %T', |
| 226 |
w_value, |
| 227 |
w_result |
| 228 |
) |
| 229 |
raise oefmt( |
| 230 |
space.w_TypeError, "path should be %s, not %T", allowed_types, w_value) |
| 231 |
|
| 232 |
class _PathOrFd(Unwrapper): |
| 233 |
def unwrap(self, space, w_value): |
| 234 |
return _unwrap_path(space, w_value, allow_fd=True) |
| 235 |
|
| 236 |
class _JustPath(Unwrapper): |
| 237 |
def unwrap(self, space, w_value): |
| 238 |
return _unwrap_path(space, w_value, allow_fd=False) |
| 239 |
|
| 240 |
class _NullablePathOrFd(Unwrapper): |
| 241 |
def unwrap(self, space, w_value): |
| 242 |
return _unwrap_path(space, w_value, allow_fd=True, nullable=True) |
| 243 |
|
| 244 |
class _NullablePath(Unwrapper): |
| 245 |
def unwrap(self, space, w_value): |
| 246 |
return _unwrap_path(space, w_value, allow_fd=False, nullable=True) |
| 247 |
|
| 248 |
|
| 249 |
def path_or_fd(allow_fd=True, nullable=False): |
| 250 |
if nullable: |
| 251 |
return _NullablePathOrFd if allow_fd else _NullablePath |
| 252 |
else: |
| 253 |
return _PathOrFd if allow_fd else _JustPath |
| 254 |
|
| 255 |
_HAVE_AT_FDCWD = getattr(rposix, 'AT_FDCWD', None) is not None |
| 256 |
DEFAULT_DIR_FD = rposix.AT_FDCWD if _HAVE_AT_FDCWD else -100 |
| 257 |
DIR_FD_AVAILABLE = False |
| 258 |
|
| 259 |
@specialize.arg(2) |
| 260 |
def unwrap_fd(space, w_value, allowed_types='integer'): |
| 261 |
try: |
| 262 |
result = space.c_int_w(w_value) |
| 263 |
except OperationError as e: |
| 264 |
if not e.match(space, space.w_OverflowError): |
| 265 |
raise oefmt(space.w_TypeError, |
| 266 |
"argument should be %s, not %T", allowed_types, w_value) |
| 267 |
else: |
| 268 |
raise |
| 269 |
if result == -1: |
| 270 |
|
| 271 |
raise oefmt(space.w_OSError, "invalid file descriptor: -1") |
| 272 |
return result |
| 273 |
|
| 274 |
def _unwrap_dirfd(space, w_value): |
| 275 |
if space.is_none(w_value): |
| 276 |
return DEFAULT_DIR_FD |
| 277 |
else: |
| 278 |
return unwrap_fd(space, w_value, allowed_types="integer or None") |
| 279 |
|
| 280 |
class _DirFD(Unwrapper): |
| 281 |
text_default = None |
| 282 |
def unwrap(self, space, w_value): |
| 283 |
return _unwrap_dirfd(space, w_value) |
| 284 |
|
| 285 |
class _DirFD_Unavailable(Unwrapper): |
| 286 |
text_default = None |
| 287 |
def unwrap(self, space, w_value): |
| 288 |
dir_fd = _unwrap_dirfd(space, w_value) |
| 289 |
if dir_fd == DEFAULT_DIR_FD: |
| 290 |
return dir_fd |
| 291 |
raise oefmt(space.w_NotImplementedError, |
| 292 |
"dir_fd unavailable on this platform") |
| 293 |
|
| 294 |
def DirFD(available=False): |
| 295 |
return _DirFD if available else _DirFD_Unavailable |
| 296 |
|
| 297 |
@specialize.arg(1, 2) |
| 298 |
def argument_unavailable(space, funcname, arg): |
| 299 |
return oefmt( |
| 300 |
space.w_NotImplementedError, |
| 301 |
"%s: %s unavailable on this platform", funcname, arg) |
| 302 |
|
| 303 |
_open_inhcache = rposix.SetNonInheritableCache() |
| 304 |
|
| 305 |
def u2utf8(space, u_str): |
| 306 |
return space.newutf8(u_str.encode('utf-8'), len(u_str)) |
| 307 |
|
| 308 |
@unwrap_spec(flags=c_int, mode=c_int, dir_fd=DirFD(rposix.HAVE_OPENAT)) |
| 309 |
def open(space, w_path, flags, mode=0o777, |
| 310 |
__kwonly__=None, dir_fd=DEFAULT_DIR_FD): |
| 311 |
"""open(path, flags, mode=0o777, *, dir_fd=None) |
| 312 |
|
| 313 |
Open a file for low level IO. Returns a file handle (integer). |
| 314 |
|
| 315 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 316 |
and path should be relative; path will then be relative to that directory. |
| 317 |
dir_fd may not be implemented on your platform. |
| 318 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 319 |
|
| 320 |
if rposix.O_CLOEXEC is not None: |
| 321 |
flags |= rposix.O_CLOEXEC |
| 322 |
space.audit("open", [w_path, space.w_None, space.newint(flags)]) |
| 323 |
while True: |
| 324 |
try: |
| 325 |
if rposix.HAVE_OPENAT and dir_fd != DEFAULT_DIR_FD: |
| 326 |
path = space.fsencode_w(w_path) |
| 327 |
fd = rposix.openat(path, flags, mode, dir_fd) |
| 328 |
else: |
| 329 |
fd = dispatch_filename(rposix.open)(space, w_path, flags, mode) |
| 330 |
break |
| 331 |
except OSError as e: |
| 332 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 333 |
try: |
| 334 |
_open_inhcache.set_non_inheritable(fd) |
| 335 |
except OSError as e: |
| 336 |
rposix.c_close(fd) |
| 337 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 338 |
return space.newint(fd) |
| 339 |
|
| 340 |
@unwrap_spec(fd=c_int, position=r_longlong, how=c_int) |
| 341 |
def lseek(space, fd, position, how): |
| 342 |
"""Set the current position of a file descriptor. Return the new position. |
| 343 |
If how == 0, 'position' is relative to the start of the file; if how == 1, to |
| 344 |
the current position; if how == 2, to the end.""" |
| 345 |
try: |
| 346 |
pos = os.lseek(fd, position, how) |
| 347 |
except OSError as e: |
| 348 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 349 |
else: |
| 350 |
return space.newint(pos) |
| 351 |
|
| 352 |
@unwrap_spec(fd=c_int) |
| 353 |
def isatty(space, fd): |
| 354 |
"""Return True if 'fd' is an open file descriptor connected to the |
| 355 |
slave end of a terminal.""" |
| 356 |
try: |
| 357 |
res = os.isatty(fd) |
| 358 |
except OSError as e: |
| 359 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 360 |
else: |
| 361 |
return space.newbool(res) |
| 362 |
|
| 363 |
@unwrap_spec(fd=c_int, length=int) |
| 364 |
def read(space, fd, length): |
| 365 |
"""Read data from a file descriptor.""" |
| 366 |
while True: |
| 367 |
try: |
| 368 |
s = os.read(fd, length) |
| 369 |
except OSError as e: |
| 370 |
wrap_oserror(space, e, eintr_retry=True) |
| 371 |
else: |
| 372 |
return space.newbytes(s) |
| 373 |
|
| 374 |
@unwrap_spec(fd=c_int) |
| 375 |
def write(space, fd, w_data): |
| 376 |
"""Write a string to a file descriptor. Return the number of bytes |
| 377 |
actually written, which may be smaller than len(data).""" |
| 378 |
data = space.charbuf_w(w_data) |
| 379 |
while True: |
| 380 |
try: |
| 381 |
res = os.write(fd, data) |
| 382 |
except OSError as e: |
| 383 |
wrap_oserror(space, e, eintr_retry=True) |
| 384 |
else: |
| 385 |
return space.newint(res) |
| 386 |
|
| 387 |
@unwrap_spec(fd=c_int) |
| 388 |
def close(space, fd): |
| 389 |
"""Close a file descriptor (for low level IO).""" |
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
try: |
| 395 |
os.close(fd) |
| 396 |
except OSError as e: |
| 397 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 398 |
|
| 399 |
@unwrap_spec(fd_low=c_int, fd_high=c_int) |
| 400 |
def closerange(fd_low, fd_high): |
| 401 |
"""Closes all file descriptors in [fd_low, fd_high), ignoring errors.""" |
| 402 |
rposix.closerange(fd_low, fd_high) |
| 403 |
|
| 404 |
@unwrap_spec(fd=c_int, length=r_longlong) |
| 405 |
def ftruncate(space, fd, length): |
| 406 |
"""Truncate a file (by file descriptor) to a specified length.""" |
| 407 |
while True: |
| 408 |
try: |
| 409 |
rposix.ftruncate(fd, length) |
| 410 |
break |
| 411 |
except OSError as e: |
| 412 |
wrap_oserror(space, e, eintr_retry=True) |
| 413 |
|
| 414 |
def truncate(space, w_path, w_length): |
| 415 |
"""Truncate a file to a specified length.""" |
| 416 |
allocated_fd = False |
| 417 |
fd = -1 |
| 418 |
try: |
| 419 |
if space.isinstance_w(w_path, space.w_int): |
| 420 |
w_fd = w_path |
| 421 |
else: |
| 422 |
w_fd = open(space, w_path, os.O_WRONLY) |
| 423 |
allocated_fd = True |
| 424 |
|
| 425 |
fd = space.c_filedescriptor_w(w_fd) |
| 426 |
length = space.int_w(w_length) |
| 427 |
return ftruncate(space, fd, length) |
| 428 |
|
| 429 |
finally: |
| 430 |
if allocated_fd and fd != -1: |
| 431 |
close(space, fd) |
| 432 |
|
| 433 |
def fsync(space, w_fd): |
| 434 |
"""Force write of file with filedescriptor to disk.""" |
| 435 |
fd = space.c_filedescriptor_w(w_fd) |
| 436 |
while True: |
| 437 |
try: |
| 438 |
os.fsync(fd) |
| 439 |
break |
| 440 |
except OSError as e: |
| 441 |
wrap_oserror(space, e, eintr_retry=True) |
| 442 |
|
| 443 |
def fdatasync(space, w_fd): |
| 444 |
"""Force write of file with filedescriptor to disk. |
| 445 |
Does not force update of metadata.""" |
| 446 |
fd = space.c_filedescriptor_w(w_fd) |
| 447 |
while True: |
| 448 |
try: |
| 449 |
os.fdatasync(fd) |
| 450 |
break |
| 451 |
except OSError as e: |
| 452 |
wrap_oserror(space, e, eintr_retry=True) |
| 453 |
|
| 454 |
def sync(space): |
| 455 |
"""Force write of everything to disk.""" |
| 456 |
rposix.sync() |
| 457 |
|
| 458 |
def fchdir(space, w_fd): |
| 459 |
"""Change to the directory of the given file descriptor. fildes must be |
| 460 |
opened on a directory, not a file.""" |
| 461 |
fd = space.c_filedescriptor_w(w_fd) |
| 462 |
while True: |
| 463 |
try: |
| 464 |
os.fchdir(fd) |
| 465 |
break |
| 466 |
except OSError as e: |
| 467 |
wrap_oserror(space, e, eintr_retry=True) |
| 468 |
|
| 469 |
@unwrap_spec(fd=c_int, length=int, offset=r_longlong) |
| 470 |
def pread(space, fd, length, offset): |
| 471 |
"""Read a string to a file descriptor at a given offset. |
| 472 |
""" |
| 473 |
while True: |
| 474 |
try: |
| 475 |
s = rposix.pread(fd, length, offset) |
| 476 |
except OSError as e: |
| 477 |
wrap_oserror(space, e, eintr_retry=True) |
| 478 |
else: |
| 479 |
return space.newbytes(s) |
| 480 |
|
| 481 |
@unwrap_spec(fd=c_int, offset=r_longlong) |
| 482 |
def pwrite(space, fd, w_data, offset): |
| 483 |
"""Write a string to a file descriptor at a given offset. |
| 484 |
""" |
| 485 |
data = space.charbuf_w(w_data) |
| 486 |
while True: |
| 487 |
try: |
| 488 |
res = rposix.pwrite(fd, data, offset) |
| 489 |
except OSError as e: |
| 490 |
wrap_oserror(space, e, eintr_retry=True) |
| 491 |
else: |
| 492 |
return space.newint(res) |
| 493 |
|
| 494 |
@unwrap_spec(fd=c_int, length=r_longlong, offset=r_longlong) |
| 495 |
def posix_fallocate(space, fd, offset, length): |
| 496 |
"""allocate file space . |
| 497 |
""" |
| 498 |
while True: |
| 499 |
try: |
| 500 |
s = rposix.posix_fallocate(fd, offset, length) |
| 501 |
except OSError as e: |
| 502 |
wrap_oserror(space, e, eintr_retry=True) |
| 503 |
else: |
| 504 |
return space.newint(s) |
| 505 |
|
| 506 |
@unwrap_spec(fd=c_int, offset=r_longlong, length=r_longlong, advice=int) |
| 507 |
def posix_fadvise(space, fd, offset, length, advice): |
| 508 |
"""predeclare an access pattern for file data . |
| 509 |
""" |
| 510 |
while True: |
| 511 |
try: |
| 512 |
rposix.posix_fadvise(fd, offset, length, advice) |
| 513 |
except OSError as e: |
| 514 |
wrap_oserror(space, e, eintr_retry=True) |
| 515 |
else: |
| 516 |
return |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
STAT_FIELDS = unrolling_iterable(enumerate(rposix_stat.STAT_FIELDS)) |
| 521 |
|
| 522 |
STATVFS_FIELDS = unrolling_iterable(enumerate(rposix_stat.STATVFS_FIELDS)) |
| 523 |
|
| 524 |
def build_stat_result(space, st): |
| 525 |
FIELDS = STAT_FIELDS |
| 526 |
lst = [None] * rposix_stat.N_INDEXABLE_FIELDS |
| 527 |
stat_float_times = space.fromcache(StatState).stat_float_times |
| 528 |
for i, (name, TYPE) in FIELDS: |
| 529 |
if i < rposix_stat.N_INDEXABLE_FIELDS: |
| 530 |
|
| 531 |
|
| 532 |
w_value = space.newint(st[i]) |
| 533 |
lst[i] = w_value |
| 534 |
else: |
| 535 |
break |
| 536 |
w_tuple = space.newtuple(lst) |
| 537 |
w_stat_result = space.getattr(space.getbuiltinmodule(os.name), |
| 538 |
space.newtext('stat_result')) |
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
w_tup_new = space.getattr(space.w_tuple, |
| 544 |
space.newtext('__new__')) |
| 545 |
|
| 546 |
w_result = space.call_function(w_tup_new, w_stat_result, w_tuple) |
| 547 |
for i, (name, TYPE) in FIELDS: |
| 548 |
if i < rposix_stat.N_INDEXABLE_FIELDS: |
| 549 |
continue |
| 550 |
else: |
| 551 |
try: |
| 552 |
value = getattr(st, name) |
| 553 |
except AttributeError: |
| 554 |
if name.startswith('nsec_'): |
| 555 |
|
| 556 |
value = rposix_stat.get_stat_ns_as_bigint(st, name[5:]) |
| 557 |
value = value.tolong() % 1000000000 |
| 558 |
elif name.startswith('st_'): |
| 559 |
|
| 560 |
pass |
| 561 |
else: |
| 562 |
assert False |
| 563 |
w_value = space.newint(value) |
| 564 |
w_result.setdictvalue(space, name, w_value) |
| 565 |
|
| 566 |
|
| 567 |
w_result.setdictvalue(space, 'st_atime_ns', |
| 568 |
space.newlong_from_rbigint( |
| 569 |
rposix_stat.get_stat_ns_as_bigint(st, 'atime'))) |
| 570 |
w_result.setdictvalue(space, 'st_mtime_ns', |
| 571 |
space.newlong_from_rbigint( |
| 572 |
rposix_stat.get_stat_ns_as_bigint(st, 'mtime'))) |
| 573 |
w_result.setdictvalue(space, 'st_ctime_ns', |
| 574 |
space.newlong_from_rbigint( |
| 575 |
rposix_stat.get_stat_ns_as_bigint(st, 'ctime'))) |
| 576 |
|
| 577 |
|
| 578 |
if stat_float_times: |
| 579 |
w_result.setdictvalue(space, 'st_atime', space.newfloat(st.st_atime)) |
| 580 |
w_result.setdictvalue(space, 'st_mtime', space.newfloat(st.st_mtime)) |
| 581 |
w_result.setdictvalue(space, 'st_ctime', space.newfloat(st.st_ctime)) |
| 582 |
else: |
| 583 |
w_result.setdictvalue(space, 'st_atime', space.newint(st[7])) |
| 584 |
w_result.setdictvalue(space, 'st_mtime', space.newint(st[8])) |
| 585 |
w_result.setdictvalue(space, 'st_ctime', space.newint(st[9])) |
| 586 |
return w_result |
| 587 |
|
| 588 |
def build_statvfs_result(space, st): |
| 589 |
vals_w = [None] * len(rposix_stat.STATVFS_FIELDS) |
| 590 |
for i, (name, _) in STATVFS_FIELDS: |
| 591 |
vals_w[i] = space.newint(getattr(st, name)) |
| 592 |
w_tuple = space.newtuple(vals_w) |
| 593 |
w_statvfs_result = space.getattr( |
| 594 |
space.getbuiltinmodule(os.name), space.newtext('statvfs_result')) |
| 595 |
return space.call_function(w_statvfs_result, w_tuple) |
| 596 |
|
| 597 |
|
| 598 |
@unwrap_spec(fd=c_int) |
| 599 |
def fstat(space, fd): |
| 600 |
"""Perform a stat system call on the file referenced to by an open |
| 601 |
file descriptor.""" |
| 602 |
while True: |
| 603 |
try: |
| 604 |
st = rposix_stat.fstat(fd) |
| 605 |
except OSError as e: |
| 606 |
wrap_oserror(space, e, eintr_retry=True) |
| 607 |
else: |
| 608 |
return build_stat_result(space, st) |
| 609 |
|
| 610 |
@unwrap_spec( |
| 611 |
path=path_or_fd(allow_fd=True), |
| 612 |
dir_fd=DirFD(rposix.HAVE_FSTATAT), |
| 613 |
follow_symlinks=bool) |
| 614 |
def stat(space, path, __kwonly__, dir_fd=DEFAULT_DIR_FD, follow_symlinks=True): |
| 615 |
"""stat(path, *, dir_fd=None, follow_symlinks=True) -> stat result |
| 616 |
|
| 617 |
Perform a stat system call on the given path. |
| 618 |
|
| 619 |
path may be specified as either a string or as an open file descriptor. |
| 620 |
|
| 621 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 622 |
and path should be relative; path will then be relative to that directory. |
| 623 |
dir_fd may not be supported on your platform; if it is unavailable, using |
| 624 |
it will raise a NotImplementedError. |
| 625 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 626 |
link, stat will examine the symbolic link itself instead of the file the |
| 627 |
link points to. |
| 628 |
It is an error to use dir_fd or follow_symlinks when specifying path as |
| 629 |
an open file descriptor.""" |
| 630 |
return do_stat(space, "stat", path, dir_fd, follow_symlinks) |
| 631 |
|
| 632 |
@specialize.arg(1) |
| 633 |
def do_stat(space, funcname, path, dir_fd, follow_symlinks): |
| 634 |
"""Common implementation for stat() and lstat()""" |
| 635 |
try: |
| 636 |
if path.as_fd != -1: |
| 637 |
if dir_fd != DEFAULT_DIR_FD: |
| 638 |
raise oefmt(space.w_ValueError, |
| 639 |
"%s: can't specify both dir_fd and fd", funcname) |
| 640 |
if not follow_symlinks: |
| 641 |
raise oefmt(space.w_ValueError, |
| 642 |
"%s: cannot use fd and follow_symlinks together", funcname) |
| 643 |
st = rposix_stat.fstat(path.as_fd) |
| 644 |
elif follow_symlinks and dir_fd == DEFAULT_DIR_FD: |
| 645 |
st = call_rposix(rposix_stat.stat3, path) |
| 646 |
elif not follow_symlinks and dir_fd == DEFAULT_DIR_FD: |
| 647 |
st = call_rposix(rposix_stat.lstat3, path) |
| 648 |
elif rposix.HAVE_FSTATAT: |
| 649 |
st = call_rposix(rposix_stat.fstatat, path, dir_fd, follow_symlinks) |
| 650 |
else: |
| 651 |
raise oefmt(space.w_NotImplementedError, |
| 652 |
"%s: unsupported argument combination", funcname) |
| 653 |
except OSError as e: |
| 654 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 655 |
else: |
| 656 |
return build_stat_result(space, st) |
| 657 |
|
| 658 |
@unwrap_spec( |
| 659 |
path=path_or_fd(allow_fd=False), |
| 660 |
dir_fd=DirFD(rposix.HAVE_FSTATAT)) |
| 661 |
def lstat(space, path, __kwonly__, dir_fd=DEFAULT_DIR_FD): |
| 662 |
"""lstat(path, *, dir_fd=None) -> stat result |
| 663 |
|
| 664 |
Like stat(), but do not follow symbolic links. |
| 665 |
Equivalent to stat(path, follow_symlinks=False).""" |
| 666 |
return do_stat(space, "lstat", path, dir_fd, False) |
| 667 |
|
| 668 |
class StatState(object): |
| 669 |
_immutable_fields_ = ["stat_float_times?"] |
| 670 |
|
| 671 |
def __init__(self, space): |
| 672 |
self.stat_float_times = True |
| 673 |
|
| 674 |
@unwrap_spec(newval=int) |
| 675 |
def stat_float_times(space, newval=-1): |
| 676 |
"""stat_float_times([newval]) -> oldval |
| 677 |
|
| 678 |
Determine whether os.[lf]stat represents time stamps as float objects. |
| 679 |
If newval is True, future calls to stat() return floats, if it is False, |
| 680 |
future calls return ints. |
| 681 |
If newval is omitted, return the current setting. |
| 682 |
""" |
| 683 |
space.warn(space.newtext("stat_float_times() is deprecated"), |
| 684 |
space.w_DeprecationWarning) |
| 685 |
state = space.fromcache(StatState) |
| 686 |
|
| 687 |
if newval == -1: |
| 688 |
return space.newbool(state.stat_float_times) |
| 689 |
else: |
| 690 |
state.stat_float_times = (newval != 0) |
| 691 |
|
| 692 |
|
| 693 |
@unwrap_spec(fd=c_int) |
| 694 |
def fstatvfs(space, fd): |
| 695 |
while True: |
| 696 |
try: |
| 697 |
st = rposix_stat.fstatvfs(fd) |
| 698 |
except OSError as e: |
| 699 |
wrap_oserror(space, e, eintr_retry=True) |
| 700 |
else: |
| 701 |
return build_statvfs_result(space, st) |
| 702 |
|
| 703 |
|
| 704 |
def statvfs(space, w_path): |
| 705 |
"""statvfs(path) |
| 706 |
|
| 707 |
Perform a statvfs system call on the given path. |
| 708 |
|
| 709 |
path may always be specified as a string. |
| 710 |
On some platforms, path may also be specified as an open file descriptor. |
| 711 |
If this functionality is unavailable, using it raises an exception.""" |
| 712 |
try: |
| 713 |
st = dispatch_filename( |
| 714 |
rposix_stat.statvfs, |
| 715 |
allow_fd_fn=rposix_stat.fstatvfs)(space, w_path) |
| 716 |
except OSError as e: |
| 717 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 718 |
else: |
| 719 |
return build_statvfs_result(space, st) |
| 720 |
|
| 721 |
|
| 722 |
@unwrap_spec(fd=c_int) |
| 723 |
def dup(space, fd): |
| 724 |
"""Create a copy of the file descriptor. Return the new file |
| 725 |
descriptor.""" |
| 726 |
try: |
| 727 |
newfd = rposix.dup(fd, inheritable=False) |
| 728 |
except OSError as e: |
| 729 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 730 |
else: |
| 731 |
return space.newint(newfd) |
| 732 |
|
| 733 |
@unwrap_spec(fd=c_int, fd2=c_int, inheritable=bool) |
| 734 |
def dup2(space, fd, fd2, inheritable=1): |
| 735 |
"""Duplicate a file descriptor.""" |
| 736 |
|
| 737 |
|
| 738 |
try: |
| 739 |
rposix.dup2(fd, fd2, inheritable) |
| 740 |
except OSError as e: |
| 741 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 742 |
return space.newint(fd2) |
| 743 |
|
| 744 |
@unwrap_spec(mode=c_int, |
| 745 |
dir_fd=DirFD(rposix.HAVE_FACCESSAT), effective_ids=bool, |
| 746 |
follow_symlinks=bool) |
| 747 |
def access(space, w_path, mode, __kwonly__, |
| 748 |
dir_fd=DEFAULT_DIR_FD, effective_ids=False, follow_symlinks=True): |
| 749 |
"""\ |
| 750 |
access(path, mode, *, dir_fd=None, effective_ids=False, follow_symlinks=True) |
| 751 |
|
| 752 |
Use the real uid/gid to test for access to a path. Returns True if granted, |
| 753 |
False otherwise. |
| 754 |
|
| 755 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 756 |
and path should be relative; path will then be relative to that directory. |
| 757 |
If effective_ids is True, access will use the effective uid/gid instead of |
| 758 |
the real uid/gid. |
| 759 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 760 |
link, access will examine the symbolic link itself instead of the file the |
| 761 |
link points to. |
| 762 |
dir_fd, effective_ids, and follow_symlinks may not be implemented |
| 763 |
on your platform. If they are unavailable, using them will raise a |
| 764 |
NotImplementedError. |
| 765 |
|
| 766 |
Note that most operations will use the effective uid/gid, therefore this |
| 767 |
routine can be used in a suid/sgid environment to test if the invoking user |
| 768 |
has the specified access to the path. |
| 769 |
The mode argument can be F_OK to test existence, or the inclusive-OR |
| 770 |
of R_OK, W_OK, and X_OK.""" |
| 771 |
if not rposix.HAVE_FACCESSAT: |
| 772 |
if not follow_symlinks: |
| 773 |
raise argument_unavailable(space, "access", "follow_symlinks") |
| 774 |
if effective_ids: |
| 775 |
raise argument_unavailable(space, "access", "effective_ids") |
| 776 |
|
| 777 |
try: |
| 778 |
if (rposix.HAVE_FACCESSAT and |
| 779 |
(dir_fd != DEFAULT_DIR_FD or not follow_symlinks or |
| 780 |
effective_ids)): |
| 781 |
path = space.fsencode_w(w_path) |
| 782 |
ok = rposix.faccessat(path, mode, |
| 783 |
dir_fd, effective_ids, follow_symlinks) |
| 784 |
else: |
| 785 |
ok = dispatch_filename(rposix.access)(space, w_path, mode) |
| 786 |
except OSError as e: |
| 787 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 788 |
else: |
| 789 |
return space.newbool(ok) |
| 790 |
|
| 791 |
|
| 792 |
def times(space): |
| 793 |
""" |
| 794 |
times() -> (utime, stime, cutime, cstime, elapsed_time) |
| 795 |
|
| 796 |
Return a tuple of floating point numbers indicating process times. |
| 797 |
""" |
| 798 |
try: |
| 799 |
times = os.times() |
| 800 |
except OSError as e: |
| 801 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 802 |
else: |
| 803 |
w_keywords = space.newdict() |
| 804 |
w_tuple = space.newtuple([space.newfloat(times[0]), |
| 805 |
space.newfloat(times[1]), |
| 806 |
space.newfloat(times[2]), |
| 807 |
space.newfloat(times[3]), |
| 808 |
space.newfloat(times[4])]) |
| 809 |
|
| 810 |
w_times_result = space.getattr(space.getbuiltinmodule(os.name), |
| 811 |
space.newtext('times_result')) |
| 812 |
return space.call_function(w_times_result, w_tuple, w_keywords) |
| 813 |
|
| 814 |
|
| 815 |
@unwrap_spec(command='fsencode') |
| 816 |
def system(space, command): |
| 817 |
"""Execute the command (a string) in a subshell.""" |
| 818 |
try: |
| 819 |
rc = os.system(command) |
| 820 |
except OSError as e: |
| 821 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 822 |
else: |
| 823 |
return space.newint(rc) |
| 824 |
|
| 825 |
@unwrap_spec(path=path_or_fd(allow_fd=False), |
| 826 |
dir_fd=DirFD(rposix.HAVE_UNLINKAT)) |
| 827 |
def unlink(space, path, __kwonly__, dir_fd=DEFAULT_DIR_FD): |
| 828 |
"""unlink(path, *, dir_fd=None) |
| 829 |
|
| 830 |
Remove a file (same as remove()). |
| 831 |
|
| 832 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 833 |
and path should be relative; path will then be relative to that directory. |
| 834 |
dir_fd may not be implemented on your platform. |
| 835 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 836 |
try: |
| 837 |
if rposix.HAVE_UNLINKAT and dir_fd != DEFAULT_DIR_FD: |
| 838 |
rposix.unlinkat(space.fsencode_w(path.w_path), |
| 839 |
dir_fd, removedir=False) |
| 840 |
else: |
| 841 |
dispatch_filename(rposix.unlink)(space, path.w_path) |
| 842 |
except OSError as e: |
| 843 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 844 |
|
| 845 |
|
| 846 |
@unwrap_spec(path=path_or_fd(allow_fd=False), |
| 847 |
dir_fd=DirFD(rposix.HAVE_UNLINKAT)) |
| 848 |
def remove(space, path, __kwonly__, dir_fd=DEFAULT_DIR_FD): |
| 849 |
"""remove(path, *, dir_fd=None) |
| 850 |
|
| 851 |
Remove a file (same as unlink()). |
| 852 |
|
| 853 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 854 |
and path should be relative; path will then be relative to that directory. |
| 855 |
dir_fd may not be implemented on your platform. |
| 856 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 857 |
try: |
| 858 |
if rposix.HAVE_UNLINKAT and dir_fd != DEFAULT_DIR_FD: |
| 859 |
rposix.unlinkat(space.fsencode_w(path.w_path), |
| 860 |
dir_fd, removedir=False) |
| 861 |
else: |
| 862 |
dispatch_filename(rposix.unlink)(space, path.w_path) |
| 863 |
except OSError as e: |
| 864 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 865 |
|
| 866 |
if _WIN32: |
| 867 |
def _getfullpathname(space, w_path): |
| 868 |
"""helper for ntpath.abspath """ |
| 869 |
path = space.fsencode_w(w_path) |
| 870 |
try: |
| 871 |
space._try_buffer_w(w_path, space.BUF_FULL_RO) |
| 872 |
except BufferInterfaceNotFound: |
| 873 |
w_obj = fspath(space, w_path) |
| 874 |
as_bytes = not space.isinstance_w(w_obj, space.w_text) |
| 875 |
else: |
| 876 |
as_bytes = True |
| 877 |
try: |
| 878 |
fullpath = rposix.getfullpathname(path) |
| 879 |
except OSError as e: |
| 880 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 881 |
if as_bytes: |
| 882 |
return space.newbytes(fullpath) |
| 883 |
else: |
| 884 |
ulen = codepoints_in_utf8(fullpath) |
| 885 |
return space.newtext(fullpath, ulen) |
| 886 |
|
| 887 |
def getcwdb(space): |
| 888 |
"""Return the current working directory.""" |
| 889 |
try: |
| 890 |
cur = os.getcwd() |
| 891 |
except OSError as e: |
| 892 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 893 |
else: |
| 894 |
return space.newbytes(cur) |
| 895 |
|
| 896 |
if _WIN32: |
| 897 |
def getcwd(space): |
| 898 |
"""Return the current working directory as a string.""" |
| 899 |
try: |
| 900 |
cur = os.getcwdu() |
| 901 |
except OSError as e: |
| 902 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 903 |
else: |
| 904 |
return u2utf8(space, cur) |
| 905 |
else: |
| 906 |
def getcwd(space): |
| 907 |
"""Return the current working directory as a string.""" |
| 908 |
return space.fsdecode(getcwdb(space)) |
| 909 |
|
| 910 |
def chdir(space, w_path): |
| 911 |
"""Change the current working directory to the specified path.""" |
| 912 |
try: |
| 913 |
if rposix.HAVE_FCHDIR: |
| 914 |
dispatch_filename(rposix.chdir, |
| 915 |
allow_fd_fn=os.fchdir)(space, w_path) |
| 916 |
else: |
| 917 |
dispatch_filename(rposix.chdir)(space, w_path) |
| 918 |
except OSError as e: |
| 919 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 920 |
|
| 921 |
@unwrap_spec(mode=c_int, dir_fd=DirFD(rposix.HAVE_MKDIRAT)) |
| 922 |
def mkdir(space, w_path, mode=0o777, __kwonly__=None, dir_fd=DEFAULT_DIR_FD): |
| 923 |
"""mkdir(path, mode=0o777, *, dir_fd=None) |
| 924 |
|
| 925 |
Create a directory. |
| 926 |
|
| 927 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 928 |
and path should be relative; path will then be relative to that directory. |
| 929 |
dir_fd may not be implemented on your platform. |
| 930 |
If it is unavailable, using it will raise a NotImplementedError. |
| 931 |
|
| 932 |
Any mode but 0700 is ignored on Windows.""" |
| 933 |
try: |
| 934 |
if rposix.HAVE_MKDIRAT and dir_fd != DEFAULT_DIR_FD: |
| 935 |
path = space.fsencode_w(w_path) |
| 936 |
rposix.mkdirat(path, mode, dir_fd) |
| 937 |
else: |
| 938 |
dispatch_filename(rposix.mkdir)(space, w_path, mode) |
| 939 |
except OSError as e: |
| 940 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 941 |
|
| 942 |
@unwrap_spec(dir_fd=DirFD(rposix.HAVE_UNLINKAT)) |
| 943 |
def rmdir(space, w_path, __kwonly__, dir_fd=DEFAULT_DIR_FD): |
| 944 |
"""rmdir(path, *, dir_fd=None) |
| 945 |
|
| 946 |
Remove a directory. |
| 947 |
|
| 948 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 949 |
and path should be relative; path will then be relative to that directory. |
| 950 |
dir_fd may not be implemented on your platform. |
| 951 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 952 |
try: |
| 953 |
if rposix.HAVE_UNLINKAT and dir_fd != DEFAULT_DIR_FD: |
| 954 |
path = space.fsencode_w(w_path) |
| 955 |
rposix.unlinkat(path, dir_fd, removedir=True) |
| 956 |
else: |
| 957 |
dispatch_filename(rposix.rmdir)(space, w_path) |
| 958 |
except OSError as e: |
| 959 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 960 |
|
| 961 |
@unwrap_spec(code=c_int) |
| 962 |
def strerror(space, code): |
| 963 |
"""Translate an error code to a message string.""" |
| 964 |
try: |
| 965 |
|
| 966 |
return space.newtext(*_strerror(code)) |
| 967 |
except ValueError: |
| 968 |
raise oefmt(space.w_ValueError, "strerror() argument out of range") |
| 969 |
|
| 970 |
def getlogin(space): |
| 971 |
"""Return the currently logged in user.""" |
| 972 |
try: |
| 973 |
if _WIN32: |
| 974 |
cur = rwin32.getlogin() |
| 975 |
else: |
| 976 |
cur = os.getlogin() |
| 977 |
except OSError as e: |
| 978 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 979 |
return space.newfilename(cur) |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
def getstatfields(space): |
| 985 |
|
| 986 |
|
| 987 |
return space.newlist([space.newtext(name) for _, (name, _) in STAT_FIELDS]) |
| 988 |
|
| 989 |
|
| 990 |
class State: |
| 991 |
def __init__(self, space): |
| 992 |
self.space = space |
| 993 |
self.w_environ = space.newdict() |
| 994 |
|
| 995 |
def startup(self, space): |
| 996 |
space.call_method(self.w_environ, 'clear') |
| 997 |
_convertenviron(space, self.w_environ) |
| 998 |
|
| 999 |
def _freeze_(self): |
| 1000 |
|
| 1001 |
self.space.call_method(self.w_environ, 'clear') |
| 1002 |
return True |
| 1003 |
|
| 1004 |
def get(space): |
| 1005 |
return space.fromcache(State) |
| 1006 |
|
| 1007 |
if _WIN32: |
| 1008 |
def _convertenviron(space, w_env): |
| 1009 |
|
| 1010 |
|
| 1011 |
rwin32._wgetenv("") |
| 1012 |
for key, value in rwin32._wenviron_items(): |
| 1013 |
space.setitem(w_env, space.newtext(key), space.newtext(value)) |
| 1014 |
|
| 1015 |
@unwrap_spec(name='text', value='text') |
| 1016 |
def putenv(space, name, value): |
| 1017 |
"""Change or add an environment variable.""" |
| 1018 |
|
| 1019 |
|
| 1020 |
if len(name) == 0 or '=' in name[1:]: |
| 1021 |
raise oefmt(space.w_ValueError, "illegal environment variable name") |
| 1022 |
|
| 1023 |
|
| 1024 |
if len(name) + len(value) + 2 > rwin32._MAX_ENV: |
| 1025 |
raise oefmt(space.w_ValueError, |
| 1026 |
"the environment variable is longer than %d " |
| 1027 |
"characters", rwin32._MAX_ENV) |
| 1028 |
|
| 1029 |
if '\x00' in name or '\x00' in value: |
| 1030 |
raise oefmt(space.w_ValueError, "embedded null character") |
| 1031 |
|
| 1032 |
try: |
| 1033 |
rwin32._wputenv(name, value) |
| 1034 |
except OSError as e: |
| 1035 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1036 |
|
| 1037 |
@unwrap_spec(name='text') |
| 1038 |
def unsetenv(space, name): |
| 1039 |
"""Change or add an environment variable.""" |
| 1040 |
|
| 1041 |
|
| 1042 |
if len(name) == 0 or '=' in name[1:]: |
| 1043 |
raise oefmt(space.w_ValueError, "illegal environment variable name") |
| 1044 |
|
| 1045 |
|
| 1046 |
if len(name) + 1 > rwin32._MAX_ENV: |
| 1047 |
raise oefmt(space.w_ValueError, |
| 1048 |
"the environment variable is longer than %d " |
| 1049 |
"characters", rwin32._MAX_ENV) |
| 1050 |
|
| 1051 |
try: |
| 1052 |
rwin32.DelEnvironmentVariableW(name) |
| 1053 |
except OSError as e: |
| 1054 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1055 |
|
| 1056 |
else: |
| 1057 |
def _convertenviron(space, w_env): |
| 1058 |
for key, value in os.environ.items(): |
| 1059 |
space.setitem(w_env, space.newbytes(key), space.newbytes(value)) |
| 1060 |
|
| 1061 |
def putenv(space, w_name, w_value): |
| 1062 |
"""Change or add an environment variable.""" |
| 1063 |
try: |
| 1064 |
dispatch_filename_2(putenv_impl)(space, w_name, w_value) |
| 1065 |
except OSError as e: |
| 1066 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1067 |
except ValueError: |
| 1068 |
raise oefmt(space.w_ValueError, |
| 1069 |
"illegal environment variable name") |
| 1070 |
|
| 1071 |
@specialize.argtype(0, 1) |
| 1072 |
def putenv_impl(name, value): |
| 1073 |
from rpython.rlib.rposix import _as_bytes |
| 1074 |
name = _as_bytes(name) |
| 1075 |
value = _as_bytes(value) |
| 1076 |
if len(name) == 0 or "=" in name: |
| 1077 |
raise ValueError |
| 1078 |
return rposix.putenv(name, value) |
| 1079 |
|
| 1080 |
def unsetenv(space, w_name): |
| 1081 |
"""Delete an environment variable.""" |
| 1082 |
try: |
| 1083 |
dispatch_filename(rposix.unsetenv)(space, w_name) |
| 1084 |
except KeyError: |
| 1085 |
pass |
| 1086 |
except OSError as e: |
| 1087 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1088 |
|
| 1089 |
|
| 1090 |
@unwrap_spec(path=path_or_fd(allow_fd=rposix.HAVE_FDOPENDIR, nullable=True)) |
| 1091 |
def listdir(space, path=None): |
| 1092 |
"""\ |
| 1093 |
Return a list containing the names of the files in the directory. |
| 1094 |
|
| 1095 |
path can be specified as either str, bytes, or a path-like object. If path is bytes, |
| 1096 |
the filenames returned will also be bytes; in all other circumstances |
| 1097 |
the filenames returned will be str. |
| 1098 |
If path is None, uses the path='.'. |
| 1099 |
On some platforms, path may also be specified as an open file descriptor;\ |
| 1100 |
the file descriptor must refer to a directory. |
| 1101 |
If this functionality is unavailable, using it raises NotImplementedError. |
| 1102 |
|
| 1103 |
The list is in arbitrary order. It does not include the special |
| 1104 |
entries '.' and '..' even if they are present in the directory.""" |
| 1105 |
|
| 1106 |
try: |
| 1107 |
space._try_buffer_w(path.w_path, space.BUF_FULL_RO) |
| 1108 |
except BufferInterfaceNotFound: |
| 1109 |
as_bytes = False |
| 1110 |
else: |
| 1111 |
as_bytes = True |
| 1112 |
if path.as_fd != -1: |
| 1113 |
if not rposix.HAVE_FDOPENDIR: |
| 1114 |
|
| 1115 |
raise oefmt(space.w_TypeError, |
| 1116 |
"listdir: illegal type for path argument") |
| 1117 |
try: |
| 1118 |
result = rposix.fdlistdir(rposix.dup(path.as_fd, inheritable=False)) |
| 1119 |
except OSError as e: |
| 1120 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1121 |
return space.newlist([space.newfilename(f) for f in result]) |
| 1122 |
elif as_bytes: |
| 1123 |
try: |
| 1124 |
path_b = path.as_bytes |
| 1125 |
assert path_b is not None |
| 1126 |
result = rposix.listdir(path_b) |
| 1127 |
except OSError as e: |
| 1128 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 1129 |
return space.newlist_bytes(result) |
| 1130 |
else: |
| 1131 |
|
| 1132 |
u = path.as_unicode |
| 1133 |
result_u = [] |
| 1134 |
result = [] |
| 1135 |
try: |
| 1136 |
path_b = path.as_bytes |
| 1137 |
if path_b is None: |
| 1138 |
|
| 1139 |
result = rposix.listdir('') |
| 1140 |
else: |
| 1141 |
result = rposix.listdir(path_b) |
| 1142 |
except OSError as e: |
| 1143 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 1144 |
len_result = len(result) |
| 1145 |
result_w = [None] * len_result |
| 1146 |
if u: |
| 1147 |
for i in range(len_result): |
| 1148 |
result_w[i] = space.newtext(result[i]) |
| 1149 |
elif _WIN32: |
| 1150 |
for i in range(len_result): |
| 1151 |
result_w[i] = space.newtext(result[i]) |
| 1152 |
return space.newlist(result_w) |
| 1153 |
else: |
| 1154 |
for i in range(len_result): |
| 1155 |
result_w[i] = space.newfilename(result[i]) |
| 1156 |
return space.newlist(result_w) |
| 1157 |
|
| 1158 |
@unwrap_spec(fd=c_int) |
| 1159 |
def get_inheritable(space, fd): |
| 1160 |
try: |
| 1161 |
return space.newbool(rposix.get_inheritable(fd)) |
| 1162 |
except OSError as e: |
| 1163 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1164 |
|
| 1165 |
@unwrap_spec(fd=c_int, inheritable=int) |
| 1166 |
def set_inheritable(space, fd, inheritable): |
| 1167 |
try: |
| 1168 |
rposix.set_inheritable(fd, inheritable) |
| 1169 |
except OSError as e: |
| 1170 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1171 |
|
| 1172 |
_pipe_inhcache = rposix.SetNonInheritableCache() |
| 1173 |
|
| 1174 |
def pipe(space): |
| 1175 |
"Create a pipe. Returns (read_end, write_end)." |
| 1176 |
try: |
| 1177 |
fd1, fd2 = rposix.pipe(rposix.O_CLOEXEC or 0) |
| 1178 |
except OSError as e: |
| 1179 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1180 |
try: |
| 1181 |
_pipe_inhcache.set_non_inheritable(fd1) |
| 1182 |
_pipe_inhcache.set_non_inheritable(fd2) |
| 1183 |
except OSError as e: |
| 1184 |
rposix.c_close(fd2) |
| 1185 |
rposix.c_close(fd1) |
| 1186 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1187 |
return space.newtuple2(space.newint(fd1), space.newint(fd2)) |
| 1188 |
|
| 1189 |
@unwrap_spec(flags=c_int) |
| 1190 |
def pipe2(space, flags): |
| 1191 |
try: |
| 1192 |
fd1, fd2 = rposix.pipe2(flags) |
| 1193 |
except OSError as e: |
| 1194 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1195 |
return space.newtuple([space.newint(fd1), space.newint(fd2)]) |
| 1196 |
|
| 1197 |
@unwrap_spec(mode=c_int, dir_fd=DirFD(rposix.HAVE_FCHMODAT), |
| 1198 |
follow_symlinks=bool) |
| 1199 |
def chmod(space, w_path, mode, __kwonly__, |
| 1200 |
dir_fd=DEFAULT_DIR_FD, follow_symlinks=True): |
| 1201 |
"""chmod(path, mode, *, dir_fd=None, follow_symlinks=True) |
| 1202 |
|
| 1203 |
Change the access permissions of a file. |
| 1204 |
|
| 1205 |
path may always be specified as a string. |
| 1206 |
On some platforms, path may also be specified as an open file descriptor. |
| 1207 |
If this functionality is unavailable, using it raises an exception. |
| 1208 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1209 |
and path should be relative; path will then be relative to that directory. |
| 1210 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 1211 |
link, chmod will modify the symbolic link itself instead of the file the |
| 1212 |
link points to. |
| 1213 |
It is an error to use dir_fd or follow_symlinks when specifying path as |
| 1214 |
an open file descriptor. |
| 1215 |
dir_fd and follow_symlinks may not be implemented on your platform. |
| 1216 |
If they are unavailable, using them will raise a NotImplementedError.""" |
| 1217 |
if not rposix.HAVE_FCHMODAT: |
| 1218 |
if not follow_symlinks: |
| 1219 |
raise argument_unavailable(space, "chmod", "follow_symlinks") |
| 1220 |
while True: |
| 1221 |
try: |
| 1222 |
dispatch_filename(rposix.chmod)(space, w_path, mode) |
| 1223 |
return |
| 1224 |
except OSError as e: |
| 1225 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 1226 |
|
| 1227 |
try: |
| 1228 |
path = space.fsencode_w(w_path) |
| 1229 |
except OperationError as operr: |
| 1230 |
if not space.isinstance_w(w_path, space.w_int): |
| 1231 |
raise oefmt(space.w_TypeError, |
| 1232 |
"argument should be string, bytes or integer, not %T", w_path) |
| 1233 |
fd = unwrap_fd(space, w_path) |
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
while True: |
| 1238 |
try: |
| 1239 |
os.fchmod(fd, mode) |
| 1240 |
return |
| 1241 |
except OSError as e: |
| 1242 |
wrap_oserror(space, e, eintr_retry=True) |
| 1243 |
while True: |
| 1244 |
try: |
| 1245 |
_chmod_path(path, mode, dir_fd, follow_symlinks) |
| 1246 |
break |
| 1247 |
except OSError as e: |
| 1248 |
if not follow_symlinks and e.errno in (ENOTSUP, EOPNOTSUPP): |
| 1249 |
|
| 1250 |
|
| 1251 |
raise argument_unavailable(space, "chmod", "follow_symlinks") |
| 1252 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 1253 |
|
| 1254 |
def _chmod_path(path, mode, dir_fd, follow_symlinks): |
| 1255 |
if dir_fd != DEFAULT_DIR_FD or not follow_symlinks: |
| 1256 |
rposix.fchmodat(path, mode, dir_fd, follow_symlinks) |
| 1257 |
else: |
| 1258 |
rposix.chmod(path, mode) |
| 1259 |
|
| 1260 |
@unwrap_spec(fd=c_int, mode=c_int) |
| 1261 |
def fchmod(space, fd, mode): |
| 1262 |
"""\ |
| 1263 |
Change the access permissions of the file given by file descriptor fd. |
| 1264 |
""" |
| 1265 |
while True: |
| 1266 |
try: |
| 1267 |
os.fchmod(fd, mode) |
| 1268 |
break |
| 1269 |
except OSError as e: |
| 1270 |
wrap_oserror(space, e, eintr_retry=True) |
| 1271 |
|
| 1272 |
@unwrap_spec(src_dir_fd=DirFD(rposix.HAVE_RENAMEAT), |
| 1273 |
dst_dir_fd=DirFD(rposix.HAVE_RENAMEAT)) |
| 1274 |
def rename(space, w_src, w_dst, __kwonly__, |
| 1275 |
src_dir_fd=DEFAULT_DIR_FD, dst_dir_fd=DEFAULT_DIR_FD): |
| 1276 |
"""rename(src, dst, *, src_dir_fd=None, dst_dir_fd=None) |
| 1277 |
|
| 1278 |
Rename a file or directory. |
| 1279 |
|
| 1280 |
If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 1281 |
descriptor open to a directory, and the respective path string (src or dst) |
| 1282 |
should be relative; the path will then be relative to that directory. |
| 1283 |
src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 1284 |
If they are unavailable, using them will raise a NotImplementedError.""" |
| 1285 |
try: |
| 1286 |
if (rposix.HAVE_RENAMEAT and |
| 1287 |
(src_dir_fd != DEFAULT_DIR_FD or dst_dir_fd != DEFAULT_DIR_FD)): |
| 1288 |
src = space.fsencode_w(w_src) |
| 1289 |
dst = space.fsencode_w(w_dst) |
| 1290 |
rposix.renameat(src, dst, src_dir_fd, dst_dir_fd) |
| 1291 |
else: |
| 1292 |
dispatch_filename_2(rposix.rename)(space, w_src, w_dst) |
| 1293 |
except OSError as e: |
| 1294 |
raise wrap_oserror2(space, e, w_filename=w_src, w_filename2=w_dst, |
| 1295 |
eintr_retry=False) |
| 1296 |
|
| 1297 |
@unwrap_spec(src_dir_fd=DirFD(rposix.HAVE_RENAMEAT), |
| 1298 |
dst_dir_fd=DirFD(rposix.HAVE_RENAMEAT)) |
| 1299 |
def replace(space, w_src, w_dst, __kwonly__, |
| 1300 |
src_dir_fd=DEFAULT_DIR_FD, dst_dir_fd=DEFAULT_DIR_FD): |
| 1301 |
"""replace(src, dst, *, src_dir_fd=None, dst_dir_fd=None) |
| 1302 |
|
| 1303 |
Rename a file or directory, overwriting the destination. |
| 1304 |
|
| 1305 |
If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 1306 |
descriptor open to a directory, and the respective path string (src or dst) |
| 1307 |
should be relative; the path will then be relative to that directory. |
| 1308 |
src_dir_fd and dst_dir_fd, may not be implemented on your platform. |
| 1309 |
If they are unavailable, using them will raise a NotImplementedError.""" |
| 1310 |
try: |
| 1311 |
if (rposix.HAVE_RENAMEAT and |
| 1312 |
(src_dir_fd != DEFAULT_DIR_FD or dst_dir_fd != DEFAULT_DIR_FD)): |
| 1313 |
src = space.fsencode_w(w_src) |
| 1314 |
dst = space.fsencode_w(w_dst) |
| 1315 |
rposix.renameat(src, dst, src_dir_fd, dst_dir_fd) |
| 1316 |
else: |
| 1317 |
dispatch_filename_2(rposix.replace)(space, w_src, w_dst) |
| 1318 |
except OSError as e: |
| 1319 |
raise wrap_oserror2(space, e, w_filename=w_src, w_filename2=w_dst, |
| 1320 |
eintr_retry=False) |
| 1321 |
|
| 1322 |
@unwrap_spec(mode=c_int, dir_fd=DirFD(rposix.HAVE_MKFIFOAT)) |
| 1323 |
def mkfifo(space, w_path, mode=0o666, __kwonly__=None, dir_fd=DEFAULT_DIR_FD): |
| 1324 |
"""mkfifo(path, mode=0o666, *, dir_fd=None) |
| 1325 |
|
| 1326 |
Create a FIFO (a POSIX named pipe). |
| 1327 |
|
| 1328 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1329 |
and path should be relative; path will then be relative to that directory. |
| 1330 |
dir_fd may not be implemented on your platform. |
| 1331 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 1332 |
|
| 1333 |
|
| 1334 |
while True: |
| 1335 |
try: |
| 1336 |
if rposix.HAVE_MKFIFOAT and dir_fd != DEFAULT_DIR_FD: |
| 1337 |
path = space.fsencode_w(w_path) |
| 1338 |
rposix.mkfifoat(path, mode, dir_fd) |
| 1339 |
else: |
| 1340 |
dispatch_filename(rposix.mkfifo)(space, w_path, mode) |
| 1341 |
break |
| 1342 |
except OSError as e: |
| 1343 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 1344 |
|
| 1345 |
@unwrap_spec(mode=c_int, device=c_int, dir_fd=DirFD(rposix.HAVE_MKNODAT)) |
| 1346 |
def mknod(space, w_path, mode=0600, device=0, |
| 1347 |
__kwonly__=None, dir_fd=DEFAULT_DIR_FD): |
| 1348 |
"""mknod(path, mode=0o600, device=0, *, dir_fd=None) |
| 1349 |
|
| 1350 |
Create a filesystem node (file, device special file or named pipe) |
| 1351 |
named 'path'. mode specifies both the permissions to use and the |
| 1352 |
type of node to be created, being combined (bitwise OR) with one of |
| 1353 |
S_IFREG, S_IFCHR, S_IFBLK, and S_IFIFO. For S_IFCHR and S_IFBLK, |
| 1354 |
device defines the newly created device special file (probably using |
| 1355 |
os.makedev()), otherwise it is ignored. |
| 1356 |
|
| 1357 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1358 |
and path should be relative; path will then be relative to that directory. |
| 1359 |
dir_fd may not be implemented on your platform. |
| 1360 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 1361 |
while True: |
| 1362 |
try: |
| 1363 |
if rposix.HAVE_MKNODAT and dir_fd != DEFAULT_DIR_FD: |
| 1364 |
fname = space.fsencode_w(w_path) |
| 1365 |
rposix.mknodat(fname, mode, device, dir_fd) |
| 1366 |
else: |
| 1367 |
dispatch_filename(rposix.mknod)(space, w_path, mode, device) |
| 1368 |
break |
| 1369 |
except OSError as e: |
| 1370 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 1371 |
|
| 1372 |
@unwrap_spec(mask=c_int) |
| 1373 |
def umask(space, mask): |
| 1374 |
"Set the current numeric umask and return the previous umask." |
| 1375 |
prevmask = os.umask(mask) |
| 1376 |
return space.newint(prevmask) |
| 1377 |
|
| 1378 |
def getpid(space): |
| 1379 |
"Return the current process id." |
| 1380 |
try: |
| 1381 |
pid = os.getpid() |
| 1382 |
except OSError as e: |
| 1383 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1384 |
return space.newint(pid) |
| 1385 |
|
| 1386 |
@unwrap_spec(pid=c_int, signal=c_int) |
| 1387 |
def kill(space, pid, signal): |
| 1388 |
"Kill a process with a signal." |
| 1389 |
try: |
| 1390 |
rposix.kill(pid, signal) |
| 1391 |
except OSError as e: |
| 1392 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1393 |
|
| 1394 |
@unwrap_spec(pgid=c_int, signal=c_int) |
| 1395 |
def killpg(space, pgid, signal): |
| 1396 |
"Kill a process group with a signal." |
| 1397 |
try: |
| 1398 |
os.killpg(pgid, signal) |
| 1399 |
except OSError as e: |
| 1400 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1401 |
|
| 1402 |
def abort(space): |
| 1403 |
"""Abort the interpreter immediately. This 'dumps core' or otherwise fails |
| 1404 |
in the hardest way possible on the hosting operating system.""" |
| 1405 |
import signal |
| 1406 |
rposix.kill(os.getpid(), signal.SIGABRT) |
| 1407 |
|
| 1408 |
@unwrap_spec( |
| 1409 |
src_dir_fd=DirFD(rposix.HAVE_LINKAT), dst_dir_fd=DirFD(rposix.HAVE_LINKAT), |
| 1410 |
follow_symlinks=bool) |
| 1411 |
def link(space, w_src, w_dst, __kwonly__, |
| 1412 |
src_dir_fd=DEFAULT_DIR_FD, dst_dir_fd=DEFAULT_DIR_FD, |
| 1413 |
follow_symlinks=True): |
| 1414 |
"""\ |
| 1415 |
link(src, dst, *, src_dir_fd=None, dst_dir_fd=None, follow_symlinks=True) |
| 1416 |
|
| 1417 |
Create a hard link to a file. |
| 1418 |
|
| 1419 |
If either src_dir_fd or dst_dir_fd is not None, it should be a file |
| 1420 |
descriptor open to a directory, and the respective path string (src or dst) |
| 1421 |
should be relative; the path will then be relative to that directory. |
| 1422 |
If follow_symlinks is False, and the last element of src is a symbolic |
| 1423 |
link, link will create a link to the symbolic link itself instead of the |
| 1424 |
file the link points to. |
| 1425 |
src_dir_fd, dst_dir_fd, and follow_symlinks may not be implemented on your |
| 1426 |
platform. If they are unavailable, using them will raise a |
| 1427 |
NotImplementedError.""" |
| 1428 |
src = space.fsencode_w(w_src) |
| 1429 |
dst = space.fsencode_w(w_dst) |
| 1430 |
try: |
| 1431 |
if (rposix.HAVE_LINKAT and |
| 1432 |
(src_dir_fd != DEFAULT_DIR_FD or dst_dir_fd != DEFAULT_DIR_FD |
| 1433 |
or not follow_symlinks)): |
| 1434 |
rposix.linkat(src, dst, src_dir_fd, dst_dir_fd, follow_symlinks) |
| 1435 |
else: |
| 1436 |
rposix.link(src, dst) |
| 1437 |
except OSError as e: |
| 1438 |
raise wrap_oserror2(space, e, w_filename=w_src, w_filename2=w_dst, |
| 1439 |
eintr_retry=False) |
| 1440 |
|
| 1441 |
|
| 1442 |
@unwrap_spec(target_is_directory=int, dir_fd=DirFD(rposix.HAVE_SYMLINKAT)) |
| 1443 |
def symlink(space, w_src, w_dst, target_is_directory=0, |
| 1444 |
__kwonly__=None, dir_fd=DEFAULT_DIR_FD): |
| 1445 |
"""symlink(src, dst, target_is_directory=0, *, dir_fd=None) |
| 1446 |
|
| 1447 |
Create a symbolic link pointing to src named dst. |
| 1448 |
|
| 1449 |
target_is_directory is required on Windows if the target is to be |
| 1450 |
interpreted as a directory. (On Windows, symlink requires |
| 1451 |
Windows 6.0 or greater, and raises a NotImplementedError otherwise.) |
| 1452 |
target_is_directory is ignored on non-Windows platforms. |
| 1453 |
|
| 1454 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1455 |
and path should be relative; path will then be relative to that directory. |
| 1456 |
dir_fd may not be implemented on your platform. |
| 1457 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 1458 |
if _WIN32: |
| 1459 |
src_utf8 = space.fsencode_w(w_src) |
| 1460 |
dst_utf8 = space.fsencode_w(w_dst) |
| 1461 |
src_wch = rffi.utf82wcharp_ex(src_utf8, codepoints_in_utf8(src_utf8)) |
| 1462 |
dst_wch = rffi.utf82wcharp_ex(dst_utf8, codepoints_in_utf8(dst_utf8)) |
| 1463 |
ret = rwin32.os_symlink_impl(src_wch, dst_wch, target_is_directory) |
| 1464 |
rffi.free_wcharp(src_wch) |
| 1465 |
rffi.free_wcharp(dst_wch) |
| 1466 |
if ret == 0: |
| 1467 |
error = rwin32.GetLastError_saved() |
| 1468 |
err = WindowsError(error, "symlink failed") |
| 1469 |
raise wrap_oserror2(space, err, w_filename=w_src, |
| 1470 |
w_filename2=w_dst, eintr_retry=False) |
| 1471 |
return |
| 1472 |
try: |
| 1473 |
if rposix.HAVE_SYMLINKAT and dir_fd != DEFAULT_DIR_FD: |
| 1474 |
src = space.fsencode_w(w_src) |
| 1475 |
dst = space.fsencode_w(w_dst) |
| 1476 |
rposix.symlinkat(src, dst, dir_fd) |
| 1477 |
else: |
| 1478 |
dispatch_filename_2(rposix.symlink)(space, w_src, w_dst) |
| 1479 |
except OSError as e: |
| 1480 |
raise wrap_oserror2(space, e, w_filename=w_src, w_filename2=w_dst, |
| 1481 |
eintr_retry=False) |
| 1482 |
|
| 1483 |
|
| 1484 |
@unwrap_spec( |
| 1485 |
path=path_or_fd(allow_fd=False), |
| 1486 |
dir_fd=DirFD(rposix.HAVE_READLINKAT)) |
| 1487 |
def readlink(space, path, __kwonly__, dir_fd=DEFAULT_DIR_FD): |
| 1488 |
"""readlink(path, *, dir_fd=None) -> path |
| 1489 |
|
| 1490 |
Return a string representing the path to which the symbolic link points. |
| 1491 |
|
| 1492 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1493 |
and path should be relative; path will then be relative to that directory. |
| 1494 |
dir_fd may not be implemented on your platform. |
| 1495 |
If it is unavailable, using it will raise a NotImplementedError.""" |
| 1496 |
if _WIN32: |
| 1497 |
src_utf8 = space.fsencode_w(path.w_path) |
| 1498 |
src_wch = rffi.utf82wcharp_ex(src_utf8, codepoints_in_utf8(src_utf8)) |
| 1499 |
size = rwin32._Py_MAXIMUM_REPARSE_DATA_BUFFER_SIZE |
| 1500 |
with rffi.scoped_alloc_buffer(size) as target_buffer: |
| 1501 |
result = lltype.malloc(rffi.CWCHARPP.TO, 1, flavor='raw') |
| 1502 |
result[0] = lltype.nullptr(rffi.CWCHARP.TO) |
| 1503 |
void_buf = rffi.cast(rffi.VOIDP, target_buffer.raw) |
| 1504 |
try: |
| 1505 |
n = rwin32.os_readlink_impl(src_wch, void_buf, result) |
| 1506 |
rffi.free_wcharp(src_wch) |
| 1507 |
if n == -1: |
| 1508 |
error = rwin32.GetLastError_saved() |
| 1509 |
err = WindowsError(error, "readlink failed") |
| 1510 |
raise wrap_oserror2(space, err, w_filename=path.w_path, |
| 1511 |
eintr_retry=False) |
| 1512 |
|
| 1513 |
if n == -2: |
| 1514 |
raise oefmt(space.w_RuntimeError, "unknown error in readlink") |
| 1515 |
utf8, codepoints = rffi.wcharp2utf8n(result[0], n) |
| 1516 |
if space.isinstance_w(path.w_path, space.w_unicode): |
| 1517 |
return space.newtext(utf8, codepoints) |
| 1518 |
else: |
| 1519 |
return space.newbytes(utf8) |
| 1520 |
finally: |
| 1521 |
lltype.free(result, flavor="raw") |
| 1522 |
try: |
| 1523 |
if rposix.HAVE_READLINKAT and dir_fd != DEFAULT_DIR_FD: |
| 1524 |
result = call_rposix(rposix.readlinkat, path, dir_fd) |
| 1525 |
else: |
| 1526 |
result = call_rposix(rposix.readlink, path) |
| 1527 |
except OSError as e: |
| 1528 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 1529 |
w_result = space.newbytes(result) |
| 1530 |
if space.isinstance_w(path.w_path, space.w_unicode): |
| 1531 |
return space.fsdecode(w_result) |
| 1532 |
return w_result |
| 1533 |
|
| 1534 |
before_fork_hooks = [] |
| 1535 |
after_fork_child_hooks = [] |
| 1536 |
after_fork_parent_hooks = [] |
| 1537 |
|
| 1538 |
@specialize.memo() |
| 1539 |
def get_fork_hooks(where): |
| 1540 |
if where == 'before': |
| 1541 |
return before_fork_hooks |
| 1542 |
elif where == 'child': |
| 1543 |
return after_fork_child_hooks |
| 1544 |
elif where == 'parent': |
| 1545 |
return after_fork_parent_hooks |
| 1546 |
else: |
| 1547 |
assert False, "Unknown fork hook" |
| 1548 |
|
| 1549 |
@not_rpython |
| 1550 |
def add_fork_hook(where, hook): |
| 1551 |
get_fork_hooks(where).append(hook) |
| 1552 |
|
| 1553 |
add_fork_hook('child', ExecutionContext._mark_thread_disappeared) |
| 1554 |
|
| 1555 |
@specialize.arg(0) |
| 1556 |
def run_fork_hooks(where, space): |
| 1557 |
for hook in get_fork_hooks(where): |
| 1558 |
hook(space) |
| 1559 |
|
| 1560 |
def _run_forking_function(space, kind): |
| 1561 |
run_fork_hooks('before', space) |
| 1562 |
try: |
| 1563 |
if kind == "F": |
| 1564 |
pid = os.fork() |
| 1565 |
master_fd = -1 |
| 1566 |
elif kind == "P": |
| 1567 |
pid, master_fd = os.forkpty() |
| 1568 |
else: |
| 1569 |
raise AssertionError |
| 1570 |
except OSError as e: |
| 1571 |
try: |
| 1572 |
run_fork_hooks('parent', space) |
| 1573 |
except: |
| 1574 |
|
| 1575 |
pass |
| 1576 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1577 |
if pid == 0: |
| 1578 |
from pypy.module.thread import os_thread |
| 1579 |
os_thread.reinit_threads(space) |
| 1580 |
run_fork_hooks('child', space) |
| 1581 |
else: |
| 1582 |
run_fork_hooks('parent', space) |
| 1583 |
return pid, master_fd |
| 1584 |
|
| 1585 |
def fork(space): |
| 1586 |
pid, irrelevant = _run_forking_function(space, "F") |
| 1587 |
return space.newint(pid) |
| 1588 |
|
| 1589 |
class ApplevelForkCallbacks(object): |
| 1590 |
def __init__(self, space): |
| 1591 |
self.space = space |
| 1592 |
self.before_w = [] |
| 1593 |
self.parent_w = [] |
| 1594 |
self.child_w = [] |
| 1595 |
|
| 1596 |
def register_at_fork(space, __args__): |
| 1597 |
""" |
| 1598 |
register_at_fork(*, [before], [after_in_child], [after_in_parent]) |
| 1599 |
Register callables to be called when forking a new process. |
| 1600 |
|
| 1601 |
before |
| 1602 |
A callable to be called in the parent before the fork() syscall. |
| 1603 |
after_in_child |
| 1604 |
A callable to be called in the child after fork(). |
| 1605 |
after_in_parent |
| 1606 |
A callable to be called in the parent after fork(). |
| 1607 |
|
| 1608 |
'before' callbacks are called in reverse order. |
| 1609 |
'after_in_child' and 'after_in_parent' callbacks are called in order. |
| 1610 |
""" |
| 1611 |
|
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
args_w, kwargs_w = __args__.unpack() |
| 1616 |
if args_w: |
| 1617 |
raise oefmt(space.w_TypeError, |
| 1618 |
"register_at_fork() takes no positional arguments") |
| 1619 |
w_before = kwargs_w.pop("before", None) |
| 1620 |
w_after_in_parent = kwargs_w.pop("after_in_parent", None) |
| 1621 |
w_after_in_child = kwargs_w.pop("after_in_child", None) |
| 1622 |
if kwargs_w: |
| 1623 |
for key in kwargs_w: |
| 1624 |
raise oefmt(space.w_TypeError, |
| 1625 |
"%s is an invalid keyword argument for register_at_fork()", key) |
| 1626 |
|
| 1627 |
registered = False |
| 1628 |
cbs = space.fromcache(ApplevelForkCallbacks) |
| 1629 |
if w_before is not None: |
| 1630 |
if not space.callable_w(w_before): |
| 1631 |
raise oefmt(space.w_TypeError, |
| 1632 |
"'before' must be callable, not %T", |
| 1633 |
w_before) |
| 1634 |
cbs.before_w.append(w_before) |
| 1635 |
registered = True |
| 1636 |
if w_after_in_parent is not None: |
| 1637 |
if not space.callable_w(w_after_in_parent): |
| 1638 |
raise oefmt(space.w_TypeError, |
| 1639 |
"'after_in_parent' must be callable, not %T", |
| 1640 |
w_after_in_parent) |
| 1641 |
cbs.parent_w.append(w_after_in_parent) |
| 1642 |
registered = True |
| 1643 |
if w_after_in_child is not None: |
| 1644 |
if not space.callable_w(w_after_in_child): |
| 1645 |
raise oefmt(space.w_TypeError, |
| 1646 |
"'after_in_child' must be callable, not %T", |
| 1647 |
w_after_in_child) |
| 1648 |
cbs.child_w.append(w_after_in_child) |
| 1649 |
registered = True |
| 1650 |
if not registered: |
| 1651 |
raise oefmt(space.w_TypeError, |
| 1652 |
"At least one argument is required.") |
| 1653 |
|
| 1654 |
|
| 1655 |
def _run_applevel_hook(space, w_callable): |
| 1656 |
try: |
| 1657 |
space.call_function(w_callable) |
| 1658 |
except OperationError as e: |
| 1659 |
e.write_unraisable(space, "fork hook") |
| 1660 |
|
| 1661 |
def run_applevel_fork_hooks(space, l_w, reverse=False): |
| 1662 |
if len(l_w) == 0: |
| 1663 |
return |
| 1664 |
if not reverse: |
| 1665 |
for i in range(len(l_w)): |
| 1666 |
_run_applevel_hook(space, l_w[i]) |
| 1667 |
else: |
| 1668 |
for i in range(len(l_w) - 1, -1, -1): |
| 1669 |
_run_applevel_hook(space, l_w[i]) |
| 1670 |
|
| 1671 |
def run_applevel_fork_hooks_before(space): |
| 1672 |
cbs = space.fromcache(ApplevelForkCallbacks) |
| 1673 |
run_applevel_fork_hooks(space, cbs.before_w, reverse=True) |
| 1674 |
|
| 1675 |
def run_applevel_fork_hooks_parent(space): |
| 1676 |
cbs = space.fromcache(ApplevelForkCallbacks) |
| 1677 |
run_applevel_fork_hooks(space, cbs.parent_w) |
| 1678 |
|
| 1679 |
def run_applevel_fork_hooks_child(space): |
| 1680 |
cbs = space.fromcache(ApplevelForkCallbacks) |
| 1681 |
run_applevel_fork_hooks(space, cbs.child_w) |
| 1682 |
|
| 1683 |
add_fork_hook('before', run_applevel_fork_hooks_before) |
| 1684 |
add_fork_hook('parent', run_applevel_fork_hooks_parent) |
| 1685 |
add_fork_hook('child', run_applevel_fork_hooks_child) |
| 1686 |
|
| 1687 |
|
| 1688 |
def openpty(space): |
| 1689 |
"Open a pseudo-terminal, returning open fd's for both master and slave end." |
| 1690 |
master_fd = slave_fd = -1 |
| 1691 |
try: |
| 1692 |
master_fd, slave_fd = os.openpty() |
| 1693 |
rposix.set_inheritable(master_fd, False) |
| 1694 |
rposix.set_inheritable(slave_fd, False) |
| 1695 |
except OSError as e: |
| 1696 |
if master_fd >= 0: |
| 1697 |
rposix.c_close(master_fd) |
| 1698 |
if slave_fd >= 0: |
| 1699 |
rposix.c_close(slave_fd) |
| 1700 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1701 |
return space.newtuple2(space.newint(master_fd), space.newint(slave_fd)) |
| 1702 |
|
| 1703 |
def forkpty(space): |
| 1704 |
pid, master_fd = _run_forking_function(space, "P") |
| 1705 |
return space.newtuple2(space.newint(pid), |
| 1706 |
space.newint(master_fd)) |
| 1707 |
|
| 1708 |
@unwrap_spec(pid=c_int, options=c_int) |
| 1709 |
def waitpid(space, pid, options): |
| 1710 |
""" waitpid(pid, options) -> (pid, status) |
| 1711 |
|
| 1712 |
Wait for completion of a given child process. |
| 1713 |
""" |
| 1714 |
while True: |
| 1715 |
try: |
| 1716 |
pid, status = os.waitpid(pid, options) |
| 1717 |
break |
| 1718 |
except OSError as e: |
| 1719 |
wrap_oserror(space, e, eintr_retry=True) |
| 1720 |
return space.newtuple2(space.newint(pid), space.newint(status)) |
| 1721 |
|
| 1722 |
|
| 1723 |
|
| 1724 |
@unwrap_spec(status=c_int) |
| 1725 |
def _exit(space, status): |
| 1726 |
os._exit(status) |
| 1727 |
|
| 1728 |
def execv(space, w_path, w_argv): |
| 1729 |
""" execv(path, args) |
| 1730 |
|
| 1731 |
Execute an executable path with arguments, replacing current process. |
| 1732 |
|
| 1733 |
path: path of executable file |
| 1734 |
args: iterable of strings |
| 1735 |
""" |
| 1736 |
command = space.fsencode_w(w_path) |
| 1737 |
try: |
| 1738 |
args_w = space.unpackiterable(w_argv) |
| 1739 |
if len(args_w) < 1: |
| 1740 |
raise oefmt(space.w_ValueError, |
| 1741 |
"execv() arg 2 must not be empty") |
| 1742 |
args = [space.fsencode_w(w_arg) for w_arg in args_w] |
| 1743 |
except OperationError as e: |
| 1744 |
if not e.match(space, space.w_TypeError): |
| 1745 |
raise |
| 1746 |
raise oefmt(space.w_TypeError, |
| 1747 |
"execv() arg 2 must be an iterable of strings") |
| 1748 |
if not args[0]: |
| 1749 |
raise oefmt(space.w_ValueError, |
| 1750 |
"execv() arg 2 first element cannot be empty") |
| 1751 |
try: |
| 1752 |
os.execv(command, args) |
| 1753 |
except OSError as e: |
| 1754 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1755 |
|
| 1756 |
|
| 1757 |
def _env2interp(space, w_env): |
| 1758 |
env = {} |
| 1759 |
w_keys = space.call_method(w_env, 'keys') |
| 1760 |
for w_key in space.unpackiterable(w_keys): |
| 1761 |
w_value = space.getitem(w_env, w_key) |
| 1762 |
key = space.fsencode_w(w_key) |
| 1763 |
val = space.fsencode_w(w_value) |
| 1764 |
|
| 1765 |
|
| 1766 |
if len(key) == 0 or '=' in key[1:]: |
| 1767 |
raise oefmt(space.w_ValueError, |
| 1768 |
"illegal environment variable name") |
| 1769 |
env[key] = val |
| 1770 |
return env |
| 1771 |
|
| 1772 |
|
| 1773 |
def execve(space, w_path, w_argv, w_env): |
| 1774 |
"""execve(path, argv, env) |
| 1775 |
|
| 1776 |
Execute a path with arguments and environment, replacing current process. |
| 1777 |
|
| 1778 |
path: path of executable file |
| 1779 |
argv: tuple or list of arguments |
| 1780 |
env: dictionary of strings mapping to strings |
| 1781 |
|
| 1782 |
On some platforms, you may specify an open file descriptor for path; |
| 1783 |
execve will execute the program the file descriptor is open to. |
| 1784 |
If this functionality is unavailable, using it raises NotImplementedError. |
| 1785 |
""" |
| 1786 |
if not (space.isinstance_w(w_argv, space.w_list) |
| 1787 |
or space.isinstance_w(w_argv, space.w_tuple)): |
| 1788 |
raise oefmt(space.w_TypeError, |
| 1789 |
"execve: argv must be a tuple or a list") |
| 1790 |
args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_argv)] |
| 1791 |
if len(args) < 1: |
| 1792 |
raise oefmt(space.w_ValueError, |
| 1793 |
"execve() arg 2 must not be empty") |
| 1794 |
if not args[0]: |
| 1795 |
raise oefmt(space.w_ValueError, |
| 1796 |
"execve() arg 2 first element cannot be empty") |
| 1797 |
env = _env2interp(space, w_env) |
| 1798 |
try: |
| 1799 |
path = space.fsencode_w(w_path) |
| 1800 |
except OperationError: |
| 1801 |
if not rposix.HAVE_FEXECVE: |
| 1802 |
raise oefmt(space.w_TypeError, |
| 1803 |
"execve: illegal type for path argument") |
| 1804 |
if not space.isinstance_w(w_path, space.w_int): |
| 1805 |
raise oefmt(space.w_TypeError, |
| 1806 |
"argument should be string, bytes or integer, not %T", w_path) |
| 1807 |
|
| 1808 |
fd = unwrap_fd(space, w_path) |
| 1809 |
try: |
| 1810 |
rposix.fexecve(fd, args, env) |
| 1811 |
except OSError as e: |
| 1812 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1813 |
else: |
| 1814 |
try: |
| 1815 |
os.execve(path, args, env) |
| 1816 |
except OSError as e: |
| 1817 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1818 |
|
| 1819 |
@unwrap_spec(mode=int, path='fsencode') |
| 1820 |
def spawnv(space, mode, path, w_argv): |
| 1821 |
if not (space.isinstance_w(w_argv, space.w_list) |
| 1822 |
or space.isinstance_w(w_argv, space.w_tuple)): |
| 1823 |
raise oefmt(space.w_TypeError, |
| 1824 |
"spawnv: argv must be a tuple or a list") |
| 1825 |
args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_argv)] |
| 1826 |
if len(args) < 1: |
| 1827 |
raise oefmt(space.w_ValueError, |
| 1828 |
"spawnv() arg 2 cannot be empty") |
| 1829 |
if not args[0]: |
| 1830 |
raise oefmt(space.w_ValueError, |
| 1831 |
"spawnv() arg 2 first element cannot be empty") |
| 1832 |
try: |
| 1833 |
ret = os.spawnv(mode, path, args) |
| 1834 |
except OSError as e: |
| 1835 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1836 |
return space.newint(ret) |
| 1837 |
|
| 1838 |
@unwrap_spec(mode=int, path='fsencode') |
| 1839 |
def spawnve(space, mode, path, w_argv, w_env): |
| 1840 |
if not (space.isinstance_w(w_argv, space.w_list) |
| 1841 |
or space.isinstance_w(w_argv, space.w_tuple)): |
| 1842 |
raise oefmt(space.w_TypeError, |
| 1843 |
"spawnve: argv must be a tuple or a list") |
| 1844 |
args = [space.fsencode_w(w_arg) for w_arg in space.unpackiterable(w_argv)] |
| 1845 |
if len(args) < 1: |
| 1846 |
raise oefmt(space.w_ValueError, |
| 1847 |
"spawnv() arg 2 cannot be empty") |
| 1848 |
env = _env2interp(space, w_env) |
| 1849 |
if not args[0]: |
| 1850 |
raise oefmt(space.w_ValueError, |
| 1851 |
"spawnve() arg 2 first element cannot be empty") |
| 1852 |
try: |
| 1853 |
ret = os.spawnve(mode, path, args, env) |
| 1854 |
except OSError as e: |
| 1855 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1856 |
return space.newint(ret) |
| 1857 |
|
| 1858 |
|
| 1859 |
@unwrap_spec( |
| 1860 |
path=path_or_fd(allow_fd=rposix.HAVE_FUTIMENS or rposix.HAVE_FUTIMES), |
| 1861 |
w_times=WrappedDefault(None), w_ns=WrappedDefault(None), |
| 1862 |
dir_fd=DirFD(rposix.HAVE_UTIMENSAT), follow_symlinks=bool) |
| 1863 |
def utime(space, path, w_times, __kwonly__, w_ns, dir_fd=DEFAULT_DIR_FD, |
| 1864 |
follow_symlinks=True): |
| 1865 |
"""utime(path, times=None, *, ns=None, dir_fd=None, follow_symlinks=True) |
| 1866 |
|
| 1867 |
Set the access and modified time of path. |
| 1868 |
|
| 1869 |
path may always be specified as a string. |
| 1870 |
On some platforms, path may also be specified as an open file descriptor. |
| 1871 |
If this functionality is unavailable, using it raises an exception. |
| 1872 |
|
| 1873 |
If times is not None, it must be a tuple (atime, mtime); |
| 1874 |
atime and mtime should be expressed as float seconds since the epoch. |
| 1875 |
If ns is not None, it must be a tuple (atime_ns, mtime_ns); |
| 1876 |
atime_ns and mtime_ns should be expressed as integer nanoseconds |
| 1877 |
since the epoch. |
| 1878 |
If both times and ns are None, utime uses the current time. |
| 1879 |
Specifying tuples for both times and ns is an error. |
| 1880 |
|
| 1881 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 1882 |
and path should be relative; path will then be relative to that directory. |
| 1883 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 1884 |
link, utime will modify the symbolic link itself instead of the file the |
| 1885 |
link points to. |
| 1886 |
It is an error to use dir_fd or follow_symlinks when specifying path |
| 1887 |
as an open file descriptor. |
| 1888 |
dir_fd and follow_symlinks may not be available on your platform. |
| 1889 |
If they are unavailable, using them will raise a NotImplementedError.""" |
| 1890 |
utime = parse_utime_args(space, w_times, w_ns) |
| 1891 |
|
| 1892 |
if path.as_fd != -1: |
| 1893 |
if dir_fd != DEFAULT_DIR_FD: |
| 1894 |
raise oefmt(space.w_ValueError, |
| 1895 |
"utime: can't specify both dir_fd and fd") |
| 1896 |
if not follow_symlinks: |
| 1897 |
raise oefmt(space.w_ValueError, |
| 1898 |
"utime: cannot use fd and follow_symlinks together") |
| 1899 |
if rposix.HAVE_FUTIMENS: |
| 1900 |
do_utimens(space, rposix.futimens, path.as_fd, utime) |
| 1901 |
elif rposix.HAVE_FUTIMES: |
| 1902 |
do_utimes(space, rposix.futimes, path.as_fd, utime) |
| 1903 |
elif rposix.HAVE_UTIMENSAT: |
| 1904 |
if path.as_bytes is None: |
| 1905 |
raise oefmt(space.w_NotImplementedError, |
| 1906 |
"utime: unsupported value for 'path'") |
| 1907 |
do_utimens(space, rposix.utimensat, path.as_bytes, utime, |
| 1908 |
dir_fd, follow_symlinks) |
| 1909 |
elif rposix.HAVE_LUTIMES and not follow_symlinks: |
| 1910 |
if path.as_bytes is None: |
| 1911 |
raise oefmt(space.w_NotImplementedError, |
| 1912 |
"utime: unsupported value for 'path'") |
| 1913 |
do_utimes(space, rposix.lutimes, path.as_bytes, utime) |
| 1914 |
elif follow_symlinks: |
| 1915 |
do_utimes(space, _dispatch_utime, path, utime) |
| 1916 |
else: |
| 1917 |
raise argument_unavailable(space, "utime", "follow_symlinks") |
| 1918 |
|
| 1919 |
def parse_utime_args(space, w_times, w_ns): |
| 1920 |
"""Parse utime's times/ns arguments into a 5-item tuple of a "now" |
| 1921 |
flag and 2 "TIMESPEC" like 2-item s/ns values |
| 1922 |
""" |
| 1923 |
if (not space.is_w(w_times, space.w_None) and |
| 1924 |
not space.is_w(w_ns, space.w_None)): |
| 1925 |
raise oefmt(space.w_ValueError, |
| 1926 |
"utime: you may specify either 'times' or 'ns' but not both") |
| 1927 |
now = False |
| 1928 |
if space.is_w(w_times, space.w_None) and space.is_w(w_ns, space.w_None): |
| 1929 |
now = True |
| 1930 |
atime_s = mtime_s = 0 |
| 1931 |
atime_ns = mtime_ns = 0 |
| 1932 |
elif not space.is_w(w_times, space.w_None): |
| 1933 |
times_w = space.fixedview(w_times) |
| 1934 |
if len(times_w) != 2 or not space.isinstance_w(w_times, space.w_tuple): |
| 1935 |
raise oefmt(space.w_TypeError, |
| 1936 |
"utime: 'times' must be either a tuple of two ints or None") |
| 1937 |
atime_s, atime_ns = convert_seconds(space, times_w[0]) |
| 1938 |
mtime_s, mtime_ns = convert_seconds(space, times_w[1]) |
| 1939 |
else: |
| 1940 |
args_w = space.fixedview(w_ns) |
| 1941 |
if len(args_w) != 2 or not space.isinstance_w(w_ns, space.w_tuple): |
| 1942 |
raise oefmt(space.w_TypeError, |
| 1943 |
"utime: 'ns' must be a tuple of two ints") |
| 1944 |
atime_s, atime_ns = convert_ns(space, args_w[0]) |
| 1945 |
mtime_s, mtime_ns = convert_ns(space, args_w[1]) |
| 1946 |
return now, atime_s, atime_ns, mtime_s, mtime_ns |
| 1947 |
|
| 1948 |
@specialize.arg(1) |
| 1949 |
def do_utimens(space, func, arg, utime, *args): |
| 1950 |
"""Common implementation for futimens/utimensat etc.""" |
| 1951 |
now, atime_s, atime_ns, mtime_s, mtime_ns = utime |
| 1952 |
if now: |
| 1953 |
atime_ns = mtime_ns = rposix.UTIME_NOW |
| 1954 |
try: |
| 1955 |
func(arg, atime_s, atime_ns, mtime_s, mtime_ns, *args) |
| 1956 |
except OSError as e: |
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
|
| 1961 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1965 |
|
| 1966 |
@specialize.arg(1) |
| 1967 |
def do_utimes(space, func, arg, utime): |
| 1968 |
"""Common implementation for f/l/utimes""" |
| 1969 |
now, atime_s, atime_ns, mtime_s, mtime_ns = utime |
| 1970 |
try: |
| 1971 |
if now: |
| 1972 |
func(arg, None) |
| 1973 |
else: |
| 1974 |
|
| 1975 |
|
| 1976 |
atime = atime_s + (atime_ns / 1e9) |
| 1977 |
mtime = mtime_s + (mtime_ns / 1e9) |
| 1978 |
func(arg, (atime, mtime)) |
| 1979 |
except OSError as e: |
| 1980 |
|
| 1981 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 1982 |
|
| 1983 |
@specialize.argtype(1) |
| 1984 |
def _dispatch_utime(path, times): |
| 1985 |
|
| 1986 |
|
| 1987 |
path_b = path.as_bytes |
| 1988 |
assert path_b is not None |
| 1989 |
return rposix.utime(path.as_bytes, times) |
| 1990 |
|
| 1991 |
|
| 1992 |
def convert_seconds(space, w_time): |
| 1993 |
if space.isinstance_w(w_time, space.w_float): |
| 1994 |
time = space.float_w(w_time) |
| 1995 |
fracpart, intpart = modf(time) |
| 1996 |
if fracpart < 0: |
| 1997 |
fracpart += 1. |
| 1998 |
intpart -= 1. |
| 1999 |
return int(intpart), int(fracpart*1e9) |
| 2000 |
else: |
| 2001 |
time = space.int_w(w_time) |
| 2002 |
return time, 0 |
| 2003 |
|
| 2004 |
def convert_ns(space, w_ns_time): |
| 2005 |
w_billion = space.newint(1000000000) |
| 2006 |
w_res = space.divmod(w_ns_time, w_billion) |
| 2007 |
res_w = space.fixedview(w_res) |
| 2008 |
time_int = space.int_w(res_w[0]) |
| 2009 |
time_frac = space.int_w(res_w[1]) |
| 2010 |
return time_int, time_frac |
| 2011 |
|
| 2012 |
|
| 2013 |
def uname(space): |
| 2014 |
""" uname() -> (sysname, nodename, release, version, machine) |
| 2015 |
|
| 2016 |
Return a tuple identifying the current operating system. |
| 2017 |
""" |
| 2018 |
try: |
| 2019 |
r = os.uname() |
| 2020 |
except OSError as e: |
| 2021 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2022 |
l_w = [space.newfilename(i) |
| 2023 |
for i in [r[0], r[1], r[2], r[3], r[4]]] |
| 2024 |
w_tuple = space.newtuple(l_w) |
| 2025 |
w_uname_result = space.getattr(space.getbuiltinmodule(os.name), |
| 2026 |
space.newtext('uname_result')) |
| 2027 |
return space.call_function(w_uname_result, w_tuple) |
| 2028 |
|
| 2029 |
def getuid(space): |
| 2030 |
""" getuid() -> uid |
| 2031 |
|
| 2032 |
Return the current process's user id. |
| 2033 |
""" |
| 2034 |
return wrap_uid(space, os.getuid()) |
| 2035 |
|
| 2036 |
@unwrap_spec(uid=c_uid_t) |
| 2037 |
def setuid(space, uid): |
| 2038 |
""" setuid(uid) |
| 2039 |
|
| 2040 |
Set the current process's user id. |
| 2041 |
""" |
| 2042 |
try: |
| 2043 |
os.setuid(uid) |
| 2044 |
except OSError as e: |
| 2045 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2046 |
|
| 2047 |
@unwrap_spec(euid=c_uid_t) |
| 2048 |
def seteuid(space, euid): |
| 2049 |
""" seteuid(euid) |
| 2050 |
|
| 2051 |
Set the current process's effective user id. |
| 2052 |
""" |
| 2053 |
try: |
| 2054 |
os.seteuid(euid) |
| 2055 |
except OSError as e: |
| 2056 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2057 |
|
| 2058 |
@unwrap_spec(gid=c_gid_t) |
| 2059 |
def setgid(space, gid): |
| 2060 |
""" setgid(gid) |
| 2061 |
|
| 2062 |
Set the current process's group id. |
| 2063 |
""" |
| 2064 |
try: |
| 2065 |
os.setgid(gid) |
| 2066 |
except OSError as e: |
| 2067 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2068 |
|
| 2069 |
@unwrap_spec(egid=c_gid_t) |
| 2070 |
def setegid(space, egid): |
| 2071 |
""" setegid(egid) |
| 2072 |
|
| 2073 |
Set the current process's effective group id. |
| 2074 |
""" |
| 2075 |
try: |
| 2076 |
os.setegid(egid) |
| 2077 |
except OSError as e: |
| 2078 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2079 |
|
| 2080 |
def chroot(space, w_path): |
| 2081 |
""" chroot(path) |
| 2082 |
|
| 2083 |
Change root directory to path. |
| 2084 |
""" |
| 2085 |
path = space.fsencode_w(w_path) |
| 2086 |
try: |
| 2087 |
os.chroot(path) |
| 2088 |
except OSError as e: |
| 2089 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 2090 |
return space.w_None |
| 2091 |
|
| 2092 |
def getgid(space): |
| 2093 |
""" getgid() -> gid |
| 2094 |
|
| 2095 |
Return the current process's group id. |
| 2096 |
""" |
| 2097 |
return wrap_gid(space, os.getgid()) |
| 2098 |
|
| 2099 |
def getegid(space): |
| 2100 |
""" getegid() -> gid |
| 2101 |
|
| 2102 |
Return the current process's effective group id. |
| 2103 |
""" |
| 2104 |
return wrap_gid(space, os.getegid()) |
| 2105 |
|
| 2106 |
def geteuid(space): |
| 2107 |
""" geteuid() -> euid |
| 2108 |
|
| 2109 |
Return the current process's effective user id. |
| 2110 |
""" |
| 2111 |
return wrap_uid(space, os.geteuid()) |
| 2112 |
|
| 2113 |
def getgroups(space): |
| 2114 |
""" getgroups() -> list of group IDs |
| 2115 |
|
| 2116 |
Return list of supplemental group IDs for the process. |
| 2117 |
""" |
| 2118 |
try: |
| 2119 |
list = os.getgroups() |
| 2120 |
except OSError as e: |
| 2121 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2122 |
return space.newlist([wrap_gid(space, e) for e in list]) |
| 2123 |
|
| 2124 |
def setgroups(space, w_groups): |
| 2125 |
""" setgroups(groups) |
| 2126 |
|
| 2127 |
Set the groups of the current process to list. |
| 2128 |
""" |
| 2129 |
list = [] |
| 2130 |
for w_gid in space.unpackiterable(w_groups): |
| 2131 |
list.append(space.c_uid_t_w(w_gid)) |
| 2132 |
try: |
| 2133 |
os.setgroups(list[:]) |
| 2134 |
except OSError as e: |
| 2135 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2136 |
|
| 2137 |
@unwrap_spec(username='text', gid=c_gid_t) |
| 2138 |
def initgroups(space, username, gid): |
| 2139 |
""" initgroups(username, gid) -> None |
| 2140 |
|
| 2141 |
Call the system initgroups() to initialize the group access list with all of |
| 2142 |
the groups of which the specified username is a member, plus the specified |
| 2143 |
group id. |
| 2144 |
""" |
| 2145 |
try: |
| 2146 |
os.initgroups(username, gid) |
| 2147 |
except OSError as e: |
| 2148 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2149 |
|
| 2150 |
@unwrap_spec(username='text', gid=c_gid_t) |
| 2151 |
def getgrouplist(space, username, gid): |
| 2152 |
""" |
| 2153 |
getgrouplist(user, group) -> list of groups to which a user belongs |
| 2154 |
|
| 2155 |
Returns a list of groups to which a user belongs. |
| 2156 |
|
| 2157 |
user: username to lookup |
| 2158 |
group: base group id of the user |
| 2159 |
""" |
| 2160 |
try: |
| 2161 |
groups = rposix.getgrouplist(username, gid) |
| 2162 |
return space.newlist([space.newint(g) for g in groups]) |
| 2163 |
except OSError as e: |
| 2164 |
raise wrap_oserror(space, e) |
| 2165 |
|
| 2166 |
|
| 2167 |
def getpgrp(space): |
| 2168 |
""" getpgrp() -> pgrp |
| 2169 |
|
| 2170 |
Return the current process group id. |
| 2171 |
""" |
| 2172 |
return space.newint(os.getpgrp()) |
| 2173 |
|
| 2174 |
def setpgrp(space): |
| 2175 |
""" setpgrp() |
| 2176 |
|
| 2177 |
Make this process a session leader. |
| 2178 |
""" |
| 2179 |
try: |
| 2180 |
os.setpgrp() |
| 2181 |
except OSError as e: |
| 2182 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2183 |
return space.w_None |
| 2184 |
|
| 2185 |
def getppid(space): |
| 2186 |
""" getppid() -> ppid |
| 2187 |
|
| 2188 |
Return the parent's process id. |
| 2189 |
""" |
| 2190 |
if not _WIN32: |
| 2191 |
return space.newint(os.getppid()) |
| 2192 |
else: |
| 2193 |
from pypy.module.posix import interp_nt as nt |
| 2194 |
try: |
| 2195 |
return space.newint(nt.win32_getppid()) |
| 2196 |
except OSError as e: |
| 2197 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2198 |
return space.w_None |
| 2199 |
|
| 2200 |
@unwrap_spec(pid=c_int) |
| 2201 |
def getpgid(space, pid): |
| 2202 |
""" getpgid(pid) -> pgid |
| 2203 |
|
| 2204 |
Call the system call getpgid(). |
| 2205 |
""" |
| 2206 |
try: |
| 2207 |
pgid = os.getpgid(pid) |
| 2208 |
except OSError as e: |
| 2209 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2210 |
return space.newint(pgid) |
| 2211 |
|
| 2212 |
@unwrap_spec(pid=c_int, pgrp=c_int) |
| 2213 |
def setpgid(space, pid, pgrp): |
| 2214 |
""" setpgid(pid, pgrp) |
| 2215 |
|
| 2216 |
Call the system call setpgid(). |
| 2217 |
""" |
| 2218 |
try: |
| 2219 |
os.setpgid(pid, pgrp) |
| 2220 |
except OSError as e: |
| 2221 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2222 |
return space.w_None |
| 2223 |
|
| 2224 |
@unwrap_spec(ruid=c_uid_t, euid=c_uid_t) |
| 2225 |
def setreuid(space, ruid, euid): |
| 2226 |
""" setreuid(ruid, euid) |
| 2227 |
|
| 2228 |
Set the current process's real and effective user ids. |
| 2229 |
""" |
| 2230 |
try: |
| 2231 |
os.setreuid(ruid, euid) |
| 2232 |
except OSError as e: |
| 2233 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2234 |
|
| 2235 |
@unwrap_spec(rgid=c_gid_t, egid=c_gid_t) |
| 2236 |
def setregid(space, rgid, egid): |
| 2237 |
""" setregid(rgid, egid) |
| 2238 |
|
| 2239 |
Set the current process's real and effective group ids. |
| 2240 |
""" |
| 2241 |
try: |
| 2242 |
os.setregid(rgid, egid) |
| 2243 |
except OSError as e: |
| 2244 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2245 |
|
| 2246 |
@unwrap_spec(pid=c_int) |
| 2247 |
def getsid(space, pid): |
| 2248 |
""" getsid(pid) -> sid |
| 2249 |
|
| 2250 |
Call the system call getsid(). |
| 2251 |
""" |
| 2252 |
try: |
| 2253 |
sid = os.getsid(pid) |
| 2254 |
except OSError as e: |
| 2255 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2256 |
return space.newint(sid) |
| 2257 |
|
| 2258 |
def setsid(space): |
| 2259 |
""" setsid() |
| 2260 |
|
| 2261 |
Call the system call setsid(). |
| 2262 |
""" |
| 2263 |
try: |
| 2264 |
os.setsid() |
| 2265 |
except OSError as e: |
| 2266 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2267 |
return space.w_None |
| 2268 |
|
| 2269 |
@unwrap_spec(fd=c_int) |
| 2270 |
def tcgetpgrp(space, fd): |
| 2271 |
""" tcgetpgrp(fd) -> pgid |
| 2272 |
|
| 2273 |
Return the process group associated with the terminal given by a fd. |
| 2274 |
""" |
| 2275 |
try: |
| 2276 |
pgid = os.tcgetpgrp(fd) |
| 2277 |
except OSError as e: |
| 2278 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2279 |
return space.newint(pgid) |
| 2280 |
|
| 2281 |
@unwrap_spec(fd=c_int, pgid=c_gid_t) |
| 2282 |
def tcsetpgrp(space, fd, pgid): |
| 2283 |
""" tcsetpgrp(fd, pgid) |
| 2284 |
|
| 2285 |
Set the process group associated with the terminal given by a fd. |
| 2286 |
""" |
| 2287 |
try: |
| 2288 |
os.tcsetpgrp(fd, pgid) |
| 2289 |
except OSError as e: |
| 2290 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2291 |
|
| 2292 |
def getresuid(space): |
| 2293 |
""" getresuid() -> (ruid, euid, suid) |
| 2294 |
|
| 2295 |
Get tuple of the current process's real, effective, and saved user ids. |
| 2296 |
""" |
| 2297 |
try: |
| 2298 |
(ruid, euid, suid) = os.getresuid() |
| 2299 |
except OSError as e: |
| 2300 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2301 |
return space.newtuple([wrap_uid(space, ruid), |
| 2302 |
wrap_uid(space, euid), |
| 2303 |
wrap_uid(space, suid)]) |
| 2304 |
|
| 2305 |
def getresgid(space): |
| 2306 |
""" getresgid() -> (rgid, egid, sgid) |
| 2307 |
|
| 2308 |
Get tuple of the current process's real, effective, and saved group ids. |
| 2309 |
""" |
| 2310 |
try: |
| 2311 |
(rgid, egid, sgid) = os.getresgid() |
| 2312 |
except OSError as e: |
| 2313 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2314 |
return space.newtuple([wrap_gid(space, rgid), |
| 2315 |
wrap_gid(space, egid), |
| 2316 |
wrap_gid(space, sgid)]) |
| 2317 |
|
| 2318 |
@unwrap_spec(ruid=c_uid_t, euid=c_uid_t, suid=c_uid_t) |
| 2319 |
def setresuid(space, ruid, euid, suid): |
| 2320 |
""" setresuid(ruid, euid, suid) |
| 2321 |
|
| 2322 |
Set the current process's real, effective, and saved user ids. |
| 2323 |
""" |
| 2324 |
try: |
| 2325 |
os.setresuid(ruid, euid, suid) |
| 2326 |
except OSError as e: |
| 2327 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2328 |
|
| 2329 |
@unwrap_spec(rgid=c_gid_t, egid=c_gid_t, sgid=c_gid_t) |
| 2330 |
def setresgid(space, rgid, egid, sgid): |
| 2331 |
""" setresgid(rgid, egid, sgid) |
| 2332 |
|
| 2333 |
Set the current process's real, effective, and saved group ids. |
| 2334 |
""" |
| 2335 |
try: |
| 2336 |
os.setresgid(rgid, egid, sgid) |
| 2337 |
except OSError as e: |
| 2338 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2339 |
|
| 2340 |
@unwrap_spec(which=int, who=int) |
| 2341 |
def getpriority(space, which, who): |
| 2342 |
""" getpriority(which, who) -> int |
| 2343 |
|
| 2344 |
Get program scheduling priority. |
| 2345 |
""" |
| 2346 |
try: |
| 2347 |
returned_priority = rposix.getpriority(which, who) |
| 2348 |
except OSError as e: |
| 2349 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2350 |
return space.newint(returned_priority) |
| 2351 |
|
| 2352 |
@unwrap_spec(which=int, who=int, priority=int) |
| 2353 |
def setpriority(space, which, who, priority): |
| 2354 |
""" setpriority(which, who, priority) |
| 2355 |
|
| 2356 |
Set program scheduling priority. |
| 2357 |
""" |
| 2358 |
try: |
| 2359 |
rposix.setpriority(which, who, priority) |
| 2360 |
except OSError as e: |
| 2361 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2362 |
|
| 2363 |
def declare_new_w_star(name): |
| 2364 |
if name in ('WEXITSTATUS', 'WSTOPSIG', 'WTERMSIG'): |
| 2365 |
@unwrap_spec(status=c_int) |
| 2366 |
def WSTAR(space, status): |
| 2367 |
return space.newint(getattr(os, name)(status)) |
| 2368 |
else: |
| 2369 |
@unwrap_spec(status=c_int) |
| 2370 |
def WSTAR(space, status): |
| 2371 |
return space.newbool(getattr(os, name)(status)) |
| 2372 |
WSTAR.__doc__ = getattr(os, name).__doc__ |
| 2373 |
WSTAR.func_name = name |
| 2374 |
return WSTAR |
| 2375 |
|
| 2376 |
for name in rposix.WAIT_MACROS: |
| 2377 |
if hasattr(os, name): |
| 2378 |
func = declare_new_w_star(name) |
| 2379 |
globals()[name] = func |
| 2380 |
|
| 2381 |
|
| 2382 |
@unwrap_spec(fd=c_int) |
| 2383 |
def ttyname(space, fd): |
| 2384 |
try: |
| 2385 |
return space.newfilename(os.ttyname(fd)) |
| 2386 |
except OSError as e: |
| 2387 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2388 |
|
| 2389 |
|
| 2390 |
def confname_w(space, w_name, namespace): |
| 2391 |
if space.isinstance_w(w_name, space.w_unicode): |
| 2392 |
try: |
| 2393 |
num = namespace[space.text_w(w_name)] |
| 2394 |
except KeyError: |
| 2395 |
raise oefmt(space.w_ValueError, "unrecognized configuration name") |
| 2396 |
else: |
| 2397 |
num = space.int_w(w_name) |
| 2398 |
return num |
| 2399 |
|
| 2400 |
def sysconf(space, w_name): |
| 2401 |
num = confname_w(space, w_name, rposix.sysconf_names) |
| 2402 |
try: |
| 2403 |
res = os.sysconf(num) |
| 2404 |
except OSError as e: |
| 2405 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2406 |
return space.newint(res) |
| 2407 |
|
| 2408 |
def sysconf_names(): |
| 2409 |
return rposix.sysconf_names |
| 2410 |
|
| 2411 |
@unwrap_spec(fd=c_int) |
| 2412 |
def fpathconf(space, fd, w_name): |
| 2413 |
num = confname_w(space, w_name, rposix.pathconf_names) |
| 2414 |
try: |
| 2415 |
res = os.fpathconf(fd, num) |
| 2416 |
except OSError as e: |
| 2417 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2418 |
return space.newint(res) |
| 2419 |
|
| 2420 |
@unwrap_spec(path=path_or_fd(allow_fd=hasattr(os, 'fpathconf'))) |
| 2421 |
def pathconf(space, path, w_name): |
| 2422 |
num = confname_w(space, w_name, rposix.pathconf_names) |
| 2423 |
if path.as_fd != -1: |
| 2424 |
try: |
| 2425 |
res = os.fpathconf(path.as_fd, num) |
| 2426 |
except OSError as e: |
| 2427 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2428 |
else: |
| 2429 |
try: |
| 2430 |
res = os.pathconf(path.as_bytes, num) |
| 2431 |
except OSError as e: |
| 2432 |
raise wrap_oserror2(space, e, path.w_path, eintr_retry=False) |
| 2433 |
return space.newint(res) |
| 2434 |
|
| 2435 |
def pathconf_names(): |
| 2436 |
return rposix.pathconf_names |
| 2437 |
|
| 2438 |
def confstr(space, w_name): |
| 2439 |
num = confname_w(space, w_name, rposix.confstr_names) |
| 2440 |
try: |
| 2441 |
res = os.confstr(num) |
| 2442 |
except OSError as e: |
| 2443 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2444 |
return space.newtext(res) |
| 2445 |
|
| 2446 |
def confstr_names(): |
| 2447 |
return rposix.confstr_names |
| 2448 |
|
| 2449 |
@unwrap_spec( |
| 2450 |
uid=c_uid_t, gid=c_gid_t, |
| 2451 |
dir_fd=DirFD(rposix.HAVE_FCHOWNAT), follow_symlinks=bool) |
| 2452 |
def chown(space, w_path, uid, gid, __kwonly__, |
| 2453 |
dir_fd=DEFAULT_DIR_FD, follow_symlinks=True): |
| 2454 |
"""chown(path, uid, gid, *, dir_fd=None, follow_symlinks=True) |
| 2455 |
|
| 2456 |
Change the owner and group id of path to the numeric uid and gid. |
| 2457 |
|
| 2458 |
path may always be specified as a string. |
| 2459 |
On some platforms, path may also be specified as an open file descriptor. |
| 2460 |
If this functionality is unavailable, using it raises an exception. |
| 2461 |
If dir_fd is not None, it should be a file descriptor open to a directory, |
| 2462 |
and path should be relative; path will then be relative to that directory. |
| 2463 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2464 |
link, chown will modify the symbolic link itself instead of the file the |
| 2465 |
link points to. |
| 2466 |
It is an error to use dir_fd or follow_symlinks when specifying path as |
| 2467 |
an open file descriptor. |
| 2468 |
dir_fd and follow_symlinks may not be implemented on your platform. |
| 2469 |
If they are unavailable, using them will raise a NotImplementedError.""" |
| 2470 |
if not (rposix.HAVE_LCHOWN or rposix.HAVE_FCHMODAT): |
| 2471 |
if not follow_symlinks: |
| 2472 |
raise argument_unavailable(space, 'chown', 'follow_symlinks') |
| 2473 |
try: |
| 2474 |
path = space.fsencode_w(w_path) |
| 2475 |
except OperationError: |
| 2476 |
if not space.isinstance_w(w_path, space.w_int): |
| 2477 |
raise oefmt(space.w_TypeError, |
| 2478 |
"argument should be string, bytes or integer, not %T", w_path) |
| 2479 |
|
| 2480 |
fd = unwrap_fd(space, w_path) |
| 2481 |
if dir_fd != DEFAULT_DIR_FD: |
| 2482 |
raise oefmt(space.w_ValueError, |
| 2483 |
"chown: can't specify both dir_fd and fd") |
| 2484 |
if not follow_symlinks: |
| 2485 |
raise oefmt(space.w_ValueError, |
| 2486 |
"chown: cannnot use fd and follow_symlinks together") |
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
while True: |
| 2491 |
try: |
| 2492 |
os.fchown(fd, uid, gid) |
| 2493 |
return |
| 2494 |
except OSError as e: |
| 2495 |
wrap_oserror(space, e, eintr_retry=True) |
| 2496 |
while True: |
| 2497 |
|
| 2498 |
try: |
| 2499 |
if (rposix.HAVE_LCHOWN and |
| 2500 |
dir_fd == DEFAULT_DIR_FD and not follow_symlinks): |
| 2501 |
os.lchown(path, uid, gid) |
| 2502 |
elif rposix.HAVE_FCHOWNAT and ( |
| 2503 |
not follow_symlinks or dir_fd != DEFAULT_DIR_FD): |
| 2504 |
rposix.fchownat(path, uid, gid, dir_fd, follow_symlinks) |
| 2505 |
else: |
| 2506 |
assert follow_symlinks |
| 2507 |
assert dir_fd == DEFAULT_DIR_FD |
| 2508 |
os.chown(path, uid, gid) |
| 2509 |
break |
| 2510 |
except OSError as e: |
| 2511 |
wrap_oserror2(space, e, w_path, eintr_retry=True) |
| 2512 |
|
| 2513 |
|
| 2514 |
@unwrap_spec(uid=c_uid_t, gid=c_gid_t) |
| 2515 |
def lchown(space, w_path, uid, gid): |
| 2516 |
"""lchown(path, uid, gid) |
| 2517 |
|
| 2518 |
Change the owner and group id of path to the numeric uid and gid. |
| 2519 |
This function will not follow symbolic links. |
| 2520 |
Equivalent to os.chown(path, uid, gid, follow_symlinks=False).""" |
| 2521 |
path = space.fsencode_w(w_path) |
| 2522 |
try: |
| 2523 |
os.lchown(path, uid, gid) |
| 2524 |
except OSError as e: |
| 2525 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 2526 |
|
| 2527 |
@unwrap_spec(uid=c_uid_t, gid=c_gid_t) |
| 2528 |
def fchown(space, w_fd, uid, gid): |
| 2529 |
"""fchown(fd, uid, gid) |
| 2530 |
|
| 2531 |
Change the owner and group id of the file given by file descriptor |
| 2532 |
fd to the numeric uid and gid. Equivalent to os.chown(fd, uid, gid).""" |
| 2533 |
fd = space.c_filedescriptor_w(w_fd) |
| 2534 |
while True: |
| 2535 |
try: |
| 2536 |
os.fchown(fd, uid, gid) |
| 2537 |
break |
| 2538 |
except OSError as e: |
| 2539 |
wrap_oserror(space, e, eintr_retry=True) |
| 2540 |
|
| 2541 |
def getloadavg(space): |
| 2542 |
try: |
| 2543 |
load = os.getloadavg() |
| 2544 |
except OSError: |
| 2545 |
raise oefmt(space.w_OSError, "Load averages are unobtainable") |
| 2546 |
return space.newtuple([space.newfloat(load[0]), |
| 2547 |
space.newfloat(load[1]), |
| 2548 |
space.newfloat(load[2])]) |
| 2549 |
|
| 2550 |
@unwrap_spec(major=c_int, minor=c_int) |
| 2551 |
def makedev(space, major, minor): |
| 2552 |
result = os.makedev(major, minor) |
| 2553 |
return space.newint(result) |
| 2554 |
|
| 2555 |
@unwrap_spec(device="c_uint") |
| 2556 |
def major(space, device): |
| 2557 |
result = os.major(intmask(device)) |
| 2558 |
return space.newint(result) |
| 2559 |
|
| 2560 |
@unwrap_spec(device="c_uint") |
| 2561 |
def minor(space, device): |
| 2562 |
result = os.minor(intmask(device)) |
| 2563 |
return space.newint(result) |
| 2564 |
|
| 2565 |
@unwrap_spec(increment=c_int) |
| 2566 |
def nice(space, increment): |
| 2567 |
"""Decrease the priority of process by 'increment' |
| 2568 |
and return the new priority.""" |
| 2569 |
try: |
| 2570 |
res = os.nice(increment) |
| 2571 |
except OSError as e: |
| 2572 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2573 |
return space.newint(res) |
| 2574 |
|
| 2575 |
class SigCheck: |
| 2576 |
pass |
| 2577 |
_sigcheck = SigCheck() |
| 2578 |
def _signal_checker(): |
| 2579 |
_sigcheck.space.getexecutioncontext().checksignals() |
| 2580 |
|
| 2581 |
@unwrap_spec(size=int) |
| 2582 |
def urandom(space, size): |
| 2583 |
"""urandom(size) -> str |
| 2584 |
|
| 2585 |
Return a string of 'size' random bytes suitable for cryptographic use. |
| 2586 |
""" |
| 2587 |
if size < 0: |
| 2588 |
raise oefmt(space.w_ValueError, "negative argument not allowed") |
| 2589 |
try: |
| 2590 |
|
| 2591 |
|
| 2592 |
|
| 2593 |
_sigcheck.space = space |
| 2594 |
return space.newbytes(rurandom.urandom(size, _signal_checker)) |
| 2595 |
except OSError as e: |
| 2596 |
|
| 2597 |
|
| 2598 |
|
| 2599 |
|
| 2600 |
raise wrap_oserror(space, e, |
| 2601 |
w_exception_class=space.w_NotImplementedError, eintr_retry=False) |
| 2602 |
|
| 2603 |
def ctermid(space): |
| 2604 |
"""ctermid() -> string |
| 2605 |
|
| 2606 |
Return the name of the controlling terminal for this process. |
| 2607 |
""" |
| 2608 |
return space.newfilename(os.ctermid()) |
| 2609 |
|
| 2610 |
@unwrap_spec(fd=c_int) |
| 2611 |
def device_encoding(space, fd): |
| 2612 |
"""device_encoding(fd) -> str |
| 2613 |
|
| 2614 |
Return a string describing the encoding of the device if the output |
| 2615 |
is a terminal; else return None. |
| 2616 |
""" |
| 2617 |
if not (os.isatty(fd)): |
| 2618 |
return space.w_None |
| 2619 |
if _WIN32: |
| 2620 |
if fd == 0: |
| 2621 |
ccp = rwin32.GetConsoleCP() |
| 2622 |
elif fd in (1, 2): |
| 2623 |
ccp = rwin32.GetConsoleOutputCP() |
| 2624 |
else: |
| 2625 |
ccp = 0 |
| 2626 |
|
| 2627 |
|
| 2628 |
if ccp != 0: |
| 2629 |
return space.newtext('cp%d' % ccp) |
| 2630 |
|
| 2631 |
if space.sys.get_flag('utf8_mode'): |
| 2632 |
return space.newtext("utf-8") |
| 2633 |
from rpython.rlib import rlocale |
| 2634 |
if rlocale.HAVE_LANGINFO: |
| 2635 |
codeset = rlocale.nl_langinfo(rlocale.CODESET) |
| 2636 |
if codeset: |
| 2637 |
return space.newtext(codeset) |
| 2638 |
return space.w_None |
| 2639 |
|
| 2640 |
if _WIN32: |
| 2641 |
from pypy.module.posix import interp_nt as nt |
| 2642 |
|
| 2643 |
@unwrap_spec(fd=c_int) |
| 2644 |
def _getfileinformation(space, fd): |
| 2645 |
try: |
| 2646 |
info = nt._getfileinformation(fd) |
| 2647 |
except OSError as e: |
| 2648 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2649 |
return space.newtuple([space.newint(info[0]), |
| 2650 |
space.newint(info[1]), |
| 2651 |
space.newint(info[2])]) |
| 2652 |
|
| 2653 |
def _getfinalpathname(space, w_path): |
| 2654 |
path = space.fsdecode_w(w_path) |
| 2655 |
if '\x00' in path: |
| 2656 |
raise oefmt(space.w_ValueError, "embedded null character") |
| 2657 |
try: |
| 2658 |
s, lgt = nt._getfinalpathname(path) |
| 2659 |
except nt.LLNotImplemented as e: |
| 2660 |
raise OperationError(space.w_NotImplementedError, |
| 2661 |
space.newtext(e.msg)) |
| 2662 |
except OSError as e: |
| 2663 |
raise wrap_oserror2(space, e, w_path, eintr_retry=False) |
| 2664 |
return space.newtext(s, lgt) |
| 2665 |
|
| 2666 |
@unwrap_spec(fd=c_int) |
| 2667 |
def get_handle_inheritable(space, fd): |
| 2668 |
handle = rffi.cast(rwin32.HANDLE, fd) |
| 2669 |
try: |
| 2670 |
return space.newbool(rwin32.get_handle_inheritable(handle)) |
| 2671 |
except OSError as e: |
| 2672 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2673 |
|
| 2674 |
@unwrap_spec(fd=c_int, inheritable=bool) |
| 2675 |
def set_handle_inheritable(space, fd, inheritable): |
| 2676 |
handle = rffi.cast(rwin32.HANDLE, fd) |
| 2677 |
try: |
| 2678 |
rwin32.set_handle_inheritable(handle, inheritable) |
| 2679 |
except OSError as e: |
| 2680 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2681 |
|
| 2682 |
|
| 2683 |
def chflags(): |
| 2684 |
"""chflags(path, flags, *, follow_symlinks=True) |
| 2685 |
|
| 2686 |
Set file flags. |
| 2687 |
|
| 2688 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2689 |
link, chflags will change flags on the symbolic link itself instead of the |
| 2690 |
file the link points to. |
| 2691 |
follow_symlinks may not be implemented on your platform. If it is |
| 2692 |
unavailable, using it will raise a NotImplementedError.""" |
| 2693 |
|
| 2694 |
def lchflags(): |
| 2695 |
"""lchflags(path, flags) |
| 2696 |
|
| 2697 |
Set file flags. |
| 2698 |
This function will not follow symbolic links. |
| 2699 |
Equivalent to chflags(path, flags, follow_symlinks=False).""" |
| 2700 |
|
| 2701 |
@unwrap_spec(path=path_or_fd(), attribute=path_or_fd(allow_fd=False), |
| 2702 |
follow_symlinks=bool) |
| 2703 |
def getxattr(space, path, attribute, __kwonly__, follow_symlinks=True): |
| 2704 |
"""getxattr(path, attribute, *, follow_symlinks=True) -> value |
| 2705 |
|
| 2706 |
Return the value of extended attribute attribute on path. |
| 2707 |
|
| 2708 |
path may be either a string or an open file descriptor. |
| 2709 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2710 |
link, getxattr will examine the symbolic link itself instead of the file |
| 2711 |
the link points to.""" |
| 2712 |
if path.as_fd != -1: |
| 2713 |
if not follow_symlinks: |
| 2714 |
raise oefmt(space.w_ValueError, |
| 2715 |
"getxattr: cannot use fd and follow_symlinks together") |
| 2716 |
try: |
| 2717 |
result = rposix.fgetxattr(path.as_fd, attribute.as_bytes) |
| 2718 |
except OSError as e: |
| 2719 |
raise wrap_oserror2(space, e, path.w_path) |
| 2720 |
else: |
| 2721 |
try: |
| 2722 |
result = rposix.getxattr(path.as_bytes, attribute.as_bytes, |
| 2723 |
follow_symlinks=follow_symlinks) |
| 2724 |
except OSError as e: |
| 2725 |
raise wrap_oserror2(space, e, path.w_path) |
| 2726 |
return space.newbytes(result) |
| 2727 |
|
| 2728 |
@unwrap_spec(path=path_or_fd(), attribute=path_or_fd(allow_fd=False), |
| 2729 |
flags=c_int, |
| 2730 |
follow_symlinks=bool) |
| 2731 |
def setxattr(space, path, attribute, w_value, flags=0, |
| 2732 |
__kwonly__=None, follow_symlinks=True): |
| 2733 |
"""setxattr(path, attribute, value, flags=0, *, follow_symlinks=True) |
| 2734 |
|
| 2735 |
Set extended attribute attribute on path to value. |
| 2736 |
path may be either a string or an open file descriptor. |
| 2737 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2738 |
link, setxattr will modify the symbolic link itself instead of the file |
| 2739 |
the link points to.""" |
| 2740 |
value = space.charbuf_w(w_value) |
| 2741 |
if path.as_fd != -1: |
| 2742 |
if not follow_symlinks: |
| 2743 |
raise oefmt(space.w_ValueError, |
| 2744 |
"setxattr: cannot use fd and follow_symlinks together") |
| 2745 |
try: |
| 2746 |
rposix.fsetxattr(path.as_fd, attribute.as_bytes, value, flags) |
| 2747 |
except OSError as e: |
| 2748 |
raise wrap_oserror2(space, e, path.w_path) |
| 2749 |
else: |
| 2750 |
try: |
| 2751 |
rposix.setxattr(path.as_bytes, attribute.as_bytes, value, flags, |
| 2752 |
follow_symlinks=follow_symlinks) |
| 2753 |
except OSError as e: |
| 2754 |
raise wrap_oserror2(space, e, path.w_path) |
| 2755 |
|
| 2756 |
|
| 2757 |
@unwrap_spec(path=path_or_fd(), attribute=path_or_fd(allow_fd=False), |
| 2758 |
follow_symlinks=bool) |
| 2759 |
def removexattr(space, path, attribute, __kwonly__, follow_symlinks=True): |
| 2760 |
"""removexattr(path, attribute, *, follow_symlinks=True) |
| 2761 |
|
| 2762 |
Remove extended attribute attribute on path. |
| 2763 |
path may be either a string or an open file descriptor. |
| 2764 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2765 |
link, removexattr will modify the symbolic link itself instead of the file |
| 2766 |
the link points to.""" |
| 2767 |
if path.as_fd != -1: |
| 2768 |
if not follow_symlinks: |
| 2769 |
raise oefmt(space.w_ValueError, |
| 2770 |
"removexattr: cannot use fd and follow_symlinks together") |
| 2771 |
try: |
| 2772 |
rposix.fremovexattr(path.as_fd, attribute.as_bytes) |
| 2773 |
except OSError as e: |
| 2774 |
raise wrap_oserror2(space, e, path.w_path) |
| 2775 |
else: |
| 2776 |
try: |
| 2777 |
rposix.removexattr(path.as_bytes, attribute.as_bytes, |
| 2778 |
follow_symlinks=follow_symlinks) |
| 2779 |
except OSError as e: |
| 2780 |
raise wrap_oserror2(space, e, path.w_path) |
| 2781 |
|
| 2782 |
|
| 2783 |
@unwrap_spec(path=path_or_fd(), follow_symlinks=bool) |
| 2784 |
def listxattr(space, path, __kwonly__, follow_symlinks=True): |
| 2785 |
"""listxattr(path='.', *, follow_symlinks=True) |
| 2786 |
|
| 2787 |
Return a list of extended attributes on path. |
| 2788 |
|
| 2789 |
path may be either None, a string, or an open file descriptor. |
| 2790 |
if path is None, listxattr will examine the current directory. |
| 2791 |
If follow_symlinks is False, and the last element of the path is a symbolic |
| 2792 |
link, listxattr will examine the symbolic link itself instead of the file |
| 2793 |
the link points to.""" |
| 2794 |
if path.as_fd != -1: |
| 2795 |
if not follow_symlinks: |
| 2796 |
raise oefmt(space.w_ValueError, |
| 2797 |
"listxattr: cannot use fd and follow_symlinks together") |
| 2798 |
try: |
| 2799 |
result = rposix.flistxattr(path.as_fd) |
| 2800 |
except OSError as e: |
| 2801 |
raise wrap_oserror2(space, e, path.w_path) |
| 2802 |
else: |
| 2803 |
try: |
| 2804 |
result = rposix.listxattr(path.as_bytes, follow_symlinks) |
| 2805 |
except OSError as e: |
| 2806 |
raise wrap_oserror2(space, e, path.w_path) |
| 2807 |
return space.newlist([space.newfilename(attr) for attr in result]) |
| 2808 |
|
| 2809 |
@unwrap_spec(name='text', flags=int) |
| 2810 |
def memfd_create(space, name, flags=getattr(rposix, "MFD_CLOEXEC", 0xdead)): |
| 2811 |
""" |
| 2812 |
os.memfd_create(name[, flags=os.MFD_CLOEXEC]) |
| 2813 |
|
| 2814 |
Create an anonymous file and return a file descriptor that refers to it. flags |
| 2815 |
must be one of the os.MFD_* constants available on the system (or a bitwise |
| 2816 |
ORed combination of them). By default, the new file descriptor is |
| 2817 |
non-inheritable. |
| 2818 |
|
| 2819 |
The name supplied in name is used as a filename and will be displayed as the |
| 2820 |
target of the corresponding symbolic link in the directory /proc/self/fd/. The |
| 2821 |
displayed name is always prefixed with memfd: and serves only for debugging |
| 2822 |
purposes. Names do not affect the behavior of the file descriptor, and as such |
| 2823 |
multiple files can have the same name without any side effects. |
| 2824 |
""" |
| 2825 |
try: |
| 2826 |
result = rposix.memfd_create(name, flags) |
| 2827 |
except OSError as e: |
| 2828 |
raise wrap_oserror2(space, e) |
| 2829 |
return space.newint(result) |
| 2830 |
|
| 2831 |
|
| 2832 |
have_functions = [] |
| 2833 |
for name in """FACCESSAT FCHDIR FCHMOD FCHMODAT FCHOWN FCHOWNAT FEXECVE |
| 2834 |
FDOPENDIR FPATHCONF FSTATAT FSTATVFS FTRUNCATE FUTIMENS FUTIMES |
| 2835 |
FUTIMESAT LINKAT LCHFLAGS LCHMOD LCHOWN LSTAT LUTIMES |
| 2836 |
MKDIRAT MKFIFOAT MKNODAT OPENAT READLINKAT RENAMEAT |
| 2837 |
SYMLINKAT UNLINKAT UTIMENSAT""".split(): |
| 2838 |
if getattr(rposix, "HAVE_%s" % name): |
| 2839 |
have_functions.append("HAVE_%s" % name) |
| 2840 |
if _WIN32: |
| 2841 |
have_functions.append("MS_WINDOWS") |
| 2842 |
|
| 2843 |
def _get_terminal_size(space, w_fd=None): |
| 2844 |
if w_fd is None: |
| 2845 |
fd = rfile.RFile(rfile.c_stdout(), close2=(None, None)).fileno() |
| 2846 |
else: |
| 2847 |
if not space.isinstance_w(w_fd, space.w_int): |
| 2848 |
raise oefmt(space.w_TypeError, |
| 2849 |
"an integer is required, got %T", w_fd) |
| 2850 |
else: |
| 2851 |
fd = space.c_int_w(w_fd) |
| 2852 |
|
| 2853 |
if _WIN32: |
| 2854 |
if fd == 0: |
| 2855 |
handle_id = rwin32.STD_INPUT_HANDLE |
| 2856 |
elif fd == 1: |
| 2857 |
handle_id = rwin32.STD_OUTPUT_HANDLE |
| 2858 |
elif fd == 2: |
| 2859 |
handle_id = rwin32.STD_ERROR_HANDLE |
| 2860 |
else: |
| 2861 |
raise oefmt(space.w_ValueError, "bad file descriptor") |
| 2862 |
|
| 2863 |
handle = rwin32.GetStdHandle(handle_id) |
| 2864 |
|
| 2865 |
if handle == rwin32.NULL_HANDLE: |
| 2866 |
raise oefmt(space.w_OSError, "handle cannot be retrieved") |
| 2867 |
elif handle == rwin32.INVALID_HANDLE_VALUE: |
| 2868 |
raise rwin32.lastSavedWindowsError() |
| 2869 |
with lltype.scoped_alloc(rwin32.CONSOLE_SCREEN_BUFFER_INFO) as buffer_info: |
| 2870 |
success = rwin32.GetConsoleScreenBufferInfo(handle, buffer_info) |
| 2871 |
if not success: |
| 2872 |
raise rwin32.lastSavedWindowsError() |
| 2873 |
w_columns = space.newint(r_int(buffer_info.c_srWindow.c_Right) - r_int(buffer_info.c_srWindow.c_Left) + 1) |
| 2874 |
w_lines = space.newint(r_int(buffer_info.c_srWindow.c_Bottom) - r_int(buffer_info.c_srWindow.c_Top) + 1) |
| 2875 |
else: |
| 2876 |
with lltype.scoped_alloc(rposix.WINSIZE) as winsize: |
| 2877 |
failed = rposix.c_ioctl_voidp(fd, rposix.TIOCGWINSZ, winsize) |
| 2878 |
if failed: |
| 2879 |
raise exception_from_saved_errno(space, space.w_OSError) |
| 2880 |
|
| 2881 |
w_columns = space.newint(r_uint(winsize.c_ws_col)) |
| 2882 |
w_lines = space.newint(r_uint(winsize.c_ws_row)) |
| 2883 |
return w_columns, w_lines |
| 2884 |
|
| 2885 |
def get_terminal_size(space, w_fd=None): |
| 2886 |
try: |
| 2887 |
w_columns, w_lines = _get_terminal_size(space, w_fd) |
| 2888 |
except OSError as e: |
| 2889 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2890 |
w_tuple = space.newtuple([w_columns, w_lines]) |
| 2891 |
w_terminal_size = space.getattr(space.getbuiltinmodule(os.name), |
| 2892 |
space.newtext('terminal_size')) |
| 2893 |
|
| 2894 |
return space.call_function(w_terminal_size, w_tuple) |
| 2895 |
|
| 2896 |
def cpu_count(space): |
| 2897 |
count = rposix.cpu_count() |
| 2898 |
if count <= 0: |
| 2899 |
return space.w_None |
| 2900 |
return space.newint(count) |
| 2901 |
|
| 2902 |
@unwrap_spec(fd=c_int) |
| 2903 |
def get_blocking(space, fd): |
| 2904 |
"""get_blocking(fd) -> bool |
| 2905 |
|
| 2906 |
Get the blocking mode of the file descriptor: |
| 2907 |
False if the O_NONBLOCK flag is set, True if the flag is cleared.""" |
| 2908 |
try: |
| 2909 |
flags = rposix.get_status_flags(fd) |
| 2910 |
except OSError as e: |
| 2911 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2912 |
return space.newbool(flags & rposix.O_NONBLOCK == 0) |
| 2913 |
|
| 2914 |
@unwrap_spec(fd=c_int, blocking=int) |
| 2915 |
def set_blocking(space, fd, blocking): |
| 2916 |
"""\ |
| 2917 |
set_blocking(fd, blocking) |
| 2918 |
|
| 2919 |
Set the blocking mode of the specified file descriptor. |
| 2920 |
Set the O_NONBLOCK flag if blocking is False, |
| 2921 |
clear the O_NONBLOCK flag otherwise.""" |
| 2922 |
try: |
| 2923 |
flags = rposix.get_status_flags(fd) |
| 2924 |
if blocking: |
| 2925 |
flags &= ~rposix.O_NONBLOCK |
| 2926 |
else: |
| 2927 |
flags |= rposix.O_NONBLOCK |
| 2928 |
rposix.set_status_flags(fd, flags) |
| 2929 |
except OSError as e: |
| 2930 |
raise wrap_oserror(space, e, eintr_retry=False) |
| 2931 |
|
| 2932 |
@unwrap_spec(out_fd=c_int, count=int) |
| 2933 |
def sendfile(space, out_fd, w_in_fd, w_offset, count): |
| 2934 |
"""\ |
| 2935 |
sendfile(out_fd, in_fd, offset, count[, headers][, trailers], flags=0) |
| 2936 |
-> byteswritten |
| 2937 |
Copy count bytes from file descriptor in to file descriptor out.""" |
| 2938 |
|
| 2939 |
|
| 2940 |
in_ = space.c_int_w(w_in_fd) |
| 2941 |
|
| 2942 |
|
| 2943 |
|
| 2944 |
if space.is_none(w_offset): |
| 2945 |
if not _LINUX: |
| 2946 |
raise oefmt(space.w_TypeError, "an integer is required (got None)") |
| 2947 |
while True: |
| 2948 |
try: |
| 2949 |
res = rposix.sendfile_no_offset(out_fd, in_, count) |
| 2950 |
break |
| 2951 |
except OSError as e: |
| 2952 |
wrap_oserror(space, e, eintr_retry=True) |
| 2953 |
else: |
| 2954 |
offset = space.gateway_r_longlong_w(w_offset) |
| 2955 |
while True: |
| 2956 |
try: |
| 2957 |
res = rposix.sendfile(out_fd, in_, offset, count) |
| 2958 |
break |
| 2959 |
except OSError as e: |
| 2960 |
wrap_oserror(space, e, eintr_retry=True) |
| 2961 |
return space.newint(res) |
| 2962 |
|
| 2963 |
@unwrap_spec(policy=int) |
| 2964 |
def sched_get_priority_max(space, policy): |
| 2965 |
"""returns the maximum priority value that |
| 2966 |
can be used with the scheduling algorithm |
| 2967 |
identified by policy |
| 2968 |
""" |
| 2969 |
while True: |
| 2970 |
try: |
| 2971 |
s = rposix.sched_get_priority_max(policy) |
| 2972 |
except OSError as e: |
| 2973 |
wrap_oserror(space, e, eintr_retry=True) |
| 2974 |
else: |
| 2975 |
return space.newint(s) |
| 2976 |
|
| 2977 |
@unwrap_spec(policy=int) |
| 2978 |
def sched_get_priority_min(space, policy): |
| 2979 |
"""returns the minimum priority value that |
| 2980 |
can be used with the scheduling algorithm |
| 2981 |
identified by policy |
| 2982 |
""" |
| 2983 |
while True: |
| 2984 |
try: |
| 2985 |
s = rposix.sched_get_priority_min(policy) |
| 2986 |
except OSError as e: |
| 2987 |
wrap_oserror(space, e, eintr_retry=True) |
| 2988 |
else: |
| 2989 |
return space.newint(s) |
| 2990 |
|
| 2991 |
@unwrap_spec(fd=c_int, cmd=c_int, length=r_longlong) |
| 2992 |
def lockf(space, fd, cmd, length): |
| 2993 |
"""apply, test or remove a POSIX lock on an |
| 2994 |
open file. |
| 2995 |
""" |
| 2996 |
while True: |
| 2997 |
try: |
| 2998 |
s = rposix.lockf(fd, cmd, length) |
| 2999 |
except OSError as e: |
| 3000 |
wrap_oserror(space, e, eintr_retry=True) |
| 3001 |
else: |
| 3002 |
return space.newint(s) |
| 3003 |
|
| 3004 |
def sched_yield(space): |
| 3005 |
""" Voluntarily relinquish the CPU""" |
| 3006 |
while True: |
| 3007 |
try: |
| 3008 |
res = rposix.sched_yield() |
| 3009 |
except OSError as e: |
| 3010 |
wrap_oserror(space, e, eintr_retry=True) |
| 3011 |
else: |
| 3012 |
return space.newint(res) |
| 3013 |
|
| 3014 |
def fspath(space, w_path): |
| 3015 |
""" |
| 3016 |
Return the file system path representation of the object. |
| 3017 |
|
| 3018 |
If the object is str or bytes, then allow it to pass through as-is. If the |
| 3019 |
object defines __fspath__(), then return the result of that method. All other |
| 3020 |
types raise a TypeError. |
| 3021 |
""" |
| 3022 |
if (space.isinstance_w(w_path, space.w_text) or |
| 3023 |
space.isinstance_w(w_path, space.w_bytes)): |
| 3024 |
return w_path |
| 3025 |
|
| 3026 |
w_fspath_method = space.lookup(w_path, '__fspath__') |
| 3027 |
if w_fspath_method is None: |
| 3028 |
raise oefmt( |
| 3029 |
space.w_TypeError, |
| 3030 |
'expected str, bytes or os.PathLike object, not %T', |
| 3031 |
w_path |
| 3032 |
) |
| 3033 |
|
| 3034 |
w_result = space.get_and_call_function(w_fspath_method, w_path) |
| 3035 |
if (space.isinstance_w(w_result, space.w_text) or |
| 3036 |
space.isinstance_w(w_result, space.w_bytes)): |
| 3037 |
return w_result |
| 3038 |
|
| 3039 |
raise oefmt( |
| 3040 |
space.w_TypeError, |
| 3041 |
'expected %T.__fspath__() to return str or bytes, not %T', |
| 3042 |
w_path, |
| 3043 |
w_result |
| 3044 |
) |
| 3045 |
|
| 3046 |
|
| 3047 |
@unwrap_spec(pid=int) |
| 3048 |
def sched_rr_get_interval(space, pid): |
| 3049 |
""" get execution time limits. """ |
| 3050 |
|
| 3051 |
try: |
| 3052 |
res = rposix.sched_rr_get_interval(pid) |
| 3053 |
except OSError as e: |
| 3054 |
wrap_oserror(space, e, eintr_retry=True) |
| 3055 |
else: |
| 3056 |
return space.newfloat(res) |
| 3057 |
|
| 3058 |
|
| 3059 |
@unwrap_spec(pid=int) |
| 3060 |
def sched_getscheduler(space, pid): |
| 3061 |
""" get scheduling policy/parameters. """ |
| 3062 |
|
| 3063 |
try: |
| 3064 |
res = rposix.sched_getscheduler(pid) |
| 3065 |
except OSError as e: |
| 3066 |
wrap_oserror(space, e, eintr_retry=True) |
| 3067 |
else: |
| 3068 |
return space.newint(res) |
| 3069 |
|
| 3070 |
|
| 3071 |
@unwrap_spec(pid=int, policy=int) |
| 3072 |
def sched_setscheduler(space, pid, policy, w_param): |
| 3073 |
""" set scheduling policy/parameters. """ |
| 3074 |
w_sched_param = space.getattr(space.getbuiltinmodule(os.name), |
| 3075 |
space.newtext('sched_param')) |
| 3076 |
if not space.isinstance_w(w_param, w_sched_param): |
| 3077 |
raise oefmt(space.w_TypeError, "must have a sched_param object") |
| 3078 |
priority = space.int_w(space.getitem(w_param, space.newint(0))) |
| 3079 |
if priority > INT_MAX or priority < INT_MIN: |
| 3080 |
raise oefmt(space.w_OverflowError, "sched_priority %d out of range", priority) |
| 3081 |
try: |
| 3082 |
res = rposix.sched_setscheduler(pid, policy, priority) |
| 3083 |
except OSError as e: |
| 3084 |
wrap_oserror(space, e, eintr_retry=True) |
| 3085 |
else: |
| 3086 |
return space.newint(res) |
| 3087 |
|
| 3088 |
|
| 3089 |
@unwrap_spec(pid=int) |
| 3090 |
def sched_getparam(space, pid): |
| 3091 |
""" get scheduling parameters. """ |
| 3092 |
|
| 3093 |
try: |
| 3094 |
res = rposix.sched_getparam(pid) |
| 3095 |
except OSError as e: |
| 3096 |
wrap_oserror(space, e, eintr_retry=True) |
| 3097 |
else: |
| 3098 |
w_sched_param = space.getattr(space.getbuiltinmodule(os.name), |
| 3099 |
space.newtext('sched_param')) |
| 3100 |
|
| 3101 |
return space.call_function(w_sched_param, space.newint(res)) |
| 3102 |
|
| 3103 |
|
| 3104 |
@unwrap_spec(pid=int, ) |
| 3105 |
def sched_setparam(space, pid, w_param): |
| 3106 |
""" set scheduling parameters. """ |
| 3107 |
w_sched_param = space.getattr(space.getbuiltinmodule(os.name), |
| 3108 |
space.newtext('sched_param')) |
| 3109 |
if not space.isinstance_w(w_param, w_sched_param): |
| 3110 |
raise oefmt(space.w_TypeError, "must have a sched_param object") |
| 3111 |
priority = space.int_w(space.getitem(w_param, space.newint(0))) |
| 3112 |
if priority > INT_MAX or priority < INT_MIN: |
| 3113 |
raise oefmt(space.w_OverflowError, "sched_priority out of range") |
| 3114 |
try: |
| 3115 |
res = rposix.sched_setparam(pid, priority) |
| 3116 |
except OSError as e: |
| 3117 |
wrap_oserror(space, e, eintr_retry=True) |
| 3118 |
else: |
| 3119 |
return space.newint(res) |
| 3120 |
|
| 3121 |
def splitdrive(p): |
| 3122 |
|
| 3123 |
|
| 3124 |
|
| 3125 |
|
| 3126 |
|
| 3127 |
if len(p) >= 2: |
| 3128 |
if isinstance(p, bytes): |
| 3129 |
sep = b'\\' |
| 3130 |
altsep = b'/' |
| 3131 |
colon = b':' |
| 3132 |
else: |
| 3133 |
sep = '\\' |
| 3134 |
altsep = '/' |
| 3135 |
colon = ':' |
| 3136 |
normp = p.replace(altsep, sep) |
| 3137 |
if (normp[0:2] == sep*2) and (normp[2:3] != sep): |
| 3138 |
|
| 3139 |
|
| 3140 |
|
| 3141 |
|
| 3142 |
index = normp.find(sep, 2) |
| 3143 |
if index < 0: |
| 3144 |
return p[:0], p |
| 3145 |
index2 = normp.find(sep, index + 1) |
| 3146 |
|
| 3147 |
|
| 3148 |
if index2 == index + 1: |
| 3149 |
return p[:0], p |
| 3150 |
if index2 < 0: |
| 3151 |
index2 = len(p) |
| 3152 |
return p[:index2+1], p[index2+1:] |
| 3153 |
if normp[1:2] == colon and normp[2:3] == sep: |
| 3154 |
return p[:3], p[3:] |
| 3155 |
elif normp[1:2] == colon: |
| 3156 |
return p[:2], p[2:] |
| 3157 |
return p[:0], p |
| 3158 |
|
| 3159 |
def _path_splitroot(space, w_path): |
| 3160 |
"""Removes everything after the root on Win32.""" |
| 3161 |
|
| 3162 |
|
| 3163 |
|
| 3164 |
|
| 3165 |
p = space.text_w(fspath(space, w_path)) |
| 3166 |
ret0, ret1 = splitdrive(p) |
| 3167 |
|
| 3168 |
return space.newtuple([space.newtext(ret0), space.newtext(ret1)]) |
| 3169 |
|
| 3170 |
class W_DLLCapsule(W_Root): |
| 3171 |
|
| 3172 |
def __init__(self, cookie): |
| 3173 |
self.cookie = cookie |
| 3174 |
|
| 3175 |
def _add_dll_directory(space, w_path): |
| 3176 |
"""os._add_dll_directory |
| 3177 |
|
| 3178 |
path: path_t |
| 3179 |
|
| 3180 |
Add a path to the DLL search path. |
| 3181 |
|
| 3182 |
This search path is used when resolving dependencies for imported |
| 3183 |
extension modules (the module itself is resolved through sys.path), |
| 3184 |
and also by ctypes. |
| 3185 |
|
| 3186 |
Returns an opaque value that may be passed to os.remove_dll_directory |
| 3187 |
to remove this directory from the search path. |
| 3188 |
""" |
| 3189 |
space.audit("os.add_dll_directory", [w_path]) |
| 3190 |
cookie = rwin32.AddDllDirectory(space.utf8_w(w_path), space.len_w(w_path)) |
| 3191 |
return W_DLLCapsule(cookie) |
| 3192 |
|
| 3193 |
def _remove_dll_directory(space, w_cookie): |
| 3194 |
"""os._remove_dll_directory |
| 3195 |
|
| 3196 |
cookie: object |
| 3197 |
|
| 3198 |
Removes a path from the DLL search path. |
| 3199 |
|
| 3200 |
The parameter is an opaque value that was returned from |
| 3201 |
os.add_dll_directory. You can only remove directories that you added |
| 3202 |
yourself. |
| 3203 |
""" |
| 3204 |
|
| 3205 |
if not isinstance(w_cookie, W_DLLCapsule): |
| 3206 |
raise oefmt(space.w_TypeError, "Provided cookie was not returned " |
| 3207 |
"from os.add_dll_directory") |
| 3208 |
cookie = w_cookie.cookie |
| 3209 |
|
| 3210 |
return space.newbool(bool(rwin32.RemoveDllDirectory(cookie))) |
| 3211 |
|
| 3212 |
def _supports_virtual_terminal(space): |
| 3213 |
"""Checks if virtual terminal is supported in windows |
| 3214 |
Currently implemented as a check on the stderr handle's mode for |
| 3215 |
ENABLE_VIRTUAL_TERMINAL_PROCESSING |
| 3216 |
""" |
| 3217 |
from rpython.rlib.rwin32 import (_GetStdHandle, GetConsoleMode, |
| 3218 |
STD_ERROR_HANDLE, ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
| 3219 |
handle = _GetStdHandle(STD_ERROR_HANDLE) |
| 3220 |
if handle == rwin32.INVALID_HANDLE_VALUE: |
| 3221 |
return space.newbool(False) |
| 3222 |
mode = lltype.malloc(rwin32.LPDWORD.TO, 1, flavor='raw') |
| 3223 |
try: |
| 3224 |
if not rwin32.GetConsoleMode(handle, mode): |
| 3225 |
return space.newbool(False) |
| 3226 |
res = bool(widen(mode[0]) & ENABLE_VIRTUAL_TERMINAL_PROCESSING) |
| 3227 |
return space.newbool(res) |
| 3228 |
finally: |
| 3229 |
lltype.free(mode, flavor='raw') |
| 3230 |
|