Repository: pymedusa/Medusa

Issue:
TypeError: LoadLibrary() argument 1 must be string, not unicode
@ratoaq2 
```
Connected to pydev debugger (build 163.9735.8)
Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm 2016.3\helpers\pydev\pydevd.py", line 1596, in <module>
    globals = debugger.run(setup['file'], None, None, is_module)
  File "C:\Program Files\JetBrains\PyCharm 2016.3\helpers\pydev\pydevd.py", line 974, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "C:/Users/Fernando/Documents/Medusa/start.py", line 4, in <module>
    from medusa.__main__ import main
  File "C:/Users/Fernando/Documents/Medusa\medusa\__main__.py", line 67, in <module>
    from . import (
  File "C:/Users/Fernando/Documents/Medusa\medusa\db.py", line 28, in <module>
    from . import app, logger
  File "C:/Users/Fernando/Documents/Medusa\medusa\logger.py", line 34, in <module>
    import knowit
  File "C:\Users\Fernando\Documents\Medusa\lib\knowit\__init__.py", line 26, in <module>
    from .api import know
  File "C:\Users\Fernando\Documents\Medusa\lib\knowit\api.py", line 10, in <module>
    ('mediainfo', MediaInfoProvider()),
  File "C:\Users\Fernando\Documents\Medusa\lib\knowit\providers\mediainfo.py", line 92, in __init__
    self.native_lib = self._create_native_lib()
  File "C:\Users\Fernando\Documents\Medusa\lib\knowit\providers\mediainfo.py", line 125, in _create_native_lib
    lib = windll.MediaInfo = windll.LoadLibrary(os.path.join(lib, 'MediaInfo.dll'))
  File "C:\Python27\lib\ctypes\__init__.py", line 440, in LoadLibrary
    return self._dlltype(name)
  File "C:\Python27\lib\ctypes\__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
TypeError: LoadLibrary() argument 1 must be string, not unicode
```


Discussion:
i can't debug anymore with pycharm. Can't code anymore lol
@ratoaq2 weird that his happened only now. maybe because of updated 2.7.13 python?

patching mediainfo in knowit lib works:

added str() to os.path.join

\Medusa\lib\knowit\providers\mediainfo.py
```python
logger.debug('Loading native mediainfo library from %s', lib)
lib = windll.MediaInfo = windll.LoadLibrary(str(os.path.join(lib, 'MediaInfo.dll')))
```

then unrar had the same issue:

\Medusa\lib\unrar2\windows.py
```python
unrar = ctypes.WinDLL(str(os.path.join(os.path.split(__file__)[0], 'UnRARDLL', dll_name)))
```

http://bugs.python.org/issue29082

Resolving patch:
diff --git a/lib/knowit/providers/mediainfo.py b/lib/knowit/providers/mediainfo.py
--- a/lib/knowit/providers/mediainfo.py
+++ b/lib/knowit/providers/mediainfo.py
@@ -122,7 +122,11 @@ def _create_native_lib():
                     arch = 'x86_64' if is_64bits else 'i386'
                     lib = os.path.join(os_folder, arch)
                     logger.debug('Loading native mediainfo library from %s', lib)
-                    lib = windll.MediaInfo = windll.LoadLibrary(os.path.join(lib, 'MediaInfo.dll'))
+                    dll_filename = os.path.join(lib, 'MediaInfo.dll')
+                    if sys.version_info[:3] == (2, 7, 13):
+                        # http://bugs.python.org/issue29082
+                        dll_filename = str(dll_filename)
+                    lib = windll.MediaInfo = windll.LoadLibrary(dll_filename)
 
             lib.MediaInfo_Inform.restype = c_wchar_p
             lib.MediaInfo_New.argtypes = []
diff --git a/lib/unrar2/windows.py b/lib/unrar2/windows.py
--- a/lib/unrar2/windows.py
+++ b/lib/unrar2/windows.py
@@ -66,15 +66,24 @@
 dll_name = "unrar.dll"
 if architecture_bits == 64:
     dll_name = "x64\\unrar64.dll"
