From 31d0b5159b9e5700fe4fd03e51ec2787b46e9719 Mon Sep 17 00:00:00 2001 From: Ankith Date: Sat, 28 Oct 2023 19:14:08 +0530 Subject: [PATCH] Update pygame.version to not be an autogen file --- .gitignore | 1 - setup.py | 52 ------------------- src_c/base.c | 11 ++++ src_c/include/_pygame.h | 1 + src_py/__init__.py | 2 +- .../version.py.in => src_py/version.py | 16 ++++-- test/version_test.py | 17 +++--- 7 files changed, 35 insertions(+), 65 deletions(-) rename buildconfig/version.py.in => src_py/version.py (91%) diff --git a/.gitignore b/.gitignore index 5f89150716..7800ac9f3d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ /Setup *.pyc /build/ -/src_py/version.py # Windows prebuilt external libraries /prebuilt-x?? diff --git a/setup.py b/setup.py index 8e072ed4fd..f8099c0d95 100644 --- a/setup.py +++ b/setup.py @@ -526,58 +526,6 @@ def run_install_headers(self): ['ref', ['*.txt']]]]]]) - -# generate the version module -def parse_version(ver): - return ', '.join(s for s in re.findall(r'\d+', ver)[0:3]) - - -def parse_source_version(): - pgh_major = -1 - pgh_minor = -1 - pgh_patch = -1 - major_exp_search = re.compile(r'define\s+PG_MAJOR_VERSION\s+([0-9]+)').search - minor_exp_search = re.compile(r'define\s+PG_MINOR_VERSION\s+([0-9]+)').search - patch_exp_search = re.compile(r'define\s+PG_PATCH_VERSION\s+([0-9]+)').search - pg_header = os.path.join('src_c', 'include', '_pygame.h') - with open(pg_header) as f: - for line in f: - if pgh_major == -1: - m = major_exp_search(line) - if m: pgh_major = int(m.group(1)) - if pgh_minor == -1: - m = minor_exp_search(line) - if m: pgh_minor = int(m.group(1)) - if pgh_patch == -1: - m = patch_exp_search(line) - if m: pgh_patch = int(m.group(1)) - if pgh_major == -1: - raise SystemExit("_pygame.h: cannot find PG_MAJOR_VERSION") - if pgh_minor == -1: - raise SystemExit("_pygame.h: cannot find PG_MINOR_VERSION") - if pgh_patch == -1: - raise SystemExit("_pygame.h: cannot find PG_PATCH_VERSION") - return (pgh_major, pgh_minor, pgh_patch) - - -def write_version_module(pygame_version, revision): - vernum = parse_version(pygame_version) - src_vernum = parse_source_version() - if vernum != ', '.join(str(e) for e in src_vernum): - raise SystemExit("_pygame.h version differs from 'METADATA' version" - ": %s vs %s" % (vernum, src_vernum)) - with open(os.path.join('buildconfig', 'version.py.in')) as header_file: - header = header_file.read() - with open(os.path.join('src_py', 'version.py'), 'w') as version_file: - version_file.write(header) - version_file.write('ver = "' + pygame_version + '" # pylint: disable=invalid-name\n') - version_file.write(f'vernum = PygameVersion({vernum})\n') - version_file.write('rev = "' + revision + '" # pylint: disable=invalid-name\n') - version_file.write('\n__all__ = ["SDL", "ver", "vernum", "rev"]\n') - - -write_version_module(METADATA['version'], revision) - # required. This will be filled if doing a Windows build. cmdclass = {} diff --git a/src_c/base.c b/src_c/base.c index 36903b8656..0f9cfc45fa 100644 --- a/src_c/base.c +++ b/src_c/base.c @@ -2275,6 +2275,17 @@ MODINIT_DEFINE(base) goto error; } + PyObject *version = + PyUnicode_FromFormat("%d.%d.%d.%s", PG_MAJOR_VERSION, PG_MINOR_VERSION, + PG_PATCH_VERSION, PG_VERSION_TAG); + if (!version) { + goto error; + } + if (PyModule_AddObject(module, "__version__", version)) { + Py_DECREF(version); + goto error; + } + /*some initialization*/ PyObject *quit = PyObject_GetAttrString(module, "quit"); PyObject *rval; diff --git a/src_c/include/_pygame.h b/src_c/include/_pygame.h index a5ae05138c..7cdc1cbd15 100644 --- a/src_c/include/_pygame.h +++ b/src_c/include/_pygame.h @@ -58,6 +58,7 @@ #define PG_MAJOR_VERSION 2 #define PG_MINOR_VERSION 4 #define PG_PATCH_VERSION 0 +#define PG_VERSION_TAG "dev3" #define PG_VERSIONNUM(MAJOR, MINOR, PATCH) \ (1000 * (MAJOR) + 100 * (MINOR) + (PATCH)) #define PG_VERSION_ATLEAST(MAJOR, MINOR, PATCH) \ diff --git a/src_py/__init__.py b/src_py/__init__.py index 26782ee22c..18deb6dd23 100644 --- a/src_py/__init__.py +++ b/src_py/__init__.py @@ -141,7 +141,7 @@ def warn(self): Vector2 = pygame.math.Vector2 Vector3 = pygame.math.Vector3 -__version__ = ver +from pygame.base import __version__ as __version__ # next, the "standard" modules # we still allow them to be missing for stripped down pygame distributions diff --git a/buildconfig/version.py.in b/src_py/version.py similarity index 91% rename from buildconfig/version.py.in rename to src_py/version.py index 4e96001fb6..0b01435bdc 100644 --- a/buildconfig/version.py.in +++ b/src_py/version.py @@ -26,16 +26,15 @@ The python version information should always compare greater than any previous releases. (hmm, until we get to versions > 10) """ -from pygame.base import get_sdl_version -############### -# This file is generated with version.py.in -## +from pygame.base import get_sdl_version, __version__ + class SoftwareVersion(tuple): """ A class for storing data about software versions. """ + __slots__ = () fields = "major", "minor", "patch" @@ -53,15 +52,24 @@ def __str__(self): minor = property(lambda self: self[1]) patch = property(lambda self: self[2]) + class PygameVersion(SoftwareVersion): """ Pygame Version class. """ + class SDLVersion(SoftwareVersion): """ SDL Version class. """ + _sdl_tuple = get_sdl_version() SDL = SDLVersion(_sdl_tuple[0], _sdl_tuple[1], _sdl_tuple[2]) + +ver = __version__ +vernum = PygameVersion(*map(int, ver.split(".")[:3])) +rev = "" # pylint: disable=invalid-name + +__all__ = ["SDL", "ver", "vernum", "rev"] diff --git a/test/version_test.py b/test/version_test.py index ba0bb3d024..a87362489d 100644 --- a/test/version_test.py +++ b/test/version_test.py @@ -1,6 +1,9 @@ import os import unittest +from importlib.metadata import version + +import pygame pg_header = os.path.join("src_c", "include", "_pygame.h") @@ -10,8 +13,6 @@ class VersionTest(unittest.TestCase): not os.path.isfile(pg_header), "Skipping because we cannot find _pygame.h" ) def test_pg_version_consistency(self): - from pygame import version - pgh_major = -1 pgh_minor = -1 pgh_patch = -1 @@ -34,14 +35,16 @@ def test_pg_version_consistency(self): m = patch_exp_search(line) if m: pgh_patch = int(m.group(1)) - self.assertEqual(pgh_major, version.vernum[0]) - self.assertEqual(pgh_minor, version.vernum[1]) - self.assertEqual(pgh_patch, version.vernum[2]) + self.assertEqual(pgh_major, pygame.version.vernum[0]) + self.assertEqual(pgh_minor, pygame.version.vernum[1]) + self.assertEqual(pgh_patch, pygame.version.vernum[2]) def test_sdl_version(self): - from pygame import version + self.assertEqual(len(pygame.version.SDL), 3) - self.assertEqual(len(version.SDL), 3) + def test_installed_version_and_dunder(self): + self.assertEqual(pygame.__version__, pygame.version.ver) + self.assertEqual(pygame.__version__, version("pygame-ce")) if __name__ == "__main__":