Skip to content

Commit 8fb4916

Browse files
committed
Add ldap.filter.is_filter() which checks filter syntax
1 parent adf4761 commit 8fb4916

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

Lib/ldap/dn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def explode_rdn(rdn, notypes=False, flags=0):
111111

112112
def is_dn(s,flags=0):
113113
"""
114-
Returns True is `s' can be parsed by ldap.dn.str2dn() like as a
114+
Returns True if `s' can be parsed by ldap.dn.str2dn() like as a
115115
distinguished host_name (DN), otherwise False is returned.
116116
"""
117117
try:

Lib/ldap/filter.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,20 @@ 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_' is 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+
pass
104+
finally:
105+
lo.unbind()
106+
return True

0 commit comments

Comments
 (0)