Moved more methods and propertys from C to Python code

This commit is contained in:
Georg Richter
2021-03-21 22:44:42 +01:00
parent 1299be5031
commit 27730cd018
10 changed files with 76 additions and 197 deletions

View File

@ -303,15 +303,3 @@ PyDoc_STRVAR(
"Returns the alphanumeric version of connected database. The numeric version\n"
"can be obtained with server_version property."
);
PyDoc_STRVAR(
connection_enter__doc__,
"(read)\n\n"
"returns a copy of the connection"
);
PyDoc_STRVAR(
connection_exit__doc__,
"--\n"
"closes the connection"
);

View File

@ -265,15 +265,3 @@ PyDoc_STRVAR(
"Indicates if the current result set contains in out or out parameter\n"
"from a previous executed stored procedure."
);
PyDoc_STRVAR(
cursor_enter__doc__,
"(read)\n\n"
"returns a copy of the cursor"
);
PyDoc_STRVAR(
cursor_exit__doc__,
"--\n"
"closes the cursor"
);

View File

@ -16,50 +16,11 @@
or write to the Free Software Foundation, Inc.,
51 Franklin St., Fifth Floor, Boston, MA 02110, USA
*************************************************************************************/
PyDoc_STRVAR(
module_binary__doc__,
"Binary(string)\n"
"--\n"
"\n"
"This function constructs an object capable of holding a binary (long)\n"
"string value.\n"
);
PyDoc_STRVAR(
module_connect__doc__,
__connect__doc__
);
PyDoc_STRVAR(
module_DateFromTicks__doc__,
"DateFromTicks(ticks)\n"
"--\n"
"\n"
"This function constructs an object holding a date value from the given\n"
"ticks value (number of seconds since the epoch). For more information\n"
"see the documentation of the standard Python time module"
);
PyDoc_STRVAR(
module_TimeFromTicks__doc__,
"TimeFromTicks(ticks)\n"
"--\n"
"\n"
"This function constructs an object holding a time value from the given\n"
"ticks value (number of seconds since the epoch). For more information\n"
"see the documentation of the standard Python time module"
);
PyDoc_STRVAR(
module_TimestampFromTicks__doc__,
"TimestampFromTicks(ticks)\n"
"--\n"
"\n"
"This function constructs an object holding a time stamp value from the given\n"
"ticks value (number of seconds since the epoch). For more information\n"
"see the documentation of the standard Python time module"
);
PyDoc_STRVAR(
module_Date__doc__,
"Date(year, month, day)\n"

View File

@ -20,8 +20,6 @@ from ._mariadb import (
PoolError,
ProgrammingError,
Warning,
__version__,
__version_info__,
mariadbapi_version,
)
@ -31,10 +29,18 @@ _POOLS= _CONNECTION_POOLS= {}
from mariadb.dbapi20 import *
from mariadb.connectionpool import *
from mariadb.cursor import Cursor
from mariadb.release_info import __version__ as __version__
from mariadb.release_info import __version_info__ as __version_info__
from mariadb.release_info import __author__ as __author__
# disable for now, until tests are in place
# from mariadb.pooling import *
def Binary(obj):
"""This method constructs an object capable of holding a binary (long)
string value."""
return bytes(obj)
def connect(*args, **kwargs):
from mariadb.connections import Connection
if kwargs and "pool_name" in kwargs:
@ -47,3 +53,4 @@ def connect(*args, **kwargs):
return Connection(*args, **kwargs)
Connection= connect

View File

@ -21,6 +21,8 @@ import mariadb
import socket
import time
from mariadb.constants import STATUS
_DEFAULT_CHARSET = "utf8mb4"
_DEFAULT_COLLATION = "utf8mb4_general_ci"
@ -33,10 +35,14 @@ class Connection(mariadb._mariadb.connection):
self.__pool = None
self.__last_used = 0
# self._autocommit= kwargs.pop("autocommit", True)
# self._converter= kwargs.pop("converter", None)
super().__init__(*args, **kwargs)
def cursor(self, *args, **kwargs):
return mariadb._mariadb.connection.cursor(self, *args, **kwargs)
def cursor(self, **kwargs):
return mariadb.Cursor(self, **kwargs)
def close(self):
if self._Connection__pool:
@ -44,6 +50,13 @@ class Connection(mariadb._mariadb.connection):
else:
super().close()
def __enter__(self):
"Returns a copy of the connection."
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"Closes connection."
self.close()
@property
def character_set(self):
@ -55,6 +68,11 @@ class Connection(mariadb._mariadb.connection):
"""Client character set collation"""
return _DEFAULT_COLLATION
@property
def server_status(self):
"""Returns server status flags."""
return super()._server_status
@property
def socket(self):
"""Returns the socket used for database connection"""

View File

@ -59,15 +59,21 @@ def Timestamp(year, month, day, hour, minute, second):
def DateFromTicks(ticks):
"""Constructs an object holding a date value from the given ticks value
(number of seconds since the epoch)."""
(number of seconds since the epoch).
For more information see the documentation of the standard Python
time module."""
return Date(*time.localtime(ticks)[:3])
def TimeFromTicks(ticks):
"""Constructs an object holding a time value from the given ticks value
(number of seconds since the epoch)."""
(number of seconds since the epoch).
For more information see the documentation of the standard Python
time module."""
return Time(*time.localtime(ticks)[3:6])
def TimestampFromTicks(ticks):
"""Constructs an object holding a datetime value from the given ticks value
(number of seconds since the epoch)."""
(number of seconds since the epoch).
For more information see the documentation of the standard Python
time module."""
return datetime.datetime(*time.localtime(ticks)[:6])

View File

@ -40,16 +40,10 @@ Mariadb_traverse(PyObject *self,
return 0;
}
static PyObject *
Mariadb_binary(PyObject *self, PyObject *args);
static PyMethodDef
Mariadb_Methods[] =
{
/* PEP-249: mandatory */
{"Binary", (PyCFunction)Mariadb_binary,
METH_VARARGS,
module_binary__doc__},
{"connect", (PyCFunction)MrdbConnection_connect,
METH_VARARGS | METH_KEYWORDS,
module_connect__doc__},
@ -99,12 +93,6 @@ static void mariadb_add_exception(PyObject *module,
PyMODINIT_FUNC PyInit__mariadb(void)
{
PyObject *module= PyModule_Create(&mariadb_module);
PyObject *version_info;
const char *pre_release="";
#ifdef PY_MARIADB_PRE_RELEASE_SEGMENT
pre_release= PY_MARIADB_PRE_RELEASE_SEGMENT;
#endif
/* Initialite DateTimeAPI */
if (mariadb_datetime_init() ||
@ -136,33 +124,8 @@ PyMODINIT_FUNC PyInit__mariadb(void)
{
goto error;
}
PyModule_AddObject(module, "cursor", (PyObject *)&MrdbCursor_Type);
/* PEP-396: Module version numbers */
PyModule_AddObject(module, "__version__",
PyUnicode_FromString(PY_MARIADB_VERSION));
if (!(version_info= PyTuple_New(5)))
{
goto error;
}
if (PyTuple_SetItem(version_info, 0, PyLong_FromLong(PY_MARIADB_MAJOR_VERSION)) ||
PyTuple_SetItem(version_info, 1, PyLong_FromLong(PY_MARIADB_MINOR_VERSION)) ||
PyTuple_SetItem(version_info, 2, PyLong_FromLong(PY_MARIADB_PATCH_VERSION)) ||
PyTuple_SetItem(version_info, 3, PyUnicode_FromString(pre_release)) ||
PyTuple_SetItem(version_info, 4, PyLong_FromLong(0L)))
{
goto error;
}
PyModule_AddObject(module, "__version_info__", version_info);
/* PEP-249: mandatory module globals */
PyModule_AddObject(module, "apilevel",
PyUnicode_FromString(MARIADB_PY_APILEVEL));
PyModule_AddObject(module, "paramstyle",
PyUnicode_FromString(MARIADB_PY_PARAMSTYLE));
PyModule_AddObject(module, "threadsafety",
PyLong_FromLong(MARIADB_PY_THREADSAFETY));
/* optional (MariaDB specific) globals */
PyModule_AddObject(module, "mariadbapi_version",
PyUnicode_FromString(mysql_get_client_info()));
@ -211,17 +174,3 @@ error:
PyErr_SetString(PyExc_ImportError, "Mariadb module initialization failed");
return NULL;
}
static PyObject *
Mariadb_binary(PyObject *self, PyObject *args)
{
PyObject *b,*o;
if (!PyArg_ParseTuple(args, "O", &o))
{
return NULL;
}
b= PyBytes_FromObject(o);
return b;
}

View File

@ -44,11 +44,6 @@ MrdbConnection_dealloc(MrdbConnection *self);
static PyObject
*MrdbConnection_cursor(MrdbConnection *self, PyObject *args, PyObject *kwargs);
static PyObject *
MrdbConnection_enter(MrdbConnection *self, PyObject *args __attribute__((unused)));
static PyObject *
MrdbConnection_exit(MrdbConnection *self, PyObject *args __attribute__((unused)));
static PyObject *
MrdbConnection_exception(PyObject *self, void *closure);
@ -95,6 +90,9 @@ MrdbConnection_setautocommit(MrdbConnection *self, PyObject *arg,
static PyObject *
MrdbConnection_get_server_version(MrdbConnection *self);
static PyObject *
MrdbConnection_get_server_status(MrdbConnection *self);
static PyGetSetDef
MrdbConnection_sets[]=
{
@ -112,6 +110,8 @@ MrdbConnection_sets[]=
NULL},
{"warnings", (getter)MrdbConnection_warnings, NULL,
connection_warnings__doc__, NULL},
{"_server_status", (getter)MrdbConnection_get_server_status, NULL,
NULL, NULL},
{"server_version", (getter)MrdbConnection_server_version, NULL,
connection_server_version__doc__, NULL},
{"server_info", (getter)MrdbConnection_server_info, NULL,
@ -209,11 +209,7 @@ MrdbConnection_Methods[] =
METH_VARARGS,
connection_escape_string__doc__
},
{"__enter__", (PyCFunction)MrdbConnection_enter,
METH_NOARGS, connection_enter__doc__},
{"__exit__", (PyCFunction)MrdbConnection_exit,
METH_VARARGS, connection_exit__doc__},
{NULL} /* alwa+ys last */
{NULL} /* always last */
};
static struct
@ -584,6 +580,7 @@ void MrdbConnection_dealloc(MrdbConnection *self)
mysql_close(self->mysql);
Py_END_ALLOW_THREADS
}
Py_DECREF(self->server_version_info);
Py_TYPE(self)->tp_free((PyObject*)self);
}
}
@ -595,7 +592,7 @@ PyObject *MrdbConnection_close(MrdbConnection *self)
statements this should be handled in mysql_close) */
if (self->converter)
Py_XDECREF(self->converter);
Py_DECREF(self->converter);
self->converter= NULL;
Py_BEGIN_ALLOW_THREADS
@ -1317,33 +1314,21 @@ static PyObject *MrdbConnection_getautocommit(MrdbConnection *self)
}
/* }}} */
static PyObject *
MrdbConnection_enter(MrdbConnection *self, PyObject *args __attribute__((unused)))
{
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *
MrdbConnection_exit(MrdbConnection *self, PyObject *args __attribute__((unused)))
{
PyObject *rc= NULL,
*tmp= NULL;
if ((tmp= PyObject_CallMethod((PyObject *)self, "close", "")))
{
rc= Py_None;
Py_INCREF(rc);
}
Py_XDECREF(tmp);
return rc;
}
static PyObject *MrdbConnection_get_server_version(MrdbConnection *self)
{
return self->server_version_info;
}
static PyObject *MrdbConnection_get_server_status(MrdbConnection *self)
{
uint32_t server_status;
MARIADB_CHECK_CONNECTION(self, NULL);
mariadb_get_infov(self->mysql, MARIADB_CONNECTION_SERVER_STATUS, &server_status);
return PyLong_FromLong((long)server_status);
}
/* vim: set tabstop=4 */
/* vim: set shiftwidth=4 */
/* vim: set expandtab */

View File

@ -72,8 +72,6 @@ field_fetch_callback(void *data, unsigned int column, unsigned char **row);
static PyObject *mariadb_get_sequence_or_tuple(MrdbCursor *self);
static PyObject * MrdbCursor_iter(PyObject *self);
static PyObject * MrdbCursor_iternext(PyObject *self);
static PyObject *MrdbCursor_enter(MrdbCursor *self, PyObject *args __attribute__((unused)));
static PyObject *MrdbCursor_exit(MrdbCursor *self, PyObject *args __attribute__((unused)));
/* todo: write more documentation, this is just a placeholder */
static char mariadb_cursor_documentation[] =
@ -178,20 +176,11 @@ static PyMethodDef MrdbCursor_Methods[] =
{"scroll", (PyCFunction)MrdbCursor_scroll,
METH_VARARGS | METH_KEYWORDS,
cursor_scroll__doc__},
{"__enter__", (PyCFunction)MrdbCursor_enter,
METH_NOARGS, cursor_enter__doc__},
{"__exit__", (PyCFunction)MrdbCursor_exit,
METH_VARARGS, cursor_exit__doc__},
{NULL} /* always last */
};
static struct PyMemberDef MrdbCursor_Members[] =
{
{"connection",
T_OBJECT,
offsetof(MrdbCursor, connection),
READONLY,
cursor_connection__doc__},
{"statement",
T_STRING,
offsetof(MrdbCursor, statement),
@ -301,7 +290,7 @@ static int MrdbCursor_traverse(
PyTypeObject MrdbCursor_Type =
{
PyVarObject_HEAD_INIT(NULL, 0)
"mariadb.connection.cursor",
"mariadb.cursor",
sizeof(MrdbCursor),
0,
(destructor)MrdbCursor_dealloc, /* tp_dealloc */
@ -1639,25 +1628,3 @@ end:
}
return rc;
}
static PyObject *
MrdbCursor_enter(MrdbCursor *self, PyObject *args __attribute__((unused)))
{
Py_INCREF(self);
return (PyObject *)self;
}
static PyObject *
MrdbCursor_exit(MrdbCursor *self, PyObject *args __attribute__((unused)))
{
PyObject *rc= NULL,
*tmp= NULL;
if ((tmp= PyObject_CallMethod((PyObject *)self, "close", "")))
{
rc= Py_None;
Py_INCREF(rc);
}
Py_XDECREF(tmp);
return rc;
}

View File

@ -28,25 +28,32 @@ cfg = get_config(options)
PY_MARIADB_AUTHORS= "Georg Richter"
PY_MARIADB_MAJOR_VERSION=1
PY_MARIADB_MINOR_VERSION=0
PY_MARIADB_PATCH_VERSION=6
# PY_MARIADB_PRE_RELEASE_SEGMENT=""
PY_MARIADB_MINOR_VERSION=1
PY_MARIADB_PATCH_VERSION=0
PY_MARIADB_PRE_RELEASE_SEGMENT="alpha"
PY_MARIADB_VERSION= "%s.%s.%s" % (PY_MARIADB_MAJOR_VERSION, PY_MARIADB_MINOR_VERSION, PY_MARIADB_PATCH_VERSION)
# Since we increase patch version even for alpha/beta/rc, pre release nr will be always zero.
PY_MARIADB_PRE_RELEASE_NR=0
PY_MARIADB_VERSION_INFO= (PY_MARIADB_MAJOR_VERSION, PY_MARIADB_MINOR_VERSION, PY_MARIADB_PATCH_VERSION,
PY_MARIADB_PRE_RELEASE_SEGMENT, PY_MARIADB_PRE_RELEASE_NR)
define_macros.append(("PY_MARIADB_MAJOR_VERSION", PY_MARIADB_MAJOR_VERSION))
define_macros.append(("PY_MARIADB_MINOR_VERSION", PY_MARIADB_MINOR_VERSION))
define_macros.append(("PY_MARIADB_PATCH_VERSION", PY_MARIADB_PATCH_VERSION))
# define_macros.append(("PY_MARIADB_PRE_RELEASE_SEGMENT", PY_MARIADB_PRE_RELEASE_SEGMENT))
define_macros.append(("PY_MARIADB_PRE_RELEASE_SEGMENT", "\"%s\"" % PY_MARIADB_PRE_RELEASE_SEGMENT))
with open("mariadb/release_info.py", "w") as rel_info:
rel_info.write("__author__='%s'\n__version__='%s'\n__version_info__=%s" %
(PY_MARIADB_AUTHORS, PY_MARIADB_VERSION, PY_MARIADB_VERSION_INFO))
setup(name='mariadb',
version=PY_MARIADB_VERSION,
python_requires='>=3.6',
classifiers = [
'Development Status :: 5 - Production/Stable',
'Development Status :: 3 - Alpha',
'Environment :: Console',
'Environment :: MacOS X',
'Environment :: Win32 (MS Windows)',
@ -88,6 +95,9 @@ setup(name='mariadb',
extra_link_args = cfg.extra_link_args,
extra_objects= cfg.extra_objects
)],
py_modules=['mariadb.__init__', 'mariadb.constants.CLIENT', 'mariadb.constants.CURSOR', 'mariadb.field', 'mariadb.dbapi20', 'mariadb.connections', 'mariadb.connectionpool',
py_modules=['mariadb.__init__', 'mariadb.constants.CLIENT', 'mariadb.constants.CURSOR',
'mariadb.constants.STATUS',
'mariadb.field', 'mariadb.dbapi20', 'mariadb.connections', 'mariadb.connectionpool',
'mariadb.cursor', 'mariadb.release_info',
'mariadb.constants.FIELD_TYPE', 'mariadb.constants.FIELD_FLAG', 'mariadb.constants.INDICATOR'],
)