| 1 |
import sys |
| 2 |
from pypy.interpreter.error import OperationError, oefmt, wrap_oserror |
| 3 |
from pypy.interpreter.baseobjspace import W_Root |
| 4 |
from pypy.interpreter.typedef import TypeDef, GetSetProperty, make_weakref_descr |
| 5 |
from pypy.interpreter.gateway import interp2app, unwrap_spec, WrappedDefault |
| 6 |
from pypy.interpreter.buffer import SimpleView |
| 7 |
|
| 8 |
from rpython.rlib import rmmap, rarithmetic, objectmodel |
| 9 |
from rpython.rlib.buffer import RawBuffer |
| 10 |
from rpython.rlib.rmmap import RValueError, RTypeError, RMMapError |
| 11 |
from rpython.rlib.rstring import StringBuilder |
| 12 |
|
| 13 |
if rmmap.HAVE_LARGEFILE_SUPPORT: |
| 14 |
OFF_T = rarithmetic.r_longlong |
| 15 |
else: |
| 16 |
OFF_T = int |
| 17 |
|
| 18 |
|
| 19 |
class W_MMap(W_Root): |
| 20 |
def __init__(self, space, mmap_obj): |
| 21 |
self.space = space |
| 22 |
self.mmap = mmap_obj |
| 23 |
|
| 24 |
def buffer_w(self, space, flags): |
| 25 |
self.check_valid() |
| 26 |
readonly = (self.mmap.access == ACCESS_READ) |
| 27 |
write_required = bool(flags & space.BUF_WRITABLE) |
| 28 |
if write_required and readonly: |
| 29 |
raise oefmt(space.w_BufferError, "Object is not writable.") |
| 30 |
return SimpleView(MMapBuffer(self.space, self.mmap, readonly), w_obj=self) |
| 31 |
|
| 32 |
def close(self): |
| 33 |
self.mmap.close() |
| 34 |
|
| 35 |
def read_byte(self): |
| 36 |
self.check_valid() |
| 37 |
try: |
| 38 |
return self.space.newint(ord(self.mmap.read_byte())) |
| 39 |
except RValueError as v: |
| 40 |
raise mmap_error(self.space, v) |
| 41 |
|
| 42 |
def readline(self): |
| 43 |
self.check_valid() |
| 44 |
return self.space.newbytes(self.mmap.readline()) |
| 45 |
|
| 46 |
@unwrap_spec(w_num=WrappedDefault(None)) |
| 47 |
def read(self, w_num): |
| 48 |
self.check_valid() |
| 49 |
if self.space.is_none(w_num): |
| 50 |
num = -1 |
| 51 |
else: |
| 52 |
num = self.space.int_w(w_num) |
| 53 |
self.check_valid() |
| 54 |
return self.space.newbytes(self.mmap.read(num)) |
| 55 |
|
| 56 |
def find(self, w_tofind, w_start=None, w_end=None): |
| 57 |
self.check_valid() |
| 58 |
space = self.space |
| 59 |
tofind = space.charbuf_w(w_tofind) |
| 60 |
if w_start is None: |
| 61 |
start = self.mmap.pos |
| 62 |
else: |
| 63 |
start = space.getindex_w(w_start, None) |
| 64 |
if w_end is None: |
| 65 |
end = self.mmap.size |
| 66 |
else: |
| 67 |
end = space.getindex_w(w_end, None) |
| 68 |
self.check_valid() |
| 69 |
return space.newint(self.mmap.find(tofind, start, end)) |
| 70 |
|
| 71 |
def rfind(self, w_tofind, w_start=None, w_end=None): |
| 72 |
self.check_valid() |
| 73 |
space = self.space |
| 74 |
tofind = space.charbuf_w(w_tofind) |
| 75 |
if w_start is None: |
| 76 |
start = self.mmap.pos |
| 77 |
else: |
| 78 |
start = space.getindex_w(w_start, None) |
| 79 |
if w_end is None: |
| 80 |
end = self.mmap.size |
| 81 |
else: |
| 82 |
end = space.getindex_w(w_end, None) |
| 83 |
self.check_valid() |
| 84 |
return space.newint(self.mmap.find(tofind, start, end, True)) |
| 85 |
|
| 86 |
@unwrap_spec(pos=OFF_T, whence=int) |
| 87 |
def seek(self, pos, whence=0): |
| 88 |
self.check_valid() |
| 89 |
try: |
| 90 |
self.mmap.seek(pos, whence) |
| 91 |
except RValueError as v: |
| 92 |
raise mmap_error(self.space, v) |
| 93 |
|
| 94 |
def tell(self): |
| 95 |
self.check_valid() |
| 96 |
return self.space.newint(self.mmap.tell()) |
| 97 |
|
| 98 |
def descr_size(self): |
| 99 |
self.check_valid() |
| 100 |
try: |
| 101 |
return self.space.newint(self.mmap.file_size()) |
| 102 |
except OSError as e: |
| 103 |
raise mmap_error(self.space, e) |
| 104 |
|
| 105 |
def write(self, w_data): |
| 106 |
self.check_valid() |
| 107 |
data = self.space.charbuf_w(w_data) |
| 108 |
self.check_writeable() |
| 109 |
try: |
| 110 |
return self.space.newint(self.mmap.write(data)) |
| 111 |
except RValueError as v: |
| 112 |
raise mmap_error(self.space, v) |
| 113 |
|
| 114 |
@unwrap_spec(byte=int) |
| 115 |
def write_byte(self, byte): |
| 116 |
self.check_valid() |
| 117 |
self.check_writeable() |
| 118 |
try: |
| 119 |
self.mmap.write_byte(chr(byte)) |
| 120 |
except RMMapError as v: |
| 121 |
raise mmap_error(self.space, v) |
| 122 |
|
| 123 |
@unwrap_spec(offset=int, size=int) |
| 124 |
def flush(self, offset=0, size=0): |
| 125 |
|
| 126 |
|
| 127 |
self.check_valid() |
| 128 |
try: |
| 129 |
self.mmap.flush(offset, size) |
| 130 |
return self.space.w_None |
| 131 |
except RValueError as v: |
| 132 |
raise mmap_error(self.space, v) |
| 133 |
except OSError as e: |
| 134 |
raise mmap_error(self.space, e) |
| 135 |
|
| 136 |
@unwrap_spec(dest=int, src=int, count=int) |
| 137 |
def move(self, dest, src, count): |
| 138 |
self.check_valid() |
| 139 |
self.check_writeable() |
| 140 |
try: |
| 141 |
self.mmap.move(dest, src, count) |
| 142 |
except RValueError as v: |
| 143 |
raise mmap_error(self.space, v) |
| 144 |
|
| 145 |
@unwrap_spec(newsize=int) |
| 146 |
def resize(self, newsize): |
| 147 |
self.check_valid() |
| 148 |
self.check_resizeable() |
| 149 |
try: |
| 150 |
self.mmap.resize(newsize) |
| 151 |
except OSError as e: |
| 152 |
raise mmap_error(self.space, e) |
| 153 |
except RValueError as e: |
| 154 |
|
| 155 |
|
| 156 |
raise OperationError(self.space.w_SystemError, |
| 157 |
self.space.newtext(e.message)) |
| 158 |
|
| 159 |
def __len__(self): |
| 160 |
self.check_valid() |
| 161 |
return self.space.newint(self.mmap.size) |
| 162 |
|
| 163 |
def closed_get(self, space): |
| 164 |
try: |
| 165 |
self.mmap.check_valid() |
| 166 |
except RValueError: |
| 167 |
return space.w_True |
| 168 |
return space.w_False |
| 169 |
|
| 170 |
def check_valid(self): |
| 171 |
try: |
| 172 |
self.mmap.check_valid() |
| 173 |
except RValueError as v: |
| 174 |
raise mmap_error(self.space, v) |
| 175 |
|
| 176 |
def check_writeable(self): |
| 177 |
try: |
| 178 |
self.mmap.check_writeable() |
| 179 |
except RMMapError as v: |
| 180 |
raise mmap_error(self.space, v) |
| 181 |
|
| 182 |
def check_resizeable(self): |
| 183 |
try: |
| 184 |
self.mmap.check_resizeable() |
| 185 |
except RMMapError as v: |
| 186 |
raise mmap_error(self.space, v) |
| 187 |
|
| 188 |
def descr_getitem(self, w_index): |
| 189 |
space = self.space |
| 190 |
start, stop, step, length = space.decode_index4(w_index, self) |
| 191 |
self.check_valid() |
| 192 |
if step == 0: |
| 193 |
return space.newint(ord(self.mmap.getitem(start))) |
| 194 |
elif step == 1: |
| 195 |
if stop - start < 0: |
| 196 |
return space.newbytes("") |
| 197 |
return space.newbytes(self.mmap.getslice(start, length)) |
| 198 |
else: |
| 199 |
b = StringBuilder(length) |
| 200 |
index = start |
| 201 |
for i in range(length): |
| 202 |
b.append(self.mmap.getitem(index)) |
| 203 |
index += step |
| 204 |
return space.newbytes(b.build()) |
| 205 |
|
| 206 |
def descr_setitem(self, w_index, w_value): |
| 207 |
space = self.space |
| 208 |
start, stop, step, length = space.decode_index4(w_index, self) |
| 209 |
self.check_valid() |
| 210 |
self.check_writeable() |
| 211 |
|
| 212 |
if step == 0: |
| 213 |
value = space.int_w(w_value) |
| 214 |
if not 0 <= value < 256: |
| 215 |
raise oefmt(space.w_ValueError, |
| 216 |
"mmap item value must be in range(0, 256)") |
| 217 |
self.mmap.setitem(start, chr(value)) |
| 218 |
else: |
| 219 |
try: |
| 220 |
value = space.bytes_w(w_value) |
| 221 |
except OperationError as e: |
| 222 |
if not e.match(space, space.w_TypeError): |
| 223 |
raise |
| 224 |
buf = space.buffer_w(w_value, space.BUF_FULL_RO) |
| 225 |
value = buf.as_str() |
| 226 |
if len(value) != length: |
| 227 |
raise oefmt(space.w_ValueError, |
| 228 |
"mmap slice assignment is wrong size") |
| 229 |
if step == 1: |
| 230 |
self.mmap.setslice(start, value) |
| 231 |
else: |
| 232 |
for i in range(length): |
| 233 |
self.mmap.setitem(start, value[i]) |
| 234 |
start += step |
| 235 |
|
| 236 |
def descr_enter(self, space): |
| 237 |
self.check_valid() |
| 238 |
return self |
| 239 |
|
| 240 |
def descr_exit(self, space, __args__): |
| 241 |
self.close() |
| 242 |
|
| 243 |
def descr_iter(self, space): |
| 244 |
return space.appexec([self], """(m): |
| 245 |
def iterate(): |
| 246 |
i = 0 |
| 247 |
while True: |
| 248 |
c = m[i:i+1] |
| 249 |
if not c: |
| 250 |
break |
| 251 |
yield c |
| 252 |
i += 1 |
| 253 |
return iterate() |
| 254 |
""") |
| 255 |
|
| 256 |
def descr_reversed(self, space): |
| 257 |
return space.appexec([self], """(m): |
| 258 |
def iterate(): |
| 259 |
i = len(m) - 1 |
| 260 |
while i >= 0: |
| 261 |
c = m[i:i+1] |
| 262 |
if not c: |
| 263 |
break |
| 264 |
yield c |
| 265 |
i -= 1 |
| 266 |
return iterate() |
| 267 |
""") |
| 268 |
|
| 269 |
@unwrap_spec(flags=int, start=int) |
| 270 |
def descr_madvise(self, space, flags, start=0, w_length=None): |
| 271 |
""" |
| 272 |
madvise(option[, start[, length]]) |
| 273 |
|
| 274 |
Send advice option to the kernel about the memory region beginning at |
| 275 |
start and extending length bytes. option must be one of the MADV_* |
| 276 |
constants available on the system. If start and length are omitted, |
| 277 |
the entire mapping is spanned. On some systems (including Linux), |
| 278 |
start must be a multiple of the PAGESIZE. |
| 279 |
""" |
| 280 |
if start < 0 or start >= self.mmap.size: |
| 281 |
raise oefmt(space.w_ValueError, "madvise start out of bounds") |
| 282 |
if w_length is None: |
| 283 |
length = self.mmap.size |
| 284 |
else: |
| 285 |
length = space.int_w(w_length) |
| 286 |
if length < 0: |
| 287 |
raise oefmt(space.w_ValueError, "madvise length invalid, can't be negative") |
| 288 |
try: |
| 289 |
end = rarithmetic.ovfcheck(start + length) |
| 290 |
except OverflowError: |
| 291 |
raise oefmt(space.w_OverflowError, "madvise length too large") |
| 292 |
else: |
| 293 |
if end > self.mmap.size: |
| 294 |
length = self.mmap.size - start |
| 295 |
self.mmap.madvise(flags, start, length) |
| 296 |
|
| 297 |
def descr_repr(self, space): |
| 298 |
try: |
| 299 |
self.mmap.check_valid() |
| 300 |
except RValueError: |
| 301 |
return space.newtext("<%s closed=True>" % space.getfulltypename(self)) |
| 302 |
if self.mmap.access == rmmap._ACCESS_DEFAULT: |
| 303 |
access_str = "ACCESS_DEFAULT" |
| 304 |
elif self.mmap.access == rmmap.ACCESS_READ: |
| 305 |
access_str = "ACCESS_READ" |
| 306 |
elif self.mmap.access == rmmap.ACCESS_WRITE: |
| 307 |
access_str = "ACCESS_WRITE" |
| 308 |
elif self.mmap.access == rmmap.ACCESS_COPY: |
| 309 |
access_str = "ACCESS_COPY" |
| 310 |
else: |
| 311 |
raise oefmt(space.w_RuntimeError, "invalid access mode in mmap") |
| 312 |
return space.newtext( |
| 313 |
"<%s closed=False, access=%s, length=%d, pos=%d, offset=%d>" %( |
| 314 |
space.getfulltypename(self), access_str, self.mmap.size, |
| 315 |
self.mmap.pos, self.mmap.offset)) |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
if rmmap._POSIX: |
| 320 |
|
| 321 |
@unwrap_spec(fileno=int, length=int, flags=int, |
| 322 |
prot=int, access=int, offset=OFF_T) |
| 323 |
def mmap(space, w_subtype, fileno, length, flags=rmmap.MAP_SHARED, |
| 324 |
prot=rmmap.PROT_WRITE | rmmap.PROT_READ, |
| 325 |
access=rmmap._ACCESS_DEFAULT, offset=0): |
| 326 |
space.audit("mmap.__new__", [ |
| 327 |
space.newint(fileno), space.newint(length), |
| 328 |
space.newint(access), space.newint(offset)]) |
| 329 |
self = space.allocate_instance(W_MMap, w_subtype) |
| 330 |
try: |
| 331 |
W_MMap.__init__(self, space, |
| 332 |
rmmap.mmap(fileno, length, flags, prot, access, |
| 333 |
offset)) |
| 334 |
except OSError as e: |
| 335 |
raise mmap_error(space, e) |
| 336 |
except RMMapError as e: |
| 337 |
raise mmap_error(space, e) |
| 338 |
return self |
| 339 |
|
| 340 |
elif rmmap._MS_WINDOWS: |
| 341 |
|
| 342 |
@unwrap_spec(fileno=int, length=int, tagname='text', |
| 343 |
access=int, offset=OFF_T) |
| 344 |
def mmap(space, w_subtype, fileno, length, tagname="", |
| 345 |
access=rmmap._ACCESS_DEFAULT, offset=0): |
| 346 |
space.audit("mmap.__new__", [ |
| 347 |
space.newint(fileno), space.newint(length), |
| 348 |
space.newint(access), space.newint(offset)]) |
| 349 |
self = space.allocate_instance(W_MMap, w_subtype) |
| 350 |
try: |
| 351 |
W_MMap.__init__(self, space, |
| 352 |
rmmap.mmap(fileno, length, tagname, access, |
| 353 |
offset)) |
| 354 |
except OSError as e: |
| 355 |
raise mmap_error(space, e) |
| 356 |
except RMMapError as e: |
| 357 |
raise mmap_error(space, e) |
| 358 |
return self |
| 359 |
|
| 360 |
optional = {} |
| 361 |
if rmmap.has_madvise: |
| 362 |
optional['madvise'] = interp2app(W_MMap.descr_madvise) |
| 363 |
|
| 364 |
W_MMap.typedef = TypeDef("mmap.mmap", None, None, 'read-write', |
| 365 |
__new__ = interp2app(mmap), |
| 366 |
close = interp2app(W_MMap.close), |
| 367 |
read_byte = interp2app(W_MMap.read_byte), |
| 368 |
readline = interp2app(W_MMap.readline), |
| 369 |
read = interp2app(W_MMap.read), |
| 370 |
find = interp2app(W_MMap.find), |
| 371 |
rfind = interp2app(W_MMap.rfind), |
| 372 |
seek = interp2app(W_MMap.seek), |
| 373 |
tell = interp2app(W_MMap.tell), |
| 374 |
size = interp2app(W_MMap.descr_size), |
| 375 |
write = interp2app(W_MMap.write), |
| 376 |
write_byte = interp2app(W_MMap.write_byte), |
| 377 |
flush = interp2app(W_MMap.flush), |
| 378 |
move = interp2app(W_MMap.move), |
| 379 |
resize = interp2app(W_MMap.resize), |
| 380 |
|
| 381 |
__len__ = interp2app(W_MMap.__len__), |
| 382 |
__getitem__ = interp2app(W_MMap.descr_getitem), |
| 383 |
__setitem__ = interp2app(W_MMap.descr_setitem), |
| 384 |
__enter__ = interp2app(W_MMap.descr_enter), |
| 385 |
__exit__ = interp2app(W_MMap.descr_exit), |
| 386 |
__weakref__ = make_weakref_descr(W_MMap), |
| 387 |
__iter__ = interp2app(W_MMap.descr_iter), |
| 388 |
__reversed__ = interp2app(W_MMap.descr_reversed), |
| 389 |
__repr__ = interp2app(W_MMap.descr_repr), |
| 390 |
|
| 391 |
closed = GetSetProperty(W_MMap.closed_get), |
| 392 |
|
| 393 |
**optional |
| 394 |
) |
| 395 |
|
| 396 |
constants = rmmap.constants |
| 397 |
PAGESIZE = rmmap.PAGESIZE |
| 398 |
ALLOCATIONGRANULARITY = rmmap.ALLOCATIONGRANULARITY |
| 399 |
ACCESS_READ = rmmap.ACCESS_READ |
| 400 |
ACCESS_WRITE = rmmap.ACCESS_WRITE |
| 401 |
ACCESS_COPY = rmmap.ACCESS_COPY |
| 402 |
ACCESS_DEFAULT = rmmap._ACCESS_DEFAULT |
| 403 |
|
| 404 |
|
| 405 |
@objectmodel.dont_inline |
| 406 |
def mmap_error(space, e): |
| 407 |
if isinstance(e, RValueError): |
| 408 |
return OperationError(space.w_ValueError, space.newtext(e.message)) |
| 409 |
elif isinstance(e, RTypeError): |
| 410 |
return OperationError(space.w_TypeError, space.newtext(e.message)) |
| 411 |
elif isinstance(e, OSError): |
| 412 |
return wrap_oserror(space, e) |
| 413 |
else: |
| 414 |
|
| 415 |
return OperationError(space.w_SystemError, space.newtext('%s' % e)) |
| 416 |
|
| 417 |
|
| 418 |
class MMapBuffer(RawBuffer): |
| 419 |
_immutable_ = True |
| 420 |
|
| 421 |
def __init__(self, space, mmap, readonly): |
| 422 |
self.space = space |
| 423 |
self.mmap = mmap |
| 424 |
self.readonly = readonly |
| 425 |
|
| 426 |
def getlength(self): |
| 427 |
return self.mmap.size |
| 428 |
|
| 429 |
def getitem(self, index): |
| 430 |
self.check_valid() |
| 431 |
return self.mmap.data[index] |
| 432 |
|
| 433 |
def getslice(self, start, step, size): |
| 434 |
self.check_valid() |
| 435 |
if step == 1: |
| 436 |
return self.mmap.getslice(start, size) |
| 437 |
else: |
| 438 |
return RawBuffer.getslice(self, start, step, size) |
| 439 |
|
| 440 |
def setitem(self, index, char): |
| 441 |
self.check_valid_writeable() |
| 442 |
self.mmap.data[index] = char |
| 443 |
|
| 444 |
def setslice(self, start, string): |
| 445 |
self.check_valid_writeable() |
| 446 |
self.mmap.setslice(start, string) |
| 447 |
|
| 448 |
def get_raw_address(self): |
| 449 |
self.check_valid() |
| 450 |
return self.mmap.data |
| 451 |
|
| 452 |
def check_valid(self): |
| 453 |
try: |
| 454 |
self.mmap.check_valid() |
| 455 |
except RValueError as v: |
| 456 |
raise mmap_error(self.space, v) |
| 457 |
|
| 458 |
def check_valid_writeable(self): |
| 459 |
try: |
| 460 |
self.mmap.check_valid() |
| 461 |
self.mmap.check_writeable() |
| 462 |
except RMMapError as v: |
| 463 |
raise mmap_error(self.space, v) |