Skip to content

Commit b9d0808

Browse files
committed
Added PY_2, PY_3 macros and used uniformly
1 parent 8448b3b commit b9d0808

File tree

13 files changed

+38
-35
lines changed

13 files changed

+38
-35
lines changed

psycopg/adapter_asis.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ asis_getquoted(asisObject *self, PyObject *args)
4444
}
4545
else {
4646
rv = PyObject_Str(self->wrapped);
47-
#if PY_MAJOR_VERSION > 2
47+
#if PY_3
4848
/* unicode to bytes in Py3 */
4949
if (rv) {
5050
PyObject *tmp = PyUnicode_AsUTF8String(rv);

psycopg/adapter_binary.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ binary_escape(unsigned char *from, size_t from_length,
4545
return PQescapeBytea(from, from_length, to_length);
4646
}
4747

48-
#define HAS_BUFFER (PY_MAJOR_VERSION < 3)
49-
5048
/* binary_quote - do the quote process on plain and unicode strings */
5149

5250
static PyObject *
@@ -77,7 +75,7 @@ binary_quote(binaryObject *self)
7775
buffer_len = view.len;
7876
}
7977

80-
#if HAS_BUFFER
78+
#if PY_2
8179
if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
8280
if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
8381
&buffer_len) < 0) {

psycopg/adapter_pdecimal.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pdecimal_getquoted(pdecimalObject *self, PyObject *args)
8080
/* res may be unicode and may suffer for issue #57 */
8181
output:
8282

83-
#if PY_MAJOR_VERSION > 2
83+
#if PY_3
8484
/* unicode to bytes in Py3 */
8585
{
8686
PyObject *tmp = PyUnicode_AsUTF8String(res);

psycopg/adapter_pfloat.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ pfloat_getquoted(pfloatObject *self, PyObject *args)
5353
goto exit;
5454
}
5555

56-
#if PY_MAJOR_VERSION > 2
56+
#if PY_3
5757
/* unicode to bytes in Py3 */
5858
{
5959
PyObject *tmp = PyUnicode_AsUTF8String(rv);

psycopg/adapter_pint.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ pint_getquoted(pintObject *self, PyObject *args)
5959
goto exit;
6060
}
6161

62-
#if PY_MAJOR_VERSION > 2
62+
#if PY_3
6363
/* unicode to bytes in Py3 */
6464
{
6565
PyObject *tmp = PyUnicode_AsUTF8String(res);

psycopg/lobject_int.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ _lobject_parse_mode(const char *mode)
8787
pos += 1;
8888
break;
8989
default:
90-
#if PY_MAJOR_VERSION < 3
90+
#if PY_2
9191
rv |= LOBJECT_BINARY;
9292
#else
9393
rv |= LOBJECT_TEXT;

psycopg/microprotocols.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ _get_superclass_adapter(PyObject *obj, PyObject *proto)
9494

9595
type = Py_TYPE(obj);
9696
if (!(
97-
#if PY_MAJOR_VERSION < 3
97+
#if PY_2
9898
(Py_TPFLAGS_HAVE_CLASS & type->tp_flags) &&
9999
#endif
100100
type->tp_mro)) {

psycopg/psycopgmodule.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ adapters_init(PyObject *module)
308308
if (0 > microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&pfloatType)) {
309309
goto exit;
310310
}
311-
#if PY_MAJOR_VERSION < 3
311+
#if PY_2
312312
if (0 > microprotocols_add(&PyInt_Type, NULL, (PyObject*)&pintType)) {
313313
goto exit;
314314
}
@@ -321,7 +321,7 @@ adapters_init(PyObject *module)
321321
}
322322

323323
/* strings */
324-
#if PY_MAJOR_VERSION < 3
324+
#if PY_2
325325
if (0 > microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType)) {
326326
goto exit;
327327
}
@@ -331,7 +331,7 @@ adapters_init(PyObject *module)
331331
}
332332

333333
/* binary */
334-
#if PY_MAJOR_VERSION < 3
334+
#if PY_2
335335
if (0 > microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType)) {
336336
goto exit;
337337
}
@@ -1048,7 +1048,7 @@ static PyMethodDef psycopgMethods[] = {
10481048
{NULL, NULL, 0, NULL} /* Sentinel */
10491049
};
10501050

