Skip to content

Commit

Permalink
py3: Use new syntax for exceptions
Browse files Browse the repository at this point in the history
except ExcType, value  ->  except ExcType as value
raise ExcType, value  ->  raise ExcType(value)
raise value  ->  raise  (when re-raising)
  • Loading branch information
pyldap contributors authored and Petr Viktorin committed Nov 24, 2017
1 parent e73f8d8 commit 17365cc
Show file tree
Hide file tree
Showing 20 changed files with 35 additions and 35 deletions.
4 changes: 2 additions & 2 deletions Demo/Lib/ldap/async/deltree.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def __init__(self,l):

def startSearch(self,searchRoot,searchScope):
if not searchScope in [ldap.SCOPE_ONELEVEL,ldap.SCOPE_SUBTREE]:
raise ValueError, "Parameter searchScope must be either ldap.SCOPE_ONELEVEL or ldap.SCOPE_SUBTREE."
raise ValueError("Parameter searchScope must be either ldap.SCOPE_ONELEVEL or ldap.SCOPE_SUBTREE.")
self.nonLeafEntries = []
self.deletedEntries = 0
ldap.async.AsyncSearchHandler.startSearch(
Expand Down Expand Up @@ -47,7 +47,7 @@ def _processSingleResult(self,resultType,resultItem):
else:
try:
self._l.delete_s(dn)
except ldap.NOT_ALLOWED_ON_NONLEAF,e:
except ldap.NOT_ALLOWED_ON_NONLEAF as e:
self.nonLeafEntries.append(dn)
else:
self.deletedEntries = self.deletedEntries+1
Expand Down
2 changes: 1 addition & 1 deletion Demo/paged_search_ext_s.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def paged_search_ext_s(self,base,scope,filterstr='(objectClass=*)',attrlist=None
else:
break # no more pages available

except ldap.SERVER_DOWN,e:
except ldap.SERVER_DOWN as e:
try:
self.reconnect(self._uri)
except AttributeError:
Expand Down
6 changes: 3 additions & 3 deletions Demo/pyasn1/dds.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
try:
ldap_url = ldapurl.LDAPUrl(sys.argv[1])
request_ttl = int(sys.argv[2])
except IndexError,ValueError:
except (IndexError, ValueError):
print('Usage: dds.py <LDAP URL> <TTL>')
sys.exit(1)

Expand All @@ -39,15 +39,15 @@
try:
ldap_conn.simple_bind_s(ldap_url.who or '',ldap_url.cred or '')

except ldap.INVALID_CREDENTIALS,e:
except ldap.INVALID_CREDENTIALS as e:
print('Simple bind failed:',str(e))
sys.exit(1)

else:
extreq = RefreshRequest(entryName=ldap_url.dn,requestTtl=request_ttl)
try:
extop_resp_obj = ldap_conn.extop_s(extreq,extop_resp_class=RefreshResponse)
except ldap.LDAPError,e:
except ldap.LDAPError as e:
print(str(e))
else:
if extop_resp_obj.responseTtl!=request_ttl:
Expand Down
4 changes: 2 additions & 2 deletions Demo/pyasn1/noopsearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
try:
ldap_conn.simple_bind_s(ldap_url.who or '',ldap_url.cred or '')

except ldap.INVALID_CREDENTIALS,e:
except ldap.INVALID_CREDENTIALS as e:
print('Simple bind failed:',str(e))
sys.exit(1)

Expand All @@ -59,7 +59,7 @@
ldap.TIMEOUT,
ldap.TIMELIMIT_EXCEEDED,
ldap.SIZELIMIT_EXCEEDED,
ldap.ADMINLIMIT_EXCEEDED),e:
ldap.ADMINLIMIT_EXCEEDED) as e:
ldap_conn.abandon(msg_id)
sys.exit(1)

Expand Down
4 changes: 2 additions & 2 deletions Demo/pyasn1/ppolicy.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

try:
ldap_url = ldapurl.LDAPUrl(sys.argv[1])
except IndexError,ValueError:
except (IndexError,ValueError):
print('Usage: ppolicy.py <LDAP URL>')
sys.exit(1)

Expand All @@ -39,7 +39,7 @@
try:
msgid = ldap_conn.simple_bind(ldap_url.who,ldap_url.cred,serverctrls=[PasswordPolicyControl()])
res_type,res_data,res_msgid,res_ctrls = ldap_conn.result3(msgid)
except ldap.INVALID_CREDENTIALS,e:
except ldap.INVALID_CREDENTIALS as e:
print('Simple bind failed:',str(e))
sys.exit(1)
else:
Expand Down
2 changes: 1 addition & 1 deletion Demo/pyasn1/psearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
try:
ldap_conn.simple_bind_s(ldap_url.who,ldap_url.cred)

