File: _kinterbasdb.c

package info (click to toggle)
python-kinterbasdb 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,432 kB
  • ctags: 1,830
  • sloc: ansic: 16,803; python: 3,514; makefile: 63; sh: 22
file content (843 lines) | stat: -rw-r--r-- 30,925 bytes parent folder | download | duplicates (2)
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
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
/* KInterbasDB Python Package - Implementation of Core
 *
 * Version 3.3
 *
 * The following contributors hold Copyright (C) over their respective
 * portions of code (see license.txt for details):
 *
 * [Original Author (maintained through version 2.0-0.3.1):]
 *   1998-2001 [alex]  Alexander Kuznetsov   <alexan@users.sourceforge.net>
 * [Maintainers (after version 2.0-0.3.1):]
 *   2001-2002 [maz]   Marek Isalski         <kinterbasdb@maz.nu>
 *   2002-2007 [dsr]   David Rushby          <woodsplitter@rocketmail.com>
 * [Contributors:]
 *   2001      [eac]   Evgeny A. Cherkashin  <eugeneai@icc.ru>
 *   2001-2002 [janez] Janez Jere            <janez.jere@void.si>
 */

#include "_kinterbasdb.h"

#if (defined(ENABLE_DB_EVENT_SUPPORT) || defined(ENABLE_CONNECTION_TIMEOUT))
  #include "_kisupport.h"
  #include "_kisupport_platform.c"
#endif

#ifdef ENABLE_DB_EVENT_SUPPORT
  #ifndef ENABLE_CONCURRENCY
    /* setup.py should've already enforced this: */
    #error "ENABLE_CONCURRENCY is required for ENABLE_DB_EVENT_SUPPORT."
  #endif

  #include "_kievents.h"
#endif

#include "_kilock.h"

/****************** "PRIVATE" DECLARATIONS:BEGIN *******************/
static int Connection_require_open(CConnection *self, char *failure_message);
static int CConnection_clear_ps_description_tuples(CConnection *);
/****************** "PRIVATE" DECLARATIONS:END *******************/

/******************** GLOBAL VARIABLES:BEGIN ********************/
/* These min/max constants are initialized in init_kinterbasdb, and retained
 * throughout the life of the process. */
static PyObject *py_SHRT_MIN = NULL;
static PyObject *py_SHRT_MAX = NULL;

static PyObject *py_INT_MIN = NULL;
static PyObject *py_INT_MAX = NULL;

static PyObject *py_LONG_MIN = NULL;
static PyObject *py_LONG_MAX = NULL;

static PyObject *py_LONG_LONG_MIN = NULL;
static PyObject *py_LONG_LONG_MAX = NULL;

static PyObject *py_PY_SSIZE_T_MIN = NULL;
static PyObject *py_PY_SSIZE_T_MAX = NULL;

/* Global references to this module's exception type objects, as documented in
 * the Python DB API (initialized by init_kidb_exceptions). */
static PyObject *Warning                   = NULL;
static PyObject *Error                     = NULL;
static PyObject *InterfaceError            = NULL;
static PyObject *DatabaseError             = NULL;
static PyObject *DataError                 = NULL;
static PyObject *OperationalError          = NULL;
static PyObject *TransactionConflict       = NULL;
#ifdef ENABLE_DB_EVENT_SUPPORT
  static PyObject *ConduitWasClosed        = NULL;
#endif
#ifdef ENABLE_CONNECTION_TIMEOUT
  static PyObject *ConnectionTimedOut      = NULL;
#endif
static PyObject *IntegrityError            = NULL;
static PyObject *InternalError             = NULL;
static PyObject *ProgrammingError          = NULL;
static PyObject *NotSupportedError         = NULL;


/* 2005.11.04: */
static int global_concurrency_level = UNKNOWN_CONCURRENCY_LEVEL;
#ifdef ENABLE_CONCURRENCY
  PyThread_type_lock _global_db_client_lock = NULL;
  /* _global_db_client_lock is not a PyObject *, but we need to pass it to the
   * _kiservices module through Python, so we wrap it in a PyCObject: */
  PyObject *_global_db_client_lock__python_Wrapper = NULL;
#endif

/* Reference to an object of ConnectionType that is used as a strongly typed
 * None: */
static CConnection *null_connection;

/* This Python callable is used by the Cursor implementation to transform
 * ordinary fetch* rows into fetch*map rows. */