1051-
#if PY_MAJOR_VERSION > 2
1051+
#if PY_3
10521052
static struct PyModuleDef psycopgmodule = {
10531053
PyModuleDef_HEAD_INIT,
10541054
"_psycopg",
@@ -1094,7 +1094,7 @@ INIT_MODULE(_psycopg)(void)
10941094
if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; }
10951095

10961096
/* initialize the module */
1097-
#if PY_MAJOR_VERSION < 3
1097+
#if PY_2
10981098
module = Py_InitModule("_psycopg", psycopgMethods);
10991099
#else
11001100
module = PyModule_Create(&psycopgmodule);
@@ -1114,7 +1114,7 @@ INIT_MODULE(_psycopg)(void)
11141114
Dprintf("psycopgmodule: module initialization complete");
11151115

11161116
exit:
1117-
#if PY_MAJOR_VERSION > 2
1117+
#if PY_3
11181118
return module;
11191119
#else
11201120
return;

psycopg/python.h

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@
2626
#ifndef PSYCOPG_PYTHON_H
2727
#define PSYCOPG_PYTHON_H 1
2828

29-
#include <structmember.h>
30-
#if PY_MAJOR_VERSION < 3
31-
#include <stringobject.h>
29+
#define PY_2 (PY_MAJOR_VERSION == 2)
30+
#define PY_3 (PY_MAJOR_VERSION == 3)
31+
32+
#if PY_2 && PY_VERSION_HEX < 0x02070000
33+
#error "psycopg requires Python 2.7"
3234
#endif
3335

34-
#if ((PY_VERSION_HEX < 0x02070000) \
35-
|| ((PY_VERSION_HEX >= 0x03000000) \
36-
&& (PY_VERSION_HEX < 0x03040000)) )
37-
# error "psycopg requires Python 2.7 or 3.4+"
36+
#if PY_3 && PY_VERSION_HEX < 0x03040000
37+
#error "psycopg requires Python 3.4"
38+
#endif
39+
40+
#include <structmember.h>
41+
#if PY_2
42+
#include <stringobject.h>
3843
#endif
3944

4045
/* hash() return size changed around version 3.2a4 on 64bit platforms. Before
@@ -59,7 +64,7 @@ typedef unsigned long Py_uhash_t;
5964
#endif
6065

6166
/* Abstract from text type. Only supported for ASCII and UTF-8 */
62-
#if PY_MAJOR_VERSION < 3
67+
#if PY_2
6368
#define Text_Type PyString_Type
6469
#define Text_Check(s) PyString_Check(s)
6570
#define Text_Format(f,a) PyString_Format(f,a)
@@ -73,7 +78,7 @@ typedef unsigned long Py_uhash_t;
7378
#define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n)
7479
#endif
7580

76-
#if PY_MAJOR_VERSION > 2
81+
#if PY_3
7782
#define PyInt_Type PyLong_Type
7883
#define PyInt_Check PyLong_Check
7984
#define PyInt_AsLong PyLong_AsLong
@@ -89,9 +94,9 @@ typedef unsigned long Py_uhash_t;
8994
#define PyNumber_Int PyNumber_Long
9095
#endif
9196

92-
#endif /* PY_MAJOR_VERSION > 2 */
97+
#endif /* PY_3 */
9398

94-
#if PY_MAJOR_VERSION < 3
99+
#if PY_2
95100
#define Bytes_Type PyString_Type
96101
#define Bytes_Check PyString_Check
97102
#define Bytes_CheckExact PyString_CheckExact
@@ -131,7 +136,7 @@ typedef unsigned long Py_uhash_t;
131136
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);
132137

133138
/* Mangle the module name into the name of the module init function */
134-
#if PY_MAJOR_VERSION > 2
139+
#if PY_3
135140
#define INIT_MODULE(m) PyInit_ ## m
136141
#else
137142
#define INIT_MODULE(m) init ## m

psycopg/typecast.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ typecast_cast(PyObject *obj, const char *str, Py_ssize_t len, PyObject *curs)
667667
* Notice that this way it is about impossible to create a python
668668
* typecaster on a binary type. */
669669
if (str) {
670-
#if PY_MAJOR_VERSION < 3
670+
#if PY_2
671671
s = PyString_FromStringAndSize(str, len);
672672
#else
673673
s = conn_decode(((cursorObject *)curs)->conn, str, len);

0 commit comments

Comments
 (0)