Mercurial > public > mercurial-scm > hg-stable
comparison mercurial/util.py @ 36845:472c68cda3f8
py3: wrap file object to write patch in native eol preserving byte-ness
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Tue, 06 Mar 2018 07:45:57 -0600 |
parents | 5bc7ff103081 |
children | 68ae7d764a65 |
comparison
equal
deleted
inserted
replaced
36844:a00c38b33047 | 36845:472c68cda3f8 |
---|---|
2424 (10, 1 << 10, _('%.1f KB')), | 2424 (10, 1 << 10, _('%.1f KB')), |
2425 (1, 1 << 10, _('%.2f KB')), | 2425 (1, 1 << 10, _('%.2f KB')), |
2426 (1, 1, _('%.0f bytes')), | 2426 (1, 1, _('%.0f bytes')), |
2427 ) | 2427 ) |
2428 | 2428 |
2429 class transformingwriter(object): | |
2430 """Writable file wrapper to transform data by function""" | |
2431 | |
2432 def __init__(self, fp, encode): | |
2433 self._fp = fp | |
2434 self._encode = encode | |
2435 | |
2436 def close(self): | |
2437 self._fp.close() | |
2438 | |
2439 def flush(self): | |
2440 self._fp.flush() | |
2441 | |
2442 def write(self, data): | |
2443 return self._fp.write(self._encode(data)) | |
2444 | |
2429 # Matches a single EOL which can either be a CRLF where repeated CR | 2445 # Matches a single EOL which can either be a CRLF where repeated CR |
2430 # are removed or a LF. We do not care about old Macintosh files, so a | 2446 # are removed or a LF. We do not care about old Macintosh files, so a |
2431 # stray CR is an error. | 2447 # stray CR is an error. |
2432 _eolre = remod.compile(br'\r*\n') | 2448 _eolre = remod.compile(br'\r*\n') |
2433 | 2449 |
2435 return _eolre.sub('\n', s) | 2451 return _eolre.sub('\n', s) |
2436 | 2452 |
2437 def tocrlf(s): | 2453 def tocrlf(s): |
2438 return _eolre.sub('\r\n', s) | 2454 return _eolre.sub('\r\n', s) |
2439 | 2455 |
2456 def _crlfwriter(fp): | |
2457 return transformingwriter(fp, tocrlf) | |
2458 | |
2440 if pycompat.oslinesep == '\r\n': | 2459 if pycompat.oslinesep == '\r\n': |
2441 tonativeeol = tocrlf | 2460 tonativeeol = tocrlf |
2442 fromnativeeol = tolf | 2461 fromnativeeol = tolf |
2462 nativeeolwriter = _crlfwriter | |
2443 else: | 2463 else: |
2444 tonativeeol = pycompat.identity | 2464 tonativeeol = pycompat.identity |
2445 fromnativeeol = pycompat.identity | 2465 fromnativeeol = pycompat.identity |
2466 nativeeolwriter = pycompat.identity | |
2446 | 2467 |
2447 def escapestr(s): | 2468 def escapestr(s): |
2448 # call underlying function of s.encode('string_escape') directly for | 2469 # call underlying function of s.encode('string_escape') directly for |
2449 # Python 3 compatibility | 2470 # Python 3 compatibility |
2450 return codecs.escape_encode(s)[0] | 2471 return codecs.escape_encode(s)[0] |