Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(168922)

Side by Side Diff: Modules/mmapmodule.c

Issue 23001: Accept mutable bytes-like objects
Patch Set: Created 4 years, 9 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 / Author: Sam Rushing <rushing@nightmare.com> 2 / Author: Sam Rushing <rushing@nightmare.com>
3 / Hacked for Unix by AMK 3 / Hacked for Unix by AMK
4 / $Id$ 4 / $Id$
5 5
6 / Modified to support mmap with offset - to map a 'window' of a file 6 / Modified to support mmap with offset - to map a 'window' of a file
7 / Author: Yotam Medini yotamm@mellanox.co.il 7 / Author: Yotam Medini yotamm@mellanox.co.il
8 / 8 /
9 / mmapmodule.cpp -- map a view of a file into memory 9 / mmapmodule.cpp -- map a view of a file into memory
10 / 10 /
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 return result; 294 return result;
295 } 295 }
296 296
297 static PyObject * 297 static PyObject *
298 mmap_gfind(mmap_object *self, 298 mmap_gfind(mmap_object *self,
299 PyObject *args, 299 PyObject *args,
300 int reverse) 300 int reverse)
301 { 301 {
302 Py_ssize_t start = self->pos; 302 Py_ssize_t start = self->pos;
303 Py_ssize_t end = self->size; 303 Py_ssize_t end = self->size;
304 const char *needle; 304 Py_buffer view;
305 Py_ssize_t len;
306 305
307 CHECK_VALID(NULL); 306 CHECK_VALID(NULL);
308 if (!PyArg_ParseTuple(args, reverse ? "y#|nn:rfind" : "y#|nn:find", 307 if (!PyArg_ParseTuple(args, reverse ? "y*|nn:rfind" : "y*|nn:find",
309 &needle, &len, &start, &end)) { 308 &view, &start, &end)) {
310 return NULL; 309 return NULL;
311 } else { 310 } else {
312 const char *p, *start_p, *end_p; 311 const char *p, *start_p, *end_p;
313 int sign = reverse ? -1 : 1; 312 int sign = reverse ? -1 : 1;
313 const char *needle = view.buf;
314 Py_ssize_t len = view.len;
314 315
315 if (start < 0) 316 if (start < 0)
316 start += self->size; 317 start += self->size;
317 if (start < 0) 318 if (start < 0)
318 start = 0; 319 start = 0;
319 else if ((size_t)start > self->size) 320 else if ((size_t)start > self->size)
320 start = self->size; 321 start = self->size;
321 322
322 if (end < 0) 323 if (end < 0)
323 end += self->size; 324 end += self->size;
324 if (end < 0) 325 if (end < 0)
325 end = 0; 326 end = 0;
326 else if ((size_t)end > self->size) 327 else if ((size_t)end > self->size)
327 end = self->size; 328 end = self->size;
328 329
329 start_p = self->data + start; 330 start_p = self->data + start;
330 end_p = self->data + end; 331 end_p = self->data + end;
331 332
332 for (p = (reverse ? end_p - len : start_p); 333 for (p = (reverse ? end_p - len : start_p);
333 (p >= start_p) && (p + len <= end_p); p += sign) { 334 (p >= start_p) && (p + len <= end_p); p += sign) {
334 Py_ssize_t i; 335 Py_ssize_t i;
335 for (i = 0; i < len && needle[i] == p[i]; ++i) 336 for (i = 0; i < len && needle[i] == p[i]; ++i)
336 /* nothing */; 337 /* nothing */;
337 if (i == len) { 338 if (i == len) {
339 PyBuffer_Release(&view);
338 return PyLong_FromSsize_t(p - self->data); 340 return PyLong_FromSsize_t(p - self->data);
339 } 341 }
340 } 342 }
343 PyBuffer_Release(&view);
341 return PyLong_FromLong(-1); 344 return PyLong_FromLong(-1);
342 } 345 }
343 } 346 }
344 347
345 static PyObject * 348 static PyObject *
346 mmap_find_method(mmap_object *self, 349 mmap_find_method(mmap_object *self,
347 PyObject *args) 350 PyObject *args)
348 { 351 {
349 return mmap_gfind(self, args, 0); 352 return mmap_gfind(self, args, 0);
350 } 353 }
(...skipping 27 matching lines...) Expand all
378 PyErr_Format(PyExc_TypeError, 381 PyErr_Format(PyExc_TypeError,
379 "mmap can't resize a readonly or copy-on-write memory map."); 382 "mmap can't resize a readonly or copy-on-write memory map.");
380 return 0; 383 return 0;
381 } 384 }
382 385
383 386
384 static PyObject * 387 static PyObject *
385 mmap_write_method(mmap_object *self, 388 mmap_write_method(mmap_object *self,
386 PyObject *args) 389 PyObject *args)
387 { 390 {
388 Py_ssize_t length; 391 Py_buffer data;
389 char *data;
390 392
391 CHECK_VALID(NULL); 393 CHECK_VALID(NULL);
392 if (!PyArg_ParseTuple(args, "y#:write", &data, &length)) 394 if (!PyArg_ParseTuple(args, "y*:write", &data))
393 return(NULL); 395 return(NULL);
394 396
395 if (!is_writable(self)) 397 if (!is_writable(self)) {
396 return NULL; 398 PyBuffer_Release(&data);
397
398 if ((self->pos + length) > self->size) {
399 PyErr_SetString(PyExc_ValueError, "data out of range");
400 return NULL; 399 return NULL;
401 } 400 }
402 memcpy(self->data+self->pos, data, length); 401
403 self->pos = self->pos+length; 402 if ((self->pos + data.len) > self->size) {
403 PyErr_SetString(PyExc_ValueError, "data out of range");
404 PyBuffer_Release(&data);
405 return NULL;
406 }
407 memcpy(self->data + self->pos, data.buf, data.len);
408 self->pos = self->pos + data.len;
409 PyBuffer_Release(&data);
404 Py_INCREF(Py_None); 410 Py_INCREF(Py_None);
405 return Py_None; 411 return Py_None;
406 } 412 }
407 413
408 static PyObject * 414 static PyObject *
409 mmap_write_byte_method(mmap_object *self, 415 mmap_write_byte_method(mmap_object *self,
410 PyObject *args) 416 PyObject *args)
411 { 417 {
412 char value; 418 char value;
413 419
(...skipping 1110 matching lines...) Expand 10 before | Expand all | Expand 10 after
1524 1530
1525 setint(dict, "PAGESIZE", (long)my_getpagesize()); 1531 setint(dict, "PAGESIZE", (long)my_getpagesize());
1526 1532
1527 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity()); 1533 setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
1528 1534
1529 setint(dict, "ACCESS_READ", ACCESS_READ); 1535 setint(dict, "ACCESS_READ", ACCESS_READ);
1530 setint(dict, "ACCESS_WRITE", ACCESS_WRITE); 1536 setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
1531 setint(dict, "ACCESS_COPY", ACCESS_COPY); 1537 setint(dict, "ACCESS_COPY", ACCESS_COPY);
1532 return module; 1538 return module;
1533 } 1539 }
OLDNEW
« Modules/fcntlmodule.c ('K') | « Modules/fcntlmodule.c ('k') | Modules/ossaudiodev.c » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+