1 /* python.h - python version compatibility stuff
2  *
3  * Copyright (C) 2003-2019 Federico Di Gregorio <fog@debian.org>
4  * Copyright (C) 2020-2021 The Psycopg Team
5  *
6  * This file is part of psycopg.
7  *
8  * psycopg2 is free software: you can redistribute it and/or modify it
9  * under the terms of the GNU Lesser General Public License as published
10  * by the Free Software Foundation, either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * In addition, as a special exception, the copyright holders give
14  * permission to link this program with the OpenSSL library (or with
15  * modified versions of OpenSSL that use the same license as OpenSSL),
16  * and distribute linked combinations including the two.
17  *
18  * You must obey the GNU Lesser General Public License in all respects for
19  * all of the code used other than OpenSSL.
20  *
21  * psycopg2 is distributed in the hope that it will be useful, but WITHOUT
22  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
24  * License for more details.
25  */
26 
27 #ifndef PSYCOPG_PYTHON_H
28 #define PSYCOPG_PYTHON_H 1
29 
30 #if PY_VERSION_HEX < 0x03060000
31 #error "psycopg requires Python 3.6"
32 #endif
33 
34 #include <structmember.h>
35 
36 /* Since Py_TYPE() is changed to the inline static function,
37  * Py_TYPE(obj) = new_type must be replaced with Py_SET_TYPE(obj, new_type)
38  * https://docs.python.org/3.10/whatsnew/3.10.html#id2
39  */
40 #if PY_VERSION_HEX < 0x030900A4
41   #define Py_SET_TYPE(obj, type) ((Py_TYPE(obj) = (type)), (void)0)
42 #endif
43 
44 /* FORMAT_CODE_PY_SSIZE_T is for Py_ssize_t: */
45 #define FORMAT_CODE_PY_SSIZE_T "%" PY_FORMAT_SIZE_T "d"
46 
47 /* FORMAT_CODE_SIZE_T is for plain size_t, not for Py_ssize_t: */
48 #ifdef _MSC_VER
49   /* For MSVC: */
50   #define FORMAT_CODE_SIZE_T "%Iu"
51 #else
52   /* C99 standard format code: */
53   #define FORMAT_CODE_SIZE_T "%zu"
54 #endif
55 
56 #define Text_Type PyUnicode_Type
57 #define Text_Check(s) PyUnicode_Check(s)
58 #define Text_Format(f,a) PyUnicode_Format(f,a)
59 #define Text_FromUTF8(s) PyUnicode_FromString(s)
60 #define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n)
61 
62 #define PyInt_Type             PyLong_Type
63 #define PyInt_Check            PyLong_Check
64 #define PyInt_AsLong           PyLong_AsLong
65 #define PyInt_FromLong         PyLong_FromLong
66 #define PyInt_FromString       PyLong_FromString
67 #define PyInt_FromSsize_t      PyLong_FromSsize_t
68 #define PyExc_StandardError    PyExc_Exception
69 #define PyString_FromFormat    PyUnicode_FromFormat
70 #define Py_TPFLAGS_HAVE_ITER   0L
71 #define Py_TPFLAGS_HAVE_RICHCOMPARE 0L
72 #define Py_TPFLAGS_HAVE_WEAKREFS 0L
73 
74 #ifndef PyNumber_Int
75 #define PyNumber_Int           PyNumber_Long
76 #endif
77 
78 #define Bytes_Type PyBytes_Type
79 #define Bytes_Check PyBytes_Check
80 #define Bytes_CheckExact PyBytes_CheckExact
81 #define Bytes_AS_STRING PyBytes_AS_STRING
82 #define Bytes_GET_SIZE PyBytes_GET_SIZE
83 #define Bytes_Size PyBytes_Size
84 #define Bytes_AsString PyBytes_AsString
85 #define Bytes_AsStringAndSize PyBytes_AsStringAndSize
86 #define Bytes_FromString PyBytes_FromString
87 #define Bytes_FromStringAndSize PyBytes_FromStringAndSize
88 #define Bytes_FromFormat PyBytes_FromFormat
89 #define Bytes_ConcatAndDel PyBytes_ConcatAndDel
90 #define _Bytes_Resize _PyBytes_Resize
91 
92 #define INIT_MODULE(m) PyInit_ ## m
93 
94 #define PyLong_FromOid(x) (PyLong_FromUnsignedLong((unsigned long)(x)))
95 
96 /* expose Oid attributes in Python C objects */
97 #define T_OID T_UINT
98 
99 #endif /* !defined(PSYCOPG_PYTHON_H) */
100