except ldap.INVALID_CREDENTIALS,e:
except ldap.INVALID_CREDENTIALS as e:
print('Simple bind failed:',str(e))
sys.exit(1)

Expand Down
4 changes: 2 additions & 2 deletions Demo/pyasn1/sessiontrack.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

try:
ldap_url = ldapurl.LDAPUrl(sys.argv[1])
except IndexError,ValueError:
except (IndexError, ValueError):
print('Usage: %s <LDAP URL>' % (sys.argv[0]))
sys.exit(1)

Expand All @@ -40,7 +40,7 @@
try:
ldap_conn.simple_bind_s(ldap_url.who or '',ldap_url.cred or '')

except ldap.INVALID_CREDENTIALS,e:
except ldap.INVALID_CREDENTIALS as e:
print('Simple bind failed:',str(e))
sys.exit(1)

Expand Down
6 changes: 3 additions & 3 deletions Demo/pyasn1/syncrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def commenceShutdown(signum, stack):
'X-BINDPW=password" db.shelve'
).format(script_name=sys.argv[0])
sys.exit(1)
except ValueError,e:
except ValueError as e:
print('Error parsing command-line arguments:',str(e))
sys.exit(1)

Expand All @@ -169,7 +169,7 @@ def commenceShutdown(signum, stack):
# Now we login to the LDAP server
try:
ldap_connection.simple_bind_s(ldap_url.who, ldap_url.cred)
except ldap.INVALID_CREDENTIALS, err:
except ldap.INVALID_CREDENTIALS as err:
logger.error('Login to LDAP server failed: %s', err)
sys.exit(1)
except ldap.SERVER_DOWN:
Expand All @@ -193,7 +193,7 @@ def commenceShutdown(signum, stack):
except KeyboardInterrupt:
# User asked to exit
commenceShutdown(None, None)
except Exception, err:
except Exception as err:
# Handle any exception
if watcher_running:
logger.exception('Unhandled exception, going to retry: %s', err)
Expand Down
4 changes: 2 additions & 2 deletions Demo/sasl_bind.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@
l.protocol_version = 3
try:
l.sasl_interactive_bind_s("", sasl_auth)
except ldap.LDAPError,e:
except ldap.LDAPError as e:
print('Error using SASL mechanism',sasl_auth.mech,str(e))
else:
print('Sucessfully bound using SASL mechanism:',sasl_auth.mech)
try:
print('Result of Who Am I? ext. op:',repr(l.whoami_s()))
except ldap.LDAPError,e:
except ldap.LDAPError as e:
print('Error using SASL mechanism',sasl_auth.mech,str(e))
try:
print('OPT_X_SASL_USERNAME',repr(l.get_option(ldap.OPT_X_SASL_USERNAME)))
Expand Down
2 changes: 1 addition & 1 deletion Demo/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
('usage',range(2)),
]
)
except KeyError,e:
except KeyError as e:
print('***KeyError',str(e))


Expand Down
2 changes: 1 addition & 1 deletion Demo/schema_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def HTMLSchemaTree(schema,se_class,se_tree,se_oid,level):

try:
options,args=getopt.getopt(sys.argv[1:],'',['html'])
except getopt.error,e:
except getopt.error:
print('Error: %s\nUsage: schema_oc_tree.py [--html] [LDAP URL]')

