Fix for python 3.x compatibility.
authorZac Medico <zmedico@gentoo.org>
Tue, 22 Sep 2009 20:33:01 +0000 (20:33 -0000)
committerZac Medico <zmedico@gentoo.org>
Tue, 22 Sep 2009 20:33:01 +0000 (20:33 -0000)
svn path=/main/trunk/; revision=14381

pym/portage/cache/anydbm.py

index 143c321e14c371914d5d75fa79d9fedfec7c54af..6add2da60c7c7448d9185f535bc0dc7edf8d17f2 100644 (file)
@@ -3,7 +3,11 @@
 # License: GPL2
 # $Id$
 
-anydbm_module = __import__("anydbm")
+try:
+       anydbm_module = __import__("anydbm")
+except ImportError:
+       # python 3.x
+       import dbm as anydbm_module
 try:
        import cPickle as pickle
 except ImportError:
@@ -31,8 +35,10 @@ class database(fs_template.FsBased):
                self._db_path = os.path.join(self.location, fs_template.gen_label(self.location, self.label)+default_db)
                self.__db = None
                try:
-                       self.__db = anydbm_module.open(
-                               _unicode_encode(self._db_path), 'w', self._perms)
+                       # dbm.open() will not work with bytes in python-3.1:
+                       #   TypeError: can't concat bytes to str
+                       self.__db = anydbm_module.open(self._db_path,
+                               'w', self._perms)
                except anydbm_module.error:
                        # XXX handle this at some point
                        try:
@@ -44,8 +50,10 @@ class database(fs_template.FsBased):
                        # try again if failed
                        try:
                                if self.__db == None:
-                                       self.__db = anydbm_module.open(
-                                               _unicode_encode(self._db_path), 'c', self._perms)
+                                       # dbm.open() will not work with bytes in python-3.1:
+                                       #   TypeError: can't concat bytes to str
+                                       self.__db = anydbm_module.open(self._db_path,
+                                               'c', self._perms)
                        except anydbm_module.error as e:
                                raise cache_errors.InitializationError(self.__class__, e)
                self._ensure_access(self._db_path)