From 5c644890e83662e7b749590c70e5ddf0dc7adca5 Mon Sep 17 00:00:00 2001 From: Labrys of Knossos Date: Sat, 5 Jan 2019 17:02:03 -0500 Subject: [PATCH] Fix shutil.copyfileobj monkey patching --- core/utils/__init__.py | 11 ++--------- core/utils/shutil_custom.py | 11 +++++++++++ 2 files changed, 13 insertions(+), 9 deletions(-) create mode 100644 core/utils/shutil_custom.py diff --git a/core/utils/__init__.py b/core/utils/__init__.py index 376c4898..7716011f 100644 --- a/core/utils/__init__.py +++ b/core/utils/__init__.py @@ -19,6 +19,7 @@ import subliminal import core from core import extractor, logger from core.utils.download_info import get_download_info, update_download_info_status +from core.utils import shutil_custom from core.utils.network import test_connection, wake_on_lan, wake_up from core.utils.parsers import ( parse_args, @@ -47,15 +48,7 @@ except ImportError: requests.packages.urllib3.disable_warnings() -# Monkey Patch shutil.copyfileobj() to adjust the buffer length to 512KB rather than 4KB -shutil.copyfileobjOrig = shutil.copyfileobj - - -def copyfileobj_fast(fsrc, fdst, length=512 * 1024): - shutil.copyfileobjOrig(fsrc, fdst, length=length) - - -shutil.copyfileobj = copyfileobj_fast +shutil_custom.monkey_patch() def sanitize_name(name): diff --git a/core/utils/shutil_custom.py b/core/utils/shutil_custom.py new file mode 100644 index 00000000..5525df1f --- /dev/null +++ b/core/utils/shutil_custom.py @@ -0,0 +1,11 @@ +from functools import partial +import shutil +from six import PY2 + + +def monkey_patch(length=512 * 1024): + if PY2: + # On Python 2 monkey patch shutil.copyfileobj() + # to adjust the buffer length to 512KB rather than 4KB + original_copyfileobj = shutil.copyfileobj + shutil.copyfileobj = partial(original_copyfileobj, length=length)