From 65fe7db04d73c15b6fd4098c9caf97ca447b080b Mon Sep 17 00:00:00 2001 From: Federico Di Gregorio Date: Tue, 15 Nov 2005 11:51:05 +0000 Subject: [PATCH] Does not crash on importing needed modules. (Closes: #32) --- ChangeLog | 5 +++++ ZPsycopgDA/DA.py | 14 ++++++-------- ZPsycopgDA/__init__.py | 21 ++++++++++++--------- psycopg/psycopgmodule.c | 11 ++++++++++- 4 files changed, 33 insertions(+), 18 deletions(-) diff --git a/ChangeLog b/ChangeLog index 5f665358..f4f49518 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +2005-11-15 Federico Di Gregorio + + * psycopg/psycopgmodule.c: now bails out with correct exception when one + of the needed modules can't be imported (should fix #32.) + 2005-11-14 Federico Di Gregorio * psycopg/typecast.c: added typecast_parse_date and typecast_parse_time diff --git a/ZPsycopgDA/DA.py b/ZPsycopgDA/DA.py index a25a21ca..096ac699 100644 --- a/ZPsycopgDA/DA.py +++ b/ZPsycopgDA/DA.py @@ -42,7 +42,6 @@ from psycopg2.extensions import INTEGER, LONGINTEGER, FLOAT, BOOLEAN, DATE from psycopg2.extensions import TIME, INTERVAL from psycopg2.extensions import new_type, register_type - # add a new connection to a folder @@ -56,17 +55,16 @@ def manage_addZPsycopgConnection(self, id, title, connection_string, zdatetime, check, tilevel)) if REQUEST is not None: return self.manage_main(self, REQUEST) - # the connection object class Connection(Shared.DC.ZRDB.Connection.Connection): - """ZPsycopg Connection.""" + """ZPsycopg 2 Connection.""" _isAnSQLConnection = 1 - id = 'Psycopg_database_connection' - database_type = 'Psycopg' - meta_type = title = 'Z Psycopg Database Connection' + id = 'Psycopg2_database_connection' + database_type = 'Psycopg2' + meta_type = title = 'Z Psycopg 2 Database Connection' icon = 'misc_/ZPsycopgDA/conn' def __init__(self, id, title, connection_string, @@ -187,7 +185,7 @@ class Connection(Shared.DC.ZRDB.Connection.Connection): classes = (Connection,) -meta_types = ({'name':'Z Psycopg Database Connection', +meta_types = ({'name':'Z Psycopg 2 Database Connection', 'action':'manage_addZPsycopgConnectionForm'},) folder_methods = { @@ -195,7 +193,7 @@ folder_methods = { 'manage_addZPsycopgConnectionForm': manage_addZPsycopgConnectionForm} __ac_permissions__ = ( - ('Add Z Psycopg Database Connections', + ('Add Z Psycopg 2 Database Connections', ('manage_addZPsycopgConnectionForm', 'manage_addZPsycopgConnection')),) # add icons diff --git a/ZPsycopgDA/__init__.py b/ZPsycopgDA/__init__.py index f9244ff2..5a0403ed 100644 --- a/ZPsycopgDA/__init__.py +++ b/ZPsycopgDA/__init__.py @@ -17,7 +17,7 @@ # # See the LICENSE file for details. -__doc__ = "ZPsycopg Database Adalper Registration." +__doc__ = "ZPsycopg 2 Database Adapter Registration." __version__ = '2.0' import DA @@ -27,12 +27,15 @@ classes = DA.classes meta_types = DA.meta_types misc_ = DA.misc_ -__ac_permissions__=DA.__ac_permissions__ +__ac_permissions__ = DA.__ac_permissions__ -def initialize(context): - context.registerClass( - DA.Connection, - permission = 'Add Z Psycopg Database Connections', - constructors = (DA.manage_addZPsycopgConnectionForm, - DA.manage_addZPsycopgConnection), - icon = SOFTWARE_HOME + '/Shared/DC/ZRDB/www/DBAdapterFolder_icon.gif') +# FIXME: isn't this crazy? Apparently the variables above are enough +# to have the ZPsycopgDA product installed and working. :/ +# +#def initialize(context): +# context.registerClass( +# DA.Connection, +# permission = 'Add Z Psycopg 2 Database Connections', +# constructors = (DA.manage_addZPsycopgConnectionForm, +# DA.manage_addZPsycopgConnection), +# icon = SOFTWARE_HOME + '/Shared/DC/ZRDB/www/DBAdapterFolder_icon.gif') diff --git a/psycopg/psycopgmodule.c b/psycopg/psycopgmodule.c index 387d3d74..cb2e71fb 100644 --- a/psycopg/psycopgmodule.c +++ b/psycopg/psycopgmodule.c @@ -484,7 +484,11 @@ init_psycopg(void) /* import python builtin datetime module, if available */ #ifdef HAVE_PYDATETIME pyDateTimeModuleP = PyImport_ImportModule("datetime"); - + if (pyDateTimeModuleP == NULL) { + Dprintf("initpsycopg: can't import datetime module"); + PyErr_SetString(PyExc_ImportError, "can't import datetime module"); + return; + } pydatetimeType.ob_type = &PyType_Type; if (PyType_Ready(&pydatetimeType) == -1) return; @@ -498,6 +502,11 @@ init_psycopg(void) /* import psycopg2.tz anyway (TODO: replace with C-level module?) */ pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz"); + if (pyPsycopgTzModule == NULL) { + Dprintf("initpsycopg: can't import psycopg2.tz module"); + PyErr_SetString(PyExc_ImportError, "can't import psycopg2.tz module"); + return; + } pyPsycopgTzLOCAL = PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL"); pyPsycopgTzFixedOffsetTimezone =