mirror of
https://github.com/psycopg/psycopg2.git
synced 2024-11-11 03:26:37 +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 {
|
||||
rv = PyObject_Str(self->wrapped);
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
/* unicode to bytes in Py3 */
|
||||
if (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);
|
||||
}
|
||||
|
||||
#define HAS_BUFFER (PY_MAJOR_VERSION < 3)
|
||||
|
||||
/* binary_quote - do the quote process on plain and unicode strings */
|
||||
|
||||
static PyObject *
|
||||
|
@ -77,7 +75,7 @@ binary_quote(binaryObject *self)
|
|||
buffer_len = view.len;
|
||||
}
|
||||
|
||||
#if HAS_BUFFER
|
||||
#if PY_2
|
||||
if (!buffer && (Bytes_Check(self->wrapped) || PyBuffer_Check(self->wrapped))) {
|
||||
if (PyObject_AsReadBuffer(self->wrapped, (const void **)&buffer,
|
||||
&buffer_len) < 0) {
|
||||
|
|
|
@ -80,7 +80,7 @@ pdecimal_getquoted(pdecimalObject *self, PyObject *args)
|
|||
/* res may be unicode and may suffer for issue #57 */
|
||||
output:
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
/* unicode to bytes in Py3 */
|
||||
{
|
||||
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
||||
|
|
|
@ -53,7 +53,7 @@ pfloat_getquoted(pfloatObject *self, PyObject *args)
|
|||
goto exit;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
/* unicode to bytes in Py3 */
|
||||
{
|
||||
PyObject *tmp = PyUnicode_AsUTF8String(rv);
|
||||
|
|
|
@ -59,7 +59,7 @@ pint_getquoted(pintObject *self, PyObject *args)
|
|||
goto exit;
|
||||
}
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
/* unicode to bytes in Py3 */
|
||||
{
|
||||
PyObject *tmp = PyUnicode_AsUTF8String(res);
|
||||
|
|
|
@ -87,7 +87,7 @@ _lobject_parse_mode(const char *mode)
|
|||
pos += 1;
|
||||
break;
|
||||
default:
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
rv |= LOBJECT_BINARY;
|
||||
#else
|
||||
rv |= LOBJECT_TEXT;
|
||||
|
|
|
@ -94,7 +94,7 @@ _get_superclass_adapter(PyObject *obj, PyObject *proto)
|
|||
|
||||
type = Py_TYPE(obj);
|
||||
if (!(
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
(Py_TPFLAGS_HAVE_CLASS & type->tp_flags) &&
|
||||
#endif
|
||||
type->tp_mro)) {
|
||||
|
|
|
@ -308,7 +308,7 @@ adapters_init(PyObject *module)
|
|||
if (0 > microprotocols_add(&PyFloat_Type, NULL, (PyObject*)&pfloatType)) {
|
||||
goto exit;
|
||||
}
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
if (0 > microprotocols_add(&PyInt_Type, NULL, (PyObject*)&pintType)) {
|
||||
goto exit;
|
||||
}
|
||||
|
@ -321,7 +321,7 @@ adapters_init(PyObject *module)
|
|||
}
|
||||
|
||||
/* strings */
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
if (0 > microprotocols_add(&PyString_Type, NULL, (PyObject*)&qstringType)) {
|
||||
goto exit;
|
||||
}
|
||||
|
@ -331,7 +331,7 @@ adapters_init(PyObject *module)
|
|||
}
|
||||
|
||||
/* binary */
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
if (0 > microprotocols_add(&PyBuffer_Type, NULL, (PyObject*)&binaryType)) {
|
||||
goto exit;
|
||||
}
|
||||
|
@ -1048,7 +1048,7 @@ static PyMethodDef psycopgMethods[] = {
|
|||
{NULL, NULL, 0, NULL} /* Sentinel */
|
||||
};
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
static struct PyModuleDef psycopgmodule = {
|
||||
PyModuleDef_HEAD_INIT,
|
||||
"_psycopg",
|
||||
|
@ -1094,7 +1094,7 @@ INIT_MODULE(_psycopg)(void)
|
|||
if (!(psyco_null = Bytes_FromString("NULL"))) { goto exit; }
|
||||
|
||||
/* initialize the module */
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
module = Py_InitModule("_psycopg", psycopgMethods);
|
||||
#else
|
||||
module = PyModule_Create(&psycopgmodule);
|
||||
|
@ -1114,7 +1114,7 @@ INIT_MODULE(_psycopg)(void)
|
|||
Dprintf("psycopgmodule: module initialization complete");
|
||||
|
||||
exit:
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
return module;
|
||||
#else
|
||||
return;
|
||||
|
|
|
@ -26,15 +26,20 @@
|
|||
#ifndef PSYCOPG_PYTHON_H
|
||||
#define PSYCOPG_PYTHON_H 1
|
||||
|
||||
#include <structmember.h>
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#include <stringobject.h>
|
||||
#define PY_2 (PY_MAJOR_VERSION == 2)
|
||||
#define PY_3 (PY_MAJOR_VERSION == 3)
|
||||
|
||||
#if PY_2 && PY_VERSION_HEX < 0x02070000
|
||||
#error "psycopg requires Python 2.7"
|
||||
#endif
|
||||
|
||||
#if ((PY_VERSION_HEX < 0x02070000) \
|
||||
|| ((PY_VERSION_HEX >= 0x03000000) \
|
||||
&& (PY_VERSION_HEX < 0x03040000)) )
|
||||
# error "psycopg requires Python 2.7 or 3.4+"
|
||||
#if PY_3 && PY_VERSION_HEX < 0x03040000
|
||||
#error "psycopg requires Python 3.4"
|
||||
#endif
|
||||
|
||||
#include <structmember.h>
|
||||
#if PY_2
|
||||
#include <stringobject.h>
|
||||
#endif
|
||||
|
||||
/* hash() return size changed around version 3.2a4 on 64bit platforms. Before
|
||||
|
@ -59,7 +64,7 @@ typedef unsigned long Py_uhash_t;
|
|||
#endif
|
||||
|
||||
/* 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_Check(s) PyString_Check(s)
|
||||
#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)
|
||||
#endif
|
||||
|
||||
#if PY_MAJOR_VERSION > 2
|
||||
#if PY_3
|
||||
#define PyInt_Type PyLong_Type
|
||||
#define PyInt_Check PyLong_Check
|
||||
#define PyInt_AsLong PyLong_AsLong
|
||||
|
@ -89,9 +94,9 @@ typedef unsigned long Py_uhash_t;
|
|||
#define PyNumber_Int PyNumber_Long
|
||||
#endif
|
||||
|
||||
#endif /* PY_MAJOR_VERSION > 2 */
|
||||
#endif /* PY_3 */
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
#define Bytes_Type PyString_Type
|
||||
#define Bytes_Check PyString_Check
|
||||
#define Bytes_CheckExact PyString_CheckExact
|
||||
|
@ -131,7 +136,7 @@ typedef unsigned long Py_uhash_t;
|
|||
HIDDEN PyObject *Bytes_Format(PyObject *format, PyObject *args);
|
||||
|
||||
/* 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
|
||||
#else
|
||||
#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
|
||||
* typecaster on a binary type. */
|
||||
if (str) {
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
s = PyString_FromStringAndSize(str, len);
|
||||
#else
|
||||
s = conn_decode(((cursorObject *)curs)->conn, str, len);
|
||||
|
|
|
@ -25,7 +25,7 @@
|
|||
|
||||
/** INTEGER - cast normal integers (4 bytes) to python int **/
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
static PyObject *
|
||||
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 (!(str = Text_FromUTF8AndSize(s, len))) { return NULL; }
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
flo = PyFloat_FromString(str, NULL);
|
||||
#else
|
||||
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 **/
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
#define typecast_STRING_cast typecast_BYTES_cast
|
||||
#else
|
||||
#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
|
||||
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;
|
||||
chunk->len = (Py_ssize_t)len;
|
||||
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
if ((res = PyBuffer_FromObject((PyObject *)chunk, 0, chunk->len)) == NULL)
|
||||
goto exit;
|
||||
#else
|
||||
|
|
|
@ -197,7 +197,7 @@ psycopg_ensure_bytes(PyObject *obj)
|
|||
STEALS(1) PyObject *
|
||||
psycopg_ensure_text(PyObject *obj)
|
||||
{
|
||||
#if PY_MAJOR_VERSION < 3
|
||||
#if PY_2
|
||||
return obj;
|
||||
#else
|
||||
if (obj) {
|
||||
|
@ -316,7 +316,7 @@ exit:
|
|||
PyObject *
|
||||
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; }
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user