Mercurial > public > mercurial-scm > hg
comparison mercurial/pycompat.py @ 43870:66af68d4c751
pycompat: allow pycompat.sysbytes() even if input already is bytes
pycompat.sysstr() on py3 accepts an input that's already str
(i.e. unicode). This patch makes it so pycompat.sysbytes() on py3
accepts an input that's already bytes. Allowing that makes it possible
to do pycompat.sysbytes(fp.name) where fp.name is either bytes or
unicode, as we'll get when fp can come from either open() or
resources.open_binary().
Differential Revision: https://phab.mercurial-scm.org/D7621
author | Martin von Zweigbergk <martinvonz@google.com> |
---|---|
date | Thu, 12 Dec 2019 10:26:09 -0800 |
parents | 765a9c299c44 |
children | 9d2b2df2c2ba |
comparison
equal
deleted
inserted
replaced
43869:cf065c6a0197 | 43870:66af68d4c751 |
---|---|
251 """Convert an internal str (e.g. keyword, __doc__) back to bytes | 251 """Convert an internal str (e.g. keyword, __doc__) back to bytes |
252 | 252 |
253 This never raises UnicodeEncodeError, but only ASCII characters | 253 This never raises UnicodeEncodeError, but only ASCII characters |
254 can be round-trip by sysstr(sysbytes(s)). | 254 can be round-trip by sysstr(sysbytes(s)). |
255 """ | 255 """ |
256 if isinstance(s, bytes): | |
257 return s | |
256 return s.encode('utf-8') | 258 return s.encode('utf-8') |
257 | 259 |
258 def sysstr(s): | 260 def sysstr(s): |
259 """Return a keyword str to be passed to Python functions such as | 261 """Return a keyword str to be passed to Python functions such as |
260 getattr() and str.encode() | 262 getattr() and str.encode() |