You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ph...@apache.org on 2013/10/08 08:44:54 UTC

svn commit: r1530166 - in /zookeeper/trunk: ./ src/contrib/zkpython/src/c/ src/contrib/zkpython/src/python/ src/contrib/zkpython/src/test/

Author: phunt
Date: Tue Oct  8 06:44:54 2013
New Revision: 1530166

URL: http://svn.apache.org/r1530166
Log:
ZOOKEEPER-877. zkpython does not work with python3.1 (Daniel Enman via phunt)

Modified:
    zookeeper/trunk/CHANGES.txt
    zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c
    zookeeper/trunk/src/contrib/zkpython/src/python/zk.py
    zookeeper/trunk/src/contrib/zkpython/src/test/connection_test.py
    zookeeper/trunk/src/contrib/zkpython/src/test/get_set_test.py
    zookeeper/trunk/src/contrib/zkpython/src/test/zktestbase.py

Modified: zookeeper/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/zookeeper/trunk/CHANGES.txt?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/CHANGES.txt (original)
+++ zookeeper/trunk/CHANGES.txt Tue Oct  8 06:44:54 2013
@@ -435,6 +435,9 @@ BUGFIXES:
   ZOOKEEPER-1774. QuorumPeerMainTest fails consistently with
   "complains about host" assertion failure (phunt)
 
+  ZOOKEEPER-877. zkpython does not work with python3.1
+  (Daniel Enman via phunt)
+
 IMPROVEMENTS:
 
   ZOOKEEPER-1170. Fix compiler (eclipse) warnings: unused imports,

