File: wgdbmodule.c

package info (click to toggle)
whitedb 0.7.2-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,452 kB
  • ctags: 2,681
  • sloc: ansic: 31,714; python: 790; lex: 359; java: 277; makefile: 172; yacc: 138; sh: 87; sed: 36
file content (1877 lines) | stat: -rw-r--r-- 57,282 bytes parent folder | download
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
/*
* $Id:  $
* $Version: $
*
* Copyright (c) Priit Jrv 2009, 2010, 2013
*
* This file is part of WhiteDB
*
* WhiteDB 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 3 of the License, or
* (at your option) any later version.
*
* WhiteDB 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 WhiteDB.  If not, see <http://www.gnu.org/licenses/>.
*
*/

 /** @file wgdbmodule.c
 *  Python extension module for accessing WhiteDB database
 *
 */

/* ====== Includes =============== */

#include <Python.h>
#include <datetime.h>

#ifdef _WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif

#include "dbapi.h"

/* ====== Private headers and defs ======== */

#if PY_VERSION_HEX >= 0x03000000
#define PYTHON3
#define ENCODEERR "surrogateescape" /* handling of charset mismatches */
#if PY_VERSION_HEX >= 0x03030000
#define HAVE_LOCALEENC /* locale dependent string encode function exists */
#endif
#endif

struct module_state {
  PyObject *wgdb_error;
};

typedef struct {
  PyObject_HEAD
  void *db;
  int local;
} wg_database;

typedef struct {
  PyObject_HEAD
  void *rec;
} wg_record;

typedef struct {
  PyObject_HEAD
  wg_query *query;
  wg_database *db;
  wg_query_arg *arglist;
  int argc;
  void *matchrec;
  int reclen;
} wg_query_ob;  /* append _ob to avoid name clash with dbapi.h */


/* ======= Private protos ================ */

static PyObject *wgdb_attach_database(PyObject *self, PyObject *args,
                                        PyObject *kwds);
static PyObject *wgdb_attach_existing_database(PyObject *self, PyObject *args);
static PyObject *wgdb_delete_database(PyObject *self, PyObject *args);
static PyObject *wgdb_detach_database(PyObject *self, PyObject *args);

static PyObject *wgdb_create_record(PyObject *self, PyObject *args);
static PyObject *wgdb_create_raw_record(PyObject *self, PyObject *args);
static PyObject *wgdb_get_first_record(PyObject *self, PyObject *args);
static PyObject *wgdb_get_next_record(PyObject *self, PyObject *args);
static PyObject *wgdb_get_record_len(PyObject *self, PyObject *args);
static PyObject *wgdb_is_record(PyObject *self, PyObject *args);
static PyObject *wgdb_delete_record(PyObject *self, PyObject *args);

