Skip to content

Commit c150975

Browse files
committed
CONPY-63:
Implement module attributes __version__ and __version_info__. __version__ returns the module version number as a string in format "MAJOR.MINOR.PATCH" while __version_info__ returns the version number as a tuple in format (MAJOR, MINOR, PATCH, PRE, 0). Pre is the pre-release segment as described in PEP-440.
1 parent f66e185 commit c150975

File tree

3 files changed

+82
-3
lines changed

3 files changed

+82
-3
lines changed

setup.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
1212
long_description = f.read()
1313

14+
define_macros= []
15+
1416
# read settings from site.cfg
1517
c= ConfigParser()
1618
c.read(['site.cfg'])
@@ -23,10 +25,27 @@
2325

2426
cfg = get_config(options)
2527

28+
PY_MARIADB_AUTHORS= "Georg Richter"
29+
30+
PY_MARIADB_MAJOR_VERSION=0
31+
PY_MARIADB_MINOR_VERSION=9
32+
PY_MARIADB_PATCH_VERSION=59
33+
PY_MARIADB_PRE_RELEASE_SEGMENT="b"
34+
35+
PY_MARIADB_VERSION= "%s.%s.%s" % (PY_MARIADB_MAJOR_VERSION, PY_MARIADB_MINOR_VERSION, PY_MARIADB_PATCH_VERSION)
36+
37+
# Since we increase patch version even for alpha/beta/rc, pre release nr will be always zero.
38+
PY_MARIADB_PRE_RELEASE_NR=0
2639

40+
define_macros.append(("PY_MARIADB_VERSION", "\"%s\"" % PY_MARIADB_VERSION))
41+
define_macros.append(("PY_MARIADB_MAJOR_VERSION", PY_MARIADB_MAJOR_VERSION))
42+
define_macros.append(("PY_MARIADB_MINOR_VERSION", PY_MARIADB_MINOR_VERSION))
43+
define_macros.append(("PY_MARIADB_PATCH_VERSION", PY_MARIADB_PATCH_VERSION))
44+
define_macros.append(("PY_MARIADB_PRE_RELEASE_SEGMENT", "\"%s\"" % PY_MARIADB_PRE_RELEASE_SEGMENT))
45+
define_macros.append(("PY_MARIADB_AUTHORS", "\"%s\"" % PY_MARIADB_AUTHORS))
2746

2847
setup(name='mariadb',
29-
version='0.9.59',
48+
version=PY_MARIADB_VERSION,
3049
python_requires='>=3.6',
3150
classifiers = [
3251
'Development Status :: 4 - Beta',
@@ -50,7 +69,7 @@
5069
description='Python MariaDB extension',
5170
long_description=long_description,
5271
long_description_content_type='text/markdown',
53-
author='Georg Richter',
72+
author=PY_MARIADB_AUTHORS,
5473
license='LGPL 2.1',
5574
url='https://www.github.com/mariadb-corporation/mariadb-connector-python',
5675
project_urls={
@@ -64,6 +83,7 @@
6483
'src/mariadb_parser.c',
6584
'src/mariadb_pooling.c',
6685
'src/mariadb_dbapitype.c', 'src/mariadb_indicator.c'],
86+
define_macros= define_macros,
6787
include_dirs=cfg.includes,
6888
library_dirs=cfg.lib_dirs,
6989
libraries=cfg.libs,

src/mariadb.c

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ static void mariadb_add_exception(PyObject *module,
140140
PyMODINIT_FUNC PyInit_mariadb(void)
141141
{
142142
PyObject *module= PyModule_Create(&mariadb_module);
143+
PyObject *version_info;
143144
struct st_constants *intvals= int_constants;
144145

145146
Py_TYPE(&MrdbConnection_Type) = &PyType_Type;
@@ -193,7 +194,31 @@ PyMODINIT_FUNC PyInit_mariadb(void)
193194
PyModule_AddIntConstant(module, intvals->name,
194195
intvals->u.lvalue);
195196
intvals++;
196-
}
197+
}
198+
199+
/* PEP-396: Module version numbers */
200+
PyModule_AddObject(module, "__version__",
201+
PyUnicode_FromString(PY_MARIADB_VERSION));
202+
203+
if (!(version_info= PyTuple_New(5)))
204+
{
205+
goto error;
206+
}
207+
if (PyTuple_SetItem(version_info, 0, PyLong_FromLong(PY_MARIADB_MAJOR_VERSION)) ||
208+
PyTuple_SetItem(version_info, 1, PyLong_FromLong(PY_MARIADB_MINOR_VERSION)) ||
209+
PyTuple_SetItem(version_info, 2, PyLong_FromLong(PY_MARIADB_PATCH_VERSION)) ||
210+
PyTuple_SetItem(version_info, 3, PyUnicode_FromString(PY_MARIADB_PRE_RELEASE_SEGMENT)) ||
211+
PyTuple_SetItem(version_info, 4, PyLong_FromLong(0L)))
212+
{
213+
goto error;
214+
}
215+
216+
PyModule_AddObject(module, "__version_info__", version_info);
217+
218+
219+
220+
PyModule_AddObject(module, "__author__",
221+
PyUnicode_FromString(PY_MARIADB_AUTHORS));
197222

198223
/* PEP-249: mandatory module globals */
199224
PyModule_AddObject(module, "apilevel",

test/integration/test_module.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env python -O
2+
# -*- coding: utf-8 -*-
3+
4+
import os
5+
import unittest
6+
7+
import mariadb
8+
9+
from test.base_test import create_connection
10+
from test.conf_test import conf
11+
import platform
12+
13+
14+
class TestConnection(unittest.TestCase):
15+
16+
def setUp(self):
17+
self.connection = create_connection()
18+
19+
def tearDown(self):
20+
del self.connection
21+
22+
def test_conpy_63(self):
23+
version= mariadb.__version__
24+
version_info= mariadb.__version_info__
25+
26+
num_version= list(map(int, version.split('.')))
27+
28+
self.assertEqual(num_version[0], version_info[0])
29+
self.assertEqual(num_version[1], version_info[1])
30+
self.assertEqual(num_version[2], version_info[2])
31+
self.assertEqual(0, version_info[4])
32+
33+
if __name__ == '__main__':
34+
unittest.main()

0 commit comments

Comments
 (0)