/Modules/mmapmodule.c

http://unladen-swallow.googlecode.com/ · C · 1472 lines · 1248 code · 132 blank · 92 comment · 275 complexity · 9634f5045edd9fb68560fbb863c86be3 MD5 · raw file

  1. /*
  2. / Author: Sam Rushing <rushing@nightmare.com>
  3. / Hacked for Unix by AMK
  4. / $Id: mmapmodule.c 73682 2009-06-29 14:37:28Z hirokazu.yamamoto $
  5. / Modified to support mmap with offset - to map a 'window' of a file
  6. / Author: Yotam Medini yotamm@mellanox.co.il
  7. /
  8. / mmapmodule.cpp -- map a view of a file into memory
  9. /
  10. / todo: need permission flags, perhaps a 'chsize' analog
  11. / not all functions check range yet!!!
  12. /
  13. /
  14. / This version of mmapmodule.c has been changed significantly
  15. / from the original mmapfile.c on which it was based.
  16. / The original version of mmapfile is maintained by Sam at
  17. / ftp://squirl.nightmare.com/pub/python/python-ext.
  18. */
  19. #define PY_SSIZE_T_CLEAN
  20. #include <Python.h>
  21. #ifndef MS_WINDOWS
  22. #define UNIX
  23. #endif
  24. #ifdef MS_WINDOWS
  25. #include <windows.h>
  26. static int
  27. my_getpagesize(void)
  28. {
  29. SYSTEM_INFO si;
  30. GetSystemInfo(&si);
  31. return si.dwPageSize;
  32. }
  33. static int
  34. my_getallocationgranularity (void)
  35. {
  36. SYSTEM_INFO si;
  37. GetSystemInfo(&si);
  38. return si.dwAllocationGranularity;
  39. }
  40. #endif
  41. #ifdef UNIX
  42. #include <sys/mman.h>
  43. #include <sys/stat.h>
  44. #if defined(HAVE_SYSCONF) && defined(_SC_PAGESIZE)
  45. static int
  46. my_getpagesize(void)
  47. {
  48. return sysconf(_SC_PAGESIZE);
  49. }
  50. #define my_getallocationgranularity my_getpagesize
  51. #else
  52. #define my_getpagesize getpagesize
  53. #endif
  54. #endif /* UNIX */
  55. #include <string.h>
  56. #ifdef HAVE_SYS_TYPES_H
  57. #include <sys/types.h>
  58. #endif /* HAVE_SYS_TYPES_H */
  59. /* Prefer MAP_ANONYMOUS since MAP_ANON is deprecated according to man page. */
  60. #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
  61. # define MAP_ANONYMOUS MAP_ANON
  62. #endif
  63. static PyObject *mmap_module_error;
  64. typedef enum
  65. {
  66. ACCESS_DEFAULT,
  67. ACCESS_READ,
  68. ACCESS_WRITE,
  69. ACCESS_COPY
  70. } access_mode;
  71. typedef struct {
  72. PyObject_HEAD
  73. char * data;
  74. size_t size;
  75. size_t pos; /* relative to offset */
  76. size_t offset;
  77. #ifdef MS_WINDOWS
  78. HANDLE map_handle;
  79. HANDLE file_handle;
  80. char * tagname;
  81. #endif
  82. #ifdef UNIX
  83. int fd;
  84. #endif
  85. access_mode access;
  86. } mmap_object;
  87. static void
  88. mmap_object_dealloc(mmap_object *m_obj)
  89. {
  90. #ifdef MS_WINDOWS
  91. if (m_obj->data != NULL)
  92. UnmapViewOfFile (m_obj->data);
  93. if (m_obj->map_handle != NULL)
  94. CloseHandle (m_obj->map_handle);
  95. if (m_obj->file_handle != INVALID_HANDLE_VALUE)
  96. CloseHandle (m_obj->file_handle);
  97. if (m_obj->tagname)
  98. PyMem_Free(m_obj->tagname);
  99. #endif /* MS_WINDOWS */
  100. #ifdef UNIX
  101. if (m_obj->fd >= 0)
  102. (void) close(m_obj->fd);
  103. if (m_obj->data!=NULL) {
  104. msync(m_obj->data, m_obj->size, MS_SYNC);
  105. munmap(m_obj->data, m_obj->size);
  106. }
  107. #endif /* UNIX */
  108. Py_TYPE(m_obj)->tp_free((PyObject*)m_obj);
  109. }
  110. static PyObject *
  111. mmap_close_method(mmap_object *self, PyObject *unused)
  112. {
  113. #ifdef MS_WINDOWS
  114. /* For each resource we maintain, we need to check
  115. the value is valid, and if so, free the resource
  116. and set the member value to an invalid value so
  117. the dealloc does not attempt to resource clearing
  118. again.
  119. TODO - should we check for errors in the close operations???
  120. */
  121. if (self->data != NULL) {
  122. UnmapViewOfFile(self->data);
  123. self->data = NULL;
  124. }
  125. if (self->map_handle != NULL) {
  126. CloseHandle(self->map_handle);
  127. self->map_handle = NULL;
  128. }
  129. if (self->file_handle != INVALID_HANDLE_VALUE) {
  130. CloseHandle(self->file_handle);
  131. self->file_handle = INVALID_HANDLE_VALUE;
  132. }
  133. #endif /* MS_WINDOWS */
  134. #ifdef UNIX
  135. if (0 <= self->fd)
  136. (void) close(self->fd);
  137. self->fd = -1;
  138. if (self->data != NULL) {
  139. munmap(self->data, self->size);
  140. self->data = NULL;
  141. }
  142. #endif
  143. Py_INCREF(Py_None);
  144. return Py_None;
  145. }
  146. #ifdef MS_WINDOWS
  147. #define CHECK_VALID(err) \
  148. do { \
  149. if (self->map_handle == NULL) { \
  150. PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
  151. return err; \
  152. } \
  153. } while (0)
  154. #endif /* MS_WINDOWS */
  155. #ifdef UNIX
  156. #define CHECK_VALID(err) \
  157. do { \
  158. if (self->data == NULL) { \
  159. PyErr_SetString(PyExc_ValueError, "mmap closed or invalid"); \
  160. return err; \
  161. } \
  162. } while (0)
  163. #endif /* UNIX */
  164. static PyObject *
  165. mmap_read_byte_method(mmap_object *self,
  166. PyObject *unused)
  167. {
  168. CHECK_VALID(NULL);
  169. if (self->pos < self->size) {
  170. char value = self->data[self->pos];
  171. self->pos += 1;
  172. return Py_BuildValue("c", value);
  173. } else {
  174. PyErr_SetString(PyExc_ValueError, "read byte out of range");
  175. return NULL;
  176. }
  177. }
  178. static PyObject *
  179. mmap_read_line_method(mmap_object *self,
  180. PyObject *unused)
  181. {
  182. char *start = self->data+self->pos;
  183. char *eof = self->data+self->size;
  184. char *eol;
  185. PyObject *result;
  186. CHECK_VALID(NULL);
  187. eol = memchr(start, '\n', self->size - self->pos);
  188. if (!eol)
  189. eol = eof;
  190. else
  191. ++eol; /* we're interested in the position after the
  192. newline. */
  193. result = PyString_FromStringAndSize(start, (eol - start));
  194. self->pos += (eol - start);
  195. return result;
  196. }
  197. static PyObject *
  198. mmap_read_method(mmap_object *self,
  199. PyObject *args)
  200. {
  201. Py_ssize_t num_bytes, n;
  202. PyObject *result;
  203. CHECK_VALID(NULL);
  204. if (!PyArg_ParseTuple(args, "n:read", &num_bytes))
  205. return(NULL);
  206. /* silently 'adjust' out-of-range requests */
  207. assert(self->size >= self->pos);
  208. n = self->size - self->pos;
  209. /* The difference can overflow, only if self->size is greater than
  210. * PY_SSIZE_T_MAX. But then the operation cannot possibly succeed,
  211. * because the mapped area and the returned string each need more
  212. * than half of the addressable memory. So we clip the size, and let
  213. * the code below raise MemoryError.
  214. */
  215. if (n < 0)
  216. n = PY_SSIZE_T_MAX;
  217. if (num_bytes < 0 || num_bytes > n) {
  218. num_bytes = n;
  219. }
  220. result = Py_BuildValue("s#", self->data+self->pos, num_bytes);
  221. self->pos += num_bytes;
  222. return result;
  223. }
  224. static PyObject *
  225. mmap_gfind(mmap_object *self,
  226. PyObject *args,
  227. int reverse)
  228. {
  229. Py_ssize_t start = self->pos;
  230. Py_ssize_t end = self->size;
  231. const char *needle;
  232. Py_ssize_t len;
  233. CHECK_VALID(NULL);
  234. if (!PyArg_ParseTuple(args, reverse ? "s#|nn:rfind" : "s#|nn:find",
  235. &needle, &len, &start, &end)) {
  236. return NULL;
  237. } else {
  238. const char *p, *start_p, *end_p;
  239. int sign = reverse ? -1 : 1;
  240. if (start < 0)
  241. start += self->size;
  242. if (start < 0)
  243. start = 0;
  244. else if ((size_t)start > self->size)
  245. start = self->size;
  246. if (end < 0)
  247. end += self->size;
  248. if (end < 0)
  249. end = 0;
  250. else if ((size_t)end > self->size)
  251. end = self->size;
  252. start_p = self->data + start;
  253. end_p = self->data + end;
  254. for (p = (reverse ? end_p - len : start_p);
  255. (p >= start_p) && (p + len <= end_p); p += sign) {
  256. Py_ssize_t i;
  257. for (i = 0; i < len && needle[i] == p[i]; ++i)
  258. /* nothing */;
  259. if (i == len) {
  260. return PyInt_FromSsize_t(p - self->data);
  261. }
  262. }
  263. return PyInt_FromLong(-1);
  264. }
  265. }
  266. static PyObject *
  267. mmap_find_method(mmap_object *self,
  268. PyObject *args)
  269. {
  270. return mmap_gfind(self, args, 0);
  271. }
  272. static PyObject *
  273. mmap_rfind_method(mmap_object *self,
  274. PyObject *args)
  275. {
  276. return mmap_gfind(self, args, 1);
  277. }
  278. static int
  279. is_writeable(mmap_object *self)
  280. {
  281. if (self->access != ACCESS_READ)
  282. return 1;
  283. PyErr_Format(PyExc_TypeError, "mmap can't modify a readonly memory map.");
  284. return 0;
  285. }
  286. static int
  287. is_resizeable(mmap_object *self)
  288. {
  289. if ((self->access == ACCESS_WRITE) || (self->access == ACCESS_DEFAULT))
  290. return 1;
  291. PyErr_Format(PyExc_TypeError,
  292. "mmap can't resize a readonly or copy-on-write memory map.");
  293. return 0;
  294. }
  295. static PyObject *
  296. mmap_write_method(mmap_object *self,
  297. PyObject *args)
  298. {
  299. Py_ssize_t length;
  300. char *data;
  301. CHECK_VALID(NULL);
  302. if (!PyArg_ParseTuple(args, "s#:write", &data, &length))
  303. return(NULL);
  304. if (!is_writeable(self))
  305. return NULL;
  306. if ((self->pos + length) > self->size) {
  307. PyErr_SetString(PyExc_ValueError, "data out of range");
  308. return NULL;
  309. }
  310. memcpy(self->data+self->pos, data, length);
  311. self->pos = self->pos+length;
  312. Py_INCREF(Py_None);
  313. return Py_None;
  314. }
  315. static PyObject *
  316. mmap_write_byte_method(mmap_object *self,
  317. PyObject *args)
  318. {
  319. char value;
  320. CHECK_VALID(NULL);
  321. if (!PyArg_ParseTuple(args, "c:write_byte", &value))
  322. return(NULL);
  323. if (!is_writeable(self))
  324. return NULL;
  325. if (self->pos < self->size) {
  326. *(self->data+self->pos) = value;
  327. self->pos += 1;
  328. Py_INCREF(Py_None);
  329. return Py_None;
  330. }
  331. else {
  332. PyErr_SetString(PyExc_ValueError, "write byte out of range");
  333. return NULL;
  334. }
  335. }
  336. static PyObject *
  337. mmap_size_method(mmap_object *self,
  338. PyObject *unused)
  339. {
  340. CHECK_VALID(NULL);
  341. #ifdef MS_WINDOWS
  342. if (self->file_handle != INVALID_HANDLE_VALUE) {
  343. DWORD low,high;
  344. PY_LONG_LONG size;
  345. low = GetFileSize(self->file_handle, &high);
  346. if (low == INVALID_FILE_SIZE) {
  347. /* It might be that the function appears to have failed,
  348. when indeed its size equals INVALID_FILE_SIZE */
  349. DWORD error = GetLastError();
  350. if (error != NO_ERROR)
  351. return PyErr_SetFromWindowsErr(error);
  352. }
  353. if (!high && low < LONG_MAX)
  354. return PyInt_FromLong((long)low);
  355. size = (((PY_LONG_LONG)high)<<32) + low;
  356. return PyLong_FromLongLong(size);
  357. } else {
  358. return PyInt_FromSsize_t(self->size);
  359. }
  360. #endif /* MS_WINDOWS */
  361. #ifdef UNIX
  362. {
  363. struct stat buf;
  364. if (-1 == fstat(self->fd, &buf)) {
  365. PyErr_SetFromErrno(mmap_module_error);
  366. return NULL;
  367. }
  368. return PyInt_FromSsize_t(buf.st_size);
  369. }
  370. #endif /* UNIX */
  371. }
  372. /* This assumes that you want the entire file mapped,
  373. / and when recreating the map will make the new file
  374. / have the new size
  375. /
  376. / Is this really necessary? This could easily be done
  377. / from python by just closing and re-opening with the
  378. / new size?
  379. */
  380. static PyObject *
  381. mmap_resize_method(mmap_object *self,
  382. PyObject *args)
  383. {
  384. Py_ssize_t new_size;
  385. CHECK_VALID(NULL);
  386. if (!PyArg_ParseTuple(args, "n:resize", &new_size) ||
  387. !is_resizeable(self)) {
  388. return NULL;
  389. #ifdef MS_WINDOWS
  390. } else {
  391. DWORD dwErrCode = 0;
  392. DWORD off_hi, off_lo, newSizeLow, newSizeHigh;
  393. /* First, unmap the file view */
  394. UnmapViewOfFile(self->data);
  395. self->data = NULL;
  396. /* Close the mapping object */
  397. CloseHandle(self->map_handle);
  398. self->map_handle = NULL;
  399. /* Move to the desired EOF position */
  400. #if SIZEOF_SIZE_T > 4
  401. newSizeHigh = (DWORD)((self->offset + new_size) >> 32);
  402. newSizeLow = (DWORD)((self->offset + new_size) & 0xFFFFFFFF);
  403. off_hi = (DWORD)(self->offset >> 32);
  404. off_lo = (DWORD)(self->offset & 0xFFFFFFFF);
  405. #else
  406. newSizeHigh = 0;
  407. newSizeLow = (DWORD)(self->offset + new_size);
  408. off_hi = 0;
  409. off_lo = (DWORD)self->offset;
  410. #endif
  411. SetFilePointer(self->file_handle,
  412. newSizeLow, &newSizeHigh, FILE_BEGIN);
  413. /* Change the size of the file */
  414. SetEndOfFile(self->file_handle);
  415. /* Create another mapping object and remap the file view */
  416. self->map_handle = CreateFileMapping(
  417. self->file_handle,
  418. NULL,
  419. PAGE_READWRITE,
  420. 0,
  421. 0,
  422. self->tagname);
  423. if (self->map_handle != NULL) {
  424. self->data = (char *) MapViewOfFile(self->map_handle,
  425. FILE_MAP_WRITE,
  426. off_hi,
  427. off_lo,
  428. new_size);
  429. if (self->data != NULL) {
  430. self->size = new_size;
  431. Py_INCREF(Py_None);
  432. return Py_None;
  433. } else {
  434. dwErrCode = GetLastError();
  435. CloseHandle(self->map_handle);
  436. self->map_handle = NULL;
  437. }
  438. } else {
  439. dwErrCode = GetLastError();
  440. }
  441. PyErr_SetFromWindowsErr(dwErrCode);
  442. return NULL;
  443. #endif /* MS_WINDOWS */
  444. #ifdef UNIX
  445. #ifndef HAVE_MREMAP
  446. } else {
  447. PyErr_SetString(PyExc_SystemError,
  448. "mmap: resizing not available--no mremap()");
  449. return NULL;
  450. #else
  451. } else {
  452. void *newmap;
  453. if (ftruncate(self->fd, self->offset + new_size) == -1) {
  454. PyErr_SetFromErrno(mmap_module_error);
  455. return NULL;
  456. }
  457. #ifdef MREMAP_MAYMOVE
  458. newmap = mremap(self->data, self->size, new_size, MREMAP_MAYMOVE);
  459. #else
  460. #if defined(__NetBSD__)
  461. newmap = mremap(self->data, self->size, self->data, new_size, 0);
  462. #else
  463. newmap = mremap(self->data, self->size, new_size, 0);
  464. #endif /* __NetBSD__ */
  465. #endif
  466. if (newmap == (void *)-1)
  467. {
  468. PyErr_SetFromErrno(mmap_module_error);
  469. return NULL;
  470. }
  471. self->data = newmap;
  472. self->size = new_size;
  473. Py_INCREF(Py_None);
  474. return Py_None;
  475. #endif /* HAVE_MREMAP */
  476. #endif /* UNIX */
  477. }
  478. }
  479. static PyObject *
  480. mmap_tell_method(mmap_object *self, PyObject *unused)
  481. {
  482. CHECK_VALID(NULL);
  483. return PyInt_FromSize_t(self->pos);
  484. }
  485. static PyObject *
  486. mmap_flush_method(mmap_object *self, PyObject *args)
  487. {
  488. Py_ssize_t offset = 0;
  489. Py_ssize_t size = self->size;
  490. CHECK_VALID(NULL);
  491. if (!PyArg_ParseTuple(args, "|nn:flush", &offset, &size))
  492. return NULL;
  493. if ((size_t)(offset + size) > self->size) {
  494. PyErr_SetString(PyExc_ValueError, "flush values out of range");
  495. return NULL;
  496. }
  497. #ifdef MS_WINDOWS
  498. return PyInt_FromLong((long) FlushViewOfFile(self->data+offset, size));
  499. #elif defined(UNIX)
  500. /* XXX semantics of return value? */
  501. /* XXX flags for msync? */
  502. if (-1 == msync(self->data + offset, size, MS_SYNC)) {
  503. PyErr_SetFromErrno(mmap_module_error);
  504. return NULL;
  505. }
  506. return PyInt_FromLong(0);
  507. #else
  508. PyErr_SetString(PyExc_ValueError, "flush not supported on this system");
  509. return NULL;
  510. #endif
  511. }
  512. static PyObject *
  513. mmap_seek_method(mmap_object *self, PyObject *args)
  514. {
  515. Py_ssize_t dist;
  516. int how=0;
  517. CHECK_VALID(NULL);
  518. if (!PyArg_ParseTuple(args, "n|i:seek", &dist, &how))
  519. return NULL;
  520. else {
  521. size_t where;
  522. switch (how) {
  523. case 0: /* relative to start */
  524. if (dist < 0)
  525. goto onoutofrange;
  526. where = dist;
  527. break;
  528. case 1: /* relative to current position */
  529. if ((Py_ssize_t)self->pos + dist < 0)
  530. goto onoutofrange;
  531. where = self->pos + dist;
  532. break;
  533. case 2: /* relative to end */
  534. if ((Py_ssize_t)self->size + dist < 0)
  535. goto onoutofrange;
  536. where = self->size + dist;
  537. break;
  538. default:
  539. PyErr_SetString(PyExc_ValueError, "unknown seek type");
  540. return NULL;
  541. }
  542. if (where > self->size)
  543. goto onoutofrange;
  544. self->pos = where;
  545. Py_INCREF(Py_None);
  546. return Py_None;
  547. }
  548. onoutofrange:
  549. PyErr_SetString(PyExc_ValueError, "seek out of range");
  550. return NULL;
  551. }
  552. static PyObject *
  553. mmap_move_method(mmap_object *self, PyObject *args)
  554. {
  555. unsigned long dest, src, count;
  556. CHECK_VALID(NULL);
  557. if (!PyArg_ParseTuple(args, "kkk:move", &dest, &src, &count) ||
  558. !is_writeable(self)) {
  559. return NULL;
  560. } else {
  561. /* bounds check the values */
  562. unsigned long pos = src > dest ? src : dest;
  563. if (self->size < pos || count > self->size - pos) {
  564. PyErr_SetString(PyExc_ValueError,
  565. "source or destination out of range");
  566. return NULL;
  567. } else {
  568. memmove(self->data+dest, self->data+src, count);
  569. Py_INCREF(Py_None);
  570. return Py_None;
  571. }
  572. }
  573. }
  574. static struct PyMethodDef mmap_object_methods[] = {
  575. {"close", (PyCFunction) mmap_close_method, METH_NOARGS},
  576. {"find", (PyCFunction) mmap_find_method, METH_VARARGS},
  577. {"rfind", (PyCFunction) mmap_rfind_method, METH_VARARGS},
  578. {"flush", (PyCFunction) mmap_flush_method, METH_VARARGS},
  579. {"move", (PyCFunction) mmap_move_method, METH_VARARGS},
  580. {"read", (PyCFunction) mmap_read_method, METH_VARARGS},
  581. {"read_byte", (PyCFunction) mmap_read_byte_method, METH_NOARGS},
  582. {"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
  583. {"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
  584. {"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
  585. {"size", (PyCFunction) mmap_size_method, METH_NOARGS},
  586. {"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
  587. {"write", (PyCFunction) mmap_write_method, METH_VARARGS},
  588. {"write_byte", (PyCFunction) mmap_write_byte_method, METH_VARARGS},
  589. {NULL, NULL} /* sentinel */
  590. };
  591. /* Functions for treating an mmap'ed file as a buffer */
  592. static Py_ssize_t
  593. mmap_buffer_getreadbuf(mmap_object *self, Py_ssize_t index, const void **ptr)
  594. {
  595. CHECK_VALID(-1);
  596. if (index != 0) {
  597. PyErr_SetString(PyExc_SystemError,
  598. "Accessing non-existent mmap segment");
  599. return -1;
  600. }
  601. *ptr = self->data;
  602. return self->size;
  603. }
  604. static Py_ssize_t
  605. mmap_buffer_getwritebuf(mmap_object *self, Py_ssize_t index, const void **ptr)
  606. {
  607. CHECK_VALID(-1);
  608. if (index != 0) {
  609. PyErr_SetString(PyExc_SystemError,
  610. "Accessing non-existent mmap segment");
  611. return -1;
  612. }
  613. if (!is_writeable(self))
  614. return -1;
  615. *ptr = self->data;
  616. return self->size;
  617. }
  618. static Py_ssize_t
  619. mmap_buffer_getsegcount(mmap_object *self, Py_ssize_t *lenp)
  620. {
  621. CHECK_VALID(-1);
  622. if (lenp)
  623. *lenp = self->size;
  624. return 1;
  625. }
  626. static Py_ssize_t
  627. mmap_buffer_getcharbuffer(mmap_object *self, Py_ssize_t index, const void **ptr)
  628. {
  629. if (index != 0) {
  630. PyErr_SetString(PyExc_SystemError,
  631. "accessing non-existent buffer segment");
  632. return -1;
  633. }
  634. *ptr = (const char *)self->data;
  635. return self->size;
  636. }
  637. static Py_ssize_t
  638. mmap_length(mmap_object *self)
  639. {
  640. CHECK_VALID(-1);
  641. return self->size;
  642. }
  643. static PyObject *
  644. mmap_item(mmap_object *self, Py_ssize_t i)
  645. {
  646. CHECK_VALID(NULL);
  647. if (i < 0 || (size_t)i >= self->size) {
  648. PyErr_SetString(PyExc_IndexError, "mmap index out of range");
  649. return NULL;
  650. }
  651. return PyString_FromStringAndSize(self->data + i, 1);
  652. }
  653. static PyObject *
  654. mmap_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh)
  655. {
  656. CHECK_VALID(NULL);
  657. if (ilow < 0)
  658. ilow = 0;
  659. else if ((size_t)ilow > self->size)
  660. ilow = self->size;
  661. if (ihigh < 0)
  662. ihigh = 0;
  663. if (ihigh < ilow)
  664. ihigh = ilow;
  665. else if ((size_t)ihigh > self->size)
  666. ihigh = self->size;
  667. return PyString_FromStringAndSize(self->data + ilow, ihigh-ilow);
  668. }
  669. static PyObject *
  670. mmap_subscript(mmap_object *self, PyObject *item)
  671. {
  672. CHECK_VALID(NULL);
  673. if (PyIndex_Check(item)) {
  674. Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
  675. if (i == -1 && PyErr_Occurred())
  676. return NULL;
  677. if (i < 0)
  678. i += self->size;
  679. if (i < 0 || (size_t)i >= self->size) {
  680. PyErr_SetString(PyExc_IndexError,
  681. "mmap index out of range");
  682. return NULL;
  683. }
  684. return PyString_FromStringAndSize(self->data + i, 1);
  685. }
  686. else if (PySlice_Check(item)) {
  687. Py_ssize_t start, stop, step, slicelen;
  688. if (PySlice_GetIndicesEx((PySliceObject *)item, self->size,
  689. &start, &stop, &step, &slicelen) < 0) {
  690. return NULL;
  691. }
  692. if (slicelen <= 0)
  693. return PyString_FromStringAndSize("", 0);
  694. else if (step == 1)
  695. return PyString_FromStringAndSize(self->data + start,
  696. slicelen);
  697. else {
  698. char *result_buf = (char *)PyMem_Malloc(slicelen);
  699. Py_ssize_t cur, i;
  700. PyObject *result;
  701. if (result_buf == NULL)
  702. return PyErr_NoMemory();
  703. for (cur = start, i = 0; i < slicelen;
  704. cur += step, i++) {
  705. result_buf[i] = self->data[cur];
  706. }
  707. result = PyString_FromStringAndSize(result_buf,
  708. slicelen);
  709. PyMem_Free(result_buf);
  710. return result;
  711. }
  712. }
  713. else {
  714. PyErr_SetString(PyExc_TypeError,
  715. "mmap indices must be integers");
  716. return NULL;
  717. }
  718. }
  719. static PyObject *
  720. mmap_concat(mmap_object *self, PyObject *bb)
  721. {
  722. CHECK_VALID(NULL);
  723. PyErr_SetString(PyExc_SystemError,
  724. "mmaps don't support concatenation");
  725. return NULL;
  726. }
  727. static PyObject *
  728. mmap_repeat(mmap_object *self, Py_ssize_t n)
  729. {
  730. CHECK_VALID(NULL);
  731. PyErr_SetString(PyExc_SystemError,
  732. "mmaps don't support repeat operation");
  733. return NULL;
  734. }
  735. static int
  736. mmap_ass_slice(mmap_object *self, Py_ssize_t ilow, Py_ssize_t ihigh, PyObject *v)
  737. {
  738. const char *buf;
  739. CHECK_VALID(-1);
  740. if (ilow < 0)
  741. ilow = 0;
  742. else if ((size_t)ilow > self->size)
  743. ilow = self->size;
  744. if (ihigh < 0)
  745. ihigh = 0;
  746. if (ihigh < ilow)
  747. ihigh = ilow;
  748. else if ((size_t)ihigh > self->size)
  749. ihigh = self->size;
  750. if (v == NULL) {
  751. PyErr_SetString(PyExc_TypeError,
  752. "mmap object doesn't support slice deletion");
  753. return -1;
  754. }
  755. if (! (PyString_Check(v)) ) {
  756. PyErr_SetString(PyExc_IndexError,
  757. "mmap slice assignment must be a string");
  758. return -1;
  759. }
  760. if (PyString_Size(v) != (ihigh - ilow)) {
  761. PyErr_SetString(PyExc_IndexError,
  762. "mmap slice assignment is wrong size");
  763. return -1;
  764. }
  765. if (!is_writeable(self))
  766. return -1;
  767. buf = PyString_AsString(v);
  768. memcpy(self->data + ilow, buf, ihigh-ilow);
  769. return 0;
  770. }
  771. static int
  772. mmap_ass_item(mmap_object *self, Py_ssize_t i, PyObject *v)
  773. {
  774. const char *buf;
  775. CHECK_VALID(-1);
  776. if (i < 0 || (size_t)i >= self->size) {
  777. PyErr_SetString(PyExc_IndexError, "mmap index out of range");
  778. return -1;
  779. }
  780. if (v == NULL) {
  781. PyErr_SetString(PyExc_TypeError,
  782. "mmap object doesn't support item deletion");
  783. return -1;
  784. }
  785. if (! (PyString_Check(v) && PyString_Size(v)==1) ) {
  786. PyErr_SetString(PyExc_IndexError,
  787. "mmap assignment must be single-character string");
  788. return -1;
  789. }
  790. if (!is_writeable(self))
  791. return -1;
  792. buf = PyString_AsString(v);
  793. self->data[i] = buf[0];
  794. return 0;
  795. }
  796. static int
  797. mmap_ass_subscript(mmap_object *self, PyObject *item, PyObject *value)
  798. {
  799. CHECK_VALID(-1);
  800. if (PyIndex_Check(item)) {
  801. Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
  802. const char *buf;
  803. if (i == -1 && PyErr_Occurred())
  804. return -1;
  805. if (i < 0)
  806. i += self->size;
  807. if (i < 0 || (size_t)i >= self->size) {
  808. PyErr_SetString(PyExc_IndexError,
  809. "mmap index out of range");
  810. return -1;
  811. }
  812. if (value == NULL) {
  813. PyErr_SetString(PyExc_TypeError,
  814. "mmap object doesn't support item deletion");
  815. return -1;
  816. }
  817. if (!PyString_Check(value) || PyString_Size(value) != 1) {
  818. PyErr_SetString(PyExc_IndexError,
  819. "mmap assignment must be single-character string");
  820. return -1;
  821. }
  822. if (!is_writeable(self))
  823. return -1;
  824. buf = PyString_AsString(value);
  825. self->data[i] = buf[0];
  826. return 0;
  827. }
  828. else if (PySlice_Check(item)) {
  829. Py_ssize_t start, stop, step, slicelen;
  830. if (PySlice_GetIndicesEx((PySliceObject *)item,
  831. self->size, &start, &stop,
  832. &step, &slicelen) < 0) {
  833. return -1;
  834. }
  835. if (value == NULL) {
  836. PyErr_SetString(PyExc_TypeError,
  837. "mmap object doesn't support slice deletion");
  838. return -1;
  839. }
  840. if (!PyString_Check(value)) {
  841. PyErr_SetString(PyExc_IndexError,
  842. "mmap slice assignment must be a string");
  843. return -1;
  844. }
  845. if (PyString_Size(value) != slicelen) {
  846. PyErr_SetString(PyExc_IndexError,
  847. "mmap slice assignment is wrong size");
  848. return -1;
  849. }
  850. if (!is_writeable(self))
  851. return -1;
  852. if (slicelen == 0)
  853. return 0;
  854. else if (step == 1) {
  855. const char *buf = PyString_AsString(value);
  856. if (buf == NULL)
  857. return -1;
  858. memcpy(self->data + start, buf, slicelen);
  859. return 0;
  860. }
  861. else {
  862. Py_ssize_t cur, i;
  863. const char *buf = PyString_AsString(value);
  864. if (buf == NULL)
  865. return -1;
  866. for (cur = start, i = 0; i < slicelen;
  867. cur += step, i++) {
  868. self->data[cur] = buf[i];
  869. }
  870. return 0;
  871. }
  872. }
  873. else {
  874. PyErr_SetString(PyExc_TypeError,
  875. "mmap indices must be integer");
  876. return -1;
  877. }
  878. }
  879. static PySequenceMethods mmap_as_sequence = {
  880. (lenfunc)mmap_length, /*sq_length*/
  881. (binaryfunc)mmap_concat, /*sq_concat*/
  882. (ssizeargfunc)mmap_repeat, /*sq_repeat*/
  883. (ssizeargfunc)mmap_item, /*sq_item*/
  884. (ssizessizeargfunc)mmap_slice, /*sq_slice*/
  885. (ssizeobjargproc)mmap_ass_item, /*sq_ass_item*/
  886. (ssizessizeobjargproc)mmap_ass_slice, /*sq_ass_slice*/
  887. };
  888. static PyMappingMethods mmap_as_mapping = {
  889. (lenfunc)mmap_length,
  890. (binaryfunc)mmap_subscript,
  891. (objobjargproc)mmap_ass_subscript,
  892. };
  893. static PyBufferProcs mmap_as_buffer = {
  894. (readbufferproc)mmap_buffer_getreadbuf,
  895. (writebufferproc)mmap_buffer_getwritebuf,
  896. (segcountproc)mmap_buffer_getsegcount,
  897. (charbufferproc)mmap_buffer_getcharbuffer,
  898. };
  899. static PyObject *
  900. new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict);
  901. PyDoc_STRVAR(mmap_doc,
  902. "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\
  903. \n\
  904. Maps length bytes from the file specified by the file handle fileno,\n\
  905. and returns a mmap object. If length is larger than the current size\n\
  906. of the file, the file is extended to contain length bytes. If length\n\
  907. is 0, the maximum length of the map is the current size of the file,\n\
  908. except that if the file is empty Windows raises an exception (you cannot\n\
  909. create an empty mapping on Windows).\n\
  910. \n\
  911. Unix: mmap(fileno, length[, flags[, prot[, access[, offset]]]])\n\
  912. \n\
  913. Maps length bytes from the file specified by the file descriptor fileno,\n\
  914. and returns a mmap object. If length is 0, the maximum length of the map\n\
  915. will be the current size of the file when mmap is called.\n\
  916. flags specifies the nature of the mapping. MAP_PRIVATE creates a\n\
  917. private copy-on-write mapping, so changes to the contents of the mmap\n\
  918. object will be private to this process, and MAP_SHARED creates a mapping\n\
  919. that's shared with all other processes mapping the same areas of the file.\n\
  920. The default value is MAP_SHARED.\n\
  921. \n\
  922. To map anonymous memory, pass -1 as the fileno (both versions).");
  923. static PyTypeObject mmap_object_type = {
  924. PyVarObject_HEAD_INIT(NULL, 0)
  925. "mmap.mmap", /* tp_name */
  926. sizeof(mmap_object), /* tp_size */
  927. 0, /* tp_itemsize */
  928. /* methods */
  929. (destructor) mmap_object_dealloc, /* tp_dealloc */
  930. 0, /* tp_print */
  931. 0, /* tp_getattr */
  932. 0, /* tp_setattr */
  933. 0, /* tp_compare */
  934. 0, /* tp_repr */
  935. 0, /* tp_as_number */
  936. &mmap_as_sequence, /*tp_as_sequence*/
  937. &mmap_as_mapping, /*tp_as_mapping*/
  938. 0, /*tp_hash*/
  939. 0, /*tp_call*/
  940. 0, /*tp_str*/
  941. PyObject_GenericGetAttr, /*tp_getattro*/
  942. 0, /*tp_setattro*/
  943. &mmap_as_buffer, /*tp_as_buffer*/
  944. Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GETCHARBUFFER, /*tp_flags*/
  945. mmap_doc, /*tp_doc*/
  946. 0, /* tp_traverse */
  947. 0, /* tp_clear */
  948. 0, /* tp_richcompare */
  949. 0, /* tp_weaklistoffset */
  950. 0, /* tp_iter */
  951. 0, /* tp_iternext */
  952. mmap_object_methods, /* tp_methods */
  953. 0, /* tp_members */
  954. 0, /* tp_getset */
  955. 0, /* tp_base */
  956. 0, /* tp_dict */
  957. 0, /* tp_descr_get */
  958. 0, /* tp_descr_set */
  959. 0, /* tp_dictoffset */
  960. 0, /* tp_init */
  961. PyType_GenericAlloc, /* tp_alloc */
  962. new_mmap_object, /* tp_new */
  963. PyObject_Del, /* tp_free */
  964. };
  965. /* extract the map size from the given PyObject
  966. Returns -1 on error, with an appropriate Python exception raised. On
  967. success, the map size is returned. */
  968. static Py_ssize_t
  969. _GetMapSize(PyObject *o, const char* param)
  970. {
  971. if (o == NULL)
  972. return 0;
  973. if (PyIndex_Check(o)) {
  974. Py_ssize_t i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
  975. if (i==-1 && PyErr_Occurred())
  976. return -1;
  977. if (i < 0) {
  978. PyErr_Format(PyExc_OverflowError,
  979. "memory mapped %s must be positive",
  980. param);
  981. return -1;
  982. }
  983. return i;
  984. }
  985. PyErr_SetString(PyExc_TypeError, "map size must be an integral value");
  986. return -1;
  987. }
  988. #ifdef UNIX
  989. static PyObject *
  990. new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
  991. {
  992. #ifdef HAVE_FSTAT
  993. struct stat st;
  994. #endif
  995. mmap_object *m_obj;
  996. PyObject *map_size_obj = NULL, *offset_obj = NULL;
  997. Py_ssize_t map_size, offset;
  998. int fd, flags = MAP_SHARED, prot = PROT_WRITE | PROT_READ;
  999. int devzero = -1;
  1000. int access = (int)ACCESS_DEFAULT;
  1001. static char *keywords[] = {"fileno", "length",
  1002. "flags", "prot",
  1003. "access", "offset", NULL};
  1004. if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|iiiO", keywords,
  1005. &fd, &map_size_obj, &flags, &prot,
  1006. &access, &offset_obj))
  1007. return NULL;
  1008. map_size = _GetMapSize(map_size_obj, "size");
  1009. if (map_size < 0)
  1010. return NULL;
  1011. offset = _GetMapSize(offset_obj, "offset");
  1012. if (offset < 0)
  1013. return NULL;
  1014. if ((access != (int)ACCESS_DEFAULT) &&
  1015. ((flags != MAP_SHARED) || (prot != (PROT_WRITE | PROT_READ))))
  1016. return PyErr_Format(PyExc_ValueError,
  1017. "mmap can't specify both access and flags, prot.");
  1018. switch ((access_mode)access) {
  1019. case ACCESS_READ:
  1020. flags = MAP_SHARED;
  1021. prot = PROT_READ;
  1022. break;
  1023. case ACCESS_WRITE:
  1024. flags = MAP_SHARED;
  1025. prot = PROT_READ | PROT_WRITE;
  1026. break;
  1027. case ACCESS_COPY:
  1028. flags = MAP_PRIVATE;
  1029. prot = PROT_READ | PROT_WRITE;
  1030. break;
  1031. case ACCESS_DEFAULT:
  1032. /* use the specified or default values of flags and prot */
  1033. break;
  1034. default:
  1035. return PyErr_Format(PyExc_ValueError,
  1036. "mmap invalid access parameter.");
  1037. }
  1038. if (prot == PROT_READ) {
  1039. access = ACCESS_READ;
  1040. }
  1041. #ifdef HAVE_FSTAT
  1042. # ifdef __VMS
  1043. /* on OpenVMS we must ensure that all bytes are written to the file */
  1044. if (fd != -1) {
  1045. fsync(fd);
  1046. }
  1047. # endif
  1048. if (fd != -1 && fstat(fd, &st) == 0 && S_ISREG(st.st_mode)) {
  1049. if (map_size == 0) {
  1050. map_size = st.st_size;
  1051. } else if ((size_t)offset + (size_t)map_size > st.st_size) {
  1052. PyErr_SetString(PyExc_ValueError,
  1053. "mmap length is greater than file size");
  1054. return NULL;
  1055. }
  1056. }
  1057. #endif
  1058. m_obj = (mmap_object *)type->tp_alloc(type, 0);
  1059. if (m_obj == NULL) {return NULL;}
  1060. m_obj->data = NULL;
  1061. m_obj->size = (size_t) map_size;
  1062. m_obj->pos = (size_t) 0;
  1063. m_obj->offset = offset;
  1064. if (fd == -1) {
  1065. m_obj->fd = -1;
  1066. /* Assume the caller wants to map anonymous memory.
  1067. This is the same behaviour as Windows. mmap.mmap(-1, size)
  1068. on both Windows and Unix map anonymous memory.
  1069. */
  1070. #ifdef MAP_ANONYMOUS
  1071. /* BSD way to map anonymous memory */
  1072. flags |= MAP_ANONYMOUS;
  1073. #else
  1074. /* SVR4 method to map anonymous memory is to open /dev/zero */
  1075. fd = devzero = open("/dev/zero", O_RDWR);
  1076. if (devzero == -1) {
  1077. Py_DECREF(m_obj);
  1078. PyErr_SetFromErrno(mmap_module_error);
  1079. return NULL;
  1080. }
  1081. #endif
  1082. } else {
  1083. m_obj->fd = dup(fd);
  1084. if (m_obj->fd == -1) {
  1085. Py_DECREF(m_obj);
  1086. PyErr_SetFromErrno(mmap_module_error);
  1087. return NULL;
  1088. }
  1089. }
  1090. m_obj->data = mmap(NULL, map_size,
  1091. prot, flags,
  1092. fd, offset);
  1093. if (devzero != -1) {
  1094. close(devzero);
  1095. }
  1096. if (m_obj->data == (char *)-1) {
  1097. m_obj->data = NULL;
  1098. Py_DECREF(m_obj);
  1099. PyErr_SetFromErrno(mmap_module_error);
  1100. return NULL;
  1101. }
  1102. m_obj->access = (access_mode)access;
  1103. return (PyObject *)m_obj;
  1104. }
  1105. #endif /* UNIX */
  1106. #ifdef MS_WINDOWS
  1107. static PyObject *
  1108. new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
  1109. {
  1110. mmap_object *m_obj;
  1111. PyObject *map_size_obj = NULL, *offset_obj = NULL;
  1112. Py_ssize_t map_size, offset;
  1113. DWORD off_hi; /* upper 32 bits of offset */
  1114. DWORD off_lo; /* lower 32 bits of offset */
  1115. DWORD size_hi; /* upper 32 bits of size */
  1116. DWORD size_lo; /* lower 32 bits of size */
  1117. char *tagname = "";
  1118. DWORD dwErr = 0;
  1119. int fileno;
  1120. HANDLE fh = 0;
  1121. int access = (access_mode)ACCESS_DEFAULT;
  1122. DWORD flProtect, dwDesiredAccess;
  1123. static char *keywords[] = { "fileno", "length",
  1124. "tagname",
  1125. "access", "offset", NULL };
  1126. if (!PyArg_ParseTupleAndKeywords(args, kwdict, "iO|ziO", keywords,
  1127. &fileno, &map_size_obj,
  1128. &tagname, &access, &offset_obj)) {
  1129. return NULL;
  1130. }
  1131. switch((access_mode)access) {
  1132. case ACCESS_READ:
  1133. flProtect = PAGE_READONLY;
  1134. dwDesiredAccess = FILE_MAP_READ;
  1135. break;
  1136. case ACCESS_DEFAULT: case ACCESS_WRITE:
  1137. flProtect = PAGE_READWRITE;
  1138. dwDesiredAccess = FILE_MAP_WRITE;
  1139. break;
  1140. case ACCESS_COPY:
  1141. flProtect = PAGE_WRITECOPY;
  1142. dwDesiredAccess = FILE_MAP_COPY;
  1143. break;
  1144. default:
  1145. return PyErr_Format(PyExc_ValueError,
  1146. "mmap invalid access parameter.");
  1147. }
  1148. map_size = _GetMapSize(map_size_obj, "size");
  1149. if (map_size < 0)
  1150. return NULL;
  1151. offset = _GetMapSize(offset_obj, "offset");
  1152. if (offset < 0)
  1153. return NULL;
  1154. /* assume -1 and 0 both mean invalid filedescriptor
  1155. to 'anonymously' map memory.
  1156. XXX: fileno == 0 is a valid fd, but was accepted prior to 2.5.
  1157. XXX: Should this code be added?
  1158. if (fileno == 0)
  1159. PyErr_Warn(PyExc_DeprecationWarning,
  1160. "don't use 0 for anonymous memory");
  1161. */
  1162. if (fileno != -1 && fileno != 0) {
  1163. fh = (HANDLE)_get_osfhandle(fileno);
  1164. if (fh==(HANDLE)-1) {
  1165. PyErr_SetFromErrno(mmap_module_error);
  1166. return NULL;
  1167. }
  1168. /* Win9x appears to need us seeked to zero */
  1169. lseek(fileno, 0, SEEK_SET);
  1170. }
  1171. m_obj = (mmap_object *)type->tp_alloc(type, 0);
  1172. if (m_obj == NULL)
  1173. return NULL;
  1174. /* Set every field to an invalid marker, so we can safely
  1175. destruct the object in the face of failure */
  1176. m_obj->data = NULL;
  1177. m_obj->file_handle = INVALID_HANDLE_VALUE;
  1178. m_obj->map_handle = NULL;
  1179. m_obj->tagname = NULL;
  1180. m_obj->offset = offset;
  1181. if (fh) {
  1182. /* It is necessary to duplicate the handle, so the
  1183. Python code can close it on us */
  1184. if (!DuplicateHandle(
  1185. GetCurrentProcess(), /* source process handle */
  1186. fh, /* handle to be duplicated */
  1187. GetCurrentProcess(), /* target proc handle */
  1188. (LPHANDLE)&m_obj->file_handle, /* result */
  1189. 0, /* access - ignored due to options value */
  1190. FALSE, /* inherited by child processes? */
  1191. DUPLICATE_SAME_ACCESS)) { /* options */
  1192. dwErr = GetLastError();
  1193. Py_DECREF(m_obj);
  1194. PyErr_SetFromWindowsErr(dwErr);
  1195. return NULL;
  1196. }
  1197. if (!map_size) {
  1198. DWORD low,high;
  1199. low = GetFileSize(fh, &high);
  1200. /* low might just happen to have the value INVALID_FILE_SIZE;
  1201. so we need to check the last error also. */
  1202. if (low == INVALID_FILE_SIZE &&
  1203. (dwErr = GetLastError()) != NO_ERROR) {
  1204. Py_DECREF(m_obj);
  1205. return PyErr_SetFromWindowsErr(dwErr);
  1206. }
  1207. #if SIZEOF_SIZE_T > 4
  1208. m_obj->size = (((size_t)high)<<32) + low;
  1209. #else
  1210. if (high)
  1211. /* File is too large to map completely */
  1212. m_obj->size = (size_t)-1;
  1213. else
  1214. m_obj->size = low;
  1215. #endif
  1216. } else {
  1217. m_obj->size = map_size;
  1218. }
  1219. }
  1220. else {
  1221. m_obj->size = map_size;
  1222. }
  1223. /* set the initial position */
  1224. m_obj->pos = (size_t) 0;
  1225. /* set the tag name */
  1226. if (tagname != NULL && *tagname != '\0') {
  1227. m_obj->tagname = PyMem_Malloc(strlen(tagname)+1);
  1228. if (m_obj->tagname == NULL) {
  1229. PyErr_NoMemory();
  1230. Py_DECREF(m_obj);
  1231. return NULL;
  1232. }
  1233. strcpy(m_obj->tagname, tagname);
  1234. }
  1235. else
  1236. m_obj->tagname = NULL;
  1237. m_obj->access = (access_mode)access;
  1238. /* DWORD is a 4-byte int. If we're on a box where size_t consumes
  1239. * more than 4 bytes, we need to break it apart. Else (size_t
  1240. * consumes 4 bytes), C doesn't define what happens if we shift
  1241. * right by 32, so we need different code.
  1242. */
  1243. #if SIZEOF_SIZE_T > 4
  1244. size_hi = (DWORD)((offset + m_obj->size) >> 32);
  1245. size_lo = (DWORD)((offset + m_obj->size) & 0xFFFFFFFF);
  1246. off_hi = (DWORD)(offset >> 32);
  1247. off_lo = (DWORD)(offset & 0xFFFFFFFF);
  1248. #else
  1249. size_hi = 0;
  1250. size_lo = (DWORD)(offset + m_obj->size);
  1251. off_hi = 0;
  1252. off_lo = (DWORD)offset;
  1253. #endif
  1254. /* For files, it would be sufficient to pass 0 as size.
  1255. For anonymous maps, we have to pass the size explicitly. */
  1256. m_obj->map_handle = CreateFileMapping(m_obj->file_handle,
  1257. NULL,
  1258. flProtect,
  1259. size_hi,
  1260. size_lo,
  1261. m_obj->tagname);
  1262. if (m_obj->map_handle != NULL) {
  1263. m_obj->data = (char *) MapViewOfFile(m_obj->map_handle,
  1264. dwDesiredAccess,
  1265. off_hi,
  1266. off_lo,
  1267. m_obj->size);
  1268. if (m_obj->data != NULL)
  1269. return (PyObject *)m_obj;
  1270. else {
  1271. dwErr = GetLastError();
  1272. CloseHandle(m_obj->map_handle);
  1273. m_obj->map_handle = NULL;
  1274. }
  1275. } else
  1276. dwErr = GetLastError();
  1277. Py_DECREF(m_obj);
  1278. PyErr_SetFromWindowsErr(dwErr);
  1279. return NULL;
  1280. }
  1281. #endif /* MS_WINDOWS */
  1282. static void
  1283. setint(PyObject *d, const char *name, long value)
  1284. {
  1285. PyObject *o = PyInt_FromLong(value);
  1286. if (o && PyDict_SetItemString(d, name, o) == 0) {
  1287. Py_DECREF(o);
  1288. }
  1289. }
  1290. PyMODINIT_FUNC
  1291. initmmap(void)
  1292. {
  1293. PyObject *dict, *module;
  1294. if (PyType_Ready(&mmap_object_type) < 0)
  1295. return;
  1296. module = Py_InitModule("mmap", NULL);
  1297. if (module == NULL)
  1298. return;
  1299. dict = PyModule_GetDict(module);
  1300. if (!dict)
  1301. return;
  1302. mmap_module_error = PyErr_NewException("mmap.error",
  1303. PyExc_EnvironmentError , NULL);
  1304. if (mmap_module_error == NULL)
  1305. return;
  1306. PyDict_SetItemString(dict, "error", mmap_module_error);
  1307. PyDict_SetItemString(dict, "mmap", (PyObject*) &mmap_object_type);
  1308. #ifdef PROT_EXEC
  1309. setint(dict, "PROT_EXEC", PROT_EXEC);
  1310. #endif
  1311. #ifdef PROT_READ
  1312. setint(dict, "PROT_READ", PROT_READ);
  1313. #endif
  1314. #ifdef PROT_WRITE
  1315. setint(dict, "PROT_WRITE", PROT_WRITE);
  1316. #endif
  1317. #ifdef MAP_SHARED
  1318. setint(dict, "MAP_SHARED", MAP_SHARED);
  1319. #endif
  1320. #ifdef MAP_PRIVATE
  1321. setint(dict, "MAP_PRIVATE", MAP_PRIVATE);
  1322. #endif
  1323. #ifdef MAP_DENYWRITE
  1324. setint(dict, "MAP_DENYWRITE", MAP_DENYWRITE);
  1325. #endif
  1326. #ifdef MAP_EXECUTABLE
  1327. setint(dict, "MAP_EXECUTABLE", MAP_EXECUTABLE);
  1328. #endif
  1329. #ifdef MAP_ANONYMOUS
  1330. setint(dict, "MAP_ANON", MAP_ANONYMOUS);
  1331. setint(dict, "MAP_ANONYMOUS", MAP_ANONYMOUS);
  1332. #endif
  1333. setint(dict, "PAGESIZE", (long)my_getpagesize());
  1334. setint(dict, "ALLOCATIONGRANULARITY", (long)my_getallocationgranularity());
  1335. setint(dict, "ACCESS_READ", ACCESS_READ);
  1336. setint(dict, "ACCESS_WRITE", ACCESS_WRITE);
  1337. setint(dict, "ACCESS_COPY", ACCESS_COPY);
  1338. }