Modified: zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c (original)
+++ zookeeper/trunk/src/contrib/zkpython/src/c/zookeeper.c Tue Oct  8 06:44:54 2013
@@ -275,7 +275,11 @@ PyObject *build_string_vector(const stru
   if (ret) {
     int i;
     for (i=0;i<sv->count;++i)  {
+#if PY_MAJOR_VERSION >= 3
+      PyObject *s = PyUnicode_FromString(sv->data[i]);
+#else
       PyObject *s = PyString_FromString(sv->data[i]);
+#endif
       if (!s) {
         if (ret != Py_None) {
           Py_DECREF(ret);
@@ -381,9 +385,15 @@ int parse_acls(struct ACL_vector *acls, 
     a = PyList_GetItem(pyacls, i);
     // a is now a dictionary
     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" ) ) );
+#else
     acls->data[i].perms = (int32_t)(PyInt_AsLong(perms));
     acls->data[i].id.id = strdup( PyString_AsString( PyDict_GetItemString( a, "id" ) ) );
     acls->data[i].id.scheme = strdup( PyString_AsString( PyDict_GetItemString( a, "scheme" ) ) );
+#endif
   }
   return 1;
 }
@@ -1432,7 +1442,14 @@ PyObject *pyzoo_set_log_stream(PyObject 
     PyErr_SetString(PyExc_ValueError, "Must supply a Python object to set_log_stream");
     return NULL;
   }
-  if (!PyFile_Check(pystream)) {
+  
+#if PY_MAJOR_VERSION >= 3
+  extern PyTypeObject PyIOBase_Type;
+  if (!PyObject_IsInstance(pystream, (PyObject *)&PyIOBase_Type)) {
+#else
+  if(!PyFile_Check(pystream)) {
+#endif
+
     PyErr_SetString(PyExc_ValueError, "Must supply a file object to set_log_stream");
     return NULL;
   }
@@ -1443,7 +1460,14 @@ PyObject *pyzoo_set_log_stream(PyObject 
 
   log_stream = pystream;
   Py_INCREF(log_stream);
-  zoo_set_log_stream(PyFile_AsFile(log_stream));
+
+#if PY_MAJOR_VERSION >= 3
+  int fd = PyObject_AsFileDescriptor(log_stream);
+  FILE *fp = fdopen(fd, "w");
+#else 
+  FILE *fp = PyFile_AsFile(log_stream);
+#endif
+  zoo_set_log_stream(fp);
 
   Py_INCREF(Py_None);
   return Py_None;
@@ -1505,6 +1529,20 @@ static PyMethodDef ZooKeeperMethods[] = 
   {NULL, NULL}
 };
 
+#if PY_MAJOR_VERSION >= 3 
+static struct PyModuleDef zookeeper_moddef = {
+  PyModuleDef_HEAD_INIT,
+  "zookeeper",
+  NULL,
+  0,
+  ZooKeeperMethods,
+  0,
+  0,
+  0,
+  0
+};
+#endif
+
 #define ADD_INTCONSTANT(x) PyModule_AddIntConstant(module, #x, ZOO_##x)
 #define ADD_INTCONSTANTZ(x) PyModule_AddIntConstant(module, #x, Z##x)
 
@@ -1512,10 +1550,18 @@ static PyMethodDef ZooKeeperMethods[] = 
   Py_INCREF(x);                                                         \
   PyModule_AddObject(module, #x, x);
 
-
+#if PY_MAJOR_VERSION >= 3
+PyMODINIT_FUNC PyInit_zookeeper(void) {
+#else
 PyMODINIT_FUNC initzookeeper(void) {
+#endif
   PyEval_InitThreads();
-  PyObject *module = Py_InitModule("zookeeper", ZooKeeperMethods );
+
+#if PY_MAJOR_VERSION >= 3
+  PyObject *module = PyModule_Create(&zookeeper_moddef);
+#else
+  PyObject *module = Py_InitModule("zookeeper", ZooKeeperMethods);
+#endif
   if (init_zhandles(32) == 0) {
     return; // TODO: Is there any way to raise an exception here?
   }
@@ -1611,4 +1657,8 @@ PyMODINIT_FUNC initzookeeper(void) {
   ADD_EXCEPTION(ClosingException);
   ADD_EXCEPTION(NothingException);
   ADD_EXCEPTION(SessionMovedException);
+
+#if PY_MAJOR_VERSION >= 3
+  return module;
+#endif
 }

Modified: zookeeper/trunk/src/contrib/zkpython/src/python/zk.py
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zkpython/src/python/zk.py?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/src/contrib/zkpython/src/python/zk.py (original)
+++ zookeeper/trunk/src/contrib/zkpython/src/python/zk.py Tue Oct  8 06:44:54 2013
@@ -24,22 +24,22 @@ conn_cv = threading.Condition( )
 
 def my_connection_watcher(handle,type,state,path):
     global connected, conn_cv
-    print "Connected, handle is ", handle
+    print("Connected, handle is ", handle)
     conn_cv.acquire()
     connected = True
     conn_cv.notifyAll()
     conn_cv.release()
     
 conn_cv.acquire()
-print "Connecting to localhost:2181 -- "
+print("Connecting to localhost:2181 -- ")
 handle = zookeeper.init("localhost:2181", my_connection_watcher, 10000, 0)
 while not connected:
     conn_cv.wait()
 conn_cv.release()
 
 def my_getc_watch( handle, type, state, path ):
-    print "Watch fired -- "
-    print type, state, path
+    print("Watch fired -- ")
+    print(type, state, path)
 
 ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"};
 
@@ -47,12 +47,12 @@ try:
     zookeeper.create(handle, "/zk-python", "data", [ZOO_OPEN_ACL_UNSAFE], 0)
     zookeeper.get_children(handle, "/zk-python", my_getc_watch)
     for i in xrange(5):
-        print "Creating sequence node ", i, " ", zookeeper.create(handle, "/zk-python/sequencenode", "data", [ZOO_OPEN_ACL_UNSAFE], zookeeper.SEQUENCE )
+        print("Creating sequence node ", i, " ", zookeeper.create(handle, "/zk-python/sequencenode", "data", [ZOO_OPEN_ACL_UNSAFE], zookeeper.SEQUENCE ))
 except:
     pass
 
 def pp_zk(handle,root, indent = 0):
-    """Pretty print a zookeeper tree, starting at root"""
+    """Pretty print(a zookeeper tree, starting at root""")
     def make_path(child):
         if root == "/":
             return "/" + child
@@ -62,15 +62,15 @@ def pp_zk(handle,root, indent = 0):
     for i in xrange(indent):
         out += "\t"
     out += "|---"+root + " :: " + zookeeper.get(handle, root, None)[0]
-    print out
+    print(out)
     for child in children:
         pp_zk(handle,make_path(child),indent+1)
 
-print "ZNode tree -- "
+print("ZNode tree -- ")
 pp_zk(handle,"/")
 
-print "Getting ACL / Stat for /zk-python --"
+print("Getting ACL / Stat for /zk-python --")
 (stat, acl) =  zookeeper.get_acl(handle, "/zk-python")
-print "Stat:: ", stat
-print "Acl:: ", acl
+print("Stat:: ", stat)
+print("Acl:: ", acl)
 

Modified: zookeeper/trunk/src/contrib/zkpython/src/test/connection_test.py
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zkpython/src/test/connection_test.py?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/src/contrib/zkpython/src/test/connection_test.py (original)
+++ zookeeper/trunk/src/contrib/zkpython/src/test/connection_test.py Tue Oct  8 06:44:54 2013
@@ -16,7 +16,9 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import unittest, threading, re
+import unittest, threading, re, sys
+if sys.version_info < (3,):
+	range = xrange
 
 import zookeeper, zktestbase
 ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}
@@ -71,7 +73,7 @@ class ConnectionTest(zktestbase.TestBase
             cv.release()
 
         cv.acquire()
-        handles = [ zookeeper.init(self.host) for i in xrange(10) ]
+        handles = [ zookeeper.init(self.host) for i in range(10) ]
         ret = zookeeper.init(self.host, connection_watcher)
         cv.wait(15.0)
         cv.release()
@@ -93,7 +95,7 @@ class ConnectionTest(zktestbase.TestBase
         """
         # We'd like to do more, but currently the C client doesn't
         # work with > 83 handles (fails to create a pipe) on MacOS 10.5.8
-        handles = [ zookeeper.init(self.host) for i in xrange(63) ]
+        handles = [ zookeeper.init(self.host) for i in range(9) ]
 
         cv = threading.Condition()
         self.connected = False

Modified: zookeeper/trunk/src/contrib/zkpython/src/test/get_set_test.py
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zkpython/src/test/get_set_test.py?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/src/contrib/zkpython/src/test/get_set_test.py (original)
+++ zookeeper/trunk/src/contrib/zkpython/src/test/get_set_test.py Tue Oct  8 06:44:54 2013
@@ -16,7 +16,10 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-import zookeeper, zktestbase, unittest, threading
+import zookeeper, zktestbase, unittest, threading, sys
+if sys.version_info < (3,):
+	range = xrange
+
 ZOO_OPEN_ACL_UNSAFE = {"perms":0x1f, "scheme":"world", "id" :"anyone"}
 
 class GetSetTest(zktestbase.TestBase):
@@ -84,7 +87,7 @@ class GetSetTest(zktestbase.TestBase):
         1Mb with default parameters (depends on ZooKeeper server).
         """
 
-        data = ''.join(["A" for x in xrange(1024*1023)])
+        data = ''.join(["A" for x in range(1024*1023)])
         self.ensureDeleted("/zk-python-test-large-datanode")
         zookeeper.create(self.handle, "/zk-python-test-large-datanode", data,
                          [{"perms":0x1f, "scheme":"world", "id" :"anyone"}])

Modified: zookeeper/trunk/src/contrib/zkpython/src/test/zktestbase.py
URL: http://svn.apache.org/viewvc/zookeeper/trunk/src/contrib/zkpython/src/test/zktestbase.py?rev=1530166&r1=1530165&r2=1530166&view=diff
==============================================================================
--- zookeeper/trunk/src/contrib/zkpython/src/test/zktestbase.py (original)
+++ zookeeper/trunk/src/contrib/zkpython/src/test/zktestbase.py Tue Oct  8 06:44:54 2013
@@ -34,7 +34,7 @@ class TestBase(unittest.TestCase):
             f = open(logfile,"w")
             zookeeper.set_log_stream(f)
         except IOError:
-            print "Couldn't open " + logfile + " for writing"
+            print("Couldn't open " + logfile + " for writing")
 
 
     def setUp(self):