Message ID | 20180419093321.2838-1-jkbs@redhat.com |
---|---|
State | Accepted |
Headers | show |
Series | [ovs-dev,v2] python: Tighten the check if we need encoding | expand |
On Thu, Apr 19, 2018 at 11:33:21AM +0200, Jakub Sitnicki wrote: > Check if we are dealing with a Unicode string that needs > encoding for both Python 2 & 3. > > Also, do the encoding the same way for Python 2 & 3 and avoid using > negation to make the code simpler. > > v1 -> v2: Rewrite the check for Unicode string to avoid flake8 warnings. > > Signed-off-by: Jakub Sitnicki <jkbs@redhat.com> > --- > > Ben, > > Thanks for pointing out the flake8 warnings. It was poorly done. > > This version should read much better, and be free of warnings. Thanks, I applied this version to master.
diff --git a/python/ovs/stream.py b/python/ovs/stream.py index 7b15bcd7d..d6c447a97 100644 --- a/python/ovs/stream.py +++ b/python/ovs/stream.py @@ -383,11 +383,8 @@ class Stream(object): elif len(buf) == 0: return 0 - # Python 3 has separate types for strings and bytes. We must have - # bytes here. - if six.PY3 and not isinstance(buf, bytes): - buf = bytes(buf, 'utf-8') - elif six.PY2: + # We must have bytes for sending. + if isinstance(buf, six.text_type): buf = buf.encode('utf-8') if sys.platform == 'win32' and self.socket is None:
Check if we are dealing with a Unicode string that needs encoding for both Python 2 & 3. Also, do the encoding the same way for Python 2 & 3 and avoid using negation to make the code simpler. v1 -> v2: Rewrite the check for Unicode string to avoid flake8 warnings. Signed-off-by: Jakub Sitnicki <jkbs@redhat.com> --- Ben, Thanks for pointing out the flake8 warnings. It was poorly done. This version should read much better, and be free of warnings. -Jakub python/ovs/stream.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) -- 2.14.3