Skip to content

Commit b135f90

Browse files
committed
For python3 support, change default stdin to be buffered,
and adjust end-of-line terminator to be \n instead of \r\n
1 parent 7401b61 commit b135f90

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

splunklib/searchcommands/internals.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939

4040
csv.field_size_limit(10485760) # The default value is 128KB; upping to 10MB. See SPL-12117 for background on this issue
4141

42-
if sys.platform == 'win32':
42+
# SPL-175233 -- python3 stdout is already binary
43+
if sys.platform == 'win32' and sys.version_info <= (3, 0):
4344
# Work around the fact that on Windows '\n' is mapped to '\r\n'. The typical solution is to simply open files in
4445
# binary mode, but stdout is already open, thus this hack. 'CPython' and 'PyPy' work differently. We assume that
4546
# 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):
339340
doublequote = True
340341
skipinitialspace = False
341342
lineterminator = '\r\n'
343+
if sys.version_info >= (3, 0) and sys.platform == 'win32':
344+
lineterminator = '\n'
342345
quoting = csv.QUOTE_MINIMAL
343346

344347

@@ -361,6 +364,10 @@ def read(self, ifile):
361364
name, value = None, None
362365

363366
for line in ifile:
367+
# SPL-175233 -- input is buffered, needs to be decoded
368+
if sys.version_info >= (3, 0):
369+
line = line.decode()
370+
364371
if line == '\n':
365372
break
366373
item = line.split(':', 1)
@@ -658,6 +665,13 @@ class RecordWriterV1(RecordWriter):
658665

659666
def flush(self, finished=None, partial=None):
660667

668+
# SPL-175233
669+
def writeEOL():
670+
if sys.version_info >= (3, 0) and sys.platform == 'win32':
671+
write('\n')
672+
else:
673+
write('\r\n')
674+
661675
RecordWriter.flush(self, finished, partial) # validates arguments and the state of this instance
662676

663677
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):
678692
write(message_level(level, level))
679693
write('=')
680694
write(text)
681-
write('\r\n')
695+
writeEOL()
682696

683-
write('\r\n')
697+
writeEOL()
684698

685699
elif messages is not None:
686700

splunklib/searchcommands/search_command.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1054,7 +1054,13 @@ def iteritems(self):
10541054
SearchMetric = namedtuple('SearchMetric', ('elapsed_seconds', 'invocation_count', 'input_count', 'output_count'))
10551055

10561056

1057-
def dispatch(command_class, argv=sys.argv, input_file=sys.stdin, output_file=sys.stdout, module_name=None):
1057+
# SPL-175233, set default stdin to be buffered
1058+
if sys.version_info >= (3, 0) and sys.platform == 'win32':
1059+
stdinput = sys.stdin.buffer
1060+
else:
1061+
stdinput = sys.stdin
1062+
1063+
def dispatch(command_class, argv=sys.argv, input_file=stdinput, output_file=sys.stdout, module_name=None):
10581064
""" Instantiates and executes a search command class
10591065
10601066
This function implements a `conditional script stanza <https://docs.python.org/2/library/__main__.html>`_ based on the value of

0 commit comments

Comments
 (0)