html_output = options and options[0][0]=='--html'
Expand Down
4 changes: 2 additions & 2 deletions Lib/ldap/controls/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,9 @@ def DecodeControlTuples(ldapControlTuples,knownLDAPControls=None):
control.controlType,control.criticality = controlType,criticality
try:
control.decodeControlValue(encodedControlValue)
except PyAsn1Error,e:
except PyAsn1Error:
if criticality:
raise e
raise
else:
result.append(control)
return result
Expand Down
2 changes: 1 addition & 1 deletion Lib/ldap/controls/openldap.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def noop_search_st(self,base,scope=ldap.SCOPE_SUBTREE,filterstr='(objectClass=*)
ldap.TIMELIMIT_EXCEEDED,
ldap.SIZELIMIT_EXCEEDED,
ldap.ADMINLIMIT_EXCEEDED
),e:
) as e:
self.abandon(msg_id)
raise e
else:
Expand Down
2 changes: 1 addition & 1 deletion Lib/ldap/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def _ldap_function_call(lock,func,*args,**kwargs):
finally:
if lock:
lock.release()
except LDAPError,e:
except LDAPError as e:
if __debug__ and ldap._trace_level>=2:
ldap._trace_file.write('=> LDAPError: %s\n' % (str(e)))
raise
Expand Down
10 changes: 5 additions & 5 deletions Lib/ldap/ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def _ldap_call(self,func,*args,**kwargs):
diagnostic_message_success = self._l.get_option(ldap.OPT_DIAGNOSTIC_MESSAGE)
finally:
self._ldap_object_lock.release()
except LDAPError, e:
except LDAPError as e:
exc_type,exc_value,exc_traceback = sys.exc_info()
try:
if 'info' not in e.args[0] and 'errno' in e.args[0]:
Expand Down Expand Up @@ -125,9 +125,9 @@ def __getattr__(self,name):
elif name in self.__dict__:
return self.__dict__[name]
else:
raise AttributeError,'%s has no attribute %s' % (
raise AttributeError('%s has no attribute %s' % (
self.__class__.__name__,repr(name)
)
))

def fileno(self):
"""
Expand Down Expand Up @@ -865,14 +865,14 @@ def reconnect(self,uri,retry_max=1,retry_delay=60.0):
SimpleLDAPObject.start_tls_s(self)
# Repeat last simple or SASL bind
self._apply_last_bind()
except (ldap.SERVER_DOWN,ldap.TIMEOUT),e:
except (ldap.SERVER_DOWN,ldap.TIMEOUT):
if __debug__ and self._trace_level>=1:
self._trace_file.write('*** %s reconnect to %s failed\n' % (
counter_text,uri
))
reconnect_counter = reconnect_counter-1
if not reconnect_counter:
raise e
raise
if __debug__ and self._trace_level>=1:
self._trace_file.write('=> delay %s...\n' % (retry_delay))
time.sleep(retry_delay)
Expand Down
2 changes: 1 addition & 1 deletion Lib/ldap/schema/subentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ def attribute_types(
schema_attr_type = self.sed[AttributeType][a]
except KeyError:
if raise_keyerror:
raise KeyError,'No attribute type found in sub schema by name %s' % (a)
raise KeyError('No attribute type found in sub schema by name %s' % (a))
# If there's no schema element for this attribute type
# but still KeyError is to be ignored we filter it away
del l[a]
Expand Down
2 changes: 1 addition & 1 deletion Lib/ldif.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ def _next_key_and_value(self):
return '-',None
try:
colon_pos = unfolded_line.index(':')
except ValueError,e:
except ValueError as e:
raise ValueError('no value-spec in %s' % (repr(unfolded_line)))
attr_type = unfolded_line[0:colon_pos]
# if needed attribute value is BASE64 decoded
Expand Down
4 changes: 2 additions & 2 deletions Tests/t_cext.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ def test_abandon(self):
self.assertNone(ret)
try:
r = l.result4(m, _ldap.MSG_ALL, 0.3) # (timeout /could/ be longer)
except _ldap.TIMEOUT, e:
except _ldap.TIMEOUT as e:
pass
else:
self.fail("expected TIMEOUT, got %r" % r)
Expand Down Expand Up @@ -683,7 +683,7 @@ def test_errno107(self):
try:
m = l.simple_bind("", "")
r = l.result4(m, _ldap.MSG_ALL, self.timeout)
except _ldap.SERVER_DOWN, ldap_err:
except _ldap.SERVER_DOWN as ldap_err:
errno = ldap_err.args[0]['errno']
if errno != 107:
self.fail("expected errno=107, got %d" % errno)
Expand Down
2 changes: 1 addition & 1 deletion Tests/t_ldapobject.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def test004_errno107(self):
try:
m = l.simple_bind_s("", "")
r = l.result4(m, ldap.MSG_ALL, self.timeout)
except ldap.SERVER_DOWN, ldap_err:
except ldap.SERVER_DOWN as ldap_err:
errno = ldap_err.args[0]['errno']
if errno != 107:
self.fail("expected errno=107, got %d" % errno)
Expand Down
2 changes: 1 addition & 1 deletion Tests/t_ldif.py
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,7 @@ def test_bad_change_records(self):
ldif_string = textwrap.dedent(bad_ldif_string).lstrip() + '\n'
try:
res = self._parse_records(ldif_string)
except ValueError, value_error:
except ValueError as value_error:
pass
else:
self.fail("should have raised ValueError: %r" % bad_ldif_string)
Expand Down

0 comments on commit 17365cc

Please sign in to comment.