Skip to content

Commit

Permalink
Add support for new libversion features and upcoming 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AMDmi3 committed Sep 11, 2019
1 parent c8d8330 commit 9c63fa7
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ matrix:
- python: 3.6
env: CC=gcc LIBVERSION=2.8.2
- python: 3.6
env: CC=gcc LIBVERSION=2.9.0
env: CC=gcc LIBVERSION=2.9.1
before_script:
- pip install flake8
- pip install flake8-builtins
Expand Down
6 changes: 6 additions & 0 deletions libversion/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

from libversion._libversion import ANY_IS_PATCH as ANY_IS_PATCH
from libversion._libversion import P_IS_PATCH as P_IS_PATCH
from libversion._libversion import LOWER_BOUND as LOWER_BOUND
from libversion._libversion import UPPER_BOUND as UPPER_BOUND
from libversion._libversion import version_compare as version_compare
from libversion._libversion import version_compare2 as version_compare2
from libversion._libversion import version_compare4 as version_compare4
Expand All @@ -36,6 +38,8 @@

'ANY_IS_PATCH',
'P_IS_PATCH',
'LOWER_BOUND',
'UPPER_BOUND',

'Version'
]
Expand All @@ -46,6 +50,8 @@ class Version:

P_IS_PATCH = P_IS_PATCH
ANY_IS_PATCH = ANY_IS_PATCH
LOWER_BOUND = LOWER_BOUND
UPPER_BOUND = UPPER_BOUND

def __init__(self, value: str, flags: int = 0) -> None:
self.value = value
Expand Down
2 changes: 2 additions & 0 deletions libversion/_libversion.pyi
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
ANY_IS_PATCH: int
P_IS_PATCH: int
LOWER_BOUND: int
UPPER_BOUND: int

def version_compare2(v1: str, v2: str) -> int: ...
def version_compare4(v1: str, v2: str, flags1: int, flags2: int) -> int: ...
Expand Down
40 changes: 25 additions & 15 deletions src/_libversion.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,19 @@
#include <Python.h>
#include <libversion/version.h>

#if defined(LIBVERSION_VERSION_ATLEAST)
# if LIBVERSION_VERSION_ATLEAST(2, 7, 0)
# define HAS_VERSION_COMPARE2 1
# define HAS_VERSION_COMPARE4 1
# endif
#endif

#if defined(LIBVERSION_VERSION_ATLEAST)
# if LIBVERSION_VERSION_ATLEAST(3, 0, 0)
# define HAS_VERSION_BOUNDS 1
# endif
#endif

static PyObject* py_version_compare2(PyObject *self, PyObject *args) {
(void)self; // (unused)

Expand All @@ -33,12 +46,8 @@ static PyObject* py_version_compare2(PyObject *self, PyObject *args) {
return NULL;

return PyLong_FromLong(
#if defined(LIBVERSION_VERSION_ATLEAST)
# if LIBVERSION_VERSION_ATLEAST(2, 7, 0)
#ifdef HAS_VERSION_COMPARE2
version_compare2(v1, v2)
# else
version_compare_simple(v1, v2)
# endif
#else
version_compare_simple(v1, v2)
#endif
Expand All @@ -57,12 +66,8 @@ static PyObject* py_version_compare4(PyObject *self, PyObject *args) {
return NULL;

return PyLong_FromLong(
#if defined(LIBVERSION_VERSION_ATLEAST)
# if LIBVERSION_VERSION_ATLEAST(2, 7, 0)
#ifdef HAS_VERSION_COMPARE4
version_compare4(v1, v2, flags1, flags2)
# else
version_compare_flags2(v1, v2, flags1, flags2)
# endif
#else
version_compare_flags2(v1, v2, flags1, flags2)
#endif
Expand All @@ -81,12 +86,8 @@ static PyObject* py_version_compare(PyObject *self, PyObject *args) {
return NULL;

return PyLong_FromLong(
#if defined(LIBVERSION_VERSION_ATLEAST)
# if LIBVERSION_VERSION_ATLEAST(2, 7, 0)
#ifdef HAS_VERSION_COMPARE4
version_compare4(v1, v2, flags1, flags2)
# else
version_compare_flags2(v1, v2, flags1, flags2)
# endif
#else
version_compare_flags2(v1, v2, flags1, flags2)
#endif
Expand Down Expand Up @@ -121,5 +122,14 @@ PyMODINIT_FUNC PyInit__libversion(void) {
PyModule_AddIntConstant(m, "P_IS_PATCH", VERSIONFLAG_P_IS_PATCH);
PyModule_AddIntConstant(m, "ANY_IS_PATCH", VERSIONFLAG_ANY_IS_PATCH);

#ifdef HAS_VERSION_BOUNDS
PyModule_AddIntConstant(m, "LOWER_BOUND", VERSIONFLAG_LOWER_BOUND);
PyModule_AddIntConstant(m, "UPPER_BOUND", VERSIONFLAG_UPPER_BOUND);
#else
// stub
PyModule_AddIntConstant(m, "LOWER_BOUND", 0);
PyModule_AddIntConstant(m, "UPPER_BOUND", 0);
#endif

return m;
}
10 changes: 9 additions & 1 deletion tests/test_cversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import unittest

from libversion import ANY_IS_PATCH, P_IS_PATCH, version_compare
from libversion import ANY_IS_PATCH, P_IS_PATCH, LOWER_BOUND, UPPER_BOUND, version_compare


class TestLibVersion(unittest.TestCase):
Expand All @@ -37,6 +37,14 @@ def test_cversion_compare_flag_any_is_patch(self) -> None:
self.assertEqual(version_compare('1.0a1', '1.0a1', 0, ANY_IS_PATCH), -1)
self.assertEqual(version_compare('1.0a1', '1.0a1', ANY_IS_PATCH, 0), 1)

@unittest.skipIf(LOWER_BOUND == 0 or UPPER_BOUND == 0, "LOWER_BOUND/UPPER_BOUND flags are not supported by underlying libversion, please update it to 3.0.0")
def test_cversion_compare_bound_flags(self) -> None:
self.assertEqual(version_compare('1.0a1', '1.0', 0, 0), -1)
self.assertEqual(version_compare('1.0a1', '1.0', 0, LOWER_BOUND), 1)

self.assertEqual(version_compare('1.0.1', '1.0', 0, 0), 1)
self.assertEqual(version_compare('1.0.1', '1.0', 0, UPPER_BOUND), -1)


if __name__ == '__main__':
unittest.main()

0 comments on commit 9c63fa7

Please sign in to comment.