tryconnection3.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. from __future__ import print_function
  2. # This module may be retired as soon as Python 2.5 support is dropped.
  3. #
  4. # It exists only to allow trapping exceptions using the "except [exception list] as e" format
  5. # which is a syntax error in Python 2.5
  6. def try_connection(verbose, *args, **kwargs):
  7. import adodbapi
  8. if "proxy_host" in kwargs or "pyro_connection" in kwargs or "proxy_host" in args:
  9. import adodbapi.remote
  10. import Pyro4
  11. pyroError = Pyro4.errors.PyroError
  12. dbconnect = adodbapi.remote.connect
  13. remote = True
  14. else:
  15. dbconnect = adodbapi.connect
  16. pyroError = NotImplementedError # (will not occur)
  17. remote = False
  18. try:
  19. s = dbconnect(*args, **kwargs) # connect to server
  20. if verbose:
  21. print('Connected to:', s.connection_string)
  22. print('which has tables:', s.get_table_names())
  23. s.close() # thanks, it worked, goodbye
  24. except adodbapi.DatabaseError as inst:
  25. print(inst.args[0]) # should be the error message
  26. print('***Failed getting connection using=',repr(args),repr(kwargs))
  27. if remote:
  28. print('** Are you running a *Python3* ado.connection server? **')
  29. print('* Have you run "setuptestframework.py" to create server_test.mdb?')
  30. return False, (args, kwargs), None
  31. if remote:
  32. print(" (remote)",end="")
  33. print(" (successful)")
  34. return True, (args, kwargs, remote), dbconnect
  35. def try_operation_with_expected_exception(expected_exception_list, some_function, *args, **kwargs):
  36. try:
  37. some_function(*args, **kwargs)
  38. except expected_exception_list as e:
  39. return True, e
  40. except:
  41. raise # an exception other than the expected occurred
  42. return False, 'The expected exception did not occur'