static wg_int pytype_to_wgtype(PyObject *data, wg_int ftype);
static wg_int encode_pyobject_null(wg_database *db);
static wg_int encode_pyobject_record(wg_database *db, PyObject *data);
static wg_int encode_pyobject_int(wg_database *db, PyObject *data, int param);
static wg_int encode_pyobject_double(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject_str(wg_database *db, PyObject *data,
  char *ext_str, int param);
static wg_int encode_pyobject_uri(wg_database *db, PyObject *data,
  char *ext_str, int param);
static wg_int encode_pyobject_xmlliteral(wg_database *db, PyObject *data,
  char *ext_str, int param);
static wg_int encode_pyobject_char(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject_fixpoint(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject_date(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject_time(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject_var(wg_database *db, PyObject *data,
  int param);
static wg_int encode_pyobject(wg_database *db, PyObject *data,
                            wg_int ftype, char *ext_str, int param);
static wg_int encode_pyobject_ext(PyObject *self,
                            wg_database *db, PyObject *obj, int param);
static PyObject *wgdb_set_field(PyObject *self, PyObject *args,
                                        PyObject *kwds);
static PyObject *wgdb_set_new_field(PyObject *self, PyObject *args,
                                        PyObject *kwds);
static PyObject *wgdb_get_field(PyObject *self, PyObject *args);

static PyObject *wgdb_start_write(PyObject *self, PyObject *args);
static PyObject *wgdb_end_write(PyObject *self, PyObject *args);
static PyObject *wgdb_start_read(PyObject *self, PyObject *args);
static PyObject *wgdb_end_read(PyObject *self, PyObject *args);

static int parse_query_params(PyObject *self, PyObject *args,
                                    PyObject *kwds, wg_query_ob *query);
static PyObject * wgdb_make_query(PyObject *self, PyObject *args,
                                        PyObject *kwds);
static PyObject * wgdb_fetch(PyObject *self, PyObject *args);
static PyObject * wgdb_free_query(PyObject *self, PyObject *args);
static void free_query(wg_query_ob *obj);

static void wg_database_dealloc(wg_database *obj);
static void wg_query_dealloc(wg_query_ob *obj);
static PyObject *wg_database_repr(wg_database *obj);
static PyObject *wg_record_repr(wg_record *obj);
static PyObject *wg_query_repr(wg_query_ob *obj);
static PyObject *wg_query_get_res_count(wg_query_ob *obj, void *closure);
static int wg_query_set_res_count(wg_query_ob *obj,
                                      PyObject *value, void *closure);
static void wgdb_error_setstring(PyObject *self, char *err);

/* ============= Private vars ============ */

/** Module state, contains the exception object */
#ifndef PYTHON3
static struct module_state _state;
#endif

/** Database object type */
static PyTypeObject wg_database_type = {
#ifndef PYTHON3
  PyObject_HEAD_INIT(NULL)
  0,                            /*ob_size*/
#else
  PyVarObject_HEAD_INIT(NULL, 0)
#endif
  "wgdb.Database",              /*tp_name*/
  sizeof(wg_database),          /*tp_basicsize*/
  0,                            /*tp_itemsize*/
  (destructor) wg_database_dealloc, /*tp_dealloc*/
  0,                            /*tp_print*/
  0,                            /*tp_getattr*/
  0,                            /*tp_setattr*/
  0,                            /*tp_compare*/
  (reprfunc) wg_database_repr,  /*tp_repr*/
  0,                            /*tp_as_number*/
  0,                            /*tp_as_sequence*/
  0,                            /*tp_as_mapping*/
  0,                            /*tp_hash */
  0,                            /*tp_call*/
  (reprfunc) wg_database_repr,  /*tp_str*/
  0,                            /*tp_getattro*/
  0,                            /*tp_setattro*/
  0,                            /*tp_as_buffer*/
  Py_TPFLAGS_DEFAULT,           /*tp_flags*/
  "WhiteDB database object",    /* tp_doc */
};

/** Record object type */
static PyTypeObject wg_record_type = {
#ifndef PYTHON3
  PyObject_HEAD_INIT(NULL)
  0,                            /*ob_size*/
#else
  PyVarObject_HEAD_INIT(NULL, 0)
#endif
  "wgdb.Record",                /*tp_name*/
  sizeof(wg_record),            /*tp_basicsize*/
  0,                            /*tp_itemsize*/
  0,                            /*tp_dealloc*/
  0,                            /*tp_print*/
  0,                            /*tp_getattr*/
  0,                            /*tp_setattr*/
  0,                            /*tp_compare*/
  (reprfunc) wg_record_repr,    /*tp_repr*/
  0,                            /*tp_as_number*/
  0,                            /*tp_as_sequence*/
  0,                            /*tp_as_mapping*/
  0,                            /*tp_hash */
  0,                            /*tp_call*/
  (reprfunc) wg_record_repr,    /*tp_str*/
  0,                            /*tp_getattro*/
  0,                            /*tp_setattro*/
  0,                            /*tp_as_buffer*/
  Py_TPFLAGS_DEFAULT,           /*tp_flags*/
  "WhiteDB record object",      /* tp_doc */
};

/** Data accessor functions for the Query type */
static PyGetSetDef wg_query_getset[] = {
  {"res_count",                         /* attribyte name */
   (getter) wg_query_get_res_count,
   (setter) wg_query_set_res_count,
   "Number of rows in the result set",  /* doc */
   NULL},                               /* closure, not used here */
  {NULL}
};

/** Query object type */
static PyTypeObject wg_query_type = {
#ifndef PYTHON3
  PyObject_HEAD_INIT(NULL)
  0,                            /*ob_size*/
#else
  PyVarObject_HEAD_INIT(NULL, 0)
#endif
  "wgdb.Query",                 /*tp_name*/
  sizeof(wg_query_ob),          /*tp_basicsize*/
  0,                            /*tp_itemsize*/
  (destructor) wg_query_dealloc, /*tp_dealloc*/
  0,                            /*tp_print*/
  0,                            /*tp_getattr*/
  0,                            /*tp_setattr*/
  0,                            /*tp_compare*/
  (reprfunc) wg_query_repr,     /*tp_repr*/
  0,                            /*tp_as_number*/
  0,                            /*tp_as_sequence*/
  0,                            /*tp_as_mapping*/
  0,                            /*tp_hash */
  0,                            /*tp_call*/
  (reprfunc) wg_query_repr,     /*tp_str*/
  0,                            /*tp_getattro*/
  0,                            /*tp_setattro*/
  0,                            /*tp_as_buffer*/
  Py_TPFLAGS_DEFAULT,           /*tp_flags*/
  "WhiteDB query object",       /* tp_doc */
  0,                            /* tp_traverse */
  0,                            /* tp_clear */
  0,                            /* tp_richcompare */
  0,                            /* tp_weaklistoffset */
  0,                            /* tp_iter */
  0,                            /* tp_iternext */
  0,                            /* tp_methods */
  0,                            /* tp_members */
  wg_query_getset,              /* tp_getset */
};

/** Method table */
static PyMethodDef wgdb_methods[] = {
  {"attach_database",  (PyCFunction) wgdb_attach_database,
   METH_VARARGS | METH_KEYWORDS,
   "Connect to a shared memory database. If the database with the "\
   "given name does not exist, it is created."},
  {"attach_existing_database",  (PyCFunction) wgdb_attach_existing_database,
   METH_VARARGS,
   "Connect to a shared memory database. Fails if the database with the "\
   "given name does not exist."},
  {"delete_database",  wgdb_delete_database, METH_VARARGS,
   "Delete a shared memory database."},
  {"detach_database",  wgdb_detach_database, METH_VARARGS,
   "Detach from shared memory database."},
  {"create_record",  wgdb_create_record, METH_VARARGS,
   "Create a record with given length."},
  {"create_raw_record",  wgdb_create_raw_record, METH_VARARGS,
   "Create a record without indexing the fields."},
  {"get_first_record",  wgdb_get_first_record, METH_VARARGS,
   "Fetch first record from database."},
  {"get_next_record",  wgdb_get_next_record, METH_VARARGS,
   "Fetch next record from database."},
  {"get_record_len",  wgdb_get_record_len, METH_VARARGS,
   "Get record length (number of fields)."},
  {"is_record",  wgdb_is_record, METH_VARARGS,
   "Determine if object is a WhiteDB record."},
  {"delete_record",  wgdb_delete_record, METH_VARARGS,
   "Delete a record."},
  {"set_field",  (PyCFunction) wgdb_set_field,
   METH_VARARGS | METH_KEYWORDS,
   "Set field value."},
  {"set_new_field",  (PyCFunction) wgdb_set_new_field,
   METH_VARARGS | METH_KEYWORDS,
   "Set field value (assumes no previous content)."},
  {"get_field",  wgdb_get_field, METH_VARARGS,
   "Get field data decoded to corresponding Python type."},
  {"start_write",  wgdb_start_write, METH_VARARGS,
   "Start writing transaction."},
  {"end_write",  wgdb_end_write, METH_VARARGS,
   "Finish writing transaction."},
  {"start_read",  wgdb_start_read, METH_VARARGS,
   "Start reading transaction."},
  {"end_read",  wgdb_end_read, METH_VARARGS,
   "Finish reading transaction."},
  {"make_query",  (PyCFunction) wgdb_make_query,
   METH_VARARGS | METH_KEYWORDS,
   "Create a query object."},
  {"fetch",  wgdb_fetch, METH_VARARGS,
   "Fetch next record from a query."},
  {"free_query",  wgdb_free_query, METH_VARARGS,
   "Unallocates the memory (local and shared) used by the query."},
  {NULL, NULL, 0, NULL} /* terminator */
};

#ifdef PYTHON3
static struct PyModuleDef wgdb_def = {
   PyModuleDef_HEAD_INIT,
   "wgdb",                          /* name of module */
   "WhiteDB database adapter",      /* module documentation, may be NULL */
   sizeof(struct module_state),     /* size of per-interpreter state */
   wgdb_methods
};
#endif

/* ============== Functions ============== */

/* Wrapped wgdb API
 * uses wg_database_type object to store the database pointer
 * when making calls from python. This type is not available
 * generally (using non-restricted values for the pointer
 * would cause segfaults), only by calling wgdb_attach_database().
 */

/* Functions for attaching and deleting */

/** Attach to memory database.
 *  Python wrapper to wg_attach_database() and wg_attach_local_database()
 */

static PyObject * wgdb_attach_database(PyObject *self, PyObject *args,
                                        PyObject *kwds) {
  wg_database *db;
  char *shmname = NULL;
  wg_int sz = 0;
  wg_int local = 0;
  static char *kwlist[] = {"shmname", "size", "local", NULL};

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "|snn",
    kwlist, &shmname, &sz, &local))
    return NULL;

  db = (wg_database *) wg_database_type.tp_alloc(&wg_database_type, 0);
  if(!db) return NULL;

  /* Now try to actually connect. Note that this may create
   * a new database if none is found with a matching name. In case of
   * a local database, a new one is allocated every time.
   */
  if(!local)
    db->db = (void *) wg_attach_database(shmname, sz);
  else
    db->db = (void *) wg_attach_local_database(sz);
  if(!db->db) {
    wgdb_error_setstring(self, "Failed to attach to database.");
    wg_database_type.tp_free(db);
    return NULL;
  }
  db->local = local;
/*  Py_INCREF(db);*/ /* XXX: not needed? if we increment here, the
                        object is never freed, even if it's unused */
  return (PyObject *) db;
}

/** Attach to memory database.
 *  Python wrapper to wg_attach_existing_database()
 */

static PyObject *wgdb_attach_existing_database(PyObject *self,
    PyObject *args) {
  wg_database *db;
  char *shmname = NULL;

  if(!PyArg_ParseTuple(args, "|s", &shmname))
    return NULL;

  db = (wg_database *) wg_database_type.tp_alloc(&wg_database_type, 0);
  if(!db) return NULL;

  /* Try to attach to an existing database. Fails if a database
   * with a matching name is not found. Only applies to shared
   * memory databases.
   */
  db->db = (void *) wg_attach_existing_database(shmname);
  if(!db->db) {
    wgdb_error_setstring(self, "Failed to attach to database.");
    wg_database_type.tp_free(db);
    return NULL;
  }
  db->local = 0;
/*  Py_INCREF(db);*/ /* XXX: not needed? if we increment here, the
                        object is never freed, even if it's unused */
  return (PyObject *) db;
}

/** Delete memory database.
 *  Python wrapper to wg_delete_database()
 */

static PyObject * wgdb_delete_database(PyObject *self, PyObject *args) {
  char *shmname = NULL;
  int err = 0;

  if(!PyArg_ParseTuple(args, "|s", &shmname))
    return NULL;

  err = wg_delete_database(shmname);
  if(err) {
    wgdb_error_setstring(self, "Failed to delete the database.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/** Detach from memory database.
 *  Python wrapper to wg_detach_database()
 *  Detaching is generally SysV-specific (so under Win32 this
 *  is currently a no-op).
 *  In case of a local database, wg_delete_local_database() is
 *  called instead.
 */

static PyObject * wgdb_detach_database(PyObject *self, PyObject *args) {
  PyObject *db = NULL;

  if(!PyArg_ParseTuple(args, "O!", &wg_database_type, &db))
    return NULL;

  /* Only try detaching if we have a valid pointer. */
  if(((wg_database *) db)->db) {
    if(((wg_database *) db)->local) {
      /* Local database should be deleted instead */
      wg_delete_local_database(((wg_database *) db)->db);
    } else if(wg_detach_database(((wg_database *) db)->db) < 0) {
      wgdb_error_setstring(self, "Failed to detach from database.");
      return NULL;
    }
    ((wg_database *) db)->db = NULL; /* mark as detached */
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/* Functions to manipulate records. The record is also
 * represented as a custom type to avoid dealing with word
 * size issues on different platforms. So the type is essentially
 * a container for the record pointer.
 */

/** Create a record with given length.
 *  Python wrapper to wg_create_record()
 */

static PyObject * wgdb_create_record(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int length = 0;
  wg_record *rec;

  if(!PyArg_ParseTuple(args, "O!n", &wg_database_type, &db, &length))
    return NULL;

  /* Build a new record object */
  rec = (wg_record *) wg_record_type.tp_alloc(&wg_record_type, 0);
  if(!rec) return NULL;

  rec->rec = wg_create_record(((wg_database *) db)->db, length);
  if(!rec->rec) {
    wgdb_error_setstring(self, "Failed to create a record.");
    wg_record_type.tp_free(rec);
    return NULL;
  }

/*  Py_INCREF(rec);*/ /* XXX: not needed? */
  return (PyObject *) rec;
}

/** Create a record without indexing the fields.
 *  Python wrapper to wg_create_raw_record()
 */

static PyObject * wgdb_create_raw_record(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int length = 0;
  wg_record *rec;

  if(!PyArg_ParseTuple(args, "O!n", &wg_database_type, &db, &length))
    return NULL;

  /* Build a new record object */
  rec = (wg_record *) wg_record_type.tp_alloc(&wg_record_type, 0);
  if(!rec) return NULL;

  rec->rec = wg_create_raw_record(((wg_database *) db)->db, length);
  if(!rec->rec) {
    wgdb_error_setstring(self, "Failed to create a record.");
    wg_record_type.tp_free(rec);
    return NULL;
  }
  return (PyObject *) rec;
}

/** Fetch first record from database.
 *  Python wrapper to wg_get_first_record()
 */

static PyObject * wgdb_get_first_record(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_record *rec;

  if(!PyArg_ParseTuple(args, "O!", &wg_database_type, &db))
    return NULL;

  /* Build a new record object */
  rec = (wg_record *) wg_record_type.tp_alloc(&wg_record_type, 0);
  if(!rec) return NULL;

  rec->rec = wg_get_first_record(((wg_database *) db)->db);
  if(!rec->rec) {
    wgdb_error_setstring(self, "Failed to fetch a record.");
    wg_record_type.tp_free(rec);
    return NULL;
  }

  Py_INCREF(rec);
  return (PyObject *) rec;
}

/** Fetch next record from database.
 *  Python wrapper to wg_get_next_record()
 */

static PyObject * wgdb_get_next_record(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *prev = NULL;
  wg_record *rec;

  if(!PyArg_ParseTuple(args, "O!O!", &wg_database_type, &db,
      &wg_record_type, &prev))
    return NULL;

  /* Build a new record object */
  rec = (wg_record *) wg_record_type.tp_alloc(&wg_record_type, 0);
  if(!rec) return NULL;

  rec->rec = wg_get_next_record(((wg_database *) db)->db,
    ((wg_record *) prev)->rec);
  if(!rec->rec) {
    wgdb_error_setstring(self, "Failed to fetch a record.");
    wg_record_type.tp_free(rec);
    return NULL;
  }

  Py_INCREF(rec);
  return (PyObject *) rec;
}

/** Get record length (number of fields).
 *  Python wrapper to wg_get_record_len()
 */

static PyObject * wgdb_get_record_len(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *rec = NULL;
  wg_int len = 0;

  if(!PyArg_ParseTuple(args, "O!O!", &wg_database_type, &db,
      &wg_record_type, &rec))
    return NULL;

  len = wg_get_record_len(((wg_database *) db)->db,
    ((wg_record *) rec)->rec);
  if(len < 0) {
    wgdb_error_setstring(self, "Failed to get the record length.");
    return NULL;
  }
  return Py_BuildValue("i", (int) len);
}

/** Determine, if object is a record
 *  Instead of exposing the record type directly as wgdb.Record,
 *  we provide this function. The reason is that we do not want
 *  these objects to be instantiated from Python, as such instances
 *  would have no valid record pointer to the memory database.
 */

static PyObject * wgdb_is_record(PyObject *self, PyObject *args) {
  PyObject *ob = NULL;

  if(!PyArg_ParseTuple(args, "O", &ob))
    return NULL;

  if(PyObject_TypeCheck(ob, &wg_record_type))
    return Py_BuildValue("i", 1);
  else
    return Py_BuildValue("i", 0);
}

/** Delete record.
 *  Python wrapper to wg_delete_record()
 */

static PyObject * wgdb_delete_record(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *rec = NULL;
  wg_int err = 0;

  if(!PyArg_ParseTuple(args, "O!O!", &wg_database_type, &db,
      &wg_record_type, &rec))
    return NULL;

  err = wg_delete_record(((wg_database *) db)->db,
    ((wg_record *) rec)->rec);
  if(err == -1) {
    wgdb_error_setstring(self, "Record has references.");
    return NULL;
  }
  else if(err < -1) {
    wgdb_error_setstring(self, "Failed to delete record.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}


/* Functions to manipulate field contents.
 *
 * Storing data: the Python object is first converted to an appropriate
 * C data. Then wg_encode_*() is used to convert it to WhiteDB encoded
 * field data (possibly storing the actual data in the database, if the
 * object itself is hashed or does not fit in a field). The encoded data
 * is then stored with wg_set_field() or wg_set_new_field() as appropriate.
 *
 * Reading data: encoded field data is read using wg_get_field() and
 * examined to determine the type. If the type is recognized, the data
 * is converted to appropriate C data using wg_decode_*() family of
 * functions and finally to a Python object.
 */

/** Determine matching wgdb type of a Python object.
 *  ftype argument is a type hint in some cases where there's
 *  ambiguity due to multiple matching wgdb types.
 *
 *  returns -1 if the type is known, but the type hint is invalid.
 *  returns -2 if type is not recognized
 */
static wg_int pytype_to_wgtype(PyObject *data, wg_int ftype) {

  if(data==Py_None) {
    if(!ftype)
      return WG_NULLTYPE;
    else if(ftype!=WG_NULLTYPE)
      return -1;
  }
#ifndef PYTHON3
  else if(PyInt_Check(data)) {
#else
  else if(PyLong_Check(data)) {
#endif
    if(!ftype)
      return WG_INTTYPE;
    else if(ftype!=WG_INTTYPE && ftype!=WG_VARTYPE)
      return -1;
  }
  else if(PyFloat_Check(data)) {
    if(!ftype)
      return WG_DOUBLETYPE;
    else if(ftype!=WG_DOUBLETYPE && ftype!=WG_FIXPOINTTYPE)
      return -1;
  }
#ifndef PYTHON3
  else if(PyString_Check(data)) {
#else
  else if(PyUnicode_Check(data)) {
#endif
    if(!ftype)
      return WG_STRTYPE;
    else if(ftype!=WG_STRTYPE && ftype!=WG_CHARTYPE &&\
      ftype!=WG_URITYPE && ftype!=WG_XMLLITERALTYPE)
      return -1;
  }
  else if(PyObject_TypeCheck(data, &wg_record_type)) {
    if(!ftype)
      return WG_RECORDTYPE;
    else if(ftype!=WG_RECORDTYPE)
      return -1;
  }
  else if(PyDate_Check(data)) {
    if(!ftype)
      return WG_DATETYPE;
    else if(ftype!=WG_DATETYPE)
      return -1;
  }
  else if(PyTime_Check(data)) {
    if(!ftype)
      return WG_TIMETYPE;
    else if(ftype!=WG_TIMETYPE)
      return -1;
  }
  else
    /* Nothing matched */
    return -2;

  /* User-selected type was suitable */
  return ftype;
}

/** Encode an atomic value of type WG_NULLTYPE
 *  Always succeeds.
 */
static wg_int encode_pyobject_null(wg_database *db) {
  return wg_encode_null(db->db, 0);
}

/** Encode an atomic value of type WG_RECORDTYPE
 *  returns WG_ILLEGAL on failure
 */
static wg_int encode_pyobject_record(wg_database *db, PyObject *data) {
  return wg_encode_record(db->db, ((wg_record *) data)->rec);
}

/** Encode an atomic value of type WG_INTTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, the storage will be allocated in local memory (intended
 *  for encoding query parameters without write locking)
 */
static wg_int encode_pyobject_int(wg_database *db, PyObject *data, int param) {
  wg_int intdata;
#ifndef PYTHON3
  intdata = (wg_int) PyInt_AsLong(data);
#else
  intdata = (wg_int) PyLong_AsLong(data);
#endif

  if(!param) {
    return wg_encode_int(db->db, intdata);
  } else {
    return wg_encode_query_param_int(db->db, intdata);
  }
}

/** Encode an atomic value of type WG_DOUBLETYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, the storage will be allocated in local memory (intended
 *  for encoding query parameters without write locking)
 */
static wg_int encode_pyobject_double(wg_database *db, PyObject *data,
  int param) {
  if(!param) {
    return wg_encode_double(db->db, (double) PyFloat_AsDouble(data));
  } else {
    return wg_encode_query_param_double(db->db,
      (double) PyFloat_AsDouble(data));
  }
}

/** Encode an atomic value of type WG_STRTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, the storage will be allocated in local memory (intended
 *  for encoding query parameters without write locking)
 */
static wg_int encode_pyobject_str(wg_database *db, PyObject *data,
  char *ext_str, int param) {
  char *s;
#ifndef PYTHON3
  s = PyString_AsString(data);
#elif defined(HAVE_LOCALEENC)
  s = PyBytes_AsString(PyUnicode_EncodeLocale(data, ENCODEERR));
#else
  s = PyBytes_AsString(PyUnicode_AsEncodedString(data, NULL, ENCODEERR));
#endif
  /* wg_encode_str is not guaranteed to check for NULL pointer */
  if(s) {
    if(!param) {
      return wg_encode_str(db->db, s, ext_str);
    } else {
      return wg_encode_query_param_str(db->db, s, ext_str);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_URITYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, the storage will be allocated in local memory (intended
 *  for encoding query parameters without write locking)
 */
static wg_int encode_pyobject_uri(wg_database *db, PyObject *data,
  char *ext_str, int param) {
  char *s;
#ifndef PYTHON3
  s = PyString_AsString(data);
#elif defined(HAVE_LOCALEENC)
  s = PyBytes_AsString(PyUnicode_EncodeLocale(data, ENCODEERR));
#else
  s = PyBytes_AsString(PyUnicode_AsEncodedString(data, NULL, ENCODEERR));
#endif
  /* wg_encode_str is not guaranteed to check for NULL pointer */
  if(s) {
    if(!param) {
      return wg_encode_uri(db->db, s, ext_str);
    } else {
      return wg_encode_query_param_uri(db->db, s, ext_str);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_XMLLITERALTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, the storage will be allocated in local memory (intended
 *  for encoding query parameters without write locking)
 */
static wg_int encode_pyobject_xmlliteral(wg_database *db, PyObject *data,
  char *ext_str, int param) {
  char *s;
#ifndef PYTHON3
  s = PyString_AsString(data);
#elif defined(HAVE_LOCALEENC)
  s = PyBytes_AsString(PyUnicode_EncodeLocale(data, ENCODEERR));
#else
  s = PyBytes_AsString(PyUnicode_AsEncodedString(data, NULL, ENCODEERR));
#endif
  /* wg_encode_str is not guaranteed to check for NULL pointer */
  if(s) {
    if(!param) {
      return wg_encode_xmlliteral(db->db, s, ext_str);
    } else {
      return wg_encode_query_param_xmlliteral(db->db, s, ext_str);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_CHARTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, value is encoded as a query parameter.
 */
static wg_int encode_pyobject_char(wg_database *db, PyObject *data,
  int param) {
  char *s;
#ifndef PYTHON3
  s = PyString_AsString(data);
#elif defined(HAVE_LOCALEENC)
  s = PyBytes_AsString(PyUnicode_EncodeLocale(data, ENCODEERR));
#else
  s = PyBytes_AsString(PyUnicode_AsEncodedString(data, NULL, ENCODEERR));
#endif
  /* wg_encode_str is not guaranteed to check for NULL pointer */
  if(s) {
    if(!param) {
      return wg_encode_char(db->db, s[0]);
    } else {
      return wg_encode_query_param_char(db->db, s[0]);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_FIXPOINTTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, value is encoded as a query parameter.
 */
static wg_int encode_pyobject_fixpoint(wg_database *db, PyObject *data,
  int param) {
  if(!param) {
    return wg_encode_fixpoint(db->db, (double) PyFloat_AsDouble(data));
  } else {
    return wg_encode_query_param_fixpoint(db->db,
      (double) PyFloat_AsDouble(data));
  }
}

/** Encode an atomic value of type WG_DATETYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, value is encoded as a query parameter.
 */
static wg_int encode_pyobject_date(wg_database *db, PyObject *data,
  int param) {
  int datedata = wg_ymd_to_date(db->db,
    PyDateTime_GET_YEAR(data),
    PyDateTime_GET_MONTH(data),
    PyDateTime_GET_DAY(data));
  if(datedata > 0) {
    if(!param) {
      return wg_encode_date(db->db, datedata);
    } else {
      return wg_encode_query_param_date(db->db, datedata);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_TIMETYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, value is encoded as a query parameter.
 */
static wg_int encode_pyobject_time(wg_database *db, PyObject *data,
  int param) {
  int timedata = wg_hms_to_time(db->db,
    PyDateTime_TIME_GET_HOUR(data),
    PyDateTime_TIME_GET_MINUTE(data),
    PyDateTime_TIME_GET_SECOND(data),
    PyDateTime_TIME_GET_MICROSECOND(data)/10000);
  if(timedata >= 0) {
    if(!param) {
      return wg_encode_time(db->db, timedata);
    } else {
      return wg_encode_query_param_time(db->db, timedata);
    }
  } else {
    return WG_ILLEGAL;
  }
}

/** Encode an atomic value of type WG_VARTYPE
 *  returns WG_ILLEGAL on failure
 *  if param is 1, value is encoded as a query parameter.
 */
static wg_int encode_pyobject_var(wg_database *db, PyObject *data,
  int param) {
  int intdata;
#ifndef PYTHON3
  intdata = (int) PyInt_AsLong(data);
#else
  intdata = (int) PyLong_AsLong(data);
#endif

  if(!param) {
    return wg_encode_var(db->db, intdata);
  } else {
    return wg_encode_query_param_var(db->db, intdata);
  }
}

/** Encode Python object as wgdb value of specific type.
 *  returns WG_ILLEGAL if the conversion is not possible. The
 *  database API may also return WG_ILLEGAL.
 */
static wg_int encode_pyobject(wg_database *db, PyObject *data,
  wg_int ftype, char *ext_str, int param) {

  switch(ftype) {
    case WG_NULLTYPE:
      return encode_pyobject_null(db);
    case WG_RECORDTYPE:
      return encode_pyobject_record(db, data);
    case WG_INTTYPE:
      return encode_pyobject_int(db, data, param);
    case WG_DOUBLETYPE:
      return encode_pyobject_double(db, data, param);
    case WG_STRTYPE:
      return encode_pyobject_str(db, data, ext_str, param);
    case WG_URITYPE:
      return encode_pyobject_uri(db, data, ext_str, param);
    case WG_XMLLITERALTYPE:
      return encode_pyobject_xmlliteral(db, data, ext_str, param);
    case WG_CHARTYPE:
      return encode_pyobject_char(db, data, param);
    case WG_FIXPOINTTYPE:
      return encode_pyobject_fixpoint(db, data, param);
    case WG_DATETYPE:
      return encode_pyobject_date(db, data, param);
    case WG_TIMETYPE:
      return encode_pyobject_time(db, data, param);
    case WG_VARTYPE:
      return encode_pyobject_var(db, data, param);
    default:
      break;
  }

  /* Handle unknown type */
  return WG_ILLEGAL;
}

/** Encode Python object as wgdb value
 *  The object may be an immediate value or a tuple containing
 *  extended type information.
 *
 *  Conversion rules:
 *  Immediate Python value -> use the default wgdb type
 *  (value, ftype) -> use the provided field type (if possible)
 *  (value, ftype, ext_str) -> use the field type and extra string
 */
static wg_int encode_pyobject_ext(PyObject *self,
                            wg_database *db, PyObject *obj, int param) {
  PyObject *data;
  wg_int enc, ftype = 0;
  char *ext_str = NULL;

  if(PyTuple_Check(obj)) {
    /* Extended value. */
    int extargs = PyTuple_Size(obj);
    if(extargs<1 || extargs>3) {
      PyErr_SetString(PyExc_ValueError,
        "Values with extended type info must be 2/3-tuples.");
      return WG_ILLEGAL;
    }

    data = PyTuple_GetItem(obj, 0);

    if(extargs > 1) {
#ifndef PYTHON3
      ftype = (wg_int) PyInt_AsLong(PyTuple_GetItem(obj, 1));
#else
      ftype = (wg_int) PyLong_AsLong(PyTuple_GetItem(obj, 1));
#endif
      if(ftype<0) {
        PyErr_SetString(PyExc_ValueError,
          "Invalid field type for value.");
        return WG_ILLEGAL;
      }
    }

    if(extargs > 2) {
#ifndef PYTHON3
      ext_str = PyString_AsString(PyTuple_GetItem(obj, 2));
#elif defined(HAVE_LOCALEENC)
      ext_str = PyBytes_AsString(
        PyUnicode_EncodeLocale(PyTuple_GetItem(obj, 2), ENCODEERR));
#else
      ext_str = PyBytes_AsString(
        PyUnicode_AsEncodedString(PyTuple_GetItem(obj, 2), NULL, ENCODEERR));
#endif
      if(!ext_str) {
        /* Error has been set in conversion */
        return WG_ILLEGAL;
      }
    }
  } else {
    data = obj;
  }

  /* Now do the actual conversion. */
  ftype = pytype_to_wgtype(data, ftype);
  if(ftype == -1) {
    PyErr_SetString(PyExc_TypeError,
      "Requested encoding is not supported.");
    return WG_ILLEGAL;
  }
  else if(ftype == -2) {
    PyErr_SetString(PyExc_TypeError,
      "Value is of unsupported type.");
    return WG_ILLEGAL;
  }

  /* Now encode the given obj using the selected type */
  enc = encode_pyobject(db, data, ftype, ext_str, param);
  if(enc==WG_ILLEGAL)
    wgdb_error_setstring(self, "Value encoding error.");

  return enc;
}


/** Update field data.
 *  Data types supported:
 *  Python None. Translates to WhiteDB NULL (empty) field.
 *  Python integer.
 *  Python float.
 *  Python string. Embedded \0 bytes are not allowed (i.e. \0 is
 *  treated as a standard string terminator).
 *  XXX: add language support for str type?
 *  wgdb.Record object
 *  datetime.date object
 *  datetime.time object
 */

static PyObject *wgdb_set_field(PyObject *self, PyObject *args,
                                        PyObject *kwds) {
  PyObject *db = NULL, *rec = NULL;
  wg_int fieldnr, fdata = WG_ILLEGAL, err = 0, ftype = 0;
  PyObject *data;
  char *ext_str = NULL;
  static char *kwlist[] = {"db", "rec", "fieldnr", "data",
    "encoding", "ext_str", NULL};

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!nO|ns", kwlist,
      &wg_database_type, &db, &wg_record_type, &rec,
      &fieldnr, &data, &ftype, &ext_str))
    return NULL;

  /* Determine the argument type. If the optional encoding
   * argument is not supplied, default encoding for the Python type
   * of the data is selected. Otherwise the user-provided encoding
   * is used, with the limitation that the Python type must
   * be compatible with the encoding.
   */
  ftype = pytype_to_wgtype(data, ftype);
  if(ftype == -1) {
    PyErr_SetString(PyExc_TypeError,
      "Requested encoding is not supported.");
    return NULL;
  }
  else if(ftype == -2) {
    PyErr_SetString(PyExc_TypeError,
      "Argument is of unsupported type.");
    return NULL;
  }

  /* Now encode the given data using the selected type */
  fdata = encode_pyobject((wg_database *) db, data, ftype, ext_str, 0);
  if(fdata==WG_ILLEGAL) {
    wgdb_error_setstring(self, "Field data conversion error.");
    return NULL;
  }

  /* Store the encoded field data in the record */
  err = wg_set_field(((wg_database *) db)->db,
    ((wg_record *) rec)->rec, fieldnr, fdata);
  if(err < 0) {
    wgdb_error_setstring(self, "Failed to set field value.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/** Set field data (assumes no previous content)
 *  Skips some bookkeeping related to the previous field
 *  contents, making the insert faster. Using it on fields
 *  that have previous content is likely to corrupt the database.
 *  Otherwise identical to set_field().
 */

static PyObject *wgdb_set_new_field(PyObject *self, PyObject *args,
                                        PyObject *kwds) {
  PyObject *db = NULL, *rec = NULL;
  wg_int fieldnr, fdata = WG_ILLEGAL, err = 0, ftype = 0;
  PyObject *data;
  char *ext_str = NULL;
  static char *kwlist[] = {"db", "rec", "fieldnr", "data",
    "encoding", "ext_str", NULL};

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "O!O!nO|ns", kwlist,
      &wg_database_type, &db, &wg_record_type, &rec,
      &fieldnr, &data, &ftype, &ext_str))
    return NULL;

  ftype = pytype_to_wgtype(data, ftype);
  if(ftype == -1) {
    PyErr_SetString(PyExc_TypeError,
      "Requested encoding is not supported.");
    return NULL;
  }
  else if(ftype == -2) {
    PyErr_SetString(PyExc_TypeError,
      "Argument is of unsupported type.");
    return NULL;
  }

  fdata = encode_pyobject((wg_database *) db, data, ftype, ext_str, 0);
  if(fdata==WG_ILLEGAL) {
    wgdb_error_setstring(self, "Field data conversion error.");
    return NULL;
  }

  err = wg_set_new_field(((wg_database *) db)->db,
    ((wg_record *) rec)->rec, fieldnr, fdata);
  if(err < 0) {
    wgdb_error_setstring(self, "Failed to set field value.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/** Get decoded field value.
 *  Currently supported types:
 *   NULL - Python None
 *   record - wgdb.Record
 *   int - Python int
 *   double - Python float
 *   string - Python string
 *   char - Python string
 *   fixpoint - Python float
 *   date - datetime.date
 *   time - datetime.time
 */

static PyObject *wgdb_get_field(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *rec = NULL;
  wg_int fieldnr, fdata, ftype;

  if(!PyArg_ParseTuple(args, "O!O!n", &wg_database_type, &db,
      &wg_record_type, &rec, &fieldnr))
    return NULL;

  /* First retrieve the field data. The information about
   * the field type is encoded inside the field.
   */
  fdata = wg_get_field(((wg_database *) db)->db,
    ((wg_record *) rec)->rec, fieldnr);
  if(fdata==WG_ILLEGAL) {
    wgdb_error_setstring(self, "Failed to get field data.");
    return NULL;
  }

  /* Decode the type */
  ftype = wg_get_encoded_type(((wg_database *) db)->db, fdata);
  if(!ftype) {
    wgdb_error_setstring(self, "Failed to get field type.");
    return NULL;
  }

  /* Decode (or retrieve) the actual data */
  if(ftype==WG_NULLTYPE) {
    Py_INCREF(Py_None);
    return Py_None;
  }
  else if(ftype==WG_RECORDTYPE) {
    wg_record *ddata = (wg_record *) wg_record_type.tp_alloc(
      &wg_record_type, 0);
    if(!ddata) return NULL;

    ddata->rec = wg_decode_record(((wg_database *) db)->db, fdata);
    if(!ddata->rec) {
      wgdb_error_setstring(self, "Failed to fetch a record.");
      wg_record_type.tp_free(ddata);
      return NULL;
    }
    Py_INCREF(ddata);
    return (PyObject *) ddata;
  }
  else if(ftype==WG_INTTYPE) {
    wg_int ddata = wg_decode_int(((wg_database *) db)->db, fdata);
    return Py_BuildValue("n", ddata);
  }
  else if(ftype==WG_DOUBLETYPE) {
    double ddata = wg_decode_double(((wg_database *) db)->db, fdata);
    return Py_BuildValue("d", ddata);
  }
  else if(ftype==WG_STRTYPE) {
    char *ddata = wg_decode_str(((wg_database *) db)->db, fdata);
    /* Data is copied here, no leaking */
    return Py_BuildValue("s", ddata);
  }
  else if(ftype==WG_URITYPE) {
    char *ddata = wg_decode_uri(((wg_database *) db)->db, fdata);
    char *ext_str = wg_decode_uri_prefix(((wg_database *) db)->db, fdata);
    if(ext_str) {
#ifndef PYTHON3
      return PyString_FromFormat("%s%s", ext_str, ddata);
#else
      return PyUnicode_FromFormat("%s%s", ext_str, ddata);
#endif
    }
    else return Py_BuildValue("s", ddata);
  }
  else if(ftype==WG_XMLLITERALTYPE) {
    char *ddata = wg_decode_xmlliteral(((wg_database *) db)->db, fdata);
    return Py_BuildValue("s", ddata);
  }
  else if(ftype==WG_CHARTYPE) {
    char ddata[2];
    ddata[0] = wg_decode_char(((wg_database *) db)->db, fdata);
    ddata[1] = '\0';
    return Py_BuildValue("s", ddata); /* treat as single-character string */
  }
  else if(ftype==WG_FIXPOINTTYPE) {
    double ddata = wg_decode_fixpoint(((wg_database *) db)->db, fdata);
    return Py_BuildValue("d", ddata);
  }
  else if(ftype==WG_DATETYPE) {
    int year, month, day;
    int datedata = wg_decode_date(((wg_database *) db)->db, fdata);

    if(!datedata) {
      wgdb_error_setstring(self, "Failed to decode date.");
      return NULL;
    }
    wg_date_to_ymd(((wg_database *) db)->db, datedata, &year, &month, &day);
    return PyDate_FromDate(year, month, day);
  }
  else if(ftype==WG_TIMETYPE) {
    int hour, minute, second, fraction;
    int timedata = wg_decode_time(((wg_database *) db)->db, fdata);
    /* 0 is both a valid time of 00:00:00.00 and an error. So the
     * latter case is ignored here. */
    wg_time_to_hms(((wg_database *) db)->db, timedata,
      &hour, &minute, &second, &fraction);
    return PyTime_FromTime(hour, minute, second, fraction*10000);
  }
  else if(ftype==WG_VARTYPE) {
    /* XXX: returns something that, if written back to database
     * unaltered, will result in the database field actually containing
     * the same variable. The literal value in Python is not very meaningful,
     * however this approach preserves consistency in handling the type
     * conversions.
     */
    int ddata = wg_decode_var(((wg_database *) db)->db, fdata);
    return Py_BuildValue("(i,i)", ddata, WG_VARTYPE);
  }
  else {
    char buf[80];
    snprintf(buf, 80, "Cannot handle field type %d.", (int) ftype);
    wgdb_error_setstring(self, buf);
    return NULL;
  }
}

/*
 * Functions to handle transactions. Logical level of
 * concurrency control with wg_start_write() and friends
 * is implemented here. In the simplest case, these functions
 * internally map to physical locking and unlocking, however they
 * should not be relied upon to do so.
 */

/** Start a writing transaction
 *  Python wrapper to wg_start_write()
 *  Returns lock id when successful, otherwise raises an exception.
 */

static PyObject * wgdb_start_write(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int lock_id = 0;

  if(!PyArg_ParseTuple(args, "O!", &wg_database_type, &db))
    return NULL;

  lock_id = wg_start_write(((wg_database *) db)->db);
  if(!lock_id) {
    wgdb_error_setstring(self, "Failed to acquire write lock.");
    return NULL;
  }

  return Py_BuildValue("i", (int) lock_id);
}

/** Finish a writing transaction
 *  Python wrapper to wg_end_write()
 *  Returns None when successful, otherwise raises an exception.
 */

static PyObject * wgdb_end_write(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int lock_id = 0;

  if(!PyArg_ParseTuple(args, "O!n", &wg_database_type, &db, &lock_id))
    return NULL;

  if(!wg_end_write(((wg_database *) db)->db, lock_id)) {
    wgdb_error_setstring(self, "Failed to release write lock.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/** Start a reading transaction
 *  Python wrapper to wg_start_read()
 *  Returns lock id when successful, otherwise raises an exception.
 */

static PyObject * wgdb_start_read(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int lock_id = 0;

  if(!PyArg_ParseTuple(args, "O!", &wg_database_type, &db))
    return NULL;

  lock_id = wg_start_read(((wg_database *) db)->db);
  if(!lock_id) {
    wgdb_error_setstring(self, "Failed to acquire read lock.");
    return NULL;
  }

  return Py_BuildValue("i", (int) lock_id);
}

/** Finish a reading transaction
 *  Python wrapper to wg_end_read()
 *  Returns None when successful, otherwise raises an exception.
 */

static PyObject * wgdb_end_read(PyObject *self, PyObject *args) {
  PyObject *db = NULL;
  wg_int lock_id = 0;

  if(!PyArg_ParseTuple(args, "O!n", &wg_database_type, &db, &lock_id))
    return NULL;

  if(!wg_end_read(((wg_database *) db)->db, lock_id)) {
    wgdb_error_setstring(self, "Failed to release read lock.");
    return NULL;
  }

  Py_INCREF(Py_None);
  return Py_None;
}

/* Functions to create and fetch data from queries.
 * The query object defined on wgdb module level stores both
 * the pointer to the query and all the encoded parameters -
 * since the parameters use database side storage, they
 * should preferrably be freed after the query is finished.
 */

/** Parse query arguments.
 * Creates wgdb query arglist and matchrec.
 */

static int parse_query_params(PyObject *self, PyObject *args,
                            PyObject *kwds, wg_query_ob *query) {
  PyObject *db = NULL;
  PyObject *arglist = NULL;
  PyObject *matchrec = NULL;
  static char *kwlist[] = {"db", "matchrec", "arglist", NULL};

  if(!PyArg_ParseTupleAndKeywords(args, kwds, "O!|OO", kwlist,
      &wg_database_type, &db, &matchrec, &arglist))
    return 0;

  Py_INCREF(db);  /* Make sure we don't lose database connection */
  query->db = (wg_database *) db;

  /* Determine type of arglist */
  query->argc = 0;
  if(arglist && arglist!=Py_None) {
    int len, i;
    if(!PySequence_Check(arglist)) {
      PyErr_SetString(PyExc_TypeError, "Query arglist must be a sequence.");
      return 0;
    }
    len = (int) PySequence_Size(arglist);

    /* Argument list was present. Extract the individual arguments. */
    if(len > 0) {
      query->arglist = (wg_query_arg *) malloc(len * sizeof(wg_query_arg));
      if(!query->arglist) {
        wgdb_error_setstring(self, "Failed to allocate memory.");
        return 0;
      }
      memset(query->arglist, 0, len * sizeof(wg_query_arg));

      /* Now copy all the parameters. */
      for(i=0; i<len; i++) {
        int col, cond;
        wg_int enc;
        PyObject *t = PySequence_GetItem(arglist, (Py_ssize_t) i);

        if(!PyTuple_Check(t) || PyTuple_Size(t) != 3) {
          PyErr_SetString(PyExc_ValueError,
            "Query arglist item must be a 3-tuple.");
          return 0;
        }

#ifndef PYTHON3
        col = PyInt_AsLong(PyTuple_GetItem(t, 0));
#else
        col = PyLong_AsLong(PyTuple_GetItem(t, 0));
#endif
        if(col==-1 && PyErr_Occurred()) {
          PyErr_SetString(PyExc_ValueError,
            "Failed to convert query argument column number.");
          return 0;
        }

#ifndef PYTHON3
        cond = PyInt_AsLong(PyTuple_GetItem(t, 1));
#else
        cond = PyLong_AsLong(PyTuple_GetItem(t, 1));
#endif
        if(cond==-1 && PyErr_Occurred()) {
          PyErr_SetString(PyExc_ValueError,
            "Failed to convert query argument condition.");
          return 0;
        }

        enc = encode_pyobject_ext(self, query->db, PyTuple_GetItem(t, 2), 1);
        if(enc==WG_ILLEGAL) {
          /* Error set by encode function */
          return 0;
        }

        /* Finally, set the argument fields */
        query->arglist[i].column = col;
        query->arglist[i].cond = cond;
        query->arglist[i].value = enc;

        query->argc++; /* We have successfully encoded a parameter. We're
                        * not setting this to len immediately, because
                        * there might be an encoding error and part of
                        * the arguments may be left uninitialized. */
      }
    }
  }

  query->reclen = 0;
  /* Determine type of matchrec */
  if(matchrec && matchrec!=Py_None) {
    if(PyObject_TypeCheck(matchrec, &wg_record_type)) {
      /* Database record pointer was given. Pass it directly
       * to the query.
       */
      query->matchrec = ((wg_record *) matchrec)->rec;
    }
    else if(PySequence_Check(matchrec)) {
      int len = (int) PySequence_Size(matchrec);
      if(len) {
        int i;

        /* Construct the record. */
        query->matchrec = malloc(len * sizeof(wg_int));
        if(!query->matchrec) {
          wgdb_error_setstring(self, "Failed to allocate memory.");
          return 0;
        }
        memset(query->matchrec, 0, len * sizeof(wg_int));

        for(i=0; i<len; i++) {
          wg_int enc = encode_pyobject_ext(self, query->db,
            PySequence_GetItem(matchrec, i), 1);
          if(enc==WG_ILLEGAL) {
            /* Error set by encode function */
            return 0;
          }

          ((wg_int *) query->matchrec)[i] = enc;
          query->reclen++; /* Count the successfully encoded fields. */
        }
      }
    }
    else {
      PyErr_SetString(PyExc_TypeError,
        "Query match record must be a sequence or a wgdb.Record");
      return 0;
    }
  }

  return 1;
}


/** Create a query object.
 *  Python wrapper to wg_make_query()
 */

static PyObject * wgdb_make_query(PyObject *self, PyObject *args,
                                        PyObject *kwds) {
  wg_query_ob *query;

  /* Build a new query object */
  query = (wg_query_ob *) wg_query_type.tp_alloc(&wg_query_type, 0);
  if(!query) return NULL;

  query->query = NULL;
  query->db = NULL;
  query->arglist = NULL;
  query->argc = 0;
  query->matchrec = NULL;
  query->reclen = 0;

  /* Create the arglist and matchrec from parameters. */
  if(!parse_query_params(self, args, kwds, query)) {
    wg_query_dealloc(query);
    return NULL;
  }

  query->query = wg_make_query(query->db->db, query->matchrec, query->reclen,
    query->arglist, query->argc);

  if(!query->query) {
    wgdb_error_setstring(self, "Failed to create the query.");
    /* Call destructor. It should take care of all the allocated memory. */
    wg_query_dealloc(query);
    return NULL;
  }
  return (PyObject *) query;
}

/** Fetch next row from a query.
 *  Python wrapper for wg_fetch()
 */

static PyObject * wgdb_fetch(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *query = NULL;
  wg_record *rec;

  if(!PyArg_ParseTuple(args, "O!O!", &wg_database_type, &db,
      &wg_query_type, &query))
    return NULL;

  /* Build a new record object */
  rec = (wg_record *) wg_record_type.tp_alloc(&wg_record_type, 0);
  if(!rec) return NULL;

  rec->rec = wg_fetch(((wg_database *) db)->db,
    ((wg_query_ob *) query)->query);
  if(!rec->rec) {
    wgdb_error_setstring(self, "Failed to fetch a record.");
    wg_record_type.tp_free(rec);
    return NULL;
  }

  Py_INCREF(rec);
  return (PyObject *) rec;
}

/** Free query.
 *  Python wrapper to wg_free_query()
 *  In addition, this function frees the local memory for
 *  the arguments and attempts to free database-side encoded data.
 */

static PyObject * wgdb_free_query(PyObject *self, PyObject *args) {
  PyObject *db = NULL, *query = NULL;

  if(!PyArg_ParseTuple(args, "O!O!", &wg_database_type, &db,
      &wg_query_type, &query))
    return NULL;

  /* XXX: since the query contains the db pointer, ignore
   * the database object we were given (it is still required
   * for consistency between the API-s and possible future
   * extensions).
   */
  free_query((wg_query_ob *) query);
  Py_INCREF(Py_None);
  return Py_None;
}

/** Helper function to free local query memory
 *  (wg_query_ob *) query->db field is used as a marker
 *  (set to NULL for queries that do not need deallocating).
 */
static void free_query(wg_query_ob *obj) {
  if(obj->db) {
#if 0 /* Suppress the warning if we use local parameters */
    if(!obj->db->db) {
      fprintf(stderr,
        "Warning: database connection lost before freeing encoded data\n");
    }
#endif
    /* Allow freeing the query object.
     * XXX: this is hacky. db pointer may become significant
     * in the future, which makes this a timebomb.
     */
    if(obj->query)
      wg_free_query(obj->db->db, obj->query);

    if(obj->arglist) {
      if(obj->db->db) {
        int i;
        for(i=0; i<obj->argc; i++)
          wg_free_query_param(obj->db->db, obj->arglist[i].value);
      }
      free(obj->arglist);
    }
    if(obj->matchrec && obj->reclen) {
      if(obj->db->db) {
        int i;
        for(i=0; i<obj->reclen; i++)
          wg_free_query_param(obj->db->db, ((wg_int *) obj->matchrec)[i]);
      }
      free(obj->matchrec);
    }

    Py_DECREF(obj->db);
    obj->db = NULL;
  }
}

/* Methods for data types defined by this module.
 */

/** Database object desctructor.
 * Detaches from shared memory or frees local memory.
 */
static void wg_database_dealloc(wg_database *obj) {
  if(obj->db) {
    if(obj->local)
      wg_delete_local_database(obj->db);
    else
      wg_detach_database(obj->db);
  }
#ifndef PYTHON3
  obj->ob_type->tp_free((PyObject *) obj);
#else
  Py_TYPE(obj)->tp_free((PyObject *) obj);
#endif
}

/** Query object desctructor.
 * Frees query and encoded query parameters.
 */
static void wg_query_dealloc(wg_query_ob *obj) {
  free_query(obj);
#ifndef PYTHON3
  obj->ob_type->tp_free((PyObject *) obj);
#else
  Py_TYPE(obj)->tp_free((PyObject *) obj);
#endif
}

/** String representation of database object. This is used for both
 * repr() and str()
 */
static PyObject *wg_database_repr(wg_database * obj) {
  /* XXX: this is incompatible with eval(). If a need to
   * eval() such representations should arise, new initialization
   * function is also needed for the type.
   */
#ifndef PYTHON3
  return PyString_FromFormat("<WhiteDB database at %p>",
    (void *) obj->db);
#else
  return PyUnicode_FromFormat("<WhiteDB database at %p>",
    (void *) obj->db);
#endif
}

/** String representation of record object. This is used for both
 * repr() and str()
 */
static PyObject *wg_record_repr(wg_record * obj) {
  /* XXX: incompatible with eval(). */
#ifndef PYTHON3
  return PyString_FromFormat("<WhiteDB record at %p>",
    (void *) obj->rec);
#else
  return PyUnicode_FromFormat("<WhiteDB record at %p>",
    (void *) obj->rec);
#endif
}

/** String representation of query object. Used for both repr() and str()
 */
static PyObject *wg_query_repr(wg_query_ob *obj) {
  /* XXX: incompatible with eval(). */
#ifndef PYTHON3
  return PyString_FromFormat("<WhiteDB query at %p>",
    (void *) obj->query);
#else
  return PyUnicode_FromFormat("<WhiteDB query at %p>",
    (void *) obj->query);
#endif
}

/** Get the number of rows in a query result
 */
static PyObject *wg_query_get_res_count(wg_query_ob *obj, void *closure) {
  if(obj->query) {
    if(obj->query->qtype == WG_QTYPE_PREFETCH) {
      return Py_BuildValue("K", (unsigned PY_LONG_LONG) obj->query->res_count);
    } else {
      return Py_None;
    }
  } else {
    PyErr_SetString(PyExc_ValueError, "Invalid query object");
    return NULL;
  }
  return Py_None; /* satisfy the compiler */
}

/** Set the number of rows in a query result (not allowed)
 */
static int wg_query_set_res_count(wg_query_ob *obj,
                                      PyObject *value, void *closure) {
  PyErr_SetString(PyExc_AttributeError, "res_count is a read only attribute");
  return -1;
}

/** Set module exception.
 *
 */
static void wgdb_error_setstring(PyObject *self, char *err) {
#ifndef PYTHON3
  struct module_state *st = &_state;
#else
  struct module_state *st = (struct module_state *) PyModule_GetState(self);
#endif
  PyErr_SetString(st->wgdb_error, err);
}

/** Initialize module.
 *  Standard entry point for Python extension modules, executed
 *  during import.
 */
#ifdef PYTHON3
#define INITERROR return NULL;
#else
#define INITERROR return;
#endif

#ifndef PYTHON3
PyMODINIT_FUNC initwgdb(void)
#else
PyMODINIT_FUNC PyInit_wgdb(void)
#endif
{
  PyObject *m;
  struct module_state *st;

  wg_database_type.tp_new = PyType_GenericNew;
  if (PyType_Ready(&wg_database_type) < 0)
    INITERROR

  wg_record_type.tp_new = PyType_GenericNew;
  if (PyType_Ready(&wg_record_type) < 0)
    INITERROR

  wg_query_type.tp_new = PyType_GenericNew;
  if (PyType_Ready(&wg_query_type) < 0)
    INITERROR

#ifndef PYTHON3
  m = Py_InitModule3("wgdb", wgdb_methods, "WhiteDB database adapter");
#else
  m = PyModule_Create(&wgdb_def);
#endif
  if(!m) INITERROR

#ifndef PYTHON3
  st = &_state;
#else
  st = (struct module_state *) PyModule_GetState(m);
#endif
  st->wgdb_error = PyErr_NewException("wgdb.error", NULL, NULL);
  Py_INCREF(st->wgdb_error);
  PyModule_AddObject(m, "error", st->wgdb_error);

  /* Expose wgdb internal encoding types */
  PyModule_AddIntConstant(m, "NULLTYPE", WG_NULLTYPE);
  PyModule_AddIntConstant(m, "RECORDTYPE", WG_RECORDTYPE);
  PyModule_AddIntConstant(m, "INTTYPE", WG_INTTYPE);
  PyModule_AddIntConstant(m, "DOUBLETYPE", WG_DOUBLETYPE);
  PyModule_AddIntConstant(m, "STRTYPE", WG_STRTYPE);
  PyModule_AddIntConstant(m, "XMLLITERALTYPE", WG_XMLLITERALTYPE);
  PyModule_AddIntConstant(m, "URITYPE", WG_URITYPE);
  PyModule_AddIntConstant(m, "BLOBTYPE", WG_BLOBTYPE);
  PyModule_AddIntConstant(m, "CHARTYPE", WG_CHARTYPE);
  PyModule_AddIntConstant(m, "FIXPOINTTYPE", WG_FIXPOINTTYPE);
  PyModule_AddIntConstant(m, "DATETYPE", WG_DATETYPE);
  PyModule_AddIntConstant(m, "TIMETYPE", WG_TIMETYPE);
  PyModule_AddIntConstant(m, "VARTYPE", WG_VARTYPE);

/* these types are not implemented yet:
  PyModule_AddIntConstant(m, "ANONCONSTTYPE", WG_ANONCONSTTYPE);
*/

  /* Expose query conditions */
  PyModule_AddIntConstant(m, "COND_EQUAL", WG_COND_EQUAL);
  PyModule_AddIntConstant(m, "COND_NOT_EQUAL", WG_COND_NOT_EQUAL);
  PyModule_AddIntConstant(m, "COND_LESSTHAN", WG_COND_LESSTHAN);
  PyModule_AddIntConstant(m, "COND_GREATER", WG_COND_GREATER);
  PyModule_AddIntConstant(m, "COND_LTEQUAL", WG_COND_LTEQUAL);
  PyModule_AddIntConstant(m, "COND_GTEQUAL", WG_COND_GTEQUAL);

  /* Initialize PyDateTime C API */
  PyDateTime_IMPORT;
#ifdef PYTHON3
  return m;
#endif
}