mirror of
https://github.com/psycopg/psycopg2.git
synced 2025-06-25 15:23:24 +03:00
Added PY_2, PY_3 macros and used uniformly
This commit is contained in:
parent
8448b3b840
commit
b9d0808f95
|
@ -44,7 +44,7 @@ asis_getquoted(asisObject *self, PyObject *args)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
rv = PyObject_Str(self->wrapped);
|
rv = PyObject_Str(self->wrapped);
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
/* unicode to bytes in Py3 */
|
/* unicode to bytes in Py3 */
|
||||||
if (rv) {
|
if (rv) {
|
||||||
PyObject *tmp = PyUnicode_AsUTF8String(rv);
|
PyObject *tmp = PyUnicode_AsUTF8String(rv);
|
||||||
|
|
|
@ -45,8 +45,6 @@ binary_escape(unsigned char *from, size_t from_length,
|
||||||
return PQescapeBytea(from, from_length, to_length);
|
return PQescapeBytea(from, from_length, to_length);
|
||||||
}
|
}
|
||||||
|
|
||||||
#define HAS_BUFFER (PY_MAJOR_VERSION < 3)
|
|
||||||
|
|
||||||
/* binary_quote - do the quote process on plain and unicode strings */
|
/* binary_quote - do the quote process on plain and unicode strings */
|
||||||
|
|
||||||
static PyObject *
|
static PyObject *
|
||||||
|
@ -77,7 +75,7 @@ binary_quote(binaryObject *self)
|
||||||
buffer_len = view.len;
|
buffer_len = view.len;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if HAS_BUFFER
|
#if PY_2
|
||||||
if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
|
if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
|
||||||
if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
|
if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
|
||||||
&buffer_len) < 0) {
|
&buffer_len) < 0) {
|
||||||
|
|
|
@ -80,7 +80,7 @@ pdecimal_getquoted(pdecimalObject *self, PyObject *args)
|
||||||
/* res may be unicode and may suffer for issue #57 */
|
/* res may be unicode and may suffer for issue #57 */
|
||||||
output:
|
output:
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
/* unicode to bytes in Py3 */
|
/* unicode to bytes in Py3 */
|
||||||
{
|
{
|
||||||
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
||||||
|
|
|
@ -53,7 +53,7 @@ pfloat_getquoted(pfloatObject *self, PyObject *args)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
/* unicode to bytes in Py3 */
|
/* unicode to bytes in Py3 */
|
||||||
{
|
{
|
||||||
PyObject *tmp = PyUnicode_AsUTF8String(rv);
|
PyObject *tmp = PyUnicode_AsUTF8String(rv);
|
||||||
|
|
|
@ -59,7 +59,7 @@ pint_getquoted(pintObject *self, PyObject *args)
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
/* unicode to bytes in Py3 */
|
/* unicode to bytes in Py3 */
|
||||||
{
|
{
|
||||||
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
||||||
|
|
|
@ -87,7 +87,7 @@ _lobject_parse_mode(const char *mode)
|
||||||
pos += 1;
|
pos += 1;
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
rv |= LOBJECT_BINARY;
|
rv |= LOBJECT_BINARY;
|
||||||
#else
|
#else
|
||||||
rv |= LOBJECT_TEXT;
|
rv |= LOBJECT_TEXT;
|
||||||
|
|
|
@ -94,7 +94,7 @@ _get_superclass_adapter(PyObject *obj, PyObject *proto)
|
||||||
|
|
||||||
type = Py_TYPE(obj);
|
type = Py_TYPE(obj);
|
||||||
if (!(
|
if (!(
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
(Py_TPFLAGS_HAVE_CLASS & type->tp_flags) &&
|
(Py_TPFLAGS_HAVE_CLASS & type->tp_flags) &&
|
||||||
#endif
|
#endif
|
||||||
type->tp_mro)) {
|
type->tp_mro)) {
|
||||||
|
|
|
@ -308,7 +308,7 @@ adapters_init(PyObject *module)
|
||||||
if (0 > microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&pfloatType)) {
|
if (0 > microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&pfloatType)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
if (0 > microprotocols_add(&PyInt_Type, NULL, (PyObject*)&pintType)) {
|
if (0 > microprotocols_add(&PyInt_Type, NULL, (PyObject*)&pintType)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -321,7 +321,7 @@ adapters_init(PyObject *module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* strings */
|
/* strings */
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
if (0 > microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType)) {
|
if (0 > microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -331,7 +331,7 @@ adapters_init(PyObject *module)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* binary */
|
/* binary */
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
if (0 > microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType)) {
|
if (0 > microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType)) {
|
||||||
goto exit;
|
goto exit;
|
||||||
}
|
}
|
||||||
|
@ -1048,7 +1048,7 @@ static PyMethodDef psycopgMethods[] = {
|
||||||
{NULL, NULL, 0, NULL} /* Sentinel */
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
static struct PyModuleDef psycopgmodule = {
|
static struct PyModuleDef psycopgmodule = {
|
||||||
PyModuleDef_HEAD_INIT,
|
PyModuleDef_HEAD_INIT,
|
||||||
"_psycopg",
|
"_psycopg",
|
||||||
|
@ -1094,7 +1094,7 @@ INIT_MODULE(_psycopg)(void)
|
||||||
if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; }
|
if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; }
|
||||||
|
|
||||||
/* initialize the module */
|
/* initialize the module */
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
module = Py_InitModule("_psycopg", psycopgMethods);
|
module = Py_InitModule("_psycopg", psycopgMethods);
|
||||||
#else
|
#else
|
||||||
module = PyModule_Create(&psycopgmodule);
|
module = PyModule_Create(&psycopgmodule);
|
||||||
|
@ -1114,7 +1114,7 @@ INIT_MODULE(_psycopg)(void)
|
||||||
Dprintf("psycopgmodule: module initialization complete");
|
Dprintf("psycopgmodule: module initialization complete");
|
||||||
|
|
||||||
exit:
|
exit:
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
return module;
|
return module;
|
||||||
#else
|
#else
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -26,15 +26,20 @@
|
||||||
#ifndef PSYCOPG_PYTHON_H
|
#ifndef PSYCOPG_PYTHON_H
|
||||||
#define PSYCOPG_PYTHON_H 1
|
#define PSYCOPG_PYTHON_H 1
|
||||||
|
|
||||||
#include <structmember.h>
|
#define PY_2 (PY_MAJOR_VERSION == 2)
|
||||||
#if PY_MAJOR_VERSION < 3
|
#define PY_3 (PY_MAJOR_VERSION == 3)
|
||||||
#include <stringobject.h>
|
|
||||||
|
#if PY_2 && PY_VERSION_HEX < 0x02070000
|
||||||
|
#error "psycopg requires Python 2.7"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if ((PY_VERSION_HEX < 0x02070000) \
|
#if PY_3 && PY_VERSION_HEX < 0x03040000
|
||||||
|| ((PY_VERSION_HEX >= 0x03000000) \
|
#error "psycopg requires Python 3.4"
|
||||||
&& (PY_VERSION_HEX < 0x03040000)) )
|
#endif
|
||||||
# error "psycopg requires Python 2.7 or 3.4+"
|
|
||||||
|
#include <structmember.h>
|
||||||
|
#if PY_2
|
||||||
|
#include <stringobject.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* hash() return size changed around version 3.2a4 on 64bit platforms. Before
|
/* hash() return size changed around version 3.2a4 on 64bit platforms. Before
|
||||||
|
@ -59,7 +64,7 @@ typedef unsigned long Py_uhash_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Abstract from text type. Only supported for ASCII and UTF-8 */
|
/* Abstract from text type. Only supported for ASCII and UTF-8 */
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
#define Text_Type PyString_Type
|
#define Text_Type PyString_Type
|
||||||
#define Text_Check(s) PyString_Check(s)
|
#define Text_Check(s) PyString_Check(s)
|
||||||
#define Text_Format(f,a) PyString_Format(f,a)
|
#define Text_Format(f,a) PyString_Format(f,a)
|
||||||
|
@ -73,7 +78,7 @@ typedef unsigned long Py_uhash_t;
|
||||||
#define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n)
|
#define Text_FromUTF8AndSize(s,n) PyUnicode_FromStringAndSize(s,n)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
#define PyInt_Type PyLong_Type
|
#define PyInt_Type PyLong_Type
|
||||||
#define PyInt_Check PyLong_Check
|
#define PyInt_Check PyLong_Check
|
||||||
#define PyInt_AsLong PyLong_AsLong
|
#define PyInt_AsLong PyLong_AsLong
|
||||||
|
@ -89,9 +94,9 @@ typedef unsigned long Py_uhash_t;
|
||||||
#define PyNumber_Int PyNumber_Long
|
#define PyNumber_Int PyNumber_Long
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#endif /* PY_MAJOR_VERSION > 2 */
|
#endif /* PY_3 */
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
#define Bytes_Type PyString_Type
|
#define Bytes_Type PyString_Type
|
||||||
#define Bytes_Check PyString_Check
|
#define Bytes_Check PyString_Check
|
||||||
#define Bytes_CheckExact PyString_CheckExact
|
#define Bytes_CheckExact PyString_CheckExact
|
||||||
|
@ -131,7 +136,7 @@ typedef unsigned long Py_uhash_t;
|
||||||
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);
|
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);
|
||||||
|
|
||||||
/* Mangle the module name into the name of the module init function */
|
/* Mangle the module name into the name of the module init function */
|
||||||
#if PY_MAJOR_VERSION > 2
|
#if PY_3
|
||||||
#define INIT_MODULE(m) PyInit_ ## m
|
#define INIT_MODULE(m) PyInit_ ## m
|
||||||
#else
|
#else
|
||||||
#define INIT_MODULE(m) init ## m
|
#define INIT_MODULE(m) init ## m
|
||||||
|
|
|
@ -667,7 +667,7 @@ typecast_cast(PyObject *obj, const char *str, Py_ssize_t len, PyObject *curs)
|
||||||
* Notice that this way it is about impossible to create a python
|
* Notice that this way it is about impossible to create a python
|
||||||
* typecaster on a binary type. */
|
* typecaster on a binary type. */
|
||||||
if (str) {
|
if (str) {
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
s = PyString_FromStringAndSize(str, len);
|
s = PyString_FromStringAndSize(str, len);
|
||||||
#else
|
#else
|
||||||
s = conn_decode(((cursorObject *)curs)->conn, str, len);
|
s = conn_decode(((cursorObject *)curs)->conn, str, len);
|
||||||
|
|
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
/** INTEGER - cast normal integers (4 bytes) to python int **/
|
/** INTEGER - cast normal integers (4 bytes) to python int **/
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
static PyObject *
|
static PyObject *
|
||||||
typecast_INTEGER_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
typecast_INTEGER_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
||||||
{
|
{
|
||||||
|
@ -66,7 +66,7 @@ typecast_FLOAT_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
||||||
|
|
||||||
if (s == NULL) { Py_RETURN_NONE; }
|
if (s == NULL) { Py_RETURN_NONE; }
|
||||||
if (!(str = Text_FromUTF8AndSize(s, len))) { return NULL; }
|
if (!(str = Text_FromUTF8AndSize(s, len))) { return NULL; }
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
flo = PyFloat_FromString(str, NULL);
|
flo = PyFloat_FromString(str, NULL);
|
||||||
#else
|
#else
|
||||||
flo = PyFloat_FromString(str);
|
flo = PyFloat_FromString(str);
|
||||||
|
@ -102,7 +102,7 @@ typecast_UNICODE_cast(const char *s, Py_ssize_t len, PyObject *curs)
|
||||||
|
|
||||||
/** STRING - cast strings of any type to python string **/
|
/** STRING - cast strings of any type to python string **/
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
#define typecast_STRING_cast typecast_BYTES_cast
|
#define typecast_STRING_cast typecast_BYTES_cast
|
||||||
#else
|
#else
|
||||||
#define typecast_STRING_cast typecast_UNICODE_cast
|
#define typecast_STRING_cast typecast_UNICODE_cast
|
||||||
|
|
|
@ -53,7 +53,7 @@ chunk_repr(chunkObject *self)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
|
|
||||||
static Py_ssize_t
|
static Py_ssize_t
|
||||||
chunk_getreadbuffer(chunkObject *self, Py_ssize_t segment, void **ptr)
|
chunk_getreadbuffer(chunkObject *self, Py_ssize_t segment, void **ptr)
|
||||||
|
@ -182,7 +182,7 @@ typecast_BINARY_cast(const char *s, Py_ssize_t l, PyObject *curs)
|
||||||
buffer = NULL;
|
buffer = NULL;
|
||||||
chunk->len = (Py_ssize_t)len;
|
chunk->len = (Py_ssize_t)len;
|
||||||
|
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
if ((res = PyBuffer_FromObject((PyObject *)chunk, 0, chunk->len)) == NULL)
|
if ((res = PyBuffer_FromObject((PyObject *)chunk, 0, chunk->len)) == NULL)
|
||||||
goto exit;
|
goto exit;
|
||||||
#else
|
#else
|
||||||
|
|
|
@ -197,7 +197,7 @@ psycopg_ensure_bytes(PyObject *obj)
|
||||||
STEALS(1) PyObject *
|
STEALS(1) PyObject *
|
||||||
psycopg_ensure_text(PyObject *obj)
|
psycopg_ensure_text(PyObject *obj)
|
||||||
{
|
{
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
return obj;
|
return obj;
|
||||||
#else
|
#else
|
||||||
if (obj) {
|
if (obj) {
|
||||||
|
@ -316,7 +316,7 @@ exit:
|
||||||
PyObject *
|
PyObject *
|
||||||
psycopg_text_from_chars_safe(const char *str, Py_ssize_t len, PyObject *decoder)
|
psycopg_text_from_chars_safe(const char *str, Py_ssize_t len, PyObject *decoder)
|
||||||
{
|
{
|
||||||
#if PY_MAJOR_VERSION < 3
|
#if PY_2
|
||||||
|
|
||||||
if (!str) { Py_RETURN_NONE; }
|
if (!str) { Py_RETURN_NONE; }
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user