瀏覽代碼

ZOOKEEPER-3105: Character coding problem occur when create a node using python3

when creating a node using python3, InvalidACLException occurs all the time. it`s caused by imcompatible way of parsing acl passed through python3 api.
so

```
acls->data[i].id.id = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "id" ) ) );
acls->data[i].id.scheme = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "scheme" ) ) );
```
is changed to

```
acls->data[i].id.id = strdup( PyBytes_AS_STRING( PyUnicode_AsASCIIString( PyDict_GetItemString( a, "id" ) ) ) );
acls->data[i].id.scheme = strdup( PyBytes_AS_STRING( PyUnicode_AsASCIIString( PyDict_GetItemString( a, "scheme" ) ) ) );
```

because `acls->data[i].id.id` and `acls->data[i].id.scheme` must be an ASCII string.

Author: lordofkey <yyyhhh125@163.com>
Author: yanghao <yyyhhh125@163.com>

Reviewers: phunt@apache.org

Closes #586 from lordofkey/ZOOKEEPER-3105 and squashes the following commits:

24a60d982 [lordofkey] Update zookeeper.c
9f2fd54ca [lordofkey] ZOOKEEPER-3105:Character coding problem occur when create a node usin… …
519a7805f [lordofkey] Merge remote-tracking branch 'shared/master' into HEAD
5a441ed60 [yanghao] ZOOKEEPER-3105:Character coding problem occur when create a node using python3

Change-Id: I72c104edc4c77159f75db1211fcd62b33c7d33d6
lordofkey 5 年之前
父節點
當前提交
52dcf72b0a
共有 1 個文件被更改,包括 7 次插入2 次删除
  1. 7 2
      zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c

+ 7 - 2
zookeeper-contrib/zookeeper-contrib-zkpython/src/c/zookeeper.c

@@ -387,8 +387,13 @@ int parse_acls(struct ACL_vector *acls, PyObject *pyacls)
     PyObject *perms = PyDict_GetItemString( a, "perms" );
 #if PY_MAJOR_VERSION >= 3
     acls->data[i].perms = (int32_t)(PyLong_AsLong(perms));
-    acls->data[i].id.id = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "id" ) ) );
-    acls->data[i].id.scheme = strdup( PyUnicode_AsUnicode( PyDict_GetItemString( a, "scheme" ) ) );
+    PyObject *tem_utfstring;
+    tem_utfstring = PyUnicode_AsEncodedString(PyDict_GetItemString( a, "id" ), "utf-8", NULL );
+    acls->data[i].id.id = strdup( PyBytes_AS_STRING(tem_utfstring));
+    Py_DECREF(tem_utfstring); 
+    tem_utfstring = PyUnicode_AsEncodedString(PyDict_GetItemString( a, "scheme" ), "utf-8", NULL );
+    acls->data[i].id.scheme = strdup( PyBytes_AS_STRING(tem_utfstring) );
+    Py_DECREF(tem_utfstring);
 #else
     acls->data[i].perms = (int32_t)(PyInt_AsLong(perms));
     acls->data[i].id.id = strdup( PyString_AsString( PyDict_GetItemString( a, "id" ) ) );