1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
|
/* psycopgmodule.c - psycopg module (will import other C classes)
*
* Copyright (C) 2003 Federico Di Gregorio <fog@debian.org>
*
* This file is part of psycopg.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2,
* or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <Python.h>
#define PSYCOPG_MODULE
#include "psycopg/config.h"
#include "psycopg/python.h"
#include "psycopg/psycopg.h"
#include "psycopg/connection.h"
#include "psycopg/cursor.h"
#include "psycopg/typecast.h"
#include "psycopg/microprotocols.h"
#include "psycopg/microprotocols_proto.h"
#include "psycopg/adapter_qstring.h"
#include "psycopg/adapter_binary.h"
#include "psycopg/adapter_pboolean.h"
#include "psycopg/adapter_asis.h"
#include "psycopg/adapter_list.h"
#include "psycopg/typecast_binary.h"
#ifdef HAVE_MXDATETIME
#include <mxDateTime.h>
#include "psycopg/adapter_mxdatetime.h"
mxDateTimeModule_APIObject *mxDateTimeP = NULL;
#endif
/* some module-level variables, like the datetime module */
#ifdef HAVE_PYDATETIME
#include <datetime.h>
#include "psycopg/adapter_datetime.h"
PyObject *pyDateTimeModuleP = NULL;
PyObject *pyDateTypeP = NULL;
PyObject *pyTimeTypeP = NULL;
PyObject *pyDateTimeTypeP = NULL;
PyObject *pyDeltaTypeP = NULL;
#endif
/* pointers to the psycopg.tz classes */
PyObject *pyPsycopgTzModule = NULL;
PyObject *pyPsycopgTzLOCAL = NULL;
PyObject *pyPsycopgTzFixedOffsetTimezone = NULL;
PyObject *psycoEncodings = NULL;
PyObject *decimalType = NULL;
/** connect module-level function **/
#define psyco_connect_doc \
"connect(dsn, ...) -> new connection object\n\n" \
"This function supports two different but equivalent sets of arguments.\n" \
"A single data source name or 'dsn' string can be used to specify the\n" \
"connection parameters, as follows:\n\n" \
" psycopg2.connect(\"dbname=xxx user=xxx ...\")\n\n" \
"If 'dsn' is not provided it is possible to pass the parameters as\n" \
"keyword arguments; e.g.,\n\n" \
" psycopg2.connect(database='xxx', user='xxx', ...)\n\n" \
"The full list of available parameters is:\n\n" \
" dbname -- database name (only in 'dsn')\n" \
" database -- database name (only as keyword argument)\n" \
" host -- host address (defaults to UNIX socket if not provided)\n" \
" port -- port number (defaults to 5432 if not provided)\n" \
" user -- user name used to authenticate\n" \
" password -- password used to authenticate\n" \
" sslmode -- SSL mode (see PostgreSQL documentation)\n\n" \
"If the 'connection_factory' keyword argument is not provided this\n" \
"function always return an instance of the 'psycopg2.connection' class.\n" \
"Else the given sub-class of 'psycopg2.connection' will be used to\n" \
"instantiate the connection object.\n"
static int
_psyco_connect_fill_dsn(char *dsn, char *kw, char *v, int i)
{
strcpy(&dsn[i], kw); i += strlen(kw);
strcpy(&dsn[i], v); i += strlen(v);
return i;
}
static void
_psyco_connect_fill_exc(connectionObject *conn)
{
/* fill the connection object with the exceptions */
conn->exc_Error = Error;
Py_INCREF(Error);
conn->exc_Warning = Warning;
Py_INCREF(Warning);
conn->exc_InterfaceError = Error;
Py_INCREF(InterfaceError);
conn->exc_DatabaseError = Error;
Py_INCREF(DatabaseError);
conn->exc_InternalError = Error;
Py_INCREF(InternalError);
conn->exc_ProgrammingError = Error;
Py_INCREF(ProgrammingError);
conn->exc_IntegrityError = Error;
Py_INCREF(IntegrityError);
conn->exc_DataError = Error;
Py_INCREF(DataError);
conn->exc_NotSupportedError = NotSupportedError;
Py_INCREF(NotSupportedError);
}
static PyObject *
psyco_connect(PyObject *self, PyObject *args, PyObject *keywds)
{
PyObject *conn, *factory = NULL;
int idsn=-1, iport=-1;
char *dsn=NULL, *database=NULL, *user=NULL, *password=NULL;
char *host=NULL, *sslmode=NULL;
char port[16];
static char *kwlist[] = {"dsn", "database", "host", "port",
"user", "password", "sslmode",
"connection_factory", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "|sssisssO", kwlist,
&dsn, &database, &host, &iport,
&user, &password, &sslmode, &factory)) {
return NULL;
}
if (iport > 0)
snprintf(port, 16, "%d", iport);
if (dsn == NULL) {
int l = 36; /* len("dbname= user= password= host= port=\0") */
if (database) l += strlen(database);
if (host) l += strlen(host);
if (iport > 0) l += strlen(port);
if (user) l += strlen(user);
if (password) l += strlen(password);
if (sslmode) l += strlen(sslmode);
dsn = malloc(l*sizeof(char));
if (dsn == NULL) {
PyErr_SetString(InterfaceError, "dynamic dsn allocation failed");
return NULL;
}
idsn = 0;
if (database)
idsn = _psyco_connect_fill_dsn(dsn, " dbname=", database, idsn);
if (host)
idsn = _psyco_connect_fill_dsn(dsn, " host=", host, idsn);
if (iport > 0)
idsn = _psyco_connect_fill_dsn(dsn, " port=", port, idsn);
if (user)
idsn = _psyco_connect_fill_dsn(dsn, " user=", user, idsn);
if (password)
idsn = _psyco_connect_fill_dsn(dsn, " password=", password, idsn);
if (sslmode)
idsn = _psyco_connect_fill_dsn(dsn, " sslmode=", sslmode, idsn);
if (idsn > 0) {
dsn[idsn] = '\0';
memmove(dsn, &dsn[1], idsn);
}
else {
free(dsn);
PyErr_SetString(InterfaceError, "missing dsn and no parameters");
return NULL;
}
}
Dprintf("psyco_connect: dsn = '%s'", dsn);
/* allocate connection, fill with errors and return it */
if (factory == NULL) factory = (PyObject *)&connectionType;
conn = PyObject_CallFunction(factory, "s", dsn);
if (conn) _psyco_connect_fill_exc((connectionObject*)conn);
return conn;
}
/** type registration **/
#define psyco_register_type_doc \
"register_type(obj) -> register obj with psycopg type system"
static PyObject *
psyco_register_type(PyObject *self, PyObject *args)
{
PyObject *type;
if (!PyArg_ParseTuple(args, "O!", &typecastType, &type)) {
return NULL;
}
typecast_add(type, 0);
Py_INCREF(Py_None);
return Py_None;
}
/* default adapters */
static void
psyco_adapters_init(PyObject *mod)
{
PyObject *call;
microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&asisType);
microprotocols_add(&PyInt_Type, NULL, (PyObject*)&asisType);
microprotocols_add(&PyLong_Type, NULL, (PyObject*)&asisType);
microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType);
microprotocols_add(&PyUnicode_Type, NULL, (PyObject*)&qstringType);
microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType);
microprotocols_add(&PyList_Type, NULL, (PyObject*)&listType);
#ifdef HAVE_MXDATETIME
/* the module has already been initialized, so we can obtain the callable
objects directly from its dictionary :) */
call = PyMapping_GetItemString(mod, "TimestampFromMx");
microprotocols_add(mxDateTimeP->DateTime_Type, NULL, call);
call = PyMapping_GetItemString(mod, "TimeFromMx");
microprotocols_add(mxDateTimeP->DateTimeDelta_Type, NULL, call);
#endif
#ifdef HAVE_PYDATETIME
/* as above, we use the callable objects from the psycopg module */
call = PyMapping_GetItemString(mod, "DateFromPy");
microprotocols_add((PyTypeObject*)pyDateTypeP, NULL, call);
call = PyMapping_GetItemString(mod, "TimeFromPy");
microprotocols_add((PyTypeObject*)pyTimeTypeP, NULL, call);
call = PyMapping_GetItemString(mod, "TimestampFromPy");
microprotocols_add((PyTypeObject*)pyDateTimeTypeP, NULL, call);
call = PyMapping_GetItemString(mod, "IntervalFromPy");
microprotocols_add((PyTypeObject*)pyDeltaTypeP, NULL, call);
#endif
#ifdef HAVE_PYBOOL
microprotocols_add(&PyBool_Type, NULL, (PyObject*)&pbooleanType);
#endif
#ifdef HAVE_DECIMAL
microprotocols_add((PyTypeObject*)decimalType, NULL, (PyObject*)&asisType);
#endif
}
/* psyco_encodings_fill
Fill the module's postgresql<->python encoding table */
static encodingPair encodings[] = {
{"SQL_ASCII", "ascii"},
{"LATIN1", "latin_1"},
{"UNICODE", "utf_8"},
{"UTF8", "utf_8"},
/* some compatibility stuff */
{"latin-1", "latin_1"},
{NULL, NULL}
};
static void psyco_encodings_fill(PyObject *dict)
{
encodingPair *enc;
for (enc = encodings; enc->pgenc != NULL; enc++) {
PyObject *value = PyString_FromString(enc->pyenc);
PyDict_SetItemString(dict, enc->pgenc, value);
Py_DECREF(value);
}
}
/* psyco_errors_init, psyco_errors_fill (callable from C)
Initialize the module's exceptions and after that a dictionary with a full
set of exceptions. */
PyObject *Error, *Warning, *InterfaceError, *DatabaseError,
*InternalError, *OperationalError, *ProgrammingError,
*IntegrityError, *DataError, *NotSupportedError;
static void
psyco_errors_init(void)
{
Error = PyErr_NewException("psycopg.Error", PyExc_StandardError, NULL);
Warning = PyErr_NewException("psycopg.Warning", PyExc_StandardError,NULL);
InterfaceError = PyErr_NewException("psycopg.InterfaceError", Error, NULL);
DatabaseError = PyErr_NewException("psycopg.DatabaseError", Error, NULL);
InternalError =
PyErr_NewException("psycopg.InternalError", DatabaseError, NULL);
OperationalError =
PyErr_NewException("psycopg.OperationalError", DatabaseError, NULL);
ProgrammingError =
PyErr_NewException("psycopg.ProgrammingError", DatabaseError, NULL);
IntegrityError =
PyErr_NewException("psycopg.IntegrityError", DatabaseError,NULL);
DataError =
PyErr_NewException("psycopg.DataError", DatabaseError, NULL);
NotSupportedError =
PyErr_NewException("psycopg.NotSupportedError", DatabaseError, NULL);
}
void
psyco_errors_fill(PyObject *dict)
{
PyDict_SetItemString(dict, "Error", Error);
PyDict_SetItemString(dict, "Warning", Warning);
PyDict_SetItemString(dict, "InterfaceError", InterfaceError);
PyDict_SetItemString(dict, "DatabaseError", DatabaseError);
PyDict_SetItemString(dict, "InternalError", InternalError);
PyDict_SetItemString(dict, "OperationalError", OperationalError);
PyDict_SetItemString(dict, "ProgrammingError", ProgrammingError);
PyDict_SetItemString(dict, "IntegrityError", IntegrityError);
PyDict_SetItemString(dict, "DataError", DataError);
PyDict_SetItemString(dict, "NotSupportedError", NotSupportedError);
}
void
psyco_errors_set(PyObject *type)
{
PyObject_SetAttrString(type, "Error", Error);
PyObject_SetAttrString(type, "Warning", Warning);
PyObject_SetAttrString(type, "InterfaceError", InterfaceError);
PyObject_SetAttrString(type, "DatabaseError", DatabaseError);
PyObject_SetAttrString(type, "InternalError", InternalError);
PyObject_SetAttrString(type, "OperationalError", OperationalError);
PyObject_SetAttrString(type, "ProgrammingError", ProgrammingError);
PyObject_SetAttrString(type, "IntegrityError", IntegrityError);
PyObject_SetAttrString(type, "DataError", DataError);
PyObject_SetAttrString(type, "NotSupportedError", NotSupportedError);
}
/* psyco_decimal_init
Initialize the module's pointer to the decimal type. */
void
psyco_decimal_init(void)
{
#ifdef HAVE_DECIMAL
PyObject *decimal = PyImport_ImportModule("decimal");
if (decimal) {
decimalType = PyObject_GetAttrString(decimal, "Decimal");
}
#endif
}
/** method table and module initialization **/
static PyMethodDef psycopgMethods[] = {
{"connect", (PyCFunction)psyco_connect,
METH_VARARGS|METH_KEYWORDS, psyco_connect_doc},
{"adapt", (PyCFunction)psyco_microprotocols_adapt,
METH_VARARGS, psyco_microprotocols_adapt_doc},
{"register_type", (PyCFunction)psyco_register_type,
METH_VARARGS, psyco_register_type_doc},
{"new_type", (PyCFunction)typecast_from_python,
METH_VARARGS|METH_KEYWORDS},
{"AsIs", (PyCFunction)psyco_AsIs,
METH_VARARGS, psyco_AsIs_doc},
{"QuotedString", (PyCFunction)psyco_QuotedString,
METH_VARARGS, psyco_QuotedString_doc},
{"Boolean", (PyCFunction)psyco_Boolean,
METH_VARARGS, psyco_Boolean_doc},
{"Binary", (PyCFunction)psyco_Binary,
METH_VARARGS, psyco_Binary_doc},
{"Date", (PyCFunction)psyco_Date,
METH_VARARGS, psyco_Date_doc},
{"Time", (PyCFunction)psyco_Time,
METH_VARARGS, psyco_Time_doc},
{"Timestamp", (PyCFunction)psyco_Timestamp,
METH_VARARGS, psyco_Timestamp_doc},
{"DateFromTicks", (PyCFunction)psyco_DateFromTicks,
METH_VARARGS, psyco_DateFromTicks_doc},
{"TimeFromTicks", (PyCFunction)psyco_TimeFromTicks,
METH_VARARGS, psyco_TimeFromTicks_doc},
{"TimestampFromTicks", (PyCFunction)psyco_TimestampFromTicks,
METH_VARARGS, psyco_TimestampFromTicks_doc},
{"List", (PyCFunction)psyco_List,
METH_VARARGS, psyco_List_doc},
#ifdef HAVE_MXDATETIME
{"DateFromMx", (PyCFunction)psyco_DateFromMx,
METH_VARARGS, psyco_DateFromMx_doc},
{"TimeFromMx", (PyCFunction)psyco_TimeFromMx,
METH_VARARGS, psyco_TimeFromMx_doc},
{"TimestampFromMx", (PyCFunction)psyco_TimestampFromMx,
METH_VARARGS, psyco_TimestampFromMx_doc},
{"IntervalFromMx", (PyCFunction)psyco_IntervalFromMx,
METH_VARARGS, psyco_IntervalFromMx_doc},
#endif
#ifdef HAVE_PYDATETIME
{"DateFromPy", (PyCFunction)psyco_DateFromPy,
METH_VARARGS, psyco_DateFromPy_doc},
{"TimeFromPy", (PyCFunction)psyco_TimeFromPy,
METH_VARARGS, psyco_TimeFromPy_doc},
{"TimestampFromPy", (PyCFunction)psyco_TimestampFromPy,
METH_VARARGS, psyco_TimestampFromPy_doc},
{"IntervalFromPy", (PyCFunction)psyco_IntervalFromPy,
METH_VARARGS, psyco_IntervalFromPy_doc},
#endif
{NULL, NULL, 0, NULL} /* Sentinel */
};
PyMODINIT_FUNC
init_psycopg(void)
{
static void *PSYCOPG_API[PSYCOPG_API_pointers];
PyObject *module, *dict;
PyObject *c_api_object;
Dprintf("initpsycopg: initializing psycopg %s", PSYCOPG_VERSION);
/* initialize all the new types and then the module */
connectionType.ob_type = &PyType_Type;
cursorType.ob_type = &PyType_Type;
typecastType.ob_type = &PyType_Type;
qstringType.ob_type = &PyType_Type;
binaryType.ob_type = &PyType_Type;
isqlquoteType.ob_type = &PyType_Type;
asisType.ob_type = &PyType_Type;
listType.ob_type = &PyType_Type;
chunkType.ob_type = &PyType_Type;
if (PyType_Ready(&connectionType) == -1) return;
if (PyType_Ready(&cursorType) == -1) return;
if (PyType_Ready(&typecastType) == -1) return;
if (PyType_Ready(&qstringType) == -1) return;
if (PyType_Ready(&binaryType) == -1) return;
if (PyType_Ready(&isqlquoteType) == -1) return;
if (PyType_Ready(&asisType) == -1) return;
if (PyType_Ready(&listType) == -1) return;
if (PyType_Ready(&chunkType) == -1) return;
#ifdef HAVE_PYBOOL
pbooleanType.ob_type = &PyType_Type;
if (PyType_Ready(&pbooleanType) == -1) return;
#endif
/* import mx.DateTime module, if necessary */
#ifdef HAVE_MXDATETIME
mxdatetimeType.ob_type = &PyType_Type;
if (PyType_Ready(&mxdatetimeType) == -1) return;
if (mxDateTime_ImportModuleAndAPI() != 0) {
Dprintf("initpsycopg: why marc hide mx.DateTime again?!");
PyErr_SetString(PyExc_ImportError, "can't import mx.DateTime module");
return;
}
mxDateTimeP = &mxDateTime;
#endif
/* import python builtin datetime module, if available */
#ifdef HAVE_PYDATETIME
pyDateTimeModuleP = PyImport_ImportModule("datetime");
pydatetimeType.ob_type = &PyType_Type;
if (PyType_Ready(&pydatetimeType) == -1) return;
/* now we define the datetime types, this is crazy because python should
be doing that, not us! */
pyDateTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "date");
pyTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "time");
pyDateTimeTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "datetime");
pyDeltaTypeP = PyObject_GetAttrString(pyDateTimeModuleP, "timedelta");
#endif
/* import psycopg2.tz anyway (TODO: replace with C-level module?) */
pyPsycopgTzModule = PyImport_ImportModule("psycopg2.tz");
pyPsycopgTzLOCAL =
PyObject_GetAttrString(pyPsycopgTzModule, "LOCAL");
pyPsycopgTzFixedOffsetTimezone =
PyObject_GetAttrString(pyPsycopgTzModule, "FixedOffsetTimezone");
/* initialize the module and grab module's dictionary */
module = Py_InitModule("_psycopg", psycopgMethods);
dict = PyModule_GetDict(module);
/* initialize all the module's exported functions */
/* PyBoxer_API[PyBoxer_Fake_NUM] = (void *)PyBoxer_Fake; */
/* Create a CObject containing the API pointer array's address */
c_api_object = PyCObject_FromVoidPtr((void *)PSYCOPG_API, NULL);
if (c_api_object != NULL)
PyModule_AddObject(module, "_C_API", c_api_object);
/* other mixed initializations of module-level variables */
psycoEncodings = PyDict_New();
psyco_encodings_fill(psycoEncodings);
psyco_decimal_init();
/* set some module's parameters */
PyModule_AddStringConstant(module, "__version__", PSYCOPG_VERSION);
PyModule_AddStringConstant(module, "__doc__", "psycopg PostgreSQL driver");
PyModule_AddObject(module, "apilevel", PyString_FromString(APILEVEL));
PyModule_AddObject(module, "threadsafety", PyInt_FromLong(THREADSAFETY));
PyModule_AddObject(module, "paramstyle", PyString_FromString(PARAMSTYLE));
/* put new types in module dictionary */
PyModule_AddObject(module, "connection", (PyObject*)&connectionType);
PyModule_AddObject(module, "cursor", (PyObject*)&cursorType);
PyModule_AddObject(module, "ISQLQuote", (PyObject*)&isqlquoteType);
/* encodings dictionary in module dictionary */
PyModule_AddObject(module, "encodings", psycoEncodings);
/* initialize default set of typecasters */
typecast_init(dict);
/* initialize microprotocols layer */
microprotocols_init(dict);
psyco_adapters_init(dict);
/* create a standard set of exceptions and add them to the module's dict */
psyco_errors_init();
psyco_errors_fill(dict);
/* Solve win32 build issue about non-constant initializer element */
cursorType.tp_alloc = PyType_GenericAlloc;
binaryType.tp_alloc = PyType_GenericAlloc;
isqlquoteType.tp_alloc = PyType_GenericAlloc;
pbooleanType.tp_alloc = PyType_GenericAlloc;
connectionType.tp_alloc = PyType_GenericAlloc;
asisType.tp_alloc = PyType_GenericAlloc;
qstringType.tp_alloc = PyType_GenericAlloc;
listType.tp_alloc = PyType_GenericAlloc;
chunkType.tp_alloc = PyType_GenericAlloc;
#ifdef HAVE_PYDATETIME
pydatetimeType.tp_alloc = PyType_GenericAlloc;
#endif
Dprintf("initpsycopg: module initialization complete");
}
|