115115 bytes_type = bytes
116116 text_type = str
117117
118+ # Text streams (as provided by Python)
119+ PY_STDIN_TEXT = sys.stdin
120+ PY_STDOUT_TEXT = sys.stdout
121+ PY_STDERR_TEXT = sys.stderr
122+
123+ # Binary-friendly streams (use .buffer on Py3, fall back on Py2)
124+ PY_STDIN_BUF = getattr(sys.stdin, "buffer", sys.stdin)
125+ PY_STDOUT_BUF = getattr(sys.stdout, "buffer", sys.stdout)
126+ PY_STDERR_BUF = getattr(sys.stderr, "buffer", sys.stderr)
127+
118128# Text vs bytes tuples you can use with isinstance()
119129TEXT_TYPES = (basestring,) # "str or unicode" on Py2, "str" on Py3
120130BINARY_TYPES = (bytes,) if not PY2 else (str,) # bytes on Py3, str on Py2
@@ -2383,7 +2393,7 @@ def GetTotalSize(file_list):
23832393 try:
23842394 total_size += os.path.getsize(item)
23852395 except OSError:
2386- sys.stderr .write("Error accessing file {}: {}\n".format(item, e))
2396+ PY_STDERR_TEXT .write("Error accessing file {}: {}\n".format(item, e))
23872397 return total_size
23882398
23892399
@@ -5517,10 +5527,7 @@ def ReadInFileWithContentToArray(infile, fmttype="auto", filestart=0, seekstart=
55175527 currentfilepos = fp.tell()
55185528 elif(infile == "-"):
55195529 fp = MkTempFile()
5520- if(hasattr(sys.stdin, "buffer")):
5521- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5522- else:
5523- shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5530+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
55245531 try:
55255532 fp.seek(0, 2)
55265533 except OSError:
@@ -5671,10 +5678,7 @@ def ReadInFileWithContentToList(infile, fmttype="auto", filestart=0, seekstart=0
56715678 currentfilepos = fp.tell()
56725679 elif(infile == "-"):
56735680 fp = MkTempFile()
5674- if(hasattr(sys.stdin, "buffer")):
5675- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5676- else:
5677- shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5681+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
56785682 try:
56795683 fp.seek(0, 2)
56805684 except OSError:
@@ -6048,10 +6052,7 @@ def MakeEmptyFile(outfile, fmttype="auto", compression="auto", compresswholefile
60486052 pass
60496053 if(outfile == "-"):
60506054 fp.seek(0, 0)
6051- if(hasattr(sys.stdout, "buffer")):
6052- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
6053- else:
6054- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
6055+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
60556056 elif(outfile is None):
60566057 fp.seek(0, 0)
60576058 outvar = fp.read()
@@ -6168,10 +6169,10 @@ def AppendFilesWithContent(infiles, fp, dirlistfromtxt=False, extradata=[], json
61686169 altinode = formatspecs['use_alt_inode']
61696170 if(verbose):
61706171 logging.basicConfig(format="%(message)s",
6171- stream=sys.stdout , level=logging.DEBUG)
6172+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
61726173 infilelist = []
61736174 if(infiles == "-"):
6174- for line in sys.stdin :
6175+ for line in PY_STDIN_TEXT :
61756176 infilelist.append(line.strip())
61766177 infilelist = list(filter(None, infilelist))
61776178 elif(infiles != "-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles == os.devnull)):
@@ -6489,7 +6490,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
64896490 return False
64906491 if(verbose):
64916492 logging.basicConfig(format="%(message)s",
6492- stream=sys.stdout , level=logging.DEBUG)
6493+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
64936494 curinode = 0
64946495 curfid = 0
64956496 inodelist = []
@@ -6498,10 +6499,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
64986499 inodetoforminode = {}
64996500 if(infile == "-"):
65006501 infile = MkTempFile()
6501- if(hasattr(sys.stdin, "buffer")):
6502- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6503- else:
6504- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6502+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
65056503 infile.seek(0, 0)
65066504 if(not infile):
65076505 return False
@@ -6723,7 +6721,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67236721 return False
67246722 if(verbose):
67256723 logging.basicConfig(format="%(message)s",
6726- stream=sys.stdout , level=logging.DEBUG)
6724+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
67276725 curinode = 0
67286726 curfid = 0
67296727 inodelist = []
@@ -6732,10 +6730,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67326730 inodetoforminode = {}
67336731 if(infile == "-"):
67346732 infile = MkTempFile()
6735- if(hasattr(sys.stdin, "buffer")):
6736- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6737- else:
6738- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6733+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
67396734 infile.seek(0, 0)
67406735 if(not infile):
67416736 return False
@@ -6961,7 +6956,7 @@ def AppendFilesWithContentFromRarFile(infile, fp, extradata=[], jsondata={}, com
69616956 return False
69626957 if(verbose):
69636958 logging.basicConfig(format="%(message)s",
6964- stream=sys.stdout , level=logging.DEBUG)
6959+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
69656960 curinode = 0
69666961 curfid = 0
69676962 inodelist = []
@@ -7215,7 +7210,7 @@ def AppendFilesWithContentFromSevenZipFile(infile, fp, extradata=[], jsondata={}
72157210 return False
72167211 if(verbose):
72177212 logging.basicConfig(format="%(message)s",
7218- stream=sys.stdout , level=logging.DEBUG)
7213+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
72197214 formver = formatspecs['format_ver']
72207215 fileheaderver = str(int(formver.replace(".", "")))
72217216 curinode = 0
@@ -7399,7 +7394,7 @@ def AppendListsWithContent(inlist, fp, dirlistfromtxt=False, extradata=[], jsond
73997394 if(not hasattr(fp, "write")):
74007395 return False
74017396 if(verbose):
7402- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
7397+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
74037398 GetDirList = inlist
74047399 if(not GetDirList):
74057400 return False
@@ -7523,10 +7518,7 @@ def AppendFilesWithContentToOutFile(infiles, outfile, dirlistfromtxt=False, fmtt
75237518 pass
75247519 if(outfile == "-"):
75257520 fp.seek(0, 0)
7526- if(hasattr(sys.stdout, "buffer")):
7527- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7528- else:
7529- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7521+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
75307522 elif(outfile is None):
75317523 fp.seek(0, 0)
75327524 outvar = fp.read()
@@ -7616,10 +7608,7 @@ def AppendListsWithContentToOutFile(inlist, outfile, dirlistfromtxt=False, fmtty
76167608 pass
76177609 if(outfile == "-"):
76187610 fp.seek(0, 0)
7619- if(hasattr(sys.stdout, "buffer")):
7620- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7621- else:
7622- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7611+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
76237612 elif(outfile is None):
76247613 fp.seek(0, 0)
76257614 outvar = fp.read()
@@ -7696,10 +7685,7 @@ def AppendFilesWithContentFromTarFileToOutFile(infiles, outfile, fmttype="auto",
76967685 pass
76977686 if(outfile == "-"):
76987687 fp.seek(0, 0)
7699- if(hasattr(sys.stdout, "buffer")):
7700- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7701- else:
7702- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7688+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
77037689 elif(outfile is None):
77047690 fp.seek(0, 0)
77057691 outvar = fp.read()
@@ -7791,10 +7777,7 @@ def AppendFilesWithContentFromZipFileToOutFile(infiles, outfile, fmttype="auto",
77917777 pass
77927778 if(outfile == "-"):
77937779 fp.seek(0, 0)
7794- if(hasattr(sys.stdout, "buffer")):
7795- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7796- else:
7797- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7780+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
77987781 elif(outfile is None):
77997782 fp.seek(0, 0)
78007783 outvar = fp.read()
@@ -7891,10 +7874,7 @@ def AppendFilesWithContentFromRarFileToOutFile(infiles, outfile, fmttype="auto",
78917874 pass
78927875 if(outfile == "-"):
78937876 fp.seek(0, 0)
7894- if(hasattr(sys.stdout, "buffer")):
7895- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7896- else:
7897- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7877+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
78987878 elif(outfile is None):
78997879 fp.seek(0, 0)
79007880 outvar = fp.read()
@@ -7991,10 +7971,7 @@ def AppendFilesWithContentFromSevenZipFileToOutFile(infiles, outfile, fmttype="a
79917971 pass
79927972 if(outfile == "-"):
79937973 fp.seek(0, 0)
7994- if(hasattr(sys.stdout, "buffer")):
7995- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7996- else:
7997- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7974+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
79987975 elif(outfile is None):
79997976 fp.seek(0, 0)
80007977 outvar = fp.read()
@@ -9836,7 +9813,7 @@ def PackArchiveFileFromInFile(infile, outfile, fmttype="auto", compression="auto
98369813 if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
98379814 formatspecs = formatspecs[checkcompressfile]
98389815 if(verbose):
9839- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
9816+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
98409817 if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
98419818 return PackArchiveFileFromTarFile(infile, outfile, fmttype, compression, compresswholefile, compressionlevel, compressionuselist, checksumtype, extradata, jsondata, formatspecs, verbose, returnfp)
98429819 elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
@@ -9919,7 +9896,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
99199896 formatspecs=__file_format_multi_dict__, # keep default like original
99209897 seektoend=False, verbose=False, returnfp=False):
99219898 if(verbose):
9922- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
9899+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
99239900
99249901 if(IsNestedDict(formatspecs) and fmttype!="auto" and fmttype in formatspecs):
99259902 formatspecs = formatspecs[fmttype]
@@ -9946,10 +9923,7 @@ def ArchiveFileValidate(infile, fmttype="auto", filestart=0,
99469923
99479924 elif(infile == "-"):
99489925 fp = MkTempFile()
9949- if(hasattr(sys.stdin, "buffer")):
9950- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
9951- else:
9952- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
9926+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
99539927 fp.seek(filestart, 0)
99549928 fp = UncompressFileAlt(fp, formatspecs, filestart)
99559929 checkcompressfile = CheckCompressionSubType(fp, formatspecs, filestart, True)
@@ -10669,7 +10643,7 @@ def RePackArchiveFile(infile, outfile, fmttype="auto", compression="auto", compr
1066910643 compression = "auto"
1067010644
1067110645 if verbose:
10672- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
10646+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1067310647
1067410648 # No files?
1067510649 if not listarrayfiles.get('ffilelist'):
@@ -10903,10 +10877,7 @@ def RePackArchiveFile(infile, outfile, fmttype="auto", compression="auto", compr
1090310877
1090410878 if outfile == "-":
1090510879 fp.seek(0, 0)
10906- if hasattr(sys.stdout, "buffer"):
10907- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
10908- else:
10909- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
10880+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
1091010881 elif outfile is None:
1091110882 fp.seek(0, 0)
1091210883 outvar = fp.read()
@@ -10965,7 +10936,7 @@ def UnPackArchiveFile(infile, outdir=None, followlink=False, filestart=0, seekst
1096510936 if(outdir is not None):
1096610937 outdir = RemoveWindowsPath(outdir)
1096710938 if(verbose):
10968- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
10939+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1096910940 if(isinstance(infile, dict)):
1097010941 listarrayfiles = infile
1097110942 else:
@@ -11241,7 +11212,7 @@ def ftype_to_str(ftype):
1124111212
1124211213def ArchiveFileListFiles(infile, fmttype="auto", filestart=0, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_multi_dict__, seektoend=False, verbose=False, newstyle=False, returnfp=False):
1124311214 if(verbose):
11244- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11215+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1124511216 if(isinstance(infile, dict)):
1124611217 listarrayfileslist = [infile]
1124711218 if(isinstance(infile, list)):
@@ -11354,13 +11325,10 @@ def ArchiveFileStringListFiles(instr, filestart=0, seekstart=0, seekend=0, skipc
1135411325
1135511326def TarFileListFiles(infile, verbose=False, returnfp=False):
1135611327 if(verbose):
11357- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11328+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1135811329 if(infile == "-"):
1135911330 infile = MkTempFile()
11360- if(hasattr(sys.stdin, "buffer")):
11361- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11362- else:
11363- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
11331+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
1136411332 infile.seek(0, 0)
1136511333 if(not infile):
1136611334 return False
@@ -11479,13 +11447,10 @@ def TarFileListFiles(infile, verbose=False, returnfp=False):
1147911447
1148011448def ZipFileListFiles(infile, verbose=False, returnfp=False):
1148111449 if(verbose):
11482- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11450+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1148311451 if(infile == "-"):
1148411452 infile = MkTempFile()
11485- if(hasattr(sys.stdin, "buffer")):
11486- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11487- else:
11488- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
11453+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
1148911454 infile.seek(0, 0)
1149011455 if(not infile):
1149111456 return False
@@ -11617,7 +11582,7 @@ def RarFileListFiles(infile, verbose=False, returnfp=False):
1161711582if(rarfile_support):
1161811583 def RarFileListFiles(infile, verbose=False, returnfp=False):
1161911584 if(verbose):
11620- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11585+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1162111586 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1162211587 return False
1162311588 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
@@ -11754,7 +11719,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1175411719if(py7zr_support):
1175511720 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1175611721 if(verbose):
11757- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11722+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1175811723 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1175911724 return False
1176011725 lcfi = 0
@@ -11857,7 +11822,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1185711822
1185811823def InFileListFiles(infile, verbose=False, formatspecs=__file_format_multi_dict__, seektoend=False, newstyle=False, returnfp=False):
1185911824 if(verbose):
11860- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11825+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1186111826 checkcompressfile = CheckCompressionSubType(infile, formatspecs, filestart, True)
1186211827 if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
1186311828 formatspecs = formatspecs[checkcompressfile]
0 commit comments