@@ -119,12 +119,12 @@ typedef struct {
119
119
120
120
#ifdef UNIX
121
121
int fd ;
122
- _Bool trackfd ;
123
122
int flags ;
124
123
#endif
125
124
126
125
PyObject * weakreflist ;
127
126
access_mode access ;
127
+ _Bool trackfd ;
128
128
} mmap_object ;
129
129
130
130
#define mmap_object_CAST (op ) ((mmap_object *)(op))
@@ -636,13 +636,11 @@ is_resizeable(mmap_object *self)
636
636
"mmap can't resize with extant buffers exported." );
637
637
return 0 ;
638
638
}
639
- #ifdef UNIX
640
639
if (!self -> trackfd ) {
641
640
PyErr_SetString (PyExc_ValueError ,
642
641
"mmap can't resize with trackfd=False." );
643
642
return 0 ;
644
643
}
645
- #endif
646
644
if ((self -> access == ACCESS_WRITE ) || (self -> access == ACCESS_DEFAULT ))
647
645
return 1 ;
648
646
PyErr_Format (PyExc_TypeError ,
@@ -734,8 +732,6 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
734
732
return PyLong_FromLong ((long )low );
735
733
size = (((long long )high )<<32 ) + low ;
736
734
return PyLong_FromLongLong (size );
737
- } else {
738
- return PyLong_FromSsize_t (self -> size );
739
735
}
740
736
#endif /* MS_WINDOWS */
741
737
@@ -750,6 +746,7 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
750
746
return PyLong_FromLong (status .st_size );
751
747
#endif
752
748
}
749
+ #endif /* UNIX */
753
750
else if (self -> trackfd ) {
754
751
return PyLong_FromSsize_t (self -> size );
755
752
}
@@ -758,7 +755,6 @@ mmap_size_method(PyObject *op, PyObject *Py_UNUSED(ignored))
758
755
"can't get size with trackfd=False" );
759
756
return NULL ;
760
757
}
761
- #endif /* UNIX */
762
758
}
763
759
764
760
/* This assumes that you want the entire file mapped,
@@ -1476,7 +1472,7 @@ static PyObject *
1476
1472
new_mmap_object (PyTypeObject * type , PyObject * args , PyObject * kwdict );
1477
1473
1478
1474
PyDoc_STRVAR (mmap_doc ,
1479
- "Windows: mmap(fileno, length[, tagname[, access[, offset]]])\n\
1475
+ "Windows: mmap(fileno, length[, tagname[, access[, offset[, trackfd] ]]])\n\
1480
1476
\n\
1481
1477
Maps length bytes from the file specified by the file handle fileno,\n\
1482
1478
and returns a mmap object. If length is larger than the current size\n\
@@ -1737,16 +1733,17 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
1737
1733
PyObject * tagname = Py_None ;
1738
1734
DWORD dwErr = 0 ;
1739
1735
int fileno ;
1740
- HANDLE fh = 0 ;
1736
+ HANDLE fh = INVALID_HANDLE_VALUE ;
1741
1737
int access = (access_mode )ACCESS_DEFAULT ;
1738
+ int trackfd = 1 ;
1742
1739
DWORD flProtect , dwDesiredAccess ;
1743
1740
static char * keywords [] = { "fileno" , "length" ,
1744
1741
"tagname" ,
1745
- "access" , "offset" , NULL };
1742
+ "access" , "offset" , "trackfd" , NULL };
1746
1743
1747
- if (!PyArg_ParseTupleAndKeywords (args , kwdict , "in|OiL" , keywords ,
1744
+ if (!PyArg_ParseTupleAndKeywords (args , kwdict , "in|OiL$p " , keywords ,
1748
1745
& fileno , & map_size ,
1749
- & tagname , & access , & offset )) {
1746
+ & tagname , & access , & offset , & trackfd )) {
1750
1747
return NULL ;
1751
1748
}
1752
1749
@@ -1813,30 +1810,36 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
1813
1810
m_obj -> map_handle = NULL ;
1814
1811
m_obj -> tagname = NULL ;
1815
1812
m_obj -> offset = offset ;
1813
+ m_obj -> trackfd = trackfd ;
1816
1814
1817
- if (fh ) {
1818
- /* It is necessary to duplicate the handle, so the
1819
- Python code can close it on us */
1820
- if (!DuplicateHandle (
1821
- GetCurrentProcess (), /* source process handle */
1822
- fh , /* handle to be duplicated */
1823
- GetCurrentProcess (), /* target proc handle */
1824
- (LPHANDLE )& m_obj -> file_handle , /* result */
1825
- 0 , /* access - ignored due to options value */
1826
- FALSE, /* inherited by child processes? */
1827
- DUPLICATE_SAME_ACCESS )) { /* options */
1828
- dwErr = GetLastError ();
1829
- Py_DECREF (m_obj );
1830
- PyErr_SetFromWindowsErr (dwErr );
1831
- return NULL ;
1815
+ if (fh != INVALID_HANDLE_VALUE ) {
1816
+ if (trackfd ) {
1817
+ /* It is necessary to duplicate the handle, so the
1818
+ Python code can close it on us */
1819
+ if (!DuplicateHandle (
1820
+ GetCurrentProcess (), /* source process handle */
1821
+ fh , /* handle to be duplicated */
1822
+ GetCurrentProcess (), /* target proc handle */
1823
+ & fh , /* result */
1824
+ 0 , /* access - ignored due to options value */
1825
+ FALSE, /* inherited by child processes? */
1826
+ DUPLICATE_SAME_ACCESS )) /* options */
1827
+ {
1828
+ dwErr = GetLastError ();
1829
+ Py_DECREF (m_obj );
1830
+ PyErr_SetFromWindowsErr (dwErr );
1831
+ return NULL ;
1832
+ }
1833
+ m_obj -> file_handle = fh ;
1832
1834
}
1833
1835
if (!map_size ) {
1834
1836
DWORD low ,high ;
1835
1837
low = GetFileSize (fh , & high );
1836
1838
/* low might just happen to have the value INVALID_FILE_SIZE;
1837
1839
so we need to check the last error also. */
1838
1840
if (low == INVALID_FILE_SIZE &&
1839
- (dwErr = GetLastError ()) != NO_ERROR ) {
1841
+ (dwErr = GetLastError ()) != NO_ERROR )
1842
+ {
1840
1843
Py_DECREF (m_obj );
1841
1844
return PyErr_SetFromWindowsErr (dwErr );
1842
1845
}
@@ -1898,7 +1901,7 @@ new_mmap_object(PyTypeObject *type, PyObject *args, PyObject *kwdict)
1898
1901
off_lo = (DWORD )(offset & 0xFFFFFFFF );
1899
1902
/* For files, it would be sufficient to pass 0 as size.
1900
1903
For anonymous maps, we have to pass the size explicitly. */
1901
- m_obj -> map_handle = CreateFileMappingW (m_obj -> file_handle ,
1904
+ m_obj -> map_handle = CreateFileMappingW (fh ,
1902
1905
NULL ,
1903
1906
flProtect ,
1904
1907
size_hi ,
0 commit comments