_isstring(): Factor out "stringiness" test, e.g. for StringType or

UnicodeType, which is different between Python 2.1 and 2.2.
This commit is contained in:
Barry Warsaw 2002-09-10 16:09:06 +00:00
parent 45d9bde6c1
commit 356afac41f
2 changed files with 10 additions and 2 deletions

View File

@ -30,6 +30,10 @@ def _floordiv(i, j):
return i / j return i / j
def _isstring(obj):
return isinstance(obj, StringType) or isinstance(obj, UnicodeType)
# These two functions are imported into the Iterators.py interface module. # These two functions are imported into the Iterators.py interface module.
# The Python 2.2 version uses generators for efficiency. # The Python 2.2 version uses generators for efficiency.
@ -38,7 +42,7 @@ def body_line_iterator(msg):
lines = [] lines = []
for subpart in msg.walk(): for subpart in msg.walk():
payload = subpart.get_payload() payload = subpart.get_payload()
if isinstance(payload, StringType) or isinstance(payload, UnicodeType): if _isstring(payload):
for line in StringIO(payload).readlines(): for line in StringIO(payload).readlines():
lines.append(line) lines.append(line)
return lines return lines

View File

@ -31,6 +31,10 @@ def _floordiv(i, j):
return i // j return i // j
def _isstring(obj):
return isinstance(obj, StringTypes)
# These two functions are imported into the Iterators.py interface module. # These two functions are imported into the Iterators.py interface module.
# The Python 2.2 version uses generators for efficiency. # The Python 2.2 version uses generators for efficiency.
@ -38,7 +42,7 @@ def body_line_iterator(msg):
"""Iterate over the parts, returning string payloads line-by-line.""" """Iterate over the parts, returning string payloads line-by-line."""
for subpart in msg.walk(): for subpart in msg.walk():
payload = subpart.get_payload() payload = subpart.get_payload()
if isinstance(payload, StringTypes): if _isstring(payload):
for line in StringIO(payload): for line in StringIO(payload):
yield line yield line