Skip to content

Commit

Permalink
Update pygame.version to not be an autogen file
Browse files Browse the repository at this point in the history
  • Loading branch information
ankith26 committed Oct 28, 2023
1 parent 6157363 commit 31d0b51
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 65 deletions.
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
/Setup
*.pyc
/build/
/src_py/version.py

# Windows prebuilt external libraries
/prebuilt-x??
Expand Down
52 changes: 0 additions & 52 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down
11 changes: 11 additions & 0 deletions src_c/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src_c/include/_pygame.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) \
Expand Down
2 changes: 1 addition & 1 deletion src_py/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 12 additions & 4 deletions buildconfig/version.py.in → src_py/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand All @@ -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"]
17 changes: 10 additions & 7 deletions test/version_test.py
Original file line number Diff line number Diff line change
@@ -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")

Expand All @@ -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
Expand All @@ -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__":
Expand Down

0 comments on commit 31d0b51

Please sign in to comment.