From 16401d5be8fba35599caedb4c78901d4b66ac082 Mon Sep 17 00:00:00 2001 From: Manish Kumar Date: Fri, 26 Aug 2016 12:52:51 +0530 Subject: [PATCH] fixed : Divide by zero error came when string was empty, also a test --- netlib/strutils.py | 3 +++ test/netlib/test_strutils.py | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/netlib/strutils.py b/netlib/strutils.py index 4a46b6b1f..4cb3b8056 100644 --- a/netlib/strutils.py +++ b/netlib/strutils.py @@ -121,6 +121,9 @@ def escaped_str_to_bytes(data): def is_mostly_bin(s): # type: (bytes) -> bool + if not s or len(s) == 0: + return False + return sum( i < 9 or 13 < i < 32 or 126 < i for i in six.iterbytes(s[:100]) diff --git a/test/netlib/test_strutils.py b/test/netlib/test_strutils.py index 52299e599..f8dc3adae 100644 --- a/test/netlib/test_strutils.py +++ b/test/netlib/test_strutils.py @@ -85,7 +85,7 @@ def test_escaped_str_to_bytes(): def test_is_mostly_bin(): assert not strutils.is_mostly_bin(b"foo\xFF") assert strutils.is_mostly_bin(b"foo" + b"\xFF" * 10) - + assert not strutils.is_mostly_bin("") def test_is_xml(): assert not strutils.is_xml(b"foo")