diff --git a/README.rst b/README.rst index e4bdfc2e..b0e70e2a 100644 --- a/README.rst +++ b/README.rst @@ -1280,6 +1280,7 @@ Changelog * To make it easier to deal with Windows paths in request items, ``\`` now only escapes special characters (the ones that are used as key-value separators). + * Fixed ``--output=/dev/null`` on Linux. * `0.8.0`_ (2014-01-25) * Added ``field=@file.txt`` and ``field:=@file.json`` for embedding the contents of text and JSON files into request data. diff --git a/httpie/input.py b/httpie/input.py index 44a96fa6..6420921b 100644 --- a/httpie/input.py +++ b/httpie/input.py @@ -5,6 +5,7 @@ import os import sys import re import json +import errno import mimetypes import getpass from io import BytesIO @@ -185,7 +186,14 @@ class Parser(ArgumentParser): # `stdout`. The file is opened for appending, which isn't what # we want in this case. self.args.output_file.seek(0) - self.args.output_file.truncate() + try: + self.args.output_file.truncate() + except IOError as e: + if e.errno == errno.EINVAL: + # E.g. /dev/null on Linux. + pass + else: + raise self.env.stdout = self.args.output_file self.env.stdout_isatty = False diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 4c939ddd..b22b1400 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -1,6 +1,8 @@ """Miscellaneous regression tests""" +import pytest from utils import http, HTTP_OK +from httpie.compat import is_windows def test_Host_header_overwrite(httpbin): @@ -14,3 +16,12 @@ def test_Host_header_overwrite(httpbin): assert HTTP_OK in r assert r.lower().count('host:') == 1 assert 'host: {0}'.format(host) in r + + +@pytest.mark.skipif(is_windows, reason='Unix-only') +def test_output_devnull(httpbin): + """ + https://github.com/jakubroztocil/httpie/issues/252 + + """ + http('--output=/dev/null', httpbin + '/get')