[universal] fix issue: "flush" is not available in Python under 3.3

This commit is contained in:
H.G 2016-09-05 10:50:58 +12:00
parent 44c09c542f
commit 2baeed762a
5 changed files with 20 additions and 0 deletions

View File

@ -1046,6 +1046,22 @@ def set_http_proxy(proxy):
opener = request.build_opener(proxy_support) opener = request.build_opener(proxy_support)
request.install_opener(opener) request.install_opener(opener)
def print_more_compatible(*args, **kwargs):
import builtins as __builtin__
"""Overload default print function as py (<3.3) does not support 'flush' keyword.
Although the function name can be same as print to get itself overloaded automatically,
I'd rather leave it with a different name and only overload it when importing to make less confusion. """
# nothing happens on py3.3 and later
if sys.version_info[:2] >= (3, 3):
return __builtin__.print(*args, **kwargs)
# in lower pyver (e.g. 3.2.x), remove 'flush' keyword and flush it as requested
doFlush = kwargs.pop('flush', False)
ret = __builtin__.print(*args, **kwargs)
if doFlush:
kwargs.get('file', sys.stdout).flush()
return ret
def download_main(download, download_playlist, urls, playlist, **kwargs): def download_main(download, download_playlist, urls, playlist, **kwargs):

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from .common import match1, maybe_print, download_urls, get_filename, parse_host, set_proxy, unset_proxy from .common import match1, maybe_print, download_urls, get_filename, parse_host, set_proxy, unset_proxy
from .common import print_more_compatible as print
from .util import log from .util import log
from . import json_output from . import json_output
import os import os

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python #!/usr/bin/env python
from ..common import * from ..common import *
from ..common import print_more_compatible as print
from ..extractor import VideoExtractor from ..extractor import VideoExtractor
from ..util import log from ..util import log
from .. import json_output from .. import json_output

View File

@ -4,6 +4,7 @@
__all__ = ['netease_download'] __all__ = ['netease_download']
from ..common import * from ..common import *
from ..common import print_more_compatible as print
from ..util import fs from ..util import fs
from json import loads from json import loads
import hashlib import hashlib

View File

@ -3,6 +3,7 @@
import os.path import os.path
import subprocess import subprocess
from ..util.strings import parameterize from ..util.strings import parameterize
from ..common import print_more_compatible as print
def get_usable_ffmpeg(cmd): def get_usable_ffmpeg(cmd):
try: try: