Skip to content

Commit ff33e7e

Browse files
committed
Add ldap.filter.is_filter() which checks filter syntax
1 parent 2647f59 commit ff33e7e

1 file changed

Lines changed: 18 additions & 0 deletions

File tree

Lib/ldap/filter.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,21 @@ def time_span_filter(
8787
until_timestr=strf_secs(until_timestamp),
8888
)
8989
# end of time_span_filter()
90+
91+
92+
def is_filter(filter_):
93+
"""
94+
Returns True if `filter_' can be parsed as a valid LDAP filter, otherwise False is returned.
95+
"""
96+
import ldap
97+
lo = ldap.initialize('')
98+
try:
99+
lo.search_ext_s('', ldap.SCOPE_BASE, filter_)
100+
except (ldap.FILTER_ERROR, TypeError, ValueError):
101+
return False
102+
except ldap.SERVER_DOWN:
103+
# the filter syntax is valid, as the connection is not bound we expecte SERVER_DOWN here
104+
return True
105+
finally:
106+
lo.unbind()
107+
raise RuntimeError('Could not check filter syntax.') # can not happen

0 commit comments

Comments
 (0)