Skip to content

Commit bb04d2f

Browse files
committed
utils: Simplify the unicodification of pager input
Duplicate what we're doing in pwclient. Signed-off-by: Stephen Finucane <stephen@that.guru> Fixes: #49
1 parent 1a71697 commit bb04d2f

File tree

2 files changed

+11
-24
lines changed

2 files changed

+11
-24
lines changed

git_pw/utils.py

Lines changed: 6 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
from __future__ import print_function
66

7-
import codecs
87
import csv
98
import os
109
import subprocess
@@ -81,44 +80,27 @@ def _tabulate(output, headers, fmt):
8180
sys.exit(1)
8281

8382

84-
def _is_ascii_encoding(encoding):
85-
"""Checks if a given encoding is ascii."""
86-
try:
87-
return codecs.lookup(encoding).name == 'ascii'
88-
except LookupError:
89-
return False
90-
91-
92-
def _get_best_encoding(stream):
93-
"""Returns the default stream encoding if not found."""
94-
rv = getattr(stream, 'encoding', None) or sys.getdefaultencoding()
95-
if _is_ascii_encoding(rv):
96-
return 'utf-8'
97-
return rv
98-
99-
10083
def _echo_via_pager(pager, output):
10184
env = dict(os.environ)
10285
# When the LESS environment variable is unset, Git sets it to FRX (if
10386
# LESS environment variable is set, Git does not change it at all).
10487
if 'LESS' not in env:
10588
env['LESS'] = 'FRX'
10689

107-
c = subprocess.Popen(pager, shell=True, stdin=subprocess.PIPE,
108-
env=env)
109-
encoding = _get_best_encoding(c.stdin)
90+
pager = subprocess.Popen(pager.split(), stdin=subprocess.PIPE, env=env)
91+
92+
output = six.ensure_binary(output)
11093

11194
try:
112-
for line in output:
113-
c.stdin.write(line.encode(encoding, 'replace'))
95+
pager.communicate(input=output)
11496
except (IOError, KeyboardInterrupt):
11597
pass
11698
else:
117-
c.stdin.close()
99+
pager.stdin.close()
118100

119101
while True:
120102
try:
121-
c.wait()
103+
pager.wait()
122104
except KeyboardInterrupt:
123105
pass
124106
else:
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
An issue with the unicode data when using the CSV format on Python 2.7 has
5+
been resolved.

0 commit comments

Comments
 (0)