-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule.c
More file actions
1135 lines (966 loc) · 22 KB
/
Copy pathmodule.c
File metadata and controls
1135 lines (966 loc) · 22 KB
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
/*
* The Postgres module. (import Postgres)
*/
#include <setjmp.h>
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "postgres.h"
#include "funcapi.h"
#include "miscadmin.h"
#include "access/heapam.h"
#include "access/htup.h"
#include "access/hio.h"
#include "access/sysattr.h"
#include "access/xact.h"
#include "catalog/namespace.h"
#include "catalog/pg_namespace.h"
#include "catalog/catversion.h"
#include "catalog/pg_proc.h"
#include "catalog/pg_type.h"
#include "commands/async.h"
#include "commands/trigger.h"
#include "libpq/libpq.h"
#include "libpq/libpq-fs.h"
#include "libpq/pqformat.h"
#include "libpq/be-fsstubs.h"
#include "mb/pg_wchar.h"
#include "tcop/tcopprot.h"
#include "utils/array.h"
#include "utils/builtins.h"
#include "utils/elog.h"
#include "utils/syscache.h"
#include "utils/memutils.h"
#include "utils/tuplestore.h"
#include "utils/rel.h"
#include "utils/relcache.h"
#include "utils/typcache.h"
#include "utils/plancache.h"
#include "utils/timestamp.h"
#include "storage/backendid.h"
#include "storage/large_object.h"
#include "executor/spi.h"
/*
* Just about everything gets included here;
* this is the module initialization file.
*/
#include "pypg/python.h"
#include "pypg/postgres.h"
#include "pypg/extension.h"
#include "pypg/module.h"
#include "pypg/pl.h"
#include "pypg/error.h"
#include "pypg/errordata.h"
#include "pypg/triggerdata.h"
#include "pypg/type/type.h"
#include "pypg/type/object.h"
#include "pypg/type/array.h"
#include "pypg/type/record.h"
#include "pypg/type/bitwise.h"
#include "pypg/type/string.h"
#include "pypg/type/numeric.h"
#include "pypg/type/timewise.h"
#include "pypg/type/system.h"
#include "pypg/tupledesc.h"
#include "pypg/function.h"
#include "pypg/xact.h"
#include "pypg/statement.h"
#include "pypg/cursor.h"
#include "pypg/stateful.h"
/*
* The source to the pure-Python portion of the 'Postgres' module.
* See 'src/module.py' for the human readable version.
*/
static const char module_python[] = {
#include "module.py.cfrag"
};
static const char project_module_python[] = {
#include "project.py.cfrag"
};
static PyObj
py_get_Postgres_source(PyObj self)
{
PyObj rob;
rob = PyUnicode_Decode(module_python, strlen(module_python), "ascii", "");
return(rob);
}
static PyObj
py_get_Postgres_project_source(PyObj self)
{
PyObj rob;
rob = PyUnicode_Decode(project_module_python, strlen(project_module_python), "ascii", "");
return(rob);
}
/*
* py_ereport - ereport() interface
*
* NOTE: This is usable in any PL state.
*/
static PyObj
py_ereport(PyObj self, PyObj args, PyObj kw)
{
static char *words[] = {
"severity", "message",
"detail", "hint", "context", "sqlerrcode",
"inhibit_pl_context", NULL
};
int elevel, sqlerrcode = 0;
bool inhibit_pl_context;
PyObj inhibit_pl_context_ob = NULL;
PyObj message = NULL, detail = NULL, hint = NULL, context = NULL;
volatile PyObj rob;
if (!PyArg_ParseTupleAndKeywords(args, kw, "iO|OOOiO:ereport", words,
&elevel, &message, &detail, &hint, &context, &sqlerrcode,
&inhibit_pl_context_ob))
return(NULL);
/*
* Validate elevel
*/
switch (elevel)
{
case DEBUG5:
case DEBUG4:
case DEBUG3:
case DEBUG2:
case DEBUG1:
case LOG:
case COMMERROR:
case INFO:
case NOTICE:
case WARNING:
case ERROR:
case FATAL:
case PANIC:
;
break;
default:
PyErr_Format(PyExc_ValueError, "unknown reporting level '%l'", elevel);
return(NULL);
break;
}
/*
* Controls the ECC set by the handler that ultimately invoked this ereport().
*/
if (inhibit_pl_context_ob != NULL && inhibit_pl_context_ob != Py_None)
{
if (inhibit_pl_context_ob == Py_True)
inhibit_pl_context = true;
else if (inhibit_pl_context_ob == Py_False)
inhibit_pl_context = false;
else
{
PyErr_Format(PyExc_TypeError,
"inhibit_pl_context keyword requires a bool, given '%s'",
Py_TYPE(inhibit_pl_context_ob)->tp_name);
return(NULL);
}
}
else
{
/*
* Otherwise, don't disable the context.
*/
inhibit_pl_context = false;
}
Py_ALLOCATE_OWNER();
{
Py_ACQUIRE_SPACE();
{
PG_TRY();
{
/*
* Repetitious enough to annoy me into doing this. -jwp
*
* Coerce object into a string; if it fails, relay.
*/
#define mkstr(OBJ) do { \
if (OBJ) { \
Py_INCREF(OBJ); \
PyObject_StrBytes(&OBJ); \
if (OBJ == NULL) \
PyErr_RelayException(); \
Py_ACQUIRE(OBJ); \
} \
} while(0)
mkstr(message);
mkstr(hint);
mkstr(detail);
mkstr(context);
#undef mkstr
if (errstart(elevel, "pg-python/src/module.c", 1, "<Postgres.ereport>", "python"))
{
if (sqlerrcode)
errcode(sqlerrcode);
errmsg("%s", PyBytes_AS_STRING(message));
if (hint)
errhint("%s", PyBytes_AS_STRING(hint));
if (detail)
errdetail("%s", PyBytes_AS_STRING(detail));
if (context)
errcontext("%s", PyBytes_AS_STRING(context));
errfinish(0);
}
rob = Py_None;
Py_INCREF(rob);
}
PG_CATCH();
{
PyErr_SetPgError(false);
/*
* Disable the inclusion of the traceback in the context?
*
* fetch and normalize, then
* set .inhibit_pl_context = True or False
*/
if (inhibit_pl_context)
{
PyObj exc, val, tb;
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
PyObject_SetAttr(val, PYSTR(pg_inhibit_pl_context), Py_True);
PyErr_Restore(exc, val, tb);
}
rob = NULL;
}
PG_END_TRY();
}
Py_RELEASE_SPACE();
}
Py_DEALLOCATE_OWNER();
return(rob);
}
/*
* Get the execution context's invoking function. None if none.
*/
static PyObj
py_get_func(PyObj self)
{
PyObj rob;
if (PL_CONTEXT() && PL_FN_INFO())
{
rob = PL_FN_INFO()->fi_func;
if (rob == NULL)
rob = Py_None;
}
else
rob = Py_None;
Py_INCREF(rob);
return(rob);
}
/*
* Get the current list of search paths as Oids
*/
static PyObj
py_current_schemas_oid(PyObj self, PyObj args)
{
volatile PyObj rob = NULL;
PyObj include_temps_ob = NULL;
bool include_temps;
if (!PyArg_ParseTuple(args, "|O:current_schemas_oid", &include_temps_ob))
return(NULL);
if (include_temps_ob == Py_True)
include_temps = true;
else
include_temps = false;
PG_TRY();
{
List *l;
int i = 0, len;
l = fetch_search_path(include_temps);
len = list_length(l);
rob = PyTuple_New(len);
if (rob != NULL)
{
ListCell *lc;
foreach(lc, l)
{
if (i < 0)
{
list_free(l);
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many active namespaces in path")
));
}
PyTuple_SET_ITEM(rob, i, PyLong_FromOid(lfirst_oid(lc)));
if (PyTuple_GET_ITEM(rob, i) == NULL)
{
Py_DECREF(rob);
rob = NULL;
break;
}
++i;
}
}
list_free(l);
}
PG_CATCH();
{
Py_XDECREF(rob);
rob = NULL;
PyErr_SetPgError(false);
}
PG_END_TRY();
return(rob);
}
/*
* Get the current list of search path names as strings
*/
static PyObj
py_current_schemas(PyObj self, PyObj args)
{
volatile PyObj rob = NULL;
PyObj include_temps_ob = NULL;
bool include_temps;
if (!PyArg_ParseTuple(args, "|O:current_schemas", &include_temps_ob))
return(NULL);
if (include_temps_ob == Py_True)
include_temps = true;
else
include_temps = false;
PG_TRY();
{
List *l;
int i = 0, len;
l = fetch_search_path(true);
len = list_length(l);
rob = PyTuple_New(len);
if (rob != NULL)
{
ListCell *lc;
if (PyTuple_GET_SIZE(rob) != len)
{
list_free(l);
ereport(ERROR,
(errcode(ERRCODE_PROGRAM_LIMIT_EXCEEDED),
errmsg("too many active namespaces in path")
));
}
foreach(lc, l)
{
HeapTuple ht;
PyObj name;
ht = SearchSysCache(NAMESPACEOID,
ObjectIdGetDatum(lfirst_oid(lc)),
0, 0, 0);
if (HeapTupleIsValid(ht))
{
Form_pg_namespace ns;
ns = (Form_pg_namespace) GETSTRUCT(ht);
name = PyUnicode_FromCString(NameStr(ns->nspname));
PyTuple_SET_ITEM(rob, i, name);
ReleaseSysCache(ht);
if (name == NULL)
{
Py_DECREF(rob);
rob = NULL;
break;
}
}
else
{
/*
* Schema doesn't exist anymore? Just fill it with a None.
*/
PyTuple_SET_ITEM(rob, i, Py_None);
Py_INCREF(Py_None);
}
++i;
}
}
list_free(l);
}
PG_CATCH();
{
Py_XDECREF(rob);
rob = NULL;
PyErr_SetPgError(false);
}
PG_END_TRY();
return(rob);
}
/*
* Utility function to be used with map() in order to wrap an object in a tuple.
*/
static PyObj
_tuplewrap(PyObj self, PyObj args)
{
Py_INCREF(args);
return(args);
}
static PyObj
py_make_sqlstate(PyObj self, PyObj code)
{
int r;
if (!sqlerrcode_from_PyObject(code, &r))
return(NULL);
return(PyLong_FromLong((long) r));
}
#if PG_VERSION_NUM < 80500
/*
* py_notify - pg_notify "rewrite" without payload
*/
static PyObj
py_notify(PyObj self, PyObj args)
{
PyObj channel, payload = NULL;
PyObj rob = NULL;
if (!PyArg_ParseTuple(args, "O|O:notify", &channel, &payload))
return(NULL);
if (payload != NULL)
{
PyErr_SetString(PyExc_NotImplementedError,
"payload given to Postgres.notify, but backend does not support payloads");
return(NULL);
}
if (DB_IS_NOT_READY())
return(NULL);
Py_INCREF(channel);
PyObject_StrBytes(&channel);
if (channel == NULL)
return(NULL);
PG_TRY();
{
Async_Notify(PyBytes_AS_STRING(channel));
rob = Py_None;
}
PG_CATCH();
{
PyErr_SetPgError(false);
}
PG_END_TRY();
Py_DECREF(channel);
Py_XINCREF(rob);
return(rob);
}
#else
/*
* py_notify - pg_notify "rewrite" with payload
*/
static PyObj
py_notify(PyObj self, PyObj args)
{
PyObj channel, payload = NULL;
char *payload_string = NULL;
PyObj rob = NULL;
if (!PyArg_ParseTuple(args, "O|O:notify", &channel, &payload))
return(NULL);
if (DB_IS_NOT_READY())
return(NULL);
Py_INCREF(channel);
PyObject_StrBytes(&channel);
if (channel == NULL)
return(NULL);
if (payload != NULL)
{
Py_INCREF(payload);
PyObject_StrBytes(&payload);
if (payload != NULL)
payload_string = PyBytes_AS_STRING(payload);
else
{
/*
* Failed to encode payload.
*/
Py_DECREF(channel);
return(NULL);
}
}
PG_TRY();
{
Async_Notify(PyBytes_AS_STRING(channel), payload_string);
rob = Py_None;
}
PG_CATCH();
{
PyErr_SetPgError(false);
}
PG_END_TRY();
Py_DECREF(channel);
Py_XDECREF(payload);
Py_XINCREF(rob);
return(rob);
}
#endif /* notify with or without payload */
static PyObj
pypg_uid(void)
{
return(PyPg_oid_FromObjectId(GetUserId()));
}
static PyObj
pypg_session_uid(void)
{
return(PyPg_oid_FromObjectId(GetSessionUserId()));
}
static PyObj
py_transaction_timestamp(PyObj self)
{
PyObj rob = NULL;
TimestampTz ts;
ts = GetCurrentTransactionStartTimestamp();
rob = PyPgObject_New((PyObj) &PyPg_timestamptz_Type,
TimestampTzGetDatum(ts));
return(rob);
}
static PyObj
py_statement_timestamp(PyObj self)
{
PyObj rob = NULL;
TimestampTz ts;
ts = GetCurrentStatementStartTimestamp();
rob = PyPgObject_New((PyObj) &PyPg_timestamptz_Type,
TimestampTzGetDatum(ts));
return(rob);
}
static PyObj
py_clock_timestamp(PyObj self)
{
PyObj rob = NULL;
TimestampTz ts;
ts = GetCurrentTimestamp();
rob = PyPgObject_New((PyObj) &PyPg_timestamptz_Type,
TimestampTzGetDatum(ts));
return(rob);
}
static PyObj
py_quote_ident(PyObj self, PyObj ob)
{
volatile PyObj rob = NULL;
const char *instr, *outstr;
Py_INCREF(ob);
PyObject_StrBytes(&ob);
if (ob == NULL)
return(NULL);
instr = PyBytes_AS_STRING(ob);
PG_TRY();
{
outstr = quote_identifier(instr);
rob = PyUnicode_FromCString(outstr);
if (outstr != instr)
pfree((void *) outstr);
}
PG_CATCH();
{
PyErr_SetPgError(false);
Py_XDECREF(rob);
rob = NULL;
}
PG_END_TRY();
Py_DECREF(ob);
return(rob);
}
static PyObj
py_quote_literal(PyObj self, PyObj ob)
{
volatile PyObj rob = NULL;
text *s = NULL, *txt = NULL;
Py_INCREF(ob);
PyObject_StrBytes(&ob);
if (ob == NULL)
return(NULL);
PG_TRY();
{
Py_ssize_t size = PyBytes_GET_SIZE(ob);
/* s = cstring_to_text_with_len(PyBytes_AS_STRING(ob), PyBytes_GET_SIZE(ob)); */
s = palloc((int) size + VARHDRSZ);
SET_VARSIZE(s, (int) size + VARHDRSZ);
Py_MEMCPY(VARDATA(s), PyBytes_AS_STRING(ob), size);
txt = DatumGetTextP(DirectFunctionCall1(quote_literal, PointerGetDatum(s)));
pfree(s);
rob = PyUnicode_FromTEXT(txt);
pfree(txt);
}
PG_CATCH();
{
PyErr_SetPgError(false);
Py_XDECREF(rob);
rob = NULL;
}
PG_END_TRY();
Py_DECREF(ob);
return(rob);
}
static PyObj
py_quote_nullable(PyObj self, PyObj ob)
{
if (ob == Py_None)
return(PyUnicode_FromCString("NULL"));
return(py_quote_literal(self, ob));
}
static PyObj
py_lo_create(PyObj self, PyObj args)
{
Oid lo_oid = InvalidOid;
PyObj oid_ob = NULL;
if (!PyArg_ParseTuple(args, "|O:_lo_create", &oid_ob))
return(NULL);
if (oid_ob != NULL)
{
if (Oid_FromPyObject(oid_ob, &lo_oid))
return(NULL);
}
if (DB_IS_NOT_READY())
return(NULL);
PG_TRY();
{
lo_oid = DirectFunctionCall1(lo_create, ObjectIdGetDatum(lo_oid));
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromOid(lo_oid));
}
static PyObj
py_execute(PyObj self, PyObj sql_str)
{
if (PL_FN_READONLY())
{
PyErr_SetString(PyExc_RuntimeError, "cannot execute from a non-volatile function");
return(NULL);
}
if (DB_IS_NOT_READY())
return(NULL);
Py_INCREF(sql_str);
PyObject_StrBytes(&sql_str);
if (sql_str == NULL)
return(NULL);
SPI_push();
PG_TRY();
{
execute_statements(PyBytes_AS_STRING(sql_str));
}
PG_CATCH();
{
Py_DECREF(sql_str);
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
SPI_pop();
Py_DECREF(sql_str);
Py_INCREF(Py_None);
return(Py_None);
}
static PyObj
py_lo_unlink(PyObj self, PyObj args)
{
Oid lo_id;
int32 status;
PyObj oid_ob = NULL;
if (!PyArg_ParseTuple(args, "O:_lo_unlink", &oid_ob))
return(NULL);
if (Oid_FromPyObject(oid_ob, &lo_id))
return(NULL);
if (DB_IS_NOT_READY())
return(NULL);
PG_TRY();
{
status = DatumGetInt32(DirectFunctionCall1(
lo_unlink, ObjectIdGetDatum(lo_id)));
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromLong((long) status));
}
static PyObj
py_lo_open(PyObj self, PyObj args)
{
Oid lo_id;
int32 fd, mode;
PyObj oid_ob = NULL;
if (!PyArg_ParseTuple(args, "Oi:_lo_open", &oid_ob, &mode))
return(NULL);
if (Oid_FromPyObject(oid_ob, &lo_id))
return(NULL);
if (DB_IS_NOT_READY())
return(NULL);
PG_TRY();
{
fd = DatumGetInt32(DirectFunctionCall2(
lo_open, ObjectIdGetDatum(lo_id), mode));
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromLong((long) fd));
}
static PyObj
py_lo_write(PyObj self, PyObj args)
{
int fd, len, status;
char *data;
if (DB_IS_NOT_READY())
return(NULL);
if (!PyArg_ParseTuple(args, "iy#:_lo_write", &fd, &data, &len))
return(NULL);
PG_TRY();
{
status = lo_write(fd, data, (int) len);
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromLong((long) status));
}
static PyObj
py_lo_read(PyObj self, PyObj args)
{
int fd, len;
PyObj rob = NULL;
if (DB_IS_NOT_READY())
return(NULL);
if (!PyArg_ParseTuple(args, "ii:_lo_read", &fd, &len))
return(NULL);
PG_TRY();
{
char *buf;
int readbytes;
buf = palloc(len);
readbytes = lo_read(fd, buf, len);
rob = PyBytes_FromStringAndSize(buf, readbytes);
pfree(buf);
}
PG_CATCH();
{
Py_XDECREF(rob);
rob = NULL;
PyErr_SetPgError(false);
}
PG_END_TRY();
return(rob);
}
static PyObj
py_lo_close(PyObj self, PyObj args)
{
int fd;
if (DB_IS_NOT_READY())
return(NULL);
if (!PyArg_ParseTuple(args, "i:_lo_close", &fd))
return(NULL);
PG_TRY();
{
DirectFunctionCall1(lo_close, Int32GetDatum(fd));
/*
* No status..
*/
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
Py_INCREF(Py_None);
return(Py_None);
}
static PyObj
py_lo_tell(PyObj self, PyObj args)
{
int32 fd, position;
if (DB_IS_NOT_READY())
return(NULL);
if (!PyArg_ParseTuple(args, "i:_lo_tell", &fd))
return(NULL);
PG_TRY();
{
position = DirectFunctionCall1(lo_tell,
Int32GetDatum(fd));
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromLong((long) position));
}
static PyObj
py_lo_seek(PyObj self, PyObj args)
{
int32 fd, offset, whence = 0, status = 0;
if (DB_IS_NOT_READY())
return(NULL);
if (!PyArg_ParseTuple(args, "ii|i:_lo_seek", &fd, &offset, &whence))
return(NULL);
PG_TRY();
{
status = DirectFunctionCall3(lo_lseek,
Int32GetDatum(fd),
Int32GetDatum(offset),
Int32GetDatum(whence));
}
PG_CATCH();
{
PyErr_SetPgError(false);
return(NULL);
}
PG_END_TRY();
return(PyLong_FromLong((long) status));
}
static PyObj
py_memstats(PyObj self)
{
MemoryContextStats(PythonMemoryContext);
Py_INCREF(Py_None);
return(Py_None);
}
static PyObj
py_cleartypecache(PyObj self)
{
PyPgClearTypeCache();
Py_INCREF(Py_None);
return(Py_None);
}
static PyMethodDef PyPgModule_Methods[] = {
{"_memstats", (PyCFunction) py_memstats, METH_NOARGS,
PyDoc_STR("print PythonMemoryContext stats to stderr")},
{"_cleartypecache", (PyCFunction) py_cleartypecache, METH_NOARGS,
PyDoc_STR("clear the type cache dictionary")},
{"__get_Postgres_source__", (PyCFunction) py_get_Postgres_source, METH_NOARGS,
PyDoc_STR("get the Python source to the Postgres module")},
{"__get_Postgres_project_source__", (PyCFunction) py_get_Postgres_project_source, METH_NOARGS,
PyDoc_STR("get the Python source to the Postgres.project module")},
{"__get_func__", (PyCFunction) py_get_func, METH_NOARGS,
PyDoc_STR("get the function that executed the Python code")},
{"current_schemas_oid", (PyCFunction) py_current_schemas_oid, METH_VARARGS,
PyDoc_STR("get a tuple of Oids representing the current search_path")},
{"current_schemas", (PyCFunction) py_current_schemas, METH_VARARGS,
PyDoc_STR("get a tuple of names representing the current search_path")},
{"_tuplewrap", (PyCFunction) _tuplewrap, METH_VARARGS,
PyDoc_STR("wrap the argument in a tuple")},
{"make_sqlstate", (PyCFunction) py_make_sqlstate, METH_O,
PyDoc_STR("convert a string into an SQL-state integer")},
{"ereport", (PyCFunction) py_ereport, METH_VARARGS|METH_KEYWORDS,
PyDoc_STR("emit a report using Postgres' ereport facility")},
{"quote_ident", (PyCFunction) py_quote_ident, METH_O,
PyDoc_STR("quote the identifier")},
{"quote_literal", (PyCFunction) py_quote_literal, METH_O,
PyDoc_STR("quote the literal")},