Skip to content

Make initialize() pass extra keyword arguments to LDAPObject #250

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 8 additions & 23 deletions Doc/reference/ldap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ This module defines the following functions:
:py:const:`2` for logging the method calls with arguments and the complete results and
:py:const:`9` for also logging the traceback of method calls.

Additional keyword arguments are passed to :class:`LDAPObject`.

.. seealso::

:rfc:`4516` - Lightweight Directory Access Protocol (LDAP): Uniform Resource Locator
Expand Down Expand Up @@ -579,33 +581,16 @@ LDAPObject classes

.. py:class:: LDAPObject

Instances of :py:class:`LDAPObject` are returned by :py:func:`initialize()`
and :py:func:`open()` (deprecated). The connection is automatically unbound
Instances of :py:class:`LDAPObject` are returned by :py:func:`initialize()`.
The connection is automatically unbound
and closed when the LDAP object is deleted.

Internally :py:class:`LDAPObject` is set to :py:class:`SimpleLDAPObject`
by default.

.. py:class:: SimpleLDAPObject(uri [, trace_level=0 [, trace_file=sys.stdout [, trace_stack_limit=5]]])

This basic class wraps all methods of the underlying C API object.

The arguments are same like for function :py:func:`initialize()`.

.. py:class:: ReconnectLDAPObject(uri [, trace_level=0 [, trace_file=sys.stdout [, trace_stack_limit=5] [, retry_max=1 [, retry_delay=60.0]]]])

This class is derived from :py:class:`SimpleLDAPObject` and used for automatic
reconnects when using the synchronous request methods (see below). This class
also implements the pickle protocol.

The first arguments are same like for function :py:func:`initialize()`.

For automatic reconnects it has additional arguments:
Internally :py:class:`LDAPObject` is set to
:py:class:`~ldap.ldapobject.SimpleLDAPObject` by default.

*retry_max* specifies the number of reconnect attempts before
re-raising the :py:exc:`ldap.SERVER_DOWN` exception.
.. autoclass:: ldap.ldapobject.SimpleLDAPObject

*retry_delay* specifies the time in seconds between reconnect attempts.
.. autoclass:: ldap.ldapobject.ReconnectLDAPObject


.. _ldap-controls:
Expand Down
11 changes: 9 additions & 2 deletions Lib/ldap/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def _ldap_function_call(lock,func,*args,**kwargs):
return result


def initialize(uri,trace_level=0,trace_file=sys.stdout,trace_stack_limit=None, bytes_mode=None):
def initialize(
uri, trace_level=0, trace_file=sys.stdout, trace_stack_limit=None,
bytes_mode=None, **kwargs
):
"""
Return LDAPObject instance by opening LDAP connection to
LDAP host specified by LDAP URL
Expand All @@ -81,8 +84,12 @@ def initialize(uri,trace_level=0,trace_file=sys.stdout,trace_stack_limit=None, b
Default is to use stdout.
bytes_mode
Whether to enable :ref:`bytes_mode` for backwards compatibility under Py2.

Additional keyword arguments (such as ``bytes_strictness``) are
passed to ``LDAPObject``.
"""
return LDAPObject(uri,trace_level,trace_file,trace_stack_limit,bytes_mode)
return LDAPObject(
uri, trace_level, trace_file, trace_stack_limit, bytes_mode, **kwargs)


def get_option(option):
Expand Down
27 changes: 17 additions & 10 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ class NO_UNIQUE_ENTRY(ldap.NO_SUCH_OBJECT):

class SimpleLDAPObject:
"""
Drop-in wrapper class around _ldap.LDAPObject
This basic class wraps all methods of the underlying C API object.

The arguments are same as for the :func:`~ldap.initialize()` function.
"""

CLASSATTR_OPTION_MAPPING = {
Expand Down Expand Up @@ -1057,15 +1059,20 @@ def get_naming_contexts(self):

class ReconnectLDAPObject(SimpleLDAPObject):
"""
In case of server failure (ldap.SERVER_DOWN) the implementations
of all synchronous operation methods (search_s() etc.) are doing
an automatic reconnect and rebind and will retry the very same
operation.

This is very handy for broken LDAP server implementations
(e.g. in Lotus Domino) which drop connections very often making
it impossible to have a long-lasting control flow in the
application.
:py:class:`SimpleLDAPObject` subclass whose synchronous request methods
automatically reconnect and re-try in case of server failure
(:exc:`ldap.SERVER_DOWN`).

The first arguments are same as for the :py:func:`~ldap.initialize()`
function.
For automatic reconnects it has additional arguments:

* retry_max: specifies the number of reconnect attempts before
re-raising the :py:exc:`ldap.SERVER_DOWN` exception.

* retry_delay: specifies the time in seconds between reconnect attempts.

This class also implements the pickle protocol.
"""

__transient_attrs__ = {
Expand Down