static PyObject *py_RowMapping_constructor = NULL;
static PyObject *py__make_output_translator_return_type_dict_from_trans_dict = NULL;
static PyObject *py_look_up_array_descriptor = NULL;
static PyObject *py_look_up_array_subtype = NULL;
static PyObject *pyob_Cursor_execute_exception_type_filter = NULL;
static PyObject *pyob_validate_tpb = NULL;
static PyObject *pyob_trans_info = NULL;

/* These Python string objects are created when the module loads, then used by
 * multiple subsystems throughout the life of the process: */
static PyObject *shared___s__C_con; /* _C_con */
static PyObject *shared___s__main_trans; /* _main_trans */
static PyObject *shared___s_ascii; /* ascii (canonical name of ASCII codec) */
static PyObject *shared___s_charset; /* _charset */
static PyObject *shared___s_DB_CHAR_SET_NAME_TO_PYTHON_ENCODING_MAP;
static PyObject *shared___s_execute_immediate; /* execute_immediate */
static PyObject *shared___s_strict; /* strict */

/******************** GLOBAL VARIABLES:END ********************/

/******************** IMPLEMENTATION:BEGIN ********************/
PyObject *pyob_TrackerToList(AnyTracker *tracker);

/* CConnection-related: */
#ifdef ENABLE_CONNECTION_TIMEOUT
  static int Connection_close_from_CTT(volatile CConnection *con);

  /* When a Connection times out, it cuts loose certain terminal objects
   * (BlobReaders and PreparedStatements) which cannot under any circumstance
   * be transparently resumed.  An attempt by the client programmer to use
   * such an object should raise ConnectionTimedOut rather than
   * ProgrammingError.  To implement that, we copy pointers to all BlobReaders
   * and PreparedStatements attached to a Connection before we attempt to time
   * the Connection out.  After timing it out, we then update the state
   * indicator of the non-resumable objects so that they can raise
   * ConnectionTimedOut if the client programmer attempts to use them again. */
  static BlobReader **Connection_copy_BlobReader_pointers(
      volatile CConnection *con, Py_ssize_t *count
    );
  static void Connection_former_BlobReaders_flag_timeout_and_free(
      BlobReader **blob_readers, Py_ssize_t count
    );

  #define Connection_former_BlobReader_pointers_free(blob_readers) \
    kimem_main_free(blob_readers)

  static PreparedStatement **Connection_copy_PreparedStatement_pointers(
      volatile CConnection *con, Py_ssize_t *count
    );
  static void Connection_former_PreparedStatements_flag_timeout_and_free(
      PreparedStatement **prepared_statements, Py_ssize_t count
    );
  #define Connection_former_PreparedStatement_pointers_free(prepared_statements) \
    kimem_main_free(prepared_statements)
#endif /* ENABLE_CONNECTION_TIMEOUT */

static int Connection_attach_from_members(CConnection *con
    #ifdef ENABLE_CONNECTION_TIMEOUT
      , struct _ConnectionTimeoutParams *tp
    #endif
  );
static PyObject *pyob_Connection_x_info(
    boolean for_isc_database_info, isc_tr_handle *trans_handle_p,
    PyObject *self, PyObject *args
  );
static boolean Connection_has_any_open_transaction(CConnection *con);

/* Transaction-related: */
boolean Transaction_is_main(Transaction *self) {
  assert (self != NULL);
  assert (self->con != NULL);
  return (self->con->main_trans == self);
} /* Transaction_is_main */
static PyObject *pyob_Transaction_get_default_tpb(Transaction *self);

typedef enum {
  OP_COMMIT   =  1,
  OP_ROLLBACK =  0
} WhichTransactionOperation;

typedef enum {
  OP_RESULT_OK     =   0,
  OP_RESULT_ERROR  =  -1
} TransactionalOperationResult;

static void Transaction_dist_trans_indicate_resultion(
    Transaction *self, PyObject *group, const boolean is_resolved
  );
static int change_resolution_of_all_con_main_trans(
    PyObject *group, PyObject *cons, const boolean is_resolved
  );

static TransactionalOperationResult Transaction_commit_or_rollback(
    const WhichTransactionOperation op, Transaction *self,
    const boolean retaining, const boolean allowed_to_raise
  );

/* BlobReader-related: */
static int BlobReader_untrack(BlobReader *, boolean);

