Commits

Aymeric Augustin committed a4959c6

Add compatibility with Python 3.3+.

This requires a unofficial port of python-ldap. I'm not aware
of any official porting efforts at this time.

  • Participants
  • Parent commits fee6d04

Comments (0)

Files changed (6)

File src/mockldap/__init__.py

         If you called :meth:`~mockldap.MockLdap.start` multiple times, this is
         the easiest way to reset everything.
         """
-        for patcher in self.patchers.itervalues():
+        for patcher in self.patchers.values():
             patcher.stop()
 
         self.patchers.clear()
 
 
 # Map a dictionary by applying a function to each key/value.
-map_keys = lambda f, d: dict((f(k), v) for k, v in d.iteritems())
-map_values = lambda f, d: dict((k, f(v)) for k, v in d.iteritems())
+map_keys = lambda f, d: dict((f(k), v) for k, v in d.items())
+map_values = lambda f, d: dict((k, f(v)) for k, v in d.items())

File src/mockldap/filter.py

     def __eq__(self, other):
         return self.code == other.code
 
-    def __unicode__(self):
-        return u"%d-%d: %s %r" % (self.start, self.stop, self.code, self.content)
+    def __str__(self):
+        return "%d-%d: %s %r" % (self.start, self.stop, self.code, self.content)
 
     def __repr__(self):
-        return u"%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.code, self.content, self.start, self.stop)
+        return "%s(%r, %r, %r, %r)" % (self.__class__.__name__, self.code, self.content, self.start, self.stop)
 
     def matches(self, dn, attrs):
         raise NotImplementedError()
 def parse(filterstr):
     try:
         return ldap_filter.parse(tokenize(filterstr))
-    except NoParseError, e:
+    except NoParseError as e:
         raise ldap.FILTER_ERROR(e)
 
 
 
     for filterstr in sys.argv[1:]:
         pprint(tokenize(filterstr))
-        print parse(filterstr).unparse()
+        print(parse(filterstr).unparse())

File src/mockldap/ldapobject.py

         # Find directory entries within the requested scope
         base_parts = ldap.dn.explode_dn(base.lower())
         base_len = len(base_parts)
-        dn_parts = dict((dn, ldap.dn.explode_dn(dn)) for dn in self.directory.iterkeys())
+        dn_parts = dict((dn, ldap.dn.explode_dn(dn)) for dn in self.directory.keys())
 
         if scope == ldap.SCOPE_BASE:
-            dns = (dn for dn, parts in dn_parts.iteritems() if parts == base_parts)
+            dns = (dn for dn, parts in dn_parts.items() if parts == base_parts)
         elif scope == ldap.SCOPE_ONELEVEL:
-            dns = (dn for dn, parts in dn_parts.iteritems() if parts[1:] == base_parts)
+            dns = (dn for dn, parts in dn_parts.items() if parts[1:] == base_parts)
         elif scope == ldap.SCOPE_SUBTREE:
-            dns = (dn for dn, parts in dn_parts.iteritems() if parts[-base_len:] == base_parts)
+            dns = (dn for dn, parts in dn_parts.items() if parts[-base_len:] == base_parts)
         else:
             raise ValueError(u"Unrecognized scope: {0}".format(scope))
 
         # Apply the filter expression
         try:
             filter_expr = parse(filterstr)
-        except UnsupportedOp, e:
+        except UnsupportedOp as e:
             raise SeedRequired(e)
 
         results = ((dn, self.directory[dn]) for dn in dns
 
         # Apply attribute filtering, if any
         if attrlist is not None:
-            results = ((dn, dict((attr, values) for attr, values in attrs.iteritems() if attr in attrlist))
+            results = ((dn, dict((attr, values) for attr, values in attrs.items() if attr in attrlist))
                        for dn, attrs in results)
 
         if attrsonly:
-            results = ((dn, dict((attr, []) for attr in attrs.iterkeys()))
+            results = ((dn, dict((attr, []) for attr in attrs.keys()))
                        for dn, attrs in results)
 
         return list(results)
         return len(self.async_results) - 1
 
     def _pop_async_result(self, msgid):
-        if msgid in xrange(len(self.async_results)):
+        if msgid in range(len(self.async_results)):
             value = self.async_results[msgid]
             self.async_results[msgid] = None
         else:

File src/mockldap/recording.py

 from collections import defaultdict
 from copy import deepcopy
 from functools import partial
-from itertools import ifilter
-import types
 
 
 class SeedRequired(Exception):
         self._record(args, kwargs)
 
         try:
-            value = self._seeded_values(args, kwargs).next()[1]
+            value = next(iter(self._seeded_values(args, kwargs)))[1]
         except StopIteration:
             try:
                 value = self.func(self.instance, *args, **kwargs)
-            except SeedRequired, e:
+            except SeedRequired as e:
                 raise SeedRequired("Seed required for %s: %s" %
                                    (self._call_repr(*args, **kwargs), e))
         else:
     def _seeded_values(self, args, kwargs):
         func = partial(self._seed_matches, args, kwargs)
 
-        return ifilter(func, self._seeded_calls)
+        return filter(func, self._seeded_calls)
 
     def _seed_matches(self, args, kwargs, seed):
         return (seed[0] == (args, kwargs))
 
     def _call_repr(self, *args, **kwargs):
         arglist = [repr(arg) for arg in args]
-        arglist.extend('%s=%r' % item for item in kwargs.iteritems())
+        arglist.extend('%s=%r' % item for item in kwargs.items())
 
         return "%s(%s)" % (self.func.__name__, ", ".join(arglist))
 
         if isinstance(value, Exception):
             return True
 
-        if (isinstance(value, types.TypeType) and issubclass(value, Exception)):
+        if (isinstance(value, type) and issubclass(value, Exception)):
             return True
 
         return False

File src/mockldap/tests.py

     def test_search_s_get_all_directory_items_with_scope_subtree(self):
         results = self.ldapobj.search_s("o=test", ldap.SCOPE_SUBTREE)
 
-        self.assertEqual(sorted(results), sorted(directory.iteritems()))
+        self.assertEqual(sorted(results), sorted(directory.items()))
 
     def test_search_s_get_specific_item_with_scope_base(self):
         results = self.ldapobj.search_s("cn=alice,ou=example,o=test", ldap.SCOPE_BASE)
         try:
             self.ldapobj.search_s("ou=example,o=test", ldap.SCOPE_ONELEVEL,
                                   filterstr, attrlist=['ou'])
-        except SeedRequired, e:
+        except SeedRequired as e:
             self.assertIn("search_s('ou=example,o=test', 1, '(invalid~=bogus)', attrlist=['ou']", str(e))
         else:
             self.fail("Expected SeedRequired exception")
         self.mockldap.start('mockldap.tests.initialize')
         self.mockldap.stop()
 
-        self.assert_(self.mockldap[''] is not None)
+        self.assertTrue(self.mockldap[''] is not None)
 
     def test_stop_last(self):
         self.mockldap.start()
 [tox]
 distribute = False
-envlist = py26,py27
+envlist = py26,py27,py33,py34
 
 [testenv]
 deps = funcparserlib == 0.3.6
-       mock >= 1.0.1
        passlib >= 1.6.1
-       python-ldap >= 2.4.10
 commands = {envbindir}/python setup.py test
 
 [testenv:py26]
 deps = {[testenv]deps}
+       mock >= 1.0.1
+       python-ldap >= 2.4.10
        unittest2
 
+[testenv:py27]
+deps = {[testenv]deps}
+       mock >= 1.0.1
+       python-ldap >= 2.4.10
+
+[testenv:py33]
+deps = {[testenv]deps}
+       git+https://github.com/aaugustin/python-ldap.git@py3
+
+[testenv:py34]
+deps = {[testenv]deps}
+       git+https://github.com/aaugustin/python-ldap.git@py3
+
 [flake8]
 ignore = E501