comparison mercurial/hgweb/request.py @ 40545:6107d4549fcc stable

hgweb: cast bytearray to bytes PEP-3333 seems to indicate that bytes is the only allowed type that can be used to express the output of a WSGI application. And some WSGI environments seem to enforce this (mod_wsgi does). This commit universally casts bytearray instances to bytes to appease the WSGI specification. I found this because wireprotov2 is emitting bytearray instances. I'd like to keep things that way because the way it builds a data structure, bytearray is more efficient. I'd rather keep the low-level code efficient (and using bytearray) and cast at the edges than impose a performance penalty on code that may run outside WSGI contexts.
author Gregory Szorc <gregory.szorc@gmail.com>
date Fri, 09 Nov 2018 23:49:39 +0000
parents 3e3acf5d6a07
children 2372284d9457
comparison
equal deleted inserted replaced
40544:5b530d767e67 40545:6107d4549fcc
538 538
539 if self._bodybytes: 539 if self._bodybytes:
540 yield self._bodybytes 540 yield self._bodybytes
541 elif self._bodygen: 541 elif self._bodygen:
542 for chunk in self._bodygen: 542 for chunk in self._bodygen:
543 # PEP-3333 says that output must be bytes. And some WSGI
544 # implementations enforce this. We cast bytes-like types here
545 # for convenience.
546 if isinstance(chunk, bytearray):
547 chunk = bytes(chunk)
548
543 yield chunk 549 yield chunk
544 elif self._bodywillwrite: 550 elif self._bodywillwrite:
545 self._bodywritefn = write 551 self._bodywritefn = write
546 else: 552 else:
547 error.ProgrammingError('do not know how to send body') 553 error.ProgrammingError('do not know how to send body')