summaryrefslogtreecommitdiffstats
path: root/geo-replication/syncdaemon/py2py3.py
diff options
context:
space:
mode:
authorKotresh HR <khiremat@redhat.com>2018-10-03 00:45:09 -0400
committerAmar Tumballi <amarts@redhat.com>2018-10-08 16:36:04 +0000
commitfb6e8d0d0ca21b16d331fa69da9b9dadf6c5c35d (patch)
treedcff4d7a563b034f0f91347bf9fc4f1814143724 /geo-replication/syncdaemon/py2py3.py
parent860a990811c5364bc2de04ca07096cde312ff209 (diff)
georep: python2 to python3 compat - syscalls
1. ctypes/syscalls A) arguments is expected to be encoded B) Raw conversion of return value from bytearray into string 2. struct pack/unpack - Raw converstion of string to bytearray 3. basestring -> str Updates: #411 Change-Id: I80f939adcdec0ed0022c87c0b76d057ad5559e5a Signed-off-by: Kotresh HR <khiremat@redhat.com>
Diffstat (limited to 'geo-replication/syncdaemon/py2py3.py')
-rw-r--r--geo-replication/syncdaemon/py2py3.py49
1 files changed, 49 insertions, 0 deletions
diff --git a/geo-replication/syncdaemon/py2py3.py b/geo-replication/syncdaemon/py2py3.py
new file mode 100644
index 00000000000..4c0e1152aa8
--- /dev/null
+++ b/geo-replication/syncdaemon/py2py3.py
@@ -0,0 +1,49 @@
+#
+# Copyright (c) 2018 Red Hat, Inc. <http://www.redhat.com>
+# This file is part of GlusterFS.
+
+# This file is licensed to you under your choice of the GNU Lesser
+# General Public License, version 3 or any later version (LGPLv3 or
+# later), or the GNU General Public License, version 2 (GPLv2), in all
+# cases as published by the Free Software Foundation.
+#
+
+# All python2/python3 compatibility routines
+
+import sys
+import os
+
+
+if sys.version_info >= (3,):
+ def pipe():
+ (r, w) = os.pipe()
+ os.set_inheritable(r, True)
+ os.set_inheritable(w, True)
+ return (r, w)
+
+ # Raw conversion of bytearray to string. Used in the cases where
+ # buffer is created by create_string_buffer which is a 8-bit char
+ # array and passed to syscalls to fetch results. Using encode/decode
+ # doesn't work as it converts to string altering the size.
+ def bytearray_to_str(byte_arr):
+ return ''.join([chr(b) for b in byte_arr])
+
+ # Raw conversion of string to bytes. This is required to convert
+ # back the string into bytearray(c char array) to use in struc
+ # pack/unpacking. Again encode/decode can't be used as it
+ # converts it alters size.
+ def str_to_bytearray(string):
+ return bytes([ord(c) for c in string])
+
+else:
+ def pipe():
+ (r, w) = os.pipe()
+ return (r, w)
+
+ # Raw conversion of bytearray to string
+ def bytearray_to_str(byte_arr):
+ return ''.join([b for b in byte_arr])
+
+ # Raw conversion of string to bytearray
+ def str_to_bytearray(string):
+ return b"".join([c for c in string])