Skip to content

Commit

Permalink
Fix jsonlint -f writing to stdout under Python 3.
Browse files Browse the repository at this point in the history
Under Python3 the sys.stdout file object is open in text mode,
not binary mode.  So to write raw bytes to it (as is done when
using jsonlint's -f or -F options), you must use the underlying
raw file object (sys.stdout.buffer) that bypasses the text
mode wrapper.
  • Loading branch information
dmeranda committed Dec 22, 2015
1 parent 5f7757e commit 5bc6597
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -6,7 +6,7 @@ PYTHON=python
PYDOC=pydoc

MODULE=demjson
VERSION=2.2.3
VERSION=2.2.4

SOURCES = $(MODULE).py
SETUP = setup.py
Expand Down
5 changes: 5 additions & 0 deletions README.md
Expand Up @@ -14,6 +14,11 @@ unnecessary whitespace.

What's new
==========

<b>Version 2.2.4</b> fixes problem with jsonlint under Python 3 when
trying to reformat JSON (-f or -F options) and writing the output to
standard output.

<b>Version 2.2.3</b> fixes incorrect return values from the "jsonlint"
command. Also fixes a minor problem with the included unit tests in
certain Python versions.
Expand Down
13 changes: 8 additions & 5 deletions demjson.py
Expand Up @@ -120,11 +120,11 @@
__author__ = "Deron Meranda <http://deron.meranda.us/>"
__homepage__ = "http://deron.meranda.us/python/demjson/"

__date__ = "2014-11-12"
__version__ = "2.2.3"
__version_info__ = ( 2, 2, 3 ) # Will be converted into a namedtuple below
__date__ = "2015-12-22"
__version__ = "2.2.4"
__version_info__ = ( 2, 2, 4 ) # Will be converted into a namedtuple below

__credits__ = """Copyright (c) 2006-2014 Deron E. Meranda <http://deron.meranda.us/>
__credits__ = """Copyright (c) 2006-2015 Deron E. Meranda <http://deron.meranda.us/>
Licensed under GNU LGPL (GNU Lesser General Public License) version 3.0
or later. See LICENSE.txt included with this software.
Expand Down Expand Up @@ -6022,7 +6022,10 @@ def _lintcheck( self, filename, output_filename,
self.stderr.write('%s: %s\n' % (pfx, str(err)) )
success = False
else:
self.stdout.write( reformatted )
if hasattr(sys.stdout,'buffer'): # To write binary data rather than strings
self.stdout.buffer.write( reformatted )
else:
self.stdout.write( reformatted )
elif success == self.SUCCESS_OK and verbose_fp:
verbose_fp.write('%sok\n' % pfx)
elif success == self.SUCCESS_WARNING and verbose_fp:
Expand Down
7 changes: 7 additions & 0 deletions docs/CHANGES.txt
@@ -1,5 +1,12 @@
Change history for demjson python module.

Version 2.2.4 released 2015-12-22
=================================

* Fix problem with jsonlint under Python 3 when trying to reformat
JSON (-f or -F options) and writing the output to standard output.


Version 2.2.3 released 2014-11-12
=================================

Expand Down
1 change: 1 addition & 0 deletions docs/NEWS.txt
@@ -1,6 +1,7 @@
News announcements regarding the demjson python module.
See the file CHANGES.txt for more details.

2015-12-22 Release 2.2.4, jsonlint -f stdout bugfix under Python 3.
2014-11-12 Release 2.2.3, jsonlint return value bugfix, unit test fixes.
2014-06-25 Release 2.2.2, Python 3 installation fixes.
2014-06-24 Release 2.2.1, Minor bugfix and html-safe option.
Expand Down
18 changes: 9 additions & 9 deletions docs/demjson.txt
Expand Up @@ -3133,11 +3133,11 @@ DATA
STRICTNESS_WARN = 'warn'
WARN = 'warn'
__author__ = 'Deron Meranda <http://deron.meranda.us/>'
__credits__ = 'Copyright (c) 2006-2014 Deron E. Meranda <http:/.../lic...
__date__ = '2014-11-12'
__credits__ = 'Copyright (c) 2006-2015 Deron E. Meranda <http:/.../lic...
__date__ = '2015-12-22'
__homepage__ = 'http://deron.meranda.us/python/demjson/'
__version__ = '2.2.3'
__version_info__ = version_info(major=2, minor=2, micro=3)
__version__ = '2.2.4'
__version_info__ = version_info(major=2, minor=2, micro=4)
content_type = 'application/json'
file_ext = 'json'
float_maxexp = 308
Expand All @@ -3150,20 +3150,20 @@ DATA
sorting_methods = {'alpha': 'Sort strictly alphabetically', 'alpha_ci'...
syntax_error = demjson.undefined
undefined = demjson.undefined
version = '2.2.3'
version_info = version_info(major=2, minor=2, micro=3)
version = '2.2.4'
version_info = version_info(major=2, minor=2, micro=4)

VERSION
2.2.3
2.2.4

DATE
2014-11-12
2015-12-22

AUTHOR
Deron Meranda <http://deron.meranda.us/>

CREDITS
Copyright (c) 2006-2014 Deron E. Meranda <http://deron.meranda.us/>
Copyright (c) 2006-2015 Deron E. Meranda <http://deron.meranda.us/>

Licensed under GNU LGPL (GNU Lesser General Public License) version 3.0
or later. See LICENSE.txt included with this software.
Expand Down
6 changes: 3 additions & 3 deletions jsonlint
Expand Up @@ -7,9 +7,9 @@ Requires demjson module.
"""
__author__ = "Deron Meranda <http://deron.meranda.us/>"
__homepage__ = "http://deron.meranda.us/python/demjson/"
__date__ = "2014-11-12"
__version__ = "2.2.3"
__credits__ = """Copyright (c) 2006-2014 Deron E. Meranda <http://deron.meranda.us/>
__date__ = "2014-12-22"
__version__ = "2.2.4"
__credits__ = """Copyright (c) 2006-2015 Deron E. Meranda <http://deron.meranda.us/>
Licensed under GNU LGPL (GNU Lesser General Public License) version 3.0
or later. See LICENSE.txt included with this software.
Expand Down
2 changes: 1 addition & 1 deletion setup.py
@@ -1,7 +1,7 @@
# Python package setup script -*- coding: utf-8 -*-

name = 'demjson'
version = '2.2.3'
version = '2.2.4'

import sys
try:
Expand Down

0 comments on commit 5bc6597

Please sign in to comment.