From patchwork Mon Feb 20 11:38:25 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guoshuai Li X-Patchwork-Id: 729933 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from mail.linuxfoundation.org (mail.linuxfoundation.org [140.211.169.12]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3vRhV13G1nz9s7s for ; Mon, 20 Feb 2017 22:38:49 +1100 (AEDT) Received: from mail.linux-foundation.org (localhost [127.0.0.1]) by mail.linuxfoundation.org (Postfix) with ESMTP id 784FE93E; Mon, 20 Feb 2017 11:38:45 +0000 (UTC) X-Original-To: ovs-dev@openvswitch.org Delivered-To: ovs-dev@mail.linuxfoundation.org Received: from smtp1.linuxfoundation.org (smtp1.linux-foundation.org [172.17.192.35]) by mail.linuxfoundation.org (Postfix) with ESMTPS id 13CD18D7 for ; Mon, 20 Feb 2017 11:38:44 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from smtp2203-239.mail.aliyun.com (smtp2203-239.mail.aliyun.com [121.197.203.239]) by smtp1.linuxfoundation.org (Postfix) with ESMTP id 0A163E4 for ; Mon, 20 Feb 2017 11:38:41 +0000 (UTC) X-Alimail-AntiSpam: AC=CONTINUE; BC=0.07445408|-1; FP=0|0|0|0|0|-1|-1|-1; HT=e02c03292; MF=ligs@dtdream.com; NM=1; PH=DS; RN=2; RT=2; SC=75; SR=0; TI=SMTPD_---.7fdYNrf_1487590715; Received: from localhost.localdomain(mailfrom:ligs@dtdream.com ip:111.198.29.132) by smtp.aliyun-inc.com(10.147.41.138); Mon, 20 Feb 2017 19:38:36 +0800 From: Guoshuai Li To: ovs-dev@openvswitch.org Date: Mon, 20 Feb 2017 19:38:25 +0800 Message-Id: <20170220113825.3464-1-ligs@dtdream.com> X-Mailer: git-send-email 2.10.1.windows.1 X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on smtp1.linux-foundation.org Subject: [ovs-dev] [PATCH] python: Fix Chinese characters UnicodeEncodeError exception X-BeenThere: ovs-dev@openvswitch.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: ovs-dev-bounces@openvswitch.org Errors-To: ovs-dev-bounces@openvswitch.org The OVSDB client may send or recv Chinese characters. So we should use UTF-8 encoding data for send or recv by default. Signed-off-by: Guoshuai Li --- python/ovs/jsonrpc.py | 9 ++++----- python/ovs/stream.py | 8 ++++---- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/python/ovs/jsonrpc.py b/python/ovs/jsonrpc.py index 5a11500..5db6080 100644 --- a/python/ovs/jsonrpc.py +++ b/python/ovs/jsonrpc.py @@ -265,11 +265,10 @@ class Connection(object): while True: if not self.input: error, data = self.stream.recv(4096) - # Python 3 has separate types for strings and bytes. We - # received bytes from a socket. We expect it to be string - # data, so we convert it here as soon as possible. - if (data and not error - and not isinstance(data, six.string_types)): + # In Python 3, We received bytes from socket, and convert it + # to utf8 string by decode. In Python 2, We received ascii + # string from socket default, and convert it to utf8 by decode. + if data and not error: try: data = data.decode('utf-8') except UnicodeError: diff --git a/python/ovs/stream.py b/python/ovs/stream.py index be69534..d72dddf 100644 --- a/python/ovs/stream.py +++ b/python/ovs/stream.py @@ -384,10 +384,10 @@ class Stream(object): return self.__send_windows(buf) try: - # Python 3 has separate types for strings and bytes. We must have - # bytes here. - if six.PY3 and not isinstance(buf, six.binary_type): - buf = six.binary_type(buf, 'utf-8') + # The buf type is usually string, we convert it to bytes by encode + # in python 3. and convert it to utf-8 string by encode in Python 2. + if isinstance(buf, six.text_type): + buf = buf.encode('utf-8') return self.socket.send(buf) except socket.error as e: return -ovs.socket_util.get_exception_errno(e)