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
@@ -2390,7 +2400,7 @@ def GetTotalSize(file_list):
23902400 try:
23912401 total_size += os.path.getsize(item)
23922402 except OSError:
2393- sys.stderr .write("Error accessing file {}: {}\n".format(item, e))
2403+ PY_STDERR_TEXT .write("Error accessing file {}: {}\n".format(item, e))
23942404 return total_size
23952405
23962406
@@ -5525,10 +5535,7 @@ def ReadInFileWithContentToArray(infile, fmttype="auto", filestart=0, seekstart=
55255535 currentfilepos = fp.tell()
55265536 elif(infile == "-"):
55275537 fp = MkTempFile()
5528- if(hasattr(sys.stdin, "buffer")):
5529- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5530- else:
5531- shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5538+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
55325539 try:
55335540 fp.seek(0, 2)
55345541 except OSError:
@@ -5679,10 +5686,7 @@ def ReadInFileWithContentToList(infile, fmttype="auto", filestart=0, seekstart=0
56795686 currentfilepos = fp.tell()
56805687 elif(infile == "-"):
56815688 fp = MkTempFile()
5682- if(hasattr(sys.stdin, "buffer")):
5683- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__)
5684- else:
5685- shutil.copyfileobj(sys.stdin, fp, length=__filebuff_size__)
5689+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__)
56865690 try:
56875691 fp.seek(0, 2)
56885692 except OSError:
@@ -6056,10 +6060,7 @@ def MakeEmptyFile(outfile, fmttype="auto", compression="auto", compresswholefile
60566060 pass
60576061 if(outfile == "-"):
60586062 fp.seek(0, 0)
6059- if(hasattr(sys.stdout, "buffer")):
6060- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
6061- else:
6062- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
6063+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
60636064 elif(outfile is None):
60646065 fp.seek(0, 0)
60656066 outvar = fp.read()
@@ -6177,10 +6178,10 @@ def AppendFilesWithContent(infiles, fp, dirlistfromtxt=False, extradata=[], json
61776178 altinode = formatspecs['use_alt_inode']
61786179 if(verbose):
61796180 logging.basicConfig(format="%(message)s",
6180- stream=sys.stdout , level=logging.DEBUG)
6181+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
61816182 infilelist = []
61826183 if(infiles == "-"):
6183- for line in sys.stdin :
6184+ for line in PY_STDIN_TEXT :
61846185 infilelist.append(line.strip())
61856186 infilelist = list(filter(None, infilelist))
61866187 elif(infiles != "-" and dirlistfromtxt and os.path.exists(infiles) and (os.path.isfile(infiles) or infiles == os.devnull)):
@@ -6498,7 +6499,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
64986499 return False
64996500 if(verbose):
65006501 logging.basicConfig(format="%(message)s",
6501- stream=sys.stdout , level=logging.DEBUG)
6502+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
65026503 curinode = 0
65036504 curfid = 0
65046505 inodelist = []
@@ -6507,10 +6508,7 @@ def AppendFilesWithContentFromTarFile(infile, fp, extradata=[], jsondata={}, com
65076508 inodetoforminode = {}
65086509 if(infile == "-"):
65096510 infile = MkTempFile()
6510- if(hasattr(sys.stdin, "buffer")):
6511- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6512- else:
6513- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6511+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
65146512 infile.seek(0, 0)
65156513 if(not infile):
65166514 return False
@@ -6732,7 +6730,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67326730 return False
67336731 if(verbose):
67346732 logging.basicConfig(format="%(message)s",
6735- stream=sys.stdout , level=logging.DEBUG)
6733+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
67366734 curinode = 0
67376735 curfid = 0
67386736 inodelist = []
@@ -6741,10 +6739,7 @@ def AppendFilesWithContentFromZipFile(infile, fp, extradata=[], jsondata={}, com
67416739 inodetoforminode = {}
67426740 if(infile == "-"):
67436741 infile = MkTempFile()
6744- if(hasattr(sys.stdin, "buffer")):
6745- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
6746- else:
6747- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
6742+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
67486743 infile.seek(0, 0)
67496744 if(not infile):
67506745 return False
@@ -6970,7 +6965,7 @@ def AppendFilesWithContentFromRarFile(infile, fp, extradata=[], jsondata={}, com
69706965 return False
69716966 if(verbose):
69726967 logging.basicConfig(format="%(message)s",
6973- stream=sys.stdout , level=logging.DEBUG)
6968+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
69746969 curinode = 0
69756970 curfid = 0
69766971 inodelist = []
@@ -7224,7 +7219,7 @@ def AppendFilesWithContentFromSevenZipFile(infile, fp, extradata=[], jsondata={}
72247219 return False
72257220 if(verbose):
72267221 logging.basicConfig(format="%(message)s",
7227- stream=sys.stdout , level=logging.DEBUG)
7222+ stream=PY_STDOUT_TEXT , level=logging.DEBUG)
72287223 formver = formatspecs['format_ver']
72297224 fileheaderver = str(int(formver.replace(".", "")))
72307225 curinode = 0
@@ -7408,7 +7403,7 @@ def AppendListsWithContent(inlist, fp, dirlistfromtxt=False, extradata=[], jsond
74087403 if(not hasattr(fp, "write")):
74097404 return False
74107405 if(verbose):
7411- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
7406+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
74127407 GetDirList = inlist
74137408 if(not GetDirList):
74147409 return False
@@ -7532,10 +7527,7 @@ def AppendFilesWithContentToOutFile(infiles, outfile, dirlistfromtxt=False, fmtt
75327527 pass
75337528 if(outfile == "-"):
75347529 fp.seek(0, 0)
7535- if(hasattr(sys.stdout, "buffer")):
7536- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7537- else:
7538- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7530+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
75397531 elif(outfile is None):
75407532 fp.seek(0, 0)
75417533 outvar = fp.read()
@@ -7625,10 +7617,7 @@ def AppendListsWithContentToOutFile(inlist, outfile, dirlistfromtxt=False, fmtty
76257617 pass
76267618 if(outfile == "-"):
76277619 fp.seek(0, 0)
7628- if(hasattr(sys.stdout, "buffer")):
7629- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7630- else:
7631- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7620+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
76327621 elif(outfile is None):
76337622 fp.seek(0, 0)
76347623 outvar = fp.read()
@@ -7705,10 +7694,7 @@ def AppendFilesWithContentFromTarFileToOutFile(infiles, outfile, fmttype="auto",
77057694 pass
77067695 if(outfile == "-"):
77077696 fp.seek(0, 0)
7708- if(hasattr(sys.stdout, "buffer")):
7709- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7710- else:
7711- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7697+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
77127698 elif(outfile is None):
77137699 fp.seek(0, 0)
77147700 outvar = fp.read()
@@ -7800,10 +7786,7 @@ def AppendFilesWithContentFromZipFileToOutFile(infiles, outfile, fmttype="auto",
78007786 pass
78017787 if(outfile == "-"):
78027788 fp.seek(0, 0)
7803- if(hasattr(sys.stdout, "buffer")):
7804- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7805- else:
7806- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7789+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
78077790 elif(outfile is None):
78087791 fp.seek(0, 0)
78097792 outvar = fp.read()
@@ -7900,10 +7883,7 @@ def AppendFilesWithContentFromRarFileToOutFile(infiles, outfile, fmttype="auto",
79007883 pass
79017884 if(outfile == "-"):
79027885 fp.seek(0, 0)
7903- if(hasattr(sys.stdout, "buffer")):
7904- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
7905- else:
7906- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7886+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
79077887 elif(outfile is None):
79087888 fp.seek(0, 0)
79097889 outvar = fp.read()
@@ -8000,10 +7980,7 @@ def AppendFilesWithContentFromSevenZipFileToOutFile(infiles, outfile, fmttype="a
80007980 pass
80017981 if(outfile == "-"):
80027982 fp.seek(0, 0)
8003- if(hasattr(sys.stdout, "buffer")):
8004- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
8005- else:
8006- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
7983+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
80077984 elif(outfile is None):
80087985 fp.seek(0, 0)
80097986 outvar = fp.read()
@@ -9845,7 +9822,7 @@ def PackCatFileFromInFile(infile, outfile, fmttype="auto", compression="auto", c
98459822 if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
98469823 formatspecs = formatspecs[checkcompressfile]
98479824 if(verbose):
9848- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
9825+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
98499826 if(checkcompressfile == "tarfile" and TarFileCheck(infile)):
98509827 return PackCatFileFromTarFile(infile, outfile, fmttype, compression, compresswholefile, compressionlevel, compressionuselist, checksumtype, extradata, jsondata, formatspecs, verbose, returnfp)
98519828 elif(checkcompressfile == "zipfile" and zipfile.is_zipfile(infile)):
@@ -9928,7 +9905,7 @@ def CatFileValidate(infile, fmttype="auto", filestart=0,
99289905 formatspecs=__file_format_multi_dict__, # keep default like original
99299906 seektoend=False, verbose=False, returnfp=False):
99309907 if(verbose):
9931- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
9908+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
99329909
99339910 if(IsNestedDict(formatspecs) and fmttype!="auto" and fmttype in formatspecs):
99349911 formatspecs = formatspecs[fmttype]
@@ -9955,10 +9932,7 @@ def CatFileValidate(infile, fmttype="auto", filestart=0,
99559932
99569933 elif(infile == "-"):
99579934 fp = MkTempFile()
9958- if(hasattr(sys.stdin, "buffer")):
9959- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__, length=__filebuff_size__)
9960- else:
9961- shutil.copyfileobj(sys.stdin.buffer, fp, length=__filebuff_size__, length=__filebuff_size__)
9935+ shutil.copyfileobj(PY_STDIN_BUF, fp, length=__filebuff_size__, length=__filebuff_size__)
99629936 fp.seek(filestart, 0)
99639937 fp = UncompressFileAlt(fp, formatspecs, filestart)
99649938 checkcompressfile = CheckCompressionSubType(fp, formatspecs, filestart, True)
@@ -10678,7 +10652,7 @@ def RePackCatFile(infile, outfile, fmttype="auto", compression="auto", compressw
1067810652 compression = "auto"
1067910653
1068010654 if verbose:
10681- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
10655+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1068210656
1068310657 # No files?
1068410658 if not listarrayfiles.get('ffilelist'):
@@ -10912,10 +10886,7 @@ def RePackCatFile(infile, outfile, fmttype="auto", compression="auto", compressw
1091210886
1091310887 if outfile == "-":
1091410888 fp.seek(0, 0)
10915- if hasattr(sys.stdout, "buffer"):
10916- shutil.copyfileobj(fp, sys.stdout.buffer, length=__filebuff_size__)
10917- else:
10918- shutil.copyfileobj(fp, sys.stdout, length=__filebuff_size__)
10889+ shutil.copyfileobj(fp, PY_STDOUT_BUF, length=__filebuff_size__)
1091910890 elif outfile is None:
1092010891 fp.seek(0, 0)
1092110892 outvar = fp.read()
@@ -10974,7 +10945,7 @@ def UnPackCatFile(infile, outdir=None, followlink=False, filestart=0, seekstart=
1097410945 if(outdir is not None):
1097510946 outdir = RemoveWindowsPath(outdir)
1097610947 if(verbose):
10977- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
10948+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1097810949 if(isinstance(infile, dict)):
1097910950 listarrayfiles = infile
1098010951 else:
@@ -11250,7 +11221,7 @@ def ftype_to_str(ftype):
1125011221
1125111222def CatFileListFiles(infile, fmttype="auto", filestart=0, seekstart=0, seekend=0, skipchecksum=False, formatspecs=__file_format_multi_dict__, seektoend=False, verbose=False, newstyle=False, returnfp=False):
1125211223 if(verbose):
11253- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11224+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1125411225 if(isinstance(infile, dict)):
1125511226 listarrayfileslist = [infile]
1125611227 if(isinstance(infile, list)):
@@ -11363,13 +11334,10 @@ def CatFileStringListFiles(instr, filestart=0, seekstart=0, seekend=0, skipcheck
1136311334
1136411335def TarFileListFiles(infile, verbose=False, returnfp=False):
1136511336 if(verbose):
11366- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11337+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1136711338 if(infile == "-"):
1136811339 infile = MkTempFile()
11369- if(hasattr(sys.stdin, "buffer")):
11370- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11371- else:
11372- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
11340+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
1137311341 infile.seek(0, 0)
1137411342 if(not infile):
1137511343 return False
@@ -11488,13 +11456,10 @@ def TarFileListFiles(infile, verbose=False, returnfp=False):
1148811456
1148911457def ZipFileListFiles(infile, verbose=False, returnfp=False):
1149011458 if(verbose):
11491- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11459+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1149211460 if(infile == "-"):
1149311461 infile = MkTempFile()
11494- if(hasattr(sys.stdin, "buffer")):
11495- shutil.copyfileobj(sys.stdin.buffer, infile, length=__filebuff_size__)
11496- else:
11497- shutil.copyfileobj(sys.stdin, infile, length=__filebuff_size__)
11462+ shutil.copyfileobj(PY_STDIN_BUF, infile, length=__filebuff_size__)
1149811463 infile.seek(0, 0)
1149911464 if(not infile):
1150011465 return False
@@ -11626,7 +11591,7 @@ def RarFileListFiles(infile, verbose=False, returnfp=False):
1162611591if(rarfile_support):
1162711592 def RarFileListFiles(infile, verbose=False, returnfp=False):
1162811593 if(verbose):
11629- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11594+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1163011595 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1163111596 return False
1163211597 if(not rarfile.is_rarfile(infile) and not rarfile.is_rarfile_sfx(infile)):
@@ -11763,7 +11728,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1176311728if(py7zr_support):
1176411729 def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1176511730 if(verbose):
11766- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11731+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1176711732 if(not os.path.exists(infile) or not os.path.isfile(infile)):
1176811733 return False
1176911734 lcfi = 0
@@ -11866,7 +11831,7 @@ def SevenZipFileListFiles(infile, verbose=False, returnfp=False):
1186611831
1186711832def InFileListFiles(infile, verbose=False, formatspecs=__file_format_multi_dict__, seektoend=False, newstyle=False, returnfp=False):
1186811833 if(verbose):
11869- logging.basicConfig(format="%(message)s", stream=sys.stdout , level=logging.DEBUG)
11834+ logging.basicConfig(format="%(message)s", stream=PY_STDOUT_TEXT , level=logging.DEBUG)
1187011835 checkcompressfile = CheckCompressionSubType(infile, formatspecs, filestart, True)
1187111836 if(IsNestedDict(formatspecs) and checkcompressfile in formatspecs):
1187211837 formatspecs = formatspecs[checkcompressfile]
0 commit comments