-    
-volume_naming1 = re.compile("\.r([0-9]{2})$")    
+
+volume_naming1 = re.compile("\.r([0-9]{2})$")
 volume_naming2 = re.compile("\.([0-9]{3}).rar$")
 volume_naming3 = re.compile("\.part([0-9]+).rar$")
-    
+
 try:
-    unrar = ctypes.WinDLL(os.path.join(os.path.split(__file__)[0], 'UnRARDLL', dll_name))
+    dll_filename = os.path.join(os.path.split(__file__)[0], 'UnRARDLL', dll_name)
+
+    if sys.version_info[:3] == (2, 7, 13):
+        # http://bugs.python.org/issue29082
+        dll_filename = str(dll_filename)
+    unrar = ctypes.WinDLL(dll_filename)
 except WindowsError:
-    unrar = ctypes.WinDLL(dll_name)
+    dll_filename = dll_name
+    if sys.version_info[:3] == (2, 7, 13):
+        # http://bugs.python.org/issue29082
+        dll_filename = str(dll_filename)
+    unrar = ctypes.WinDLL(dll_filename)
 
 
 class RAROpenArchiveDataEx(ctypes.Structure):
@@ -173,7 +182,7 @@ class PassiveReader:
     def __init__(self, usercallback = None):
         self.buf = []
         self.ucb = usercallback
-    
+
     def _callback(self, msg, UserData, P1, P2):
         if msg == UCM_PROCESSDATA:
             data = (ctypes.c_char*P2).from_address(P1).raw
@@ -182,7 +191,7 @@ def _callback(self, msg, UserData, P1, P2):
             else:
                 self.buf.append(data)
         return 1
-    
+
     def get_result(self):
         return ''.join(self.buf)
 
@@ -196,10 +205,10 @@ def __init__(self, arc):
             raise IncorrectRARPassword
         self.arc.lockStatus = "locked"
         self.arc.needskip = False
-        
+
     def __iter__(self):
         return self
-        
+
     def next(self):
         if self.index>0:
             if self.arc.needskip:
@@ -207,9 +216,9 @@ def next(self):
             self.res = RARReadHeaderEx(self.arc._handle, ctypes.byref(self.headerData))
 
         if self.res:
-            raise StopIteration 
+            raise StopIteration
         self.arc.needskip = True
-        
+
         data = {}
         data['index'] = self.index
         data['filename'] = self.headerData.FileNameW
@@ -223,7 +232,7 @@ def next(self):
         self.index += 1
         return data
 
-            
+
     def __del__(self):
         self.arc.lockStatus = "finished"
 
@@ -253,9 +262,9 @@ def init(self, password=None):
 
         if password:
             RARSetPassword(self._handle, password)
-            
+
         self.lockStatus = "ready"
-        
+
         self.isVolume = archiveData.Flags & 1
 
 
@@ -287,7 +296,7 @@ def read_files(self, checker):
                 self.needskip = False
                 res.append((info, reader.get_result()))
         return res
-        
+
 
     def extract(self,  checker, path, withSubpath, overwrite):
         res = []
@@ -300,7 +309,7 @@ def extract(self,  checker, path, withSubpath, overwrite):
                         fn = os.path.split(fn)[-1]
                     target = os.path.join(path, fn)
                 else:
-                    raise DeprecationWarning, "Condition callbacks returning strings are deprecated and only supported in Windows"                    
+                    raise DeprecationWarning, "Condition callbacks returning strings are deprecated and only supported in Windows"
                     target = checkres
                 if overwrite or (not os.path.exists(target)):
                     tmpres = RARProcessFile(self._handle, RAR_EXTRACT, None, target)
@@ -327,6 +336,3 @@ def get_volume(self):
         if match1 != None:
             return int(match1.group(1)) + 1
         return 0
-            
-
-


Test patch: