Browse Source

Fix conditional on python version (#405)

Fixes #404
Filip Pytloun 7 years ago
parent
commit
831173c270
2 changed files with 11 additions and 5 deletions
  1. 9 0
      Tests/test_file_manager.py
  2. 2 5
      kalliope/core/Utils/FileManager.py

+ 9 - 0
Tests/test_file_manager.py

@@ -1,5 +1,6 @@
 import unittest
 import os
+import sys
 
 from kalliope.core.Utils.FileManager import FileManager
 
@@ -55,6 +56,14 @@ class TestFileManager(unittest.TestCase):
             self.assertEqual(content, in_file_text,
                              "Fail writing in the file ")
 
+        if sys.version_info[0] > 2:
+            # Test writing of bytes object for python3
+            FileManager.write_in_file(file_path=file_path, content=bytes(in_file_text, 'utf-8'))
+            with open(file_path, 'r') as content_file:
+                content = content_file.read()
+                self.assertEqual(content, in_file_text,
+                                "Fail writing in the file ")
+
         # Clean up
         if os.path.exists(file_path):
             os.remove(file_path)

+ 2 - 5
kalliope/core/Utils/FileManager.py

@@ -1,8 +1,6 @@
 import logging
 import os
 
-import sys
-
 logging.basicConfig()
 logger = logging.getLogger("kalliope")
 
@@ -31,17 +29,16 @@ class FileManager:
         :param file_path: the path of the file to write on
         :type file_path: str
         :param content: the contents to write in the file
-        :type content: str
+        :type content: str or bytes
 
         .. raises:: IOError
         """
         try:
             with open(file_path, "wb") as file_open:
-                if sys.version_info[0] == 2:
+                if type(content) == bytes:
                     file_open.write(content)
                 else:
                     file_open.write(content.encode())
-                file_open.close()
             return not FileManager.file_is_empty(file_path)
         except IOError as e:
             logger.error("I/O error(%s): %s", e.errno, e.strerror)