From 5248df94d3ae81517b3926f8e682f235778255f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Barrois?= Date: Thu, 14 Nov 2013 01:23:43 +0100 Subject: [PATCH] [Py3] Fix renamed functions * types.* * UserDict * has_key() * file() --- Lib/ldap/cidict.py | 11 ++--- Lib/ldap/compat.py | 10 ++++ Lib/ldap/ldapobject.py | 10 ++-- Lib/ldap/schema/models.py | 98 +++++++++++++++++-------------------- Lib/ldap/schema/subentry.py | 2 - Lib/ldapurl.py | 16 +++--- Tests/slapd.py | 3 +- Tests/t_cext.py | 2 +- Tests/t_ldapurl.py | 10 ++-- setup.py | 1 + 10 files changed, 83 insertions(+), 80 deletions(-) create mode 100644 Lib/ldap/compat.py diff --git a/Lib/ldap/cidict.py b/Lib/ldap/cidict.py index b0a0a85..0e11c0a 100644 --- a/Lib/ldap/cidict.py +++ b/Lib/ldap/cidict.py @@ -10,8 +10,7 @@ $Id: cidict.py,v 1.13 2009/04/17 14:34:34 stroeder Exp $ __version__ = """$Revision: 1.13 $""" -from UserDict import UserDict -from string import lower +from ldap.compat import UserDict class cidict(UserDict): """ @@ -24,15 +23,15 @@ class cidict(UserDict): self.update(default or {}) def __getitem__(self,key): - return self.data[lower(key)] + return self.data[key.lower()] def __setitem__(self,key,value): - lower_key = lower(key) + lower_key = key.lower() self._keys[lower_key] = key self.data[lower_key] = value def __delitem__(self,key): - lower_key = lower(key) + lower_key = key.lower() del self._keys[lower_key] del self.data[lower_key] @@ -41,7 +40,7 @@ class cidict(UserDict): self[key] = dict[key] def has_key(self,key): - return UserDict.has_key(self,lower(key)) + return UserDict.has_key(self,key.lower()) def __contains__(self,key): return self.has_key(key) diff --git a/Lib/ldap/compat.py b/Lib/ldap/compat.py new file mode 100644 index 0000000..7fbd115 --- /dev/null +++ b/Lib/ldap/compat.py @@ -0,0 +1,10 @@ +"""Compatibility wrappers for Py2/Py3.""" + +import sys + +if sys.version_info[0] < 3: + from UserDict import UserDict + from urllib import quote, unquote +else: + from collections import UserDict + from urllib.parse import quote, unquote diff --git a/Lib/ldap/ldapobject.py b/Lib/ldap/ldapobject.py index 88d59fc..9eb3858 100644 --- a/Lib/ldap/ldapobject.py +++ b/Lib/ldap/ldapobject.py @@ -121,15 +121,15 @@ class SimpleLDAPObject: return result def __setattr__(self,name,value): - if self.CLASSATTR_OPTION_MAPPING.has_key(name): + if name in self.CLASSATTR_OPTION_MAPPING: self.set_option(self.CLASSATTR_OPTION_MAPPING[name],value) else: self.__dict__[name] = value def __getattr__(self,name): - if self.CLASSATTR_OPTION_MAPPING.has_key(name): + if name in self.CLASSATTR_OPTION_MAPPING: return self.get_option(self.CLASSATTR_OPTION_MAPPING[name]) - elif self.__dict__.has_key(name): + elif name in self.__dict__: return self.__dict__[name] else: raise AttributeError('%s has no attribute %s' % ( @@ -814,7 +814,7 @@ class ReconnectLDAPObject(SimpleLDAPObject): """return data representation for pickled object""" d = {} for k,v in self.__dict__.items(): - if not self.__transient_attrs__.has_key(k): + if k not in self.__transient_attrs__: d[k] = v return d @@ -888,7 +888,7 @@ class ReconnectLDAPObject(SimpleLDAPObject): return # reconnect() def _apply_method_s(self,func,*args,**kwargs): - if not self.__dict__.has_key('_l'): + if '_l' not in self.__dict__: self.reconnect(self._uri,retry_max=self._retry_max,retry_delay=self._retry_delay) try: return func(self,*args,**kwargs) diff --git a/Lib/ldap/schema/models.py b/Lib/ldap/schema/models.py index 48d6b00..2c1fe31 100644 --- a/Lib/ldap/schema/models.py +++ b/Lib/ldap/schema/models.py @@ -6,17 +6,11 @@ See http://www.python-ldap.org/ for details. \$Id: models.py,v 1.48 2015/06/06 09:21:38 stroeder Exp $ """ -import UserDict,ldap.cidict +import ldap.cidict +from ldap.compat import UserDict from ldap.schema.tokenizer import split_tokens,extract_tokens -if __debug__: - from types import TupleType,StringType,IntType - try: - from types import BooleanType - except ImportError: - BooleanType = IntType - NOT_HUMAN_READABLE_LDAP_SYNTAXES = { '1.3.6.1.4.1.1466.115.121.1.4':None, # Audio @@ -70,7 +64,7 @@ class SchemaElement: return self.oid def key_attr(self,key,value,quoted=0): - assert value is None or type(value)==StringType,TypeError("value has to be of StringType, was %s" % repr(value)) + assert value is None or type(value)==str,TypeError("value has to be of StringType, was %s" % repr(value)) if value: if quoted: return " %s '%s'" % (key,value.replace("'","\\'")) @@ -80,7 +74,7 @@ class SchemaElement: return "" def key_list(self,key,values,sep=' ',quoted=0): - assert type(values)==TupleType,TypeError("values has to be of ListType") + assert type(values) == tuple,TypeError("values has to be of ListType") if not values: return '' if quoted: @@ -161,13 +155,13 @@ class ObjectClass(SchemaElement): self.sup = ('top',) else: self.sup = d['SUP'] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.sup)==TupleType - assert type(self.kind)==IntType - assert type(self.must)==TupleType - assert type(self.may)==TupleType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.sup) == tuple + assert type(self.kind) == int + assert type(self.must) == tuple + assert type(self.may) == tuple return def __str__(self): @@ -288,13 +282,13 @@ class AttributeType(SchemaElement): self.collective = d['COLLECTIVE']!=None self.no_user_mod = d['NO-USER-MODIFICATION']!=None self.usage = AttributeUsage.get(d['USAGE'][0],0) - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.sup)==TupleType,'attribute sup has type %s' % (type(self.sup)) - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.single_value)==BooleanType and (self.single_value==0 or self.single_value==1) - assert type(self.no_user_mod)==BooleanType and (self.no_user_mod==0 or self.no_user_mod==1) - assert self.syntax is None or type(self.syntax)==StringType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.sup) == tuple,'attribute sup has type %s' % (type(self.sup)) + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.single_value) == bool and (self.single_value==0 or self.single_value==1) + assert type(self.no_user_mod) == bool and (self.no_user_mod==0 or self.no_user_mod==1) + assert self.syntax is None or type(self.syntax) == str assert self.syntax_len is None or type(self.syntax_len)==type(0) return @@ -353,7 +347,7 @@ class LDAPSyntax(SchemaElement): NOT_HUMAN_READABLE_LDAP_SYNTAXES.has_key(self.oid) or \ d['X-NOT-HUMAN-READABLE'][0]=='TRUE' self.x_binary_transfer_required = d['X-BINARY-TRANSFER-REQUIRED'][0]=='TRUE' - assert self.desc is None or type(self.desc)==StringType + assert self.desc is None or type(self.desc) == str return def __str__(self): @@ -400,10 +394,10 @@ class MatchingRule(SchemaElement): self.desc = d['DESC'][0] self.obsolete = d['OBSOLETE']!=None self.syntax = d['SYNTAX'][0] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert self.syntax is None or type(self.syntax)==StringType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert self.syntax is None or type(self.syntax) == str return def __str__(self): @@ -450,10 +444,10 @@ class MatchingRuleUse(SchemaElement): self.desc = d['DESC'][0] self.obsolete = d['OBSOLETE']!=None self.applies = d['APPLIES'] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.applies)==TupleType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.applies) == tuple return def __str__(self): @@ -517,13 +511,13 @@ class DITContentRule(SchemaElement): self.must = d['MUST'] self.may = d['MAY'] self.nots = d['NOT'] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.aux)==TupleType - assert type(self.must)==TupleType - assert type(self.may)==TupleType - assert type(self.nots)==TupleType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.aux) == tuple + assert type(self.must) == tuple + assert type(self.may) == tuple + assert type(self.nots) == tuple return def __str__(self): @@ -584,11 +578,11 @@ class DITStructureRule(SchemaElement): self.obsolete = d['OBSOLETE']!=None self.form = d['FORM'][0] self.sup = d['SUP'] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.form)==StringType - assert type(self.sup)==TupleType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.form) == str + assert type(self.sup) == tuple return def __str__(self): @@ -648,12 +642,12 @@ class NameForm(SchemaElement): self.oc = d['OC'][0] self.must = d['MUST'] self.may = d['MAY'] - assert type(self.names)==TupleType - assert self.desc is None or type(self.desc)==StringType - assert type(self.obsolete)==BooleanType and (self.obsolete==0 or self.obsolete==1) - assert type(self.oc)==StringType - assert type(self.must)==TupleType - assert type(self.may)==TupleType + assert type(self.names) == tuple + assert self.desc is None or type(self.desc) == str + assert type(self.obsolete) == bool and (self.obsolete==0 or self.obsolete==1) + assert type(self.oc) == str + assert type(self.must) == tuple + assert type(self.may) == tuple return def __str__(self): @@ -667,7 +661,7 @@ class NameForm(SchemaElement): return '( %s )' % ''.join(result) -class Entry(UserDict.UserDict): +class Entry(UserDict): """ Schema-aware implementation of an LDAP entry class. diff --git a/Lib/ldap/schema/subentry.py b/Lib/ldap/schema/subentry.py index 24c72fa..83f99a2 100644 --- a/Lib/ldap/schema/subentry.py +++ b/Lib/ldap/schema/subentry.py @@ -10,8 +10,6 @@ import ldap.cidict,ldap.schema from ldap.schema.models import * -from UserDict import UserDict - SCHEMA_CLASS_MAPPING = ldap.cidict.cidict() SCHEMA_ATTR_MAPPING = {} diff --git a/Lib/ldapurl.py b/Lib/ldapurl.py index a43c59b..789d3af 100644 --- a/Lib/ldapurl.py +++ b/Lib/ldapurl.py @@ -23,9 +23,7 @@ __all__ = [ 'LDAPUrlExtension','LDAPUrlExtensions','LDAPUrl' ] -import UserDict - -from urllib import quote,unquote +from ldap.compat import UserDict, quote, unquote LDAP_SCOPE_BASE = 0 LDAP_SCOPE_ONELEVEL = 1 @@ -134,14 +132,14 @@ class LDAPUrlExtension: return not self.__eq__(other) -class LDAPUrlExtensions(UserDict.UserDict): +class LDAPUrlExtensions(UserDict): """ Models a collection of LDAP URL extensions as dictionary type """ def __init__(self,default=None): - UserDict.UserDict.__init__(self) + UserDict.__init__(self) for k,v in (default or {}).items(): self[k]=v @@ -395,10 +393,10 @@ class LDAPUrl: ) def __getattr__(self,name): - if self.attr2extype.has_key(name): + if name in self.attr2extype: extype = self.attr2extype[name] if self.extensions and \ - self.extensions.has_key(extype) and \ + extype in self.extensions and \ not self.extensions[extype].exvalue is None: result = unquote(self.extensions[extype].exvalue) else: @@ -410,7 +408,7 @@ class LDAPUrl: return result # __getattr__() def __setattr__(self,name,value): - if self.attr2extype.has_key(name): + if name in self.attr2extype: extype = self.attr2extype[name] if value is None: # A value of None means that extension is deleted @@ -424,7 +422,7 @@ class LDAPUrl: self.__dict__[name] = value def __delattr__(self,name): - if self.attr2extype.has_key(name): + if name in self.attr2extype: extype = self.attr2extype[name] if self.extensions: try: diff --git a/Tests/slapd.py b/Tests/slapd.py index 48747fa..192b2ae 100644 --- a/Tests/slapd.py +++ b/Tests/slapd.py @@ -154,7 +154,8 @@ class Slapd: self._log.debug("deleting existing %s", path) os.remove(path) self._log.debug("writing config to %s", path) - file(path, "w").writelines([line + "\n" for line in self._config]) + with open(path, 'w') as f: + f.writelines([line + "\n" for line in self._config]) return path def start(self): diff --git a/Tests/t_cext.py b/Tests/t_cext.py index c49e97e..0ae87f7 100644 --- a/Tests/t_cext.py +++ b/Tests/t_cext.py @@ -165,7 +165,7 @@ class TestLdapCExtension(unittest.TestCase): self.assertEquals(result, _ldap.RES_SEARCH_RESULT) self.assertEquals(pmsg[0][0], "") # rootDSE has no dn self.assertEquals(msgid, m) - self.assertTrue(pmsg[0][1].has_key('objectClass')) + self.assertIn('objectClass', pmsg[0][1]) def test_unbind(self): l = self._init() diff --git a/Tests/t_ldapurl.py b/Tests/t_ldapurl.py index 5ccd442..d2ed4f1 100644 --- a/Tests/t_ldapurl.py +++ b/Tests/t_ldapurl.py @@ -1,5 +1,7 @@ +# -*- coding: utf-8 -*- + import ldap, unittest -import urllib +from ldap.compat import quote from ldapurl import LDAPUrl @@ -26,9 +28,9 @@ class TestLDAPUrl(unittest.TestCase): u = MyLDAPUrl("ldap://127.0.0.1:1234/dc=example,dc=com" + "?attr1,attr2,attr3" + "?sub" - + "?" + urllib.quote("(objectClass=*)") - + "?bindname=" + urllib.quote("cn=d,c=au") - + ",X-BINDPW=" + urllib.quote("???") + + "?" + quote("(objectClass=*)") + + "?bindname=" + quote("cn=d,c=au") + + ",X-BINDPW=" + quote("???") + ",trace=8" ) self.assertEquals(u.urlscheme, "ldap") diff --git a/setup.py b/setup.py index 766b412..2c7ec83 100644 --- a/setup.py +++ b/setup.py @@ -163,6 +163,7 @@ setup( 'dsml', 'ldap', 'ldap.async', + 'ldap.compat', 'ldap.controls', 'ldap.controls.libldap', 'ldap.controls.openldap',