Skip to content
This repository was archived by the owner on Jul 10, 2021. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions gntp/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,18 @@
GNTP_EOL = gntp.shim.b('\r\n')
GNTP_SEP = gntp.shim.b(': ')

def is_string(var):
try:
return isinstance(var, basestring) # Python 2
except NameError:
return isinstance(var, str) # Python 3

def convert_to_string(var):
try:
return unicode(var) # Python 2
except NameError:
return str(var) # Python 3


class _GNTPBuffer(gntp.shim.StringIO):
"""GNTP Buffer class"""
Expand All @@ -44,8 +56,8 @@ def writeln(self, value=None):
self.write(GNTP_EOL)

def writeheader(self, key, value):
if not isinstance(value, str):
value = str(value)
if not is_string(value):
value = convert_to_string(value)
self.write(gntp.shim.b(key))
self.write(GNTP_SEP)
self.write(gntp.shim.b(value))
Expand Down