URL: https://github.com/freeipa/freeipa/pull/340
Author: dkupka
 Title: #340: schema_cache: Make handling of string compatible with python3
Action: synchronized

To pull the PR as Git branch:
git remote add ghfreeipa https://github.com/freeipa/freeipa
git fetch ghfreeipa pull/340/head:pr340
git checkout pr340
From 2f4982ed1255ce56fb56cf23671d85ce0b28cc6b Mon Sep 17 00:00:00 2001
From: David Kupka <dku...@redhat.com>
Date: Wed, 14 Dec 2016 17:19:52 +0100
Subject: [PATCH] schema_cache: Make handling of string compatible with python3

https://fedorahosted.org/freeipa/ticket/6559
---
 ipaclient/remote_plugins/schema.py | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/ipaclient/remote_plugins/schema.py b/ipaclient/remote_plugins/schema.py
index 02364ca..15c03f4 100644
--- a/ipaclient/remote_plugins/schema.py
+++ b/ipaclient/remote_plugins/schema.py
@@ -6,6 +6,7 @@
 import contextlib
 import errno
 import fcntl
+import io
 import json
 import os
 import sys
@@ -373,7 +374,7 @@ def __init__(self, client, fingerprint=None):
         self._dict = {}
         self._namespaces = {}
         self._help = None
-        self._file = six.StringIO()
+        self._file = six.BytesIO()
 
         for ns in self.namespaces:
             self._dict[ns] = {}
@@ -407,7 +408,7 @@ def __init__(self, client, fingerprint=None):
     def _open(self, filename, mode):
         path = os.path.join(self._DIR, filename)
 
-        with open(path, mode) as f:
+        with io.open(path, mode) as f:
             if mode.startswith('r'):
                 fcntl.flock(f, fcntl.LOCK_SH)
             else:
@@ -454,7 +455,7 @@ def _fetch(self, client, ignore_cache=False):
 
     def _read_schema(self, fingerprint):
         self._file.truncate(0)
-        with self._open(fingerprint, 'r') as f:
+        with self._open(fingerprint, 'rb') as f:
             self._file.write(f.read())
 
         with zipfile.ZipFile(self._file, 'r') as schema:
@@ -504,21 +505,24 @@ def _write_schema(self, fingerprint):
                     ns = value
                     for member in ns:
                         path = '{}/{}'.format(key, member)
-                        schema.writestr(path, json.dumps(ns[member]))
+                        schema.writestr(path,
+                                        json.dumps(ns[member]).encode('utf-8'))
                 else:
-                    schema.writestr(key, json.dumps(value))
+                    schema.writestr(key, json.dumps(value).encode('utf-8'))
 
-            schema.writestr('_help',
-                            json.dumps(self._generate_help(self._dict)))
+            schema.writestr(
+                '_help',
+                json.dumps(self._generate_help(self._dict)).encode('utf-8')
+            )
 
         self._file.seek(0)
-        with self._open(fingerprint, 'w') as f:
+        with self._open(fingerprint, 'wb') as f:
             f.truncate(0)
             f.write(self._file.read())
 
     def _read(self, path):
         with zipfile.ZipFile(self._file, 'r') as zf:
-            return json.loads(zf.read(path))
+            return json.loads(zf.read(path).decode('utf-8'))
 
     def read_namespace_member(self, namespace, member):
         value = self._dict[namespace][member]
-- 
Manage your subscription for the Freeipa-devel mailing list:
https://www.redhat.com/mailman/listinfo/freeipa-devel
Contribute to FreeIPA: http://www.freeipa.org/page/Contribute/Code

Reply via email to