comparison mercurial/hgweb/webcommands.py @ 40158:9310037f0636

py3: pass str and return bytes from mimetypes.guess_type() This function wants a str (which represents a path) and returns a str. We normalize input to str and output to bytes. Differential Revision: https://phab.mercurial-scm.org/D4967
author Gregory Szorc <gregory.szorc@gmail.com>
date Thu, 11 Oct 2018 21:47:39 +0200
parents 4f44f747f094
children d216ae4cc3f6
comparison
equal deleted inserted replaced
40157:a8be613391d1 40158:9310037f0636
121 121
122 path = fctx.path() 122 path = fctx.path()
123 text = fctx.data() 123 text = fctx.data()
124 mt = 'application/binary' 124 mt = 'application/binary'
125 if guessmime: 125 if guessmime:
126 mt = mimetypes.guess_type(path)[0] 126 mt = mimetypes.guess_type(pycompat.fsdecode(path))[0]
127 if mt is None: 127 if mt is None:
128 if stringutil.binary(text): 128 if stringutil.binary(text):
129 mt = 'application/binary' 129 mt = 'application/binary'
130 else: 130 else:
131 mt = 'text/plain' 131 mt = 'text/plain'
132 else:
133 mt = pycompat.sysbytes(mt)
134
132 if mt.startswith('text/'): 135 if mt.startswith('text/'):
133 mt += '; charset="%s"' % encoding.encoding 136 mt += '; charset="%s"' % encoding.encoding
134 137
135 web.res.headers['Content-Type'] = mt 138 web.res.headers['Content-Type'] = mt
136 filename = (path.rpartition('/')[-1] 139 filename = (path.rpartition('/')[-1]
144 text = fctx.data() 147 text = fctx.data()
145 parity = paritygen(web.stripecount) 148 parity = paritygen(web.stripecount)
146 ishead = fctx.filenode() in fctx.filelog().heads() 149 ishead = fctx.filenode() in fctx.filelog().heads()
147 150
148 if stringutil.binary(text): 151 if stringutil.binary(text):
149 mt = mimetypes.guess_type(f)[0] or 'application/octet-stream' 152 mt = pycompat.sysbytes(
153 mimetypes.guess_type(pycompat.fsdecode(f))[0]
154 or 'application/octet-stream')
150 text = '(binary:%s)' % mt 155 text = '(binary:%s)' % mt
151 156
152 def lines(context): 157 def lines(context):
153 for lineno, t in enumerate(text.splitlines(True)): 158 for lineno, t in enumerate(text.splitlines(True)):
154 yield {"line": t, 159 yield {"line": t,
855 else: 860 else:
856 context = parsecontext(web.config('web', 'comparisoncontext', '5')) 861 context = parsecontext(web.config('web', 'comparisoncontext', '5'))
857 862
858 def filelines(f): 863 def filelines(f):
859 if f.isbinary(): 864 if f.isbinary():
860 mt = mimetypes.guess_type(f.path())[0] 865 mt = pycompat.sysbytes(
861 if not mt: 866 mimetypes.guess_type(pycompat.fsdecode(f.path()))[0]
862 mt = 'application/octet-stream' 867 or 'application/octet-stream')
863 return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))] 868 return [_('(binary file %s, hash: %s)') % (mt, hex(f.filenode()))]
864 return f.data().splitlines() 869 return f.data().splitlines()
865 870
866 fctx = None 871 fctx = None
867 parent = ctx.p1() 872 parent = ctx.p1()
943 for p in parentscache[rev]: 948 for p in parentscache[rev]:
944 yield p 949 yield p
945 950
946 def annotate(context): 951 def annotate(context):
947 if fctx.isbinary(): 952 if fctx.isbinary():
948 mt = (mimetypes.guess_type(fctx.path())[0] 953 mt = pycompat.sysbytes(
949 or 'application/octet-stream') 954 mimetypes.guess_type(pycompat.fsdecode(fctx.path()))[0]
955 or 'application/octet-stream')
950 lines = [dagop.annotateline(fctx=fctx.filectx(fctx.filerev()), 956 lines = [dagop.annotateline(fctx=fctx.filectx(fctx.filerev()),
951 lineno=1, text='(binary:%s)' % mt)] 957 lineno=1, text='(binary:%s)' % mt)]
952 else: 958 else:
953 lines = webutil.annotate(web.req, fctx, web.repo.ui) 959 lines = webutil.annotate(web.req, fctx, web.repo.ui)
954 960