Skip to content
This repository was archived by the owner on Jan 13, 2021. It is now read-only.

Commit 201086f

Browse files
committed
Merge pull request #88 from t2y/fix-print-on-windows
Fixed to output html on Windows avoiding print() function
2 parents 700e6b5 + b51870c commit 201086f

File tree

3 files changed

+20
-7
lines changed

3 files changed

+20
-7
lines changed

hyper/cli.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
Command line interface for Hyper inspired by Httpie.
77
"""
88
import json
9+
import locale
910
import logging
1011
import sys
1112
from argparse import ArgumentParser, RawTextHelpFormatter
@@ -15,12 +16,12 @@
1516

1617
from hyper import HTTP20Connection
1718
from hyper import __version__
18-
from hyper.compat import urlencode, urlsplit
19+
from hyper.compat import is_py2, urlencode, urlsplit, write_to_stdout
1920

2021

2122
log = logging.getLogger('hyper')
2223

23-
FILESYSTEM_ENCODING = sys.getfilesystemencoding()
24+
PREFERRED_ENCODING = locale.getpreferredencoding()
2425

2526
# Various separators used in args
2627
SEP_HEADERS = ':'
@@ -160,13 +161,16 @@ def set_request_data(args):
160161
elif i.sep == SEP_QUERY:
161162
params[i.key] = i.value
162163
elif i.sep == SEP_DATA:
163-
body[i.key] = i.value
164+
value = i.value
165+
if is_py2: # pragma: no cover
166+
value = value.decode(PREFERRED_ENCODING)
167+
body[i.key] = value
164168

165169
if params:
166170
args.url.path += '?' + urlencode(params)
167171

168172
if body:
169-
content_type = 'application/json; charset=%s' % FILESYSTEM_ENCODING
173+
content_type = 'application/json'
170174
headers.setdefault('content-type', content_type)
171175
args.body = json.dumps(body)
172176

@@ -224,7 +228,8 @@ def request(args):
224228
def main(argv=None):
225229
args = parse_argument(argv)
226230
log.debug('Commandline Argument: %s', args)
227-
print(request(args))
231+
data = request(args)
232+
write_to_stdout(data.encode(PREFERRED_ENCODING, errors='replace'))
228233

229234

230235
if __name__ == '__main__': # pragma: no cover

hyper/compat.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ def to_byte(char):
4343
def decode_hex(b):
4444
return b.decode('hex')
4545

46+
def write_to_stdout(data):
47+
sys.stdout.write(data + '\n')
48+
sys.stdout.flush()
49+
4650
# The standard zlib.compressobj() accepts only positional arguments.
4751
def zlib_compressobj(level=6, method=zlib.DEFLATED, wbits=15, memlevel=8,
4852
strategy=zlib.Z_DEFAULT_STRATEGY):
@@ -57,6 +61,10 @@ def to_byte(char):
5761
def decode_hex(b):
5862
return bytes.fromhex(b)
5963

64+
def write_to_stdout(data):
65+
sys.stdout.buffer.write(data + b'\n')
66+
sys.stdout.buffer.flush()
67+
6068
zlib_compressobj = zlib.compressobj
6169

6270
if is_py3_3:

test/test_cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import pytest
55

6-
from hyper.cli import FILESYSTEM_ENCODING as FENC, KeyValue
6+
from hyper.cli import KeyValue
77
from hyper.cli import get_content_type_and_charset, main, parse_argument
88
from hyper.cli import set_request_data, set_url_info
99

@@ -155,7 +155,7 @@ def test_get_content_type_and_charset(response, expected):
155155
KeyValue('data2', 'test2', '=', ''),
156156
]}
157157
),
158-
{'headers': {'content-type': 'application/json; charset=%s' % FENC},
158+
{'headers': {'content-type': 'application/json'},
159159
'method': 'POST',
160160
'body': json.dumps({'data1': 'test1', 'data2': 'test2'}),
161161
}

0 commit comments

Comments
 (0)