Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save lovesegfault/4fc0dde5ff583e58a937e14c7ffdcdbe to your computer and use it in GitHub Desktop.
Save lovesegfault/4fc0dde5ff583e58a937e14c7ffdcdbe to your computer and use it in GitHub Desktop.
diff --git a/mutagen/_util.py b/mutagen/_util.py
index 1332f9d..795eec4 100644
--- a/mutagen/_util.py
+++ b/mutagen/_util.py
@@ -19,13 +19,6 @@ import errno
import decimal
from io import BytesIO
-try:
- import mmap
-except ImportError:
- # Google App Engine has no mmap:
- # https://github.com/quodlibet/mutagen/issues/286
- mmap = None
-
from collections import namedtuple
from contextlib import contextmanager
from functools import wraps
@@ -683,64 +676,6 @@ def seek_end(fileobj, offset):
fileobj.seek(-offset, 2)
-def mmap_move(fileobj, dest, src, count):
- """Mmaps the file object if possible and moves 'count' data
- from 'src' to 'dest'. All data has to be inside the file size
- (enlarging the file through this function isn't possible)
-
- Will adjust the file offset.
-
- Args:
- fileobj (fileobj)
- dest (int): The destination offset
- src (int): The source offset
- count (int) The amount of data to move
- Raises:
- mmap.error: In case move failed
- IOError: In case an operation on the fileobj fails
- ValueError: In case invalid parameters were given
- """
-
- assert mmap is not None, "no mmap support"
-
- if dest < 0 or src < 0 or count < 0:
- raise ValueError("Invalid parameters")
-
- try:
- fileno = fileobj.fileno()
- except (AttributeError, IOError):
- raise mmap.error(
- "File object does not expose/support a file descriptor")
-
- fileobj.seek(0, 2)
- filesize = fileobj.tell()
- length = max(dest, src) + count
-
- if length > filesize:
- raise ValueError("Not in file size boundary")
-
- offset = ((min(dest, src) // mmap.ALLOCATIONGRANULARITY) *
- mmap.ALLOCATIONGRANULARITY)
- assert dest >= offset
- assert src >= offset
- assert offset % mmap.ALLOCATIONGRANULARITY == 0
-
- # Windows doesn't handle empty mappings, add a fast path here instead
- if count == 0:
- return
-
- # fast path
- if src == dest:
- return
-
- fileobj.flush()
- file_map = mmap.mmap(fileno, length - offset, offset=offset)
- try:
- file_map.move(dest - offset, src - offset, count)
- finally:
- file_map.close()
-
-
def resize_file(fobj, diff, BUFFER_SIZE=2 ** 16):
"""Resize a file by `diff`.
@@ -825,8 +760,7 @@ def insert_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
"""Insert size bytes of empty space starting at offset.
fobj must be an open file object, open rb+ or
- equivalent. Mutagen tries to use mmap to resize the file, but
- falls back to a significantly slower method if mmap fails.
+ equivalent.
Args:
fobj (fileobj)
@@ -848,21 +782,14 @@ def insert_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
resize_file(fobj, size, BUFFER_SIZE)
- if mmap is not None:
- try:
- mmap_move(fobj, offset + size, offset, movesize)
- except mmap.error:
- fallback_move(fobj, offset + size, offset, movesize, BUFFER_SIZE)
- else:
- fallback_move(fobj, offset + size, offset, movesize, BUFFER_SIZE)
+ fallback_move(fobj, offset + size, offset, movesize, BUFFER_SIZE)
def delete_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
"""Delete size bytes of empty space starting at offset.
fobj must be an open file object, open rb+ or
- equivalent. Mutagen tries to use mmap to resize the file, but
- falls back to a significantly slower method if mmap fails.
+ equivalent.
Args:
fobj (fileobj)
@@ -882,13 +809,7 @@ def delete_bytes(fobj, size, offset, BUFFER_SIZE=2 ** 16):
if movesize < 0:
raise ValueError
- if mmap is not None:
- try:
- mmap_move(fobj, offset, offset + size, movesize)
- except mmap.error:
- fallback_move(fobj, offset, offset + size, movesize, BUFFER_SIZE)
- else:
- fallback_move(fobj, offset, offset + size, movesize, BUFFER_SIZE)
+ fallback_move(fobj, offset, offset + size, movesize, BUFFER_SIZE)
resize_file(fobj, -size, BUFFER_SIZE)
diff --git a/tests/test__util.py b/tests/test__util.py
index 0ed25ed..0a079dd 100644
--- a/tests/test__util.py
+++ b/tests/test__util.py
@@ -2,7 +2,7 @@
from mutagen._util import DictMixin, cdata, insert_bytes, delete_bytes, \
decode_terminated, dict_match, enum, get_size, BitReader, BitReaderError, \
- resize_bytes, seek_end, mmap_move, verify_fileobj, fileobj_name, \
+ resize_bytes, seek_end, verify_fileobj, fileobj_name, \
read_full, flags, resize_file, fallback_move, encode_endian, loadfile, \
intround, verify_filename
from mutagen._compat import text_type, itervalues, iterkeys, iteritems, PY2, \
@@ -11,7 +11,6 @@ from tests import TestCase, get_temp_empty
import os
import random
import tempfile
-import mmap
import errno
try:
@@ -376,32 +375,12 @@ class TMoveMixin(object):
self.MOVE(o, 0, 1, 2)
self.MOVE(o, 1, 0, 2)
- def test_larger_than_page_size(self):
- off = mmap.ALLOCATIONGRANULARITY
- with self.file(b"f" * off * 2) as o:
- self.MOVE(o, off, off + 1, off - 1)
- self.MOVE(o, off + 1, off, off - 1)
-
- with self.file(b"f" * off * 2 + b"x") as o:
- self.MOVE(o, off * 2 - 1, off * 2, 1)
- self.assertEqual(self.read(o)[-3:], b"fxx")
-
class Tfallback_move(TestCase, TMoveMixin):
MOVE = staticmethod(fallback_move)
-class MmapMove(TestCase, TMoveMixin):
-
- MOVE = staticmethod(mmap_move)
-
- def test_stringio(self):
- self.assertRaises(mmap.error, mmap_move, cBytesIO(), 0, 0, 0)
-
- def test_no_fileno(self):
- self.assertRaises(mmap.error, mmap_move, object(), 0, 0, 0)
-
class FileHandling(TestCase):
def file(self, contents):
@@ -588,41 +567,6 @@ class FileHandling(TestCase):
self.failUnless(fobj.read() == data)
-class FileHandlingMockedMMap(FileHandling):
-
- def setUp(self):
- def MockMMap2(*args, **kwargs):
- raise mmap.error
-
- self._orig_mmap = mmap.mmap
- mmap.mmap = MockMMap2
-
- def tearDown(self):
- mmap.mmap = self._orig_mmap
-
-
-class FileHandlingNoMMap(FileHandling):
- """Disables mmap and makes sure it raises if it still gets used somehow"""
-
- def setUp(self):
- from mutagen import _util
-
- def MockMMap2(*args, **kwargs):
- assert False
-
- self._orig_mmap = mmap.mmap
- mmap.mmap = MockMMap2
-
- _util.mmap = None
-
- def tearDown(self):
- from mutagen import _util
- import mmap
-
- _util.mmap = mmap
- mmap.mmap = self._orig_mmap
-
-
class Tdict_match(TestCase):
def test_match(self):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment