tryconnection2.py 1.8 KB

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