summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDonghee Na <donghee.na@python.org>2023-11-09 11:13:35 (GMT)
committerGitHub <noreply@github.com>2023-11-09 11:13:35 (GMT)
commit6046aec377311efb89c4438f7cf412e2c6568ba1 (patch)
tree07068685c50a1dda084f3d1fcd89e9279afa6a49
parent30ec968befde2313f66af4754d410dc5a080a20a (diff)
downloadcpython-6046aec377311efb89c4438f7cf412e2c6568ba1.zip
cpython-6046aec377311efb89c4438f7cf412e2c6568ba1.tar.gz
cpython-6046aec377311efb89c4438f7cf412e2c6568ba1.tar.bz2
gh-111835: Add seekable method to mmap.mmap (gh-111852)
-rw-r--r--Doc/library/mmap.rst8
-rw-r--r--Doc/whatsnew/3.13.rst8
-rw-r--r--Lib/test/test_mmap.py11
-rw-r--r--Misc/NEWS.d/next/Library/2023-11-08-23-32-03.gh-issue-111835.ufFiuW.rst4
-rw-r--r--Modules/mmapmodule.c21
5 files changed, 40 insertions, 12 deletions
diff --git a/Doc/library/mmap.rst b/Doc/library/mmap.rst
index 4ca7a64..3fa69e7 100644
--- a/Doc/library/mmap.rst
+++ b/Doc/library/mmap.rst
@@ -285,6 +285,14 @@ To map anonymous memory, -1 should be passed as the fileno along with the length
values are ``os.SEEK_CUR`` or ``1`` (seek relative to the current
position) and ``os.SEEK_END`` or ``2`` (seek relative to the file's end).
+ .. versionchanged:: 3.13
+ Return the new absolute position instead of ``None``.
+
+ .. method:: seekable()
+
+ Return whether the file supports seeking, and the return value is always ``True``.
+
+ .. versionadded:: 3.13
.. method:: size()
diff --git a/Doc/whatsnew/3.13.rst b/Doc/whatsnew/3.13.rst
index 428b648..ae3c88d 100644
--- a/Doc/whatsnew/3.13.rst
+++ b/Doc/whatsnew/3.13.rst
@@ -198,6 +198,14 @@ ipaddress
* Add the :attr:`ipaddress.IPv4Address.ipv6_mapped` property, which returns the IPv4-mapped IPv6 address.
(Contributed by Charles Machalow in :gh:`109466`.)
+mmap
+----
+
+* The :class:`mmap.mmap` class now has an :meth:`~mmap.mmap.seekable` method
+ that can be used where it requires a file-like object with seekable and
+ the :meth:`~mmap.mmap.seek` method return the new absolute position.
+ (Contributed by Donghee Na and Sylvie Liberman in :gh:`111835`.)
+
opcode
------
diff --git a/Lib/test/test_mmap.py b/Lib/test/test_mmap.py
index dfcf303..866ede5 100644
--- a/Lib/test/test_mmap.py
+++ b/Lib/test/test_mmap.py
@@ -93,11 +93,12 @@ class MmapTests(unittest.TestCase):
self.assertEqual(end, PAGESIZE + 6)
# test seeking around (try to overflow the seek implementation)
- m.seek(0,0)
+ self.assertTrue(m.seekable())
+ self.assertEqual(m.seek(0, 0), 0)
self.assertEqual(m.tell(), 0)
- m.seek(42,1)
+ self.assertEqual(m.seek(42, 1), 42)
self.assertEqual(m.tell(), 42)
- m.seek(0,2)
+ self.assertEqual(m.seek(0, 2), len(m))
self.assertEqual(m.tell(), len(m))
# Try to seek to negative position...
@@ -162,7 +163,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write() to
try:
- m.seek(0,0)
+ m.seek(0, 0)
m.write(b'abc')
except TypeError:
pass
@@ -171,7 +172,7 @@ class MmapTests(unittest.TestCase):
# Ensuring that readonly mmap can't be write_byte() to
try:
- m.seek(0,0)
+ m.seek(0, 0)
m.write_byte(b'd')
except TypeError:
pass
diff --git a/Misc/NEWS.d/next/Library/2023-11-08-23-32-03.gh-issue-111835.ufFiuW.rst b/Misc/NEWS.d/next/Library/2023-11-08-23-32-03.gh-issue-111835.ufFiuW.rst
new file mode 100644
index 0000000..5d06c22
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2023-11-08-23-32-03.gh-issue-111835.ufFiuW.rst
@@ -0,0 +1,4 @@
+The :class:`mmap.mmap` class now has an :meth:`~mmap.mmap.seekable` method
+that can be used where it requires a file-like object with seekable and
+the :meth:`~mmap.mmap.seek` method return the new absolute position.
+Patch by Donghee Na.
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index d11200a..66ed0b8 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -171,7 +171,7 @@ mmap_object_dealloc(mmap_object *m_obj)
}
static PyObject *
-mmap_close_method(mmap_object *self, PyObject *unused)
+mmap_close_method(mmap_object *self, PyObject *Py_UNUSED(ignored))
{
if (self->exports > 0) {
PyErr_SetString(PyExc_BufferError, "cannot close "\
@@ -260,7 +260,7 @@ do { \
static PyObject *
mmap_read_byte_method(mmap_object *self,
- PyObject *unused)
+ PyObject *Py_UNUSED(ignored))
{
CHECK_VALID(NULL);
if (self->pos >= self->size) {
@@ -272,7 +272,7 @@ mmap_read_byte_method(mmap_object *self,
static PyObject *
mmap_read_line_method(mmap_object *self,
- PyObject *unused)
+ PyObject *Py_UNUSED(ignored))
{
Py_ssize_t remaining;
char *start, *eol;
@@ -460,7 +460,7 @@ mmap_write_byte_method(mmap_object *self,
static PyObject *
mmap_size_method(mmap_object *self,
- PyObject *unused)
+ PyObject *Py_UNUSED(ignored))
{
CHECK_VALID(NULL);
@@ -657,7 +657,7 @@ mmap_resize_method(mmap_object *self,
}
static PyObject *
-mmap_tell_method(mmap_object *self, PyObject *unused)
+mmap_tell_method(mmap_object *self, PyObject *Py_UNUSED(ignored))
{
CHECK_VALID(NULL);
return PyLong_FromSize_t(self->pos);
@@ -729,7 +729,7 @@ mmap_seek_method(mmap_object *self, PyObject *args)
if (where > self->size || where < 0)
goto onoutofrange;
self->pos = where;
- Py_RETURN_NONE;
+ return PyLong_FromSsize_t(self->pos);
}
onoutofrange:
@@ -738,6 +738,12 @@ mmap_seek_method(mmap_object *self, PyObject *args)
}
static PyObject *
+mmap_seekable_method(mmap_object *self, PyObject *Py_UNUSED(ignored))
+{
+ Py_RETURN_TRUE;
+}
+
+static PyObject *
mmap_move_method(mmap_object *self, PyObject *args)
{
Py_ssize_t dest, src, cnt;
@@ -835,7 +841,7 @@ mmap__repr__method(PyObject *self)
#ifdef MS_WINDOWS
static PyObject *
-mmap__sizeof__method(mmap_object *self, void *unused)
+mmap__sizeof__method(mmap_object *self, void *Py_UNUSED(ignored))
{
size_t res = _PyObject_SIZE(Py_TYPE(self));
if (self->tagname) {
@@ -905,6 +911,7 @@ static struct PyMethodDef mmap_object_methods[] = {
{"readline", (PyCFunction) mmap_read_line_method, METH_NOARGS},
{"resize", (PyCFunction) mmap_resize_method, METH_VARARGS},
{"seek", (PyCFunction) mmap_seek_method, METH_VARARGS},
+ {"seekable", (PyCFunction) mmap_seekable_method, METH_NOARGS},
{"size", (PyCFunction) mmap_size_method, METH_NOARGS},
{"tell", (PyCFunction) mmap_tell_method, METH_NOARGS},
{"write", (PyCFunction) mmap_write_method, METH_VARARGS},