From b135f90562bdfc29f9cf83b03339c030bd483902 Mon Sep 17 00:00:00 2001 From: Wim Colgate Date: Thu, 29 Aug 2019 15:45:46 -0700 Subject: [PATCH 1/3] For python3 support, change default stdin to be buffered, and adjust end-of-line terminator to be \n instead of \r\n --- splunklib/searchcommands/internals.py | 20 +++++++++++++++++--- splunklib/searchcommands/search_command.py | 8 +++++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/splunklib/searchcommands/internals.py b/splunklib/searchcommands/internals.py index 02634c081..8ef1adf7d 100644 --- a/splunklib/searchcommands/internals.py +++ b/splunklib/searchcommands/internals.py @@ -39,7 +39,8 @@ csv.field_size_limit(10485760) # The default value is 128KB; upping to 10MB. See SPL-12117 for background on this issue -if sys.platform == 'win32': +# SPL-175233 -- python3 stdout is already binary +if sys.platform == 'win32' and sys.version_info <= (3, 0): # Work around the fact that on Windows '\n' is mapped to '\r\n'. The typical solution is to simply open files in # binary mode, but stdout is already open, thus this hack. 'CPython' and 'PyPy' work differently. We assume that # all other Python implementations are compatible with 'CPython'. This might or might not be a valid assumption. @@ -339,6 +340,8 @@ class CsvDialect(csv.Dialect): doublequote = True skipinitialspace = False lineterminator = '\r\n' + if sys.version_info >= (3, 0) and sys.platform == 'win32': + lineterminator = '\n' quoting = csv.QUOTE_MINIMAL @@ -361,6 +364,10 @@ def read(self, ifile): name, value = None, None for line in ifile: + # SPL-175233 -- input is buffered, needs to be decoded + if sys.version_info >= (3, 0): + line = line.decode() + if line == '\n': break item = line.split(':', 1) @@ -658,6 +665,13 @@ class RecordWriterV1(RecordWriter): def flush(self, finished=None, partial=None): + # SPL-175233 + def writeEOL(): + if sys.version_info >= (3, 0) and sys.platform == 'win32': + write('\n') + else: + write('\r\n') + RecordWriter.flush(self, finished, partial) # validates arguments and the state of this instance if self._record_count > 0 or (self._chunk_count == 0 and 'messages' in self._inspector): @@ -678,9 +692,9 @@ def flush(self, finished=None, partial=None): write(message_level(level, level)) write('=') write(text) - write('\r\n') + writeEOL() - write('\r\n') + writeEOL() elif messages is not None: diff --git a/splunklib/searchcommands/search_command.py b/splunklib/searchcommands/search_command.py index 64b06aba1..b93156587 100644 --- a/splunklib/searchcommands/search_command.py +++ b/splunklib/searchcommands/search_command.py @@ -1054,7 +1054,13 @@ def iteritems(self): SearchMetric = namedtuple('SearchMetric', ('elapsed_seconds', 'invocation_count', 'input_count', 'output_count')) -def dispatch(command_class, argv=sys.argv, input_file=sys.stdin, output_file=sys.stdout, module_name=None): +# SPL-175233, set default stdin to be buffered +if sys.version_info >= (3, 0) and sys.platform == 'win32': + stdinput = sys.stdin.buffer +else: + stdinput = sys.stdin + +def dispatch(command_class, argv=sys.argv, input_file=stdinput, output_file=sys.stdout, module_name=None): """ Instantiates and executes a search command class This function implements a `conditional script stanza `_ based on the value of From b298150b2331298c2e46ab5600de8baaf685cdcf Mon Sep 17 00:00:00 2001 From: Liying Jiang Date: Mon, 16 Sep 2019 10:39:44 -0700 Subject: [PATCH 2/3] update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bca13bc2d..63b77bda2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # Splunk SDK for Python Changelog +## Version 1.6.8 + +### Bug Fix + +* Fix custom search command on python 3 on windows + ## Version 1.6.7 ### Changes From 27dd408746ae022c4c88aba7145dbc762d7eed48 Mon Sep 17 00:00:00 2001 From: Liying Jiang Date: Mon, 16 Sep 2019 10:46:12 -0700 Subject: [PATCH 3/3] update version to 1.6.8 --- README.md | 2 +- examples/searchcommands_app/setup.py | 2 +- splunklib/__init__.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 945edae7d..c5d012aef 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ # The Splunk Software Development Kit for Python -#### Version 1.6.6 +#### Version 1.6.8 The Splunk Software Development Kit (SDK) for Python contains library code and examples designed to enable developers to build applications using Splunk. diff --git a/examples/searchcommands_app/setup.py b/examples/searchcommands_app/setup.py index b504dc607..27c7e1552 100755 --- a/examples/searchcommands_app/setup.py +++ b/examples/searchcommands_app/setup.py @@ -439,7 +439,7 @@ def run(self): setup( description='Custom Search Command examples', name=os.path.basename(project_dir), - version='1.6.7', + version='1.6.8', author='Splunk, Inc.', author_email='devinfo@splunk.com', url='http://github.com/splunk/splunk-sdk-python', diff --git a/splunklib/__init__.py b/splunklib/__init__.py index e0f7251df..cb25a2f42 100644 --- a/splunklib/__init__.py +++ b/splunklib/__init__.py @@ -16,5 +16,5 @@ from __future__ import absolute_import from splunklib.six.moves import map -__version_info__ = (1, 6, 7) +__version_info__ = (1, 6, 8) __version__ = ".".join(map(str, __version_info__))