From 17365cc6045b8c7ce9f5f3292ac592f8dc8e5448 Mon Sep 17 00:00:00 2001 From: pyldap contributors Date: Thu, 23 Nov 2017 21:17:56 +0100 Subject: [PATCH] py3: Use new syntax for exceptions except ExcType, value -> except ExcType as value raise ExcType, value -> raise ExcType(value) raise value -> raise (when re-raising) --- Demo/Lib/ldap/async/deltree.py | 4 ++-- Demo/paged_search_ext_s.py | 2 +- Demo/pyasn1/dds.py | 6 +++--- Demo/pyasn1/noopsearch.py | 4 ++-- Demo/pyasn1/ppolicy.py | 4 ++-- Demo/pyasn1/psearch.py | 2 +- Demo/pyasn1/sessiontrack.py | 4 ++-- Demo/pyasn1/syncrepl.py | 6 +++--- Demo/sasl_bind.py | 4 ++-- Demo/schema.py | 2 +- Demo/schema_tree.py | 2 +- Lib/ldap/controls/__init__.py | 4 ++-- Lib/ldap/controls/openldap.py | 2 +- Lib/ldap/functions.py | 2 +- Lib/ldap/ldapobject.py | 10 +++++----- Lib/ldap/schema/subentry.py | 2 +- Lib/ldif.py | 2 +- Tests/t_cext.py | 4 ++-- Tests/t_ldapobject.py | 2 +- Tests/t_ldif.py | 2 +- 20 files changed, 35 insertions(+), 35 deletions(-) diff --git a/Demo/Lib/ldap/async/deltree.py b/Demo/Lib/ldap/async/deltree.py index 04506b3..9e23f1c 100644 --- a/Demo/Lib/ldap/async/deltree.py +++ b/Demo/Lib/ldap/async/deltree.py @@ -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( @@ -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 diff --git a/Demo/paged_search_ext_s.py b/Demo/paged_search_ext_s.py index 457d79c..d0f8291 100644 --- a/Demo/paged_search_ext_s.py +++ b/Demo/paged_search_ext_s.py @@ -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: diff --git a/Demo/pyasn1/dds.py b/Demo/pyasn1/dds.py index 71722b0..c803a1d 100644 --- a/Demo/pyasn1/dds.py +++ b/Demo/pyasn1/dds.py @@ -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 ') sys.exit(1) @@ -39,7 +39,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) @@ -47,7 +47,7 @@ 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: diff --git a/Demo/pyasn1/noopsearch.py b/Demo/pyasn1/noopsearch.py index 88d2d89..2045f50 100644 --- a/Demo/pyasn1/noopsearch.py +++ b/Demo/pyasn1/noopsearch.py @@ -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) @@ -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) diff --git a/Demo/pyasn1/ppolicy.py b/Demo/pyasn1/ppolicy.py index 97720b0..cf6b2ac 100644 --- a/Demo/pyasn1/ppolicy.py +++ b/Demo/pyasn1/ppolicy.py @@ -17,7 +17,7 @@ try: ldap_url = ldapurl.LDAPUrl(sys.argv[1]) -except IndexError,ValueError: +except (IndexError,ValueError): print('Usage: ppolicy.py ') sys.exit(1) @@ -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: diff --git a/Demo/pyasn1/psearch.py b/Demo/pyasn1/psearch.py index b2300ad..3bd59e6 100644 --- a/Demo/pyasn1/psearch.py +++ b/Demo/pyasn1/psearch.py @@ -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) diff --git a/Demo/pyasn1/sessiontrack.py b/Demo/pyasn1/sessiontrack.py index 337c5e9..91909a3 100644 --- a/Demo/pyasn1/sessiontrack.py +++ b/Demo/pyasn1/sessiontrack.py @@ -18,7 +18,7 @@ try: ldap_url = ldapurl.LDAPUrl(sys.argv[1]) -except IndexError,ValueError: +except (IndexError, ValueError): print('Usage: %s ' % (sys.argv[0])) sys.exit(1) @@ -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) diff --git a/Demo/pyasn1/syncrepl.py b/Demo/pyasn1/syncrepl.py index 479a626..e4c62e8 100644 --- a/Demo/pyasn1/syncrepl.py +++ b/Demo/pyasn1/syncrepl.py @@ -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) @@ -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: @@ -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) diff --git a/Demo/sasl_bind.py b/Demo/sasl_bind.py index 7ff75bf..667221c 100644 --- a/Demo/sasl_bind.py +++ b/Demo/sasl_bind.py @@ -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))) diff --git a/Demo/schema.py b/Demo/schema.py index 9f650d2..4d350f0 100644 --- a/Demo/schema.py +++ b/Demo/schema.py @@ -49,7 +49,7 @@ ('usage',range(2)), ] ) -except KeyError,e: +except KeyError as e: print('***KeyError',str(e)) diff --git a/Demo/schema_tree.py b/Demo/schema_tree.py index ef8bcec..79c8a83 100644 --- a/Demo/schema_tree.py +++ b/Demo/schema_tree.py @@ -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' diff --git a/Lib/ldap/controls/__init__.py b/Lib/ldap/controls/__init__.py index 56d611b..932fa53 100644 --- a/Lib/ldap/controls/__init__.py +++ b/Lib/ldap/controls/__init__.py @@ -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 diff --git a/Lib/ldap/controls/openldap.py b/Lib/ldap/controls/openldap.py index ee7b575..7108c63 100644 --- a/Lib/ldap/controls/openldap.py +++ b/Lib/ldap/controls/openldap.py @@ -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: diff --git a/Lib/ldap/functions.py b/Lib/ldap/functions.py index 6ddab54..c60d42b 100644 --- a/Lib/ldap/functions.py +++ b/Lib/ldap/functions.py @@ -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 diff --git a/Lib/ldap/ldapobject.py b/Lib/ldap/ldapobject.py index 805b6e3..a199462 100644 --- a/Lib/ldap/ldapobject.py +++ b/Lib/ldap/ldapobject.py @@ -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]: @@ -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): """ @@ -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) diff --git a/Lib/ldap/schema/subentry.py b/Lib/ldap/schema/subentry.py index 99a0dc4..3612a38 100644 --- a/Lib/ldap/schema/subentry.py +++ b/Lib/ldap/schema/subentry.py @@ -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] diff --git a/Lib/ldif.py b/Lib/ldif.py index 86b8ac9..714d01c 100644 --- a/Lib/ldif.py +++ b/Lib/ldif.py @@ -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 diff --git a/Tests/t_cext.py b/Tests/t_cext.py index cac661e..7c1417b 100644 --- a/Tests/t_cext.py +++ b/Tests/t_cext.py @@ -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) @@ -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) diff --git a/Tests/t_ldapobject.py b/Tests/t_ldapobject.py index 196a9f3..5773ff2 100644 --- a/Tests/t_ldapobject.py +++ b/Tests/t_ldapobject.py @@ -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) diff --git a/Tests/t_ldif.py b/Tests/t_ldif.py index 3da213f..76701cc 100644 --- a/Tests/t_ldif.py +++ b/Tests/t_ldif.py @@ -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)