/* These streaming blob support functions are declared here rather than in
 * _kinterbasdb.h so as to avoid compiler warnings that they're "declared but
 * never defined" when the _kiservices compilation unit is built. */
static int validate_nonstandard_blob_config_dict(PyObject *,
    BlobMode *, boolean *
  );
static int BlobReaderTracker_release(BlobReaderTracker **);

static boolean _check_statement_length(Py_ssize_t);

static PyObject *pyob_Cursor_close(Cursor *self);
static int _Cursor_require_open(Cursor *self, char *failure_message);
static int Cursor_clear(Cursor *, boolean);
static void Cursor_clear_and_leave_open(Cursor *);
static int Cursor_ensure_PSCache(Cursor *self);
static int Cursor_close_prepared_statements(Cursor *self,
    const boolean allowed_to_raise, const boolean clear_ps_superior_refs
  );
static int Cursor_close_without_unlink(Cursor *, boolean);
static int Cursor_close_with_unlink(Cursor *, boolean);

static int CursorTracker_release(CursorTracker **);

typedef int (* CursorTrackerMappedFunction)(CursorTracker *, CursorTracker *);
static int Cursor_clear_ps_description_tuples(Cursor *);

typedef int (* PSTrackerMappedFunction)(PSTracker *, PSTracker *);
static int PSTracker_traverse(PSTracker *, PSTrackerMappedFunction);

/* This header file needs to be included whether connection timeout is enabled
 * or not, because (potentially empty versions of) some macros must be
 * provided: */
#include "_kicore_connection_timeout.h"

#include "_kisupport_time.c"
#include "_kinterbasdb_exception_functions.c"

#include "_kicore_transaction_support.c"
#include "_kicore_transaction_distributed.c"
#include "_kicore_transaction.c"
#include "_kiconversion.c"

#ifdef ENABLE_CONNECTION_TIMEOUT
  static TransactionalOperationResult
    Connection_resolve_all_transactions_from_CTT(
      volatile CConnection *con, const WhichTransactionOperation trans_op
    );

  #include "_kicore_connection_timeout.c"
#endif
#include "_kicore_connection.c"

#include "_kicore_xsqlda.c"
#include "_kicore_preparedstatement.c"
#include "_kicore_cursor.c"
#include "_kicore_create_drop_db.c"

#ifdef ENABLE_DB_EVENT_SUPPORT
  #include "_kinterbasdb_exception_functions_without_python.c"
  #include "_kievents.c"
#endif

static PyObject *pyob_provide_refs_to_python_entities(
    PyObject *self, PyObject *args
  )
{
  if (!PyArg_ParseTuple(args, "OOOOOOO",
       &py_RowMapping_constructor,
       &py__make_output_translator_return_type_dict_from_trans_dict,
       &py_look_up_array_descriptor,
       &py_look_up_array_subtype,
       &pyob_Cursor_execute_exception_type_filter,
       &pyob_validate_tpb,
       &pyob_trans_info
     ))
  { return NULL; }

  #define REQ_CALLABLE(py_func) \
    if (!PyCallable_Check(py_func)) { \
      raise_exception(InternalError, #py_func " is not callable."); \
      py_func = NULL; \
      return NULL; \
    }
  REQ_CALLABLE(py_RowMapping_constructor);
  REQ_CALLABLE(py__make_output_translator_return_type_dict_from_trans_dict);
  REQ_CALLABLE(py_look_up_array_descriptor);
  REQ_CALLABLE(py_look_up_array_subtype);
  REQ_CALLABLE(pyob_Cursor_execute_exception_type_filter);
  REQ_CALLABLE(pyob_validate_tpb);
  REQ_CALLABLE(pyob_trans_info);

  /* We need these objects to live forever: */
  Py_INCREF(py_RowMapping_constructor);
  Py_INCREF(py__make_output_translator_return_type_dict_from_trans_dict);
  Py_INCREF(py_look_up_array_descriptor);
  Py_INCREF(py_look_up_array_subtype);
  Py_INCREF(pyob_Cursor_execute_exception_type_filter);
  Py_INCREF(pyob_validate_tpb);
  Py_INCREF(pyob_trans_info);

  RETURN_PY_NONE;
} /* pyob_provide_refs_to_python_entities */

static PyObject *pyob_concurrency_level_get(PyObject *self) {
  if (global_concurrency_level == UNKNOWN_CONCURRENCY_LEVEL) {
    raise_exception(ProgrammingError, "The concurrency level has not been set;"
        " that must be done explicitly or implicitly via the"
        " kinterbasdb.init function."
      );
    return NULL;
  }

  return PyInt_FromLong(global_concurrency_level);
} /* pyob_concurrency_level_get */

static PyObject *pyob_concurrency_level_set(PyObject *self, PyObject *args) {
  int tentative_level;
  if (!PyArg_ParseTuple(args, "i", &tentative_level)) { return NULL; }

  if (global_concurrency_level != UNKNOWN_CONCURRENCY_LEVEL) {
    raise_exception(ProgrammingError, "The concurrency level cannot be changed"
        " once it has been set.  Use kinterbasdb.init(concurrency_level=?) to"
        " set the concurrency level legally."
      );
    return NULL;
  }

  #ifndef ENABLE_CONCURRENCY
    if (tentative_level != 0) {
  #else
    if (tentative_level != 1 && tentative_level != 2) {
  #endif
      raise_exception(ProgrammingError, "Illegal concurrency level.");
      return NULL;
    }

  global_concurrency_level = tentative_level;

  RETURN_PY_NONE;
} /* pyob_concurrency_level_set */

#ifdef ENABLE_CONCURRENCY
static PyObject *pyob_Thread_current_id(PyObject *self) {
  LONG_LONG thread_id = (LONG_LONG) Thread_current_id();
  return PythonIntOrLongFrom64BitValue(thread_id);
} /* pyob_Thread_current_id */
#endif

static PyObject *pyob_isc_portable_integer(PyObject *self, PyObject *args) {
  char *raw_bytes;
  Py_ssize_t raw_len;
  LONG_LONG result;

  if (!PyArg_ParseTuple(args, "s#", &raw_bytes, &raw_len)) { goto fail; }

  if (raw_len != 8 && raw_len != 4 && raw_len != 2 && raw_len != 1) {
    raise_exception(InternalError,
        "pyob_isc_portable_integer: len(buf) must be in (1,2,4,8)"
      );
    goto fail;
  }

  ENTER_GDAL
  result = isc_portable_integer(
      #ifndef INTERBASE_7_OR_LATER
        (unsigned char *)
      #endif
      raw_bytes,
      (short) raw_len /* Cast is safe b/c already checked val. */
    );
  LEAVE_GDAL

  return PythonIntOrLongFrom64BitValue(result);

  fail:
    assert (PyErr_Occurred());
    return NULL;
} /* pyob_isc_portable_integer */

static PyObject *pyob_raw_timestamp_to_tuple(PyObject *self, PyObject *args) {
  char *raw_bytes;
  Py_ssize_t raw_len;

  if (!PyArg_ParseTuple(args, "s#", &raw_bytes, &raw_len)) { return NULL; }

  if (raw_len != 8) {
    raise_exception(ProgrammingError, "raw_timestamp_to_tuple argument must"
        " be str of length 8."
      );
    return NULL;
  }

  return conv_out_timestamp(raw_bytes);
} /* pyob_raw_timestamp_to_tuple */

/******************** IMPLEMENTATION:END ********************/

/********************** MODULE INFRASTRUCTURE:BEGIN ***********************/
static PyObject *init_kidb_basic_header_constants(PyObject *, PyObject *);

static int init_shared_string_constants(void) {
  #define INIT_SHARED_STRING_CONST(s) \
    shared___s_ ## s = PyString_FromString(#s); \
    if (shared___s_ ## s == NULL) { goto fail; }

  INIT_SHARED_STRING_CONST(_C_con);
  INIT_SHARED_STRING_CONST(_main_trans);
  INIT_SHARED_STRING_CONST(ascii);
  INIT_SHARED_STRING_CONST(charset);
  INIT_SHARED_STRING_CONST(DB_CHAR_SET_NAME_TO_PYTHON_ENCODING_MAP);
  INIT_SHARED_STRING_CONST(execute_immediate);
  INIT_SHARED_STRING_CONST(strict);

  return 0;

  fail:
    assert (PyErr_Occurred());
    return -1;
} /* init_shared_string_constants */

static PyMethodDef kinterbasdb_GlobalMethods[] = {
  { "init_kidb_basic_header_constants", init_kidb_basic_header_constants,
      METH_VARARGS
    },

  { "create_database",      pyob_create_database,         METH_VARARGS      },

 /*********** CConnection methods: ***********/
  { "Connection_drop_database",
                            pyob_Connection_drop_database,METH_VARARGS      },
  { "Connection_connect",   pyob_Connection_connect,      METH_VARARGS      },

  { "Connection_python_wrapper_obj_set",
    pyob_Connection_python_wrapper_obj_set,               METH_VARARGS      },

  { "Connection_close",     pyob_Connection_close,        METH_VARARGS      },

  { "Connection_database_info",
                            pyob_Connection_database_info,METH_VARARGS      },
  { "Connection_transaction_info",
                            pyob_Connection_transaction_info,
                                                          METH_VARARGS      },

  /* Dynamic type trans getters/setters for CConnection: */
  { "set_Connection_type_trans_out",
                            pyob_Connection_set_type_trans_out,
                                                          METH_VARARGS      },
  { "get_Connection_type_trans_out",
                            pyob_Connection_get_type_trans_out,
                                                          METH_VARARGS      },
  { "set_Connection_type_trans_in",
                            pyob_Connection_set_type_trans_in,
                                                          METH_VARARGS      },
  { "get_Connection_type_trans_in",
                            pyob_Connection_get_type_trans_in,
                                                          METH_VARARGS      },

  { "Connection_closed_get",pyob_Connection_closed_get,   METH_VARARGS      },

  #ifdef INTERBASE_6_OR_LATER
  { "Connection_dialect_get",
                            pyob_Connection_dialect_get,  METH_VARARGS      },
  { "Connection_dialect_set",
                            pyob_Connection_dialect_set,  METH_VARARGS      },
  #endif /* INTERBASE_6_OR_LATER */

  { "Connection_main_trans_get",
                            pyob_Connection_main_trans_get,
                                                          METH_VARARGS      },
  { "Connection_transactions_get",
                            pyob_Connection_transactions_get,
                                                          METH_VARARGS      },

  { "Connection_has_active_transaction",
                            pyob_Connection_has_active_transaction,
                                                          METH_VARARGS      },

  #ifdef ENABLE_DB_EVENT_SUPPORT
  { "EventConduit_create",  pyob_EventConduit_create,     METH_VARARGS      },
  #endif /* ENABLE_DB_EVENT_SUPPORT */

  /* Can apply to either a CConnection or a Cursor: */
  { "is_purportedly_open",  pyob_CursorOrConnection_is_purportedly_open,
      METH_VARARGS
    },

  /* StandaloneTransactionHandle methods: */
  { "distributed_begin",    pyob_distributed_begin,       METH_VARARGS      },
  { "distributed_prepare",  pyob_distributed_prepare,     METH_VARARGS      },
  { "distributed_commit",   pyob_distributed_commit,      METH_VARARGS      },
  { "distributed_rollback", pyob_distributed_rollback,    METH_VARARGS      },

  /* Module-level functions that allow the Python layer to manipulate certain
   * global variables within the C layer: */
  { "provide_refs_to_python_entities",
                            pyob_provide_refs_to_python_entities,
                                                          METH_VARARGS      },

  /* Concurrency level: */
  { "concurrency_level_get",(PyCFunction) pyob_concurrency_level_get,
                                                          METH_NOARGS       },
  { "concurrency_level_set", pyob_concurrency_level_set,  METH_VARARGS      },
  #ifdef ENABLE_CONCURRENCY
  { "thread_id",            (PyCFunction) pyob_Thread_current_id,
                                                          METH_NOARGS       },
  #endif

  #ifdef ENABLE_CONNECTION_TIMEOUT
  /* Connection timeouts: */
  { "ConnectionTimeoutThread_main",
                             pyob_ConnectionTimeoutThread_main,
                                                          METH_VARARGS      },
  { "CTM_halt",              (PyCFunction) pyob_CTM_halt, METH_NOARGS       },

  { "Connection__read_activity_stamps",
                             Connection__read_activity_stamps,
                                                          METH_VARARGS      },
  #endif /* ENABLE_CONNECTION_TIMEOUT */

  /* Connection_timeout_enabled should be available whether the feature is
   * compiled in or not: */
  { "Connection_timeout_enabled",
                             pyob_Connection_timeout_enabled,
                                                          METH_VARARGS      },

  /* General utility functions: */
  { "portable_int",          pyob_isc_portable_integer,    METH_VARARGS     },
  { "raw_timestamp_to_tuple",
                             pyob_raw_timestamp_to_tuple,  METH_VARARGS     },

  /* The end: */
  { NULL,                NULL                                               }
};

static int init_kidb_exceptions(PyObject *d) {
  /* Python provides no way to recover from errors encoutered during C
   * extension module import, so the error handling here is lame. */
  #define DEFINE_EXC(targetVar, superType) \
    targetVar = PyErr_NewException("kinterbasdb." #targetVar, superType, \
        NULL \
      ); \
    if (targetVar == NULL) { goto fail; } \
    if (PyDict_SetItemString(d, #targetVar, targetVar) != 0) { goto fail; }

  DEFINE_EXC(Warning, PyExc_StandardError);
  DEFINE_EXC(Error, PyExc_StandardError);
  DEFINE_EXC(InterfaceError, Error);
  DEFINE_EXC(DatabaseError, Error);
  DEFINE_EXC(DataError, DatabaseError);

  DEFINE_EXC(OperationalError, DatabaseError);
  DEFINE_EXC(TransactionConflict, OperationalError);

  #ifdef ENABLE_DB_EVENT_SUPPORT
    DEFINE_EXC(ConduitWasClosed, OperationalError);
  #endif

  #ifdef ENABLE_CONNECTION_TIMEOUT
    DEFINE_EXC(ConnectionTimedOut, OperationalError);
  #endif

  DEFINE_EXC(IntegrityError, DatabaseError);
  DEFINE_EXC(InternalError, DatabaseError);
  DEFINE_EXC(ProgrammingError, DatabaseError);
  DEFINE_EXC(NotSupportedError, DatabaseError);

  return 0;

  fail:
    /* Not much we do due to Python's feature-poor module initialization
     * infrastructure. */
    return -1;
} /* init_kidb_exceptions */

PyTypeObject ConnectionType = {
    PyObject_HEAD_INIT(NULL)

    0,
    "kinterbasdb.ConnectionType",
    sizeof(CConnection),
    0,
    (destructor) pyob_Connection___del__,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
  };

PyTypeObject BlobReaderType = { /* new-style class */
    PyObject_HEAD_INIT(NULL)
    0,                                  /* ob_size */
    "kinterbasdb.BlobReader",           /* tp_name */
    sizeof(BlobReader),                 /* tp_basicsize */
    0,                                  /* tp_itemsize */
    (destructor) pyob_BlobReader___del__,   /* tp_dealloc */
    0,                                  /* tp_print */
    0,                                  /* tp_getattr */
    0,                                  /* tp_setattr */
    0,                                  /* tp_compare */
    (reprfunc) pyob_BlobReader_repr,    /* tp_repr */
    0,                                  /* tp_as_number */
    0,                                  /* tp_as_sequence */
    0,                                  /* tp_as_mapping */
    0,                                  /* tp_hash */
    0,                                  /* tp_call */
    0,                                  /* tp_str */
    0,                                  /* tp_getattro */
    0,                                  /* tp_setattro */
    0,                                  /* tp_as_buffer */
    Py_TPFLAGS_DEFAULT,                 /* tp_flags */
    0,                                  /* tp_doc */
    0,		                              /* tp_traverse */
    0,		                              /* tp_clear */
    0,		                              /* tp_richcompare */
    0,		                              /* tp_weaklistoffset */
    0,		                              /* tp_iter */
    0,		                              /* tp_iternext */
    BlobReader_methods,                 /* tp_methods */
    NULL,                               /* tp_members */
    BlobReader_getters_setters,         /* tp_getset */
    0,                                  /* tp_base */
    0,                                  /* tp_dict */
    0,                                  /* tp_descr_get */
    0,                                  /* tp_descr_set */
    0,                                  /* tp_dictoffset */

    /* This type does not need to be instantiable from the Python level: */
    0,                                  /* tp_init */

    0,                                  /* tp_alloc */
    0,                                  /* tp_new */
  };

PyTypeObject StandaloneTransactionHandleType = {
    PyObject_HEAD_INIT(NULL)

    0,
    "kinterbasdb.StandaloneTransactionHandle",
    sizeof(StandaloneTransactionHandle),
    0,
    (destructor) StandaloneTransactionHandle___del__,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0
  };

DL_EXPORT(void)
init_kinterbasdb(void) {
  PyObject *m, *d;

  m = Py_InitModule("_kinterbasdb", kinterbasdb_GlobalMethods);
  if (m == NULL) { goto fail; }
  d = PyModule_GetDict(m);
  if (d == NULL) { goto fail; }

  ConnectionType.ob_type = &PyType_Type;
  CursorType.ob_type = &PyType_Type;
  StandaloneTransactionHandleType.ob_type = &PyType_Type;

  #ifdef VERBOSE_DEBUGGING
  if (PyModule_AddIntConstant(m, "VERBOSE_DEBUGGING", 1) != 0) { goto fail; }
  #endif
  if (PyModule_AddIntConstant(m, "FB_API_VER", FB_API_VER) != 0) {
    goto fail;
  }

  #ifdef ENABLE_CONCURRENCY
  {
    /* See documentation in _kilock.h */
    _global_db_client_lock = PyThread_allocate_lock();
    if (_global_db_client_lock == NULL) { goto fail; }

    _global_db_client_lock__python_Wrapper = PyCObject_FromVoidPtr(
        _global_db_client_lock,
        NULL /* No automatic collection. */
      );
    if (_global_db_client_lock__python_Wrapper == NULL) { goto fail; }

    PyObject_SetAttrString(m, "_global_db_client_lock__python_Wrapper",
        _global_db_client_lock__python_Wrapper
      );
  }
  #endif /* def ENABLE_CONCURRENCY */

  if (PyModule_AddIntConstant(m, "DEFAULT_CONCURRENCY_LEVEL",
        DEFAULT_CONCURRENCY_LEVEL
      ) != 0
    ) { goto fail; }

  /* Initialize module-global Python objects to hold min/max values for integer
   * types. */
  #define INITIALIZE_INT_BOUNDARY_CONST(name) \
    py_ ## name = PyInt_FromLong(name); \
    if (py_ ## name == NULL) { goto fail; }

  #define INITIALIZE_LONG_BOUNDARY_CONST(name) \
    py_ ## name = PyLong_FromLongLong(name); \
    if (py_ ## name == NULL) { goto fail; }

  #define INITIALIZE_PY_SSIZE_T_BOUNDARY_CONST(name) \
    py_ ## name = PyInt_FromSsize_t(name); \
    if (py_ ## name == NULL) { goto fail; }

  INITIALIZE_INT_BOUNDARY_CONST(SHRT_MIN);
  INITIALIZE_INT_BOUNDARY_CONST(SHRT_MAX);
  INITIALIZE_INT_BOUNDARY_CONST(INT_MIN);
  INITIALIZE_INT_BOUNDARY_CONST(INT_MAX);
  INITIALIZE_INT_BOUNDARY_CONST(LONG_MIN);
  INITIALIZE_INT_BOUNDARY_CONST(LONG_MAX);
  INITIALIZE_LONG_BOUNDARY_CONST(LONG_LONG_MIN);
  INITIALIZE_LONG_BOUNDARY_CONST(LONG_LONG_MAX);
  INITIALIZE_PY_SSIZE_T_BOUNDARY_CONST(PY_SSIZE_T_MIN);
  INITIALIZE_PY_SSIZE_T_BOUNDARY_CONST(PY_SSIZE_T_MAX);

  /* Expose the just-created integer boundary constants to the Python level
   * (in the private _k namespace) to facilitate bounds checking in Python.
   *
   * PyModule_AddObject steals a reference to the object being added, but we
   * need these to live forever, even if a meddling client programmer comes in
   * and deletes them from the namespace of the _k module, so INCREF. */
  #define EXPOSE_INT_BOUNDARY_CONSTANT(name) \
    if (PyModule_AddObject(m, #name, py_ ## name) != 0) { \
      goto fail; \
    } else { \
      Py_INCREF(py_ ## name); \
    }

  EXPOSE_INT_BOUNDARY_CONSTANT(SHRT_MIN);
  EXPOSE_INT_BOUNDARY_CONSTANT(SHRT_MAX);
  EXPOSE_INT_BOUNDARY_CONSTANT(INT_MIN);
  EXPOSE_INT_BOUNDARY_CONSTANT(INT_MAX);
  EXPOSE_INT_BOUNDARY_CONSTANT(LONG_MIN);
  EXPOSE_INT_BOUNDARY_CONSTANT(LONG_MAX);
  EXPOSE_INT_BOUNDARY_CONSTANT(LONG_LONG_MIN);
  EXPOSE_INT_BOUNDARY_CONSTANT(LONG_LONG_MAX);
  EXPOSE_INT_BOUNDARY_CONSTANT(PY_SSIZE_T_MIN);
  EXPOSE_INT_BOUNDARY_CONSTANT(PY_SSIZE_T_MAX);

  #ifdef ENABLE_CONNECTION_TIMEOUT
    if (init_kidb_connection_timeout(m) != 0) { return; }
  #endif

  if (init_shared_string_constants() != 0) {
    PyErr_SetString(PyExc_ImportError, "Unable to initialize shared strings.");
    return;
  }

  if (init_kidb_exceptions(d) != 0) {
    PyErr_SetString(PyExc_ImportError,
        "Unable to initialize kinterbasdb exceptions."
      );
    return;
  }

  if (init_kidb_exception_support() != 0) {
    PyErr_SetString(PyExc_ImportError,
        "Unable to initialize kinterbasdb exception support code."
      );
    return;
  }

  if (init_kidb_transaction_support() != 0) {
    PyErr_SetString(PyExc_ImportError,
        "Unable to initialize kinterbasdb transaction support code."
      );
    return;
  }

  #define _INIT_C_TYPE_AND_SYS(type_name, init_func) { \
    int status = init_func(); \
    if (status == 0) { \
      /* Expose the class in the namespace of the _k module. \
       * PyModule_AddObject steals a ref, so the artificial INCREF prevents \
       * a client programmer from messing up the refcount with a statement \
       * like 'del kinterbasdb._k.ClassName'. */ \
      Py_INCREF(&type_name ## Type); \
      status = PyModule_AddObject(m, \
          #type_name, (PyObject *) &type_name ## Type \
        ); \
    } \
    if (status != 0) { \
      assert (PyErr_Occurred()); \
      /* Python's module loader doesn't really give us any error handling \
       * options: */ \
      return; \
    } \
  }

  _INIT_C_TYPE_AND_SYS(Transaction, init_kidb_transaction);
  _INIT_C_TYPE_AND_SYS(BlobReader, init_kidb_nonstandard_blob_support);
  _INIT_C_TYPE_AND_SYS(PreparedStatement, init_kidb_preparedstatement);
  _INIT_C_TYPE_AND_SYS(Cursor, init_kidb_cursor);
  #ifdef ENABLE_DB_EVENT_SUPPORT
    _INIT_C_TYPE_AND_SYS(EventConduit, init_kidb_event_system);

    /* Expose the following event-related constants solely for the sake of code
     * that tests boundary conditions: */
    if (PyModule_AddIntConstant(m, "EVENT_BLOCK_SIZE", EVENT_BLOCK_SIZE) != 0) {
      return;
    }
  #endif

  if (init_kidb_type_translation() != 0) {
    PyErr_SetString(PyExc_ImportError,
        "Unable to initialize kinterbasdb type translation."
      );
    return;
  }

  /* DSR added null_connection when moving connection state detection from the
   * Python level to the C level.  From the perspective of the Python-level
   * kinterbasdb code, _kinterbasdb.null_connection is a null value like
   * Python's None, except that it is of type ConnectionType instead of
   * NoneType.
   *   The kinterbasdb.Connection Python class can set its reference to its
   * equivalent C type (self._C_conn) to _kinterbasdb.null_connection to
   * indicate that the underlying connection is no longer valid.  Then the
   * pyob_* functions in this C code that demand an argument of ConnectionType
   * are satisfied by null_connection long enough to detect that it is not open
   * (and that therefore the requested operation is not allowed). */
  null_connection = Connection_create();
  if (null_connection == NULL) {
    PyErr_SetString(PyExc_ImportError, "Unable to create null_connection.");
    return;
  }
  PyDict_SetItemString(d, "null_connection", (PyObject *) null_connection);

  return;

  fail:
    /* Python's module loader doesn't support clean recovery from errors, so
     * there's nothing we can do except attempt to avoid a segfault by
     * returning if a required object cannot be created. */
    return;
} /* init_kinterbasdb */

#include "_kinterbasdb_constants.c"
/********************** MODULE INFRASTRUCTURE:END ***********************/