[python-ldap] LDAPError has wrong type, gives AttributeError

Michael Ströder michael at stroeder.com
Fri Jun 29 18:47:41 CEST 2012

Viktor wrote:
> I am trying to write a cronjob that asks an LDAP-server periodically to
> get the members for mailing lists that are then fed to mailman.

We're also doing something like this with a Perl script written by a colleague
based on groupOfNames/member schema and OpenLDAP's slapo-memberof.

> It already is working, but I am now trying to modularize it more in order 
> to make the version public.

I'd be interested to see this. Will you release it as open source?

Also it might be worth to think about turning it into a syncrepl client if the
LDAP server is an OpenLDAP server. This way changes to groups would be
immediately synced to Mailman.

> except ldap.LDAPError as errors:
> 	print(errors)
> 	for error in errors.args:
> 		if error.has_key('desc'):
> 			syslog.syslog(syslog.LOG_ERR, error['desc'])
> 		if error.has_key('info'):
> 			syslog.syslog(syslog.LOG_ERR, error['info'])
> 	return False
> This usually worked and I did not change this part, but now I get an error:
> AttributeError: 'int' object has no attribute 'has_key'
> If I add a "print(errors)" to the except clause, I get
> (11, 'Resource temporarily unavailable')

Yes, that's expected. Frankly the exceptions generated by the C wrapper module
_ldap.so are not very elegant and sometimes inconsequent. If you just want to
write a log message the exception the most simplest thing is to call
str(error). This will always work and practical experience shows the
information provided is enough to find most error causes.

BTW: Cleaning up exception handling is on my to do list. Everyone having some
spare time left is welcome to jump on in.

Ciao, Michael.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 3883 bytes
Desc: S/MIME Cryptographic Signature
URL: <http://mail.python.org/pipermail/python-ldap/attachments/20120629/7c7fb436/attachment.bin>

More information about the python-ldap mailing list