Commit a41070c7 authored by Stefan Behnel's avatar Stefan Behnel

Add a "CYTHON_HEX_VERSION" macro in the style of PY_HEX_VERSION that user code...

Add a "CYTHON_HEX_VERSION" macro in the style of PY_HEX_VERSION that user code can use to detect the Cython version that generated the main module.
parent 586097a5
......@@ -27,7 +27,7 @@ from . import Pythran
from .Errors import error, warning
from .PyrexTypes import py_object_type
from ..Utils import open_new_file, replace_suffix, decode_filename
from ..Utils import open_new_file, replace_suffix, decode_filename, build_hex_version
from .Code import UtilityCode, IncludeCode
from .StringEncoding import EncodedString
from .Pythran import has_np_pythran
......@@ -637,6 +637,7 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
from .. import __version__
code.putln('#define CYTHON_ABI "%s"' % __version__.replace('.', '_'))
code.putln('#define CYTHON_HEX_VERSION %s' % build_hex_version(__version__))
code.putln("#define CYTHON_FUTURE_DIVISION %d" % (
Future.division in env.context.future_directives))
......
import unittest
from ..Utils import build_hex_version
class TestCythonUtils(unittest.TestCase):
def test_build_hex_version(self):
self.assertEqual('0x001D00A1', build_hex_version('0.29a1'))
self.assertEqual('0x001D00A1', build_hex_version('0.29a1'))
self.assertEqual('0x001D03C4', build_hex_version('0.29.3rc4'))
self.assertEqual('0x001D00F0', build_hex_version('0.29'))
self.assertEqual('0x040000F0', build_hex_version('4.0'))
......@@ -495,3 +495,27 @@ def raise_error_if_module_name_forbidden(full_module_name):
#it is bad idea to call the pyx-file cython.pyx, so fail early
if full_module_name == 'cython' or full_module_name.startswith('cython.'):
raise ValueError('cython is a special module, cannot be used as a module name')
def build_hex_version(version_string):
"""
Parse and translate '4.3a1' into the readable hex representation '0x040300A1' (like PY_HEX_VERSION).
"""
# First, parse '4.12a1' into [4, 12, 0, 0xA01].
digits = []
release_status = 0xF0
for digit in re.split('([.abrc]+)', version_string):
if digit in ('a', 'b', 'rc'):
release_status = {'a': 0xA0, 'b': 0xB0, 'rc': 0xC0}[digit]
digits = (digits + [0, 0])[:3] # 1.2a1 -> 1.2.0a1
elif digit != '.':
digits.append(int(digit))
digits = (digits + [0] * 3)[:4]
digits[3] += release_status
# Then, build a single hex value, two hex digits per version part.
hexversion = 0
for digit in digits:
hexversion = (hexversion << 8) + digit
return '0x%08X' % hexversion
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment