Skip to content

Commit c6b99e8

Browse files
authored
Merge pull request #2537 from pygame-community/ankith26-version
2 parents 89f9fd3 + 6f73590 commit c6b99e8

File tree

10 files changed

+49
-67
lines changed

10 files changed

+49
-67
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/Setup
33
*.pyc
44
/build/
5-
/src_py/version.py
65

76
# Windows prebuilt external libraries
87
/prebuilt-x??

buildconfig/stubs/gen_stubs.py

+8-2
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,9 @@
6969
"mixer": ["Channel"],
7070
"time": ["Clock"],
7171
"joystick": ["Joystick"],
72-
"geometry": ["Circle"],
72+
"base": ["__version__"], # need an explicit import
73+
# uncomment below line if Circle is added to the base namespace later
74+
# "geometry": ["Circle"],
7375
}
7476

7577
# pygame modules from which __init__.py does the equivalent of
@@ -95,7 +97,11 @@ def get_all(mod: Any):
9597
pygame_all_imports[f".{k}"] = v
9698

9799
for k in PG_STAR_IMPORTS:
98-
pygame_all_imports[f".{k}"] = get_all(getattr(pygame, k))
100+
val = get_all(getattr(pygame, k))
101+
try:
102+
pygame_all_imports[f".{k}"].extend(val)
103+
except KeyError:
104+
pygame_all_imports[f".{k}"] = val
99105

100106
# misc stubs that must be added to __init__.pyi
101107
misc_stubs = """"""

buildconfig/stubs/pygame/__init__.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ from pygame import (
3737
sysfont as sysfont,
3838
_debug as _debug,
3939
system as system,
40+
geometry as geometry,
4041
)
4142

4243
from .rect import Rect as Rect, FRect as FRect
@@ -54,6 +55,7 @@ from .mixer import Channel as Channel
5455
from .time import Clock as Clock
5556
from .joystick import Joystick as Joystick
5657
from .base import (
58+
__version__ as __version__,
5759
BufferError as BufferError,
5860
HAVE_NEWBUF as HAVE_NEWBUF,
5961
error as error,

buildconfig/stubs/pygame/base.pyi

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from typing import Any, Tuple, Callable
22

3+
__version__: str
4+
35
class error(RuntimeError): ...
46
class BufferError(Exception): ...
57

setup.py

-52
Original file line numberDiff line numberDiff line change
@@ -526,58 +526,6 @@ def run_install_headers(self):
526526
['ref',
527527
['*.txt']]]]]])
528528

529-
530-
# generate the version module
531-
def parse_version(ver):
532-
return ', '.join(s for s in re.findall(r'\d+', ver)[0:3])
533-
534-
535-
def parse_source_version():
536-
pgh_major = -1
537-
pgh_minor = -1
538-
pgh_patch = -1
539-
major_exp_search = re.compile(r'define\s+PG_MAJOR_VERSION\s+([0-9]+)').search
540-
minor_exp_search = re.compile(r'define\s+PG_MINOR_VERSION\s+([0-9]+)').search
541-
patch_exp_search = re.compile(r'define\s+PG_PATCH_VERSION\s+([0-9]+)').search
542-
pg_header = os.path.join('src_c', 'include', '_pygame.h')
543-
with open(pg_header) as f:
544-
for line in f:
545-
if pgh_major == -1:
546-
m = major_exp_search(line)
547-
if m: pgh_major = int(m.group(1))
548-
if pgh_minor == -1:
549-
m = minor_exp_search(line)
550-
if m: pgh_minor = int(m.group(1))
551-
if pgh_patch == -1:
552-
m = patch_exp_search(line)
553-
if m: pgh_patch = int(m.group(1))
554-
if pgh_major == -1:
555-
raise SystemExit("_pygame.h: cannot find PG_MAJOR_VERSION")
556-
if pgh_minor == -1:
557-
raise SystemExit("_pygame.h: cannot find PG_MINOR_VERSION")
558-
if pgh_patch == -1:
559-
raise SystemExit("_pygame.h: cannot find PG_PATCH_VERSION")
560-
return (pgh_major, pgh_minor, pgh_patch)
561-
562-
563-
def write_version_module(pygame_version, revision):
564-
vernum = parse_version(pygame_version)
565-
src_vernum = parse_source_version()
566-
if vernum != ', '.join(str(e) for e in src_vernum):
567-
raise SystemExit("_pygame.h version differs from 'METADATA' version"
568-
": %s vs %s" % (vernum, src_vernum))
569-
with open(os.path.join('buildconfig', 'version.py.in')) as header_file:
570-
header = header_file.read()
571-
with open(os.path.join('src_py', 'version.py'), 'w') as version_file:
572-
version_file.write(header)
573-
version_file.write('ver = "' + pygame_version + '" # pylint: disable=invalid-name\n')
574-
version_file.write(f'vernum = PygameVersion({vernum})\n')
575-
version_file.write('rev = "' + revision + '" # pylint: disable=invalid-name\n')
576-
version_file.write('\n__all__ = ["SDL", "ver", "vernum", "rev"]\n')
577-
578-
579-
write_version_module(METADATA['version'], revision)
580-
581529
# required. This will be filled if doing a Windows build.
582530
cmdclass = {}
583531

src_c/base.c

+11
Original file line numberDiff line numberDiff line change
@@ -2275,6 +2275,17 @@ MODINIT_DEFINE(base)
22752275
goto error;
22762276
}
22772277

2278+
PyObject *version =
2279+
PyUnicode_FromFormat("%d.%d.%d%s", PG_MAJOR_VERSION, PG_MINOR_VERSION,
2280+
PG_PATCH_VERSION, PG_VERSION_TAG);
2281+
if (!version) {
2282+
goto error;
2283+
}
2284+
if (PyModule_AddObject(module, "__version__", version)) {
2285+
Py_DECREF(version);
2286+
goto error;
2287+
}
2288+
22782289
/*some initialization*/
22792290
PyObject *quit = PyObject_GetAttrString(module, "quit");
22802291
PyObject *rval;

src_c/include/_pygame.h

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@
5858
#define PG_MAJOR_VERSION 2
5959
#define PG_MINOR_VERSION 4
6060
#define PG_PATCH_VERSION 0
61+
/* The below is of the form ".devX" for dev releases, and "" (empty) for full
62+
* releases */
63+
#define PG_VERSION_TAG ".dev3"
6164
#define PG_VERSIONNUM(MAJOR, MINOR, PATCH) \
6265
(1000 * (MAJOR) + 100 * (MINOR) + (PATCH))
6366
#define PG_VERSION_ATLEAST(MAJOR, MINOR, PATCH) \

src_py/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ def warn(self):
141141
Vector2 = pygame.math.Vector2
142142
Vector3 = pygame.math.Vector3
143143

144-
__version__ = ver
144+
from pygame.base import __version__
145145

146146
# next, the "standard" modules
147147
# we still allow them to be missing for stripped down pygame distributions

buildconfig/version.py.in renamed to src_py/version.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,15 @@
2626
The python version information should always compare greater than any previous
2727
releases. (hmm, until we get to versions > 10)
2828
"""
29-
from pygame.base import get_sdl_version
3029

31-
###############
32-
# This file is generated with version.py.in
33-
##
30+
from pygame.base import get_sdl_version, __version__
31+
3432

3533
class SoftwareVersion(tuple):
3634
"""
3735
A class for storing data about software versions.
3836
"""
37+
3938
__slots__ = ()
4039
fields = "major", "minor", "patch"
4140

@@ -53,15 +52,24 @@ def __str__(self):
5352
minor = property(lambda self: self[1])
5453
patch = property(lambda self: self[2])
5554

55+
5656
class PygameVersion(SoftwareVersion):
5757
"""
5858
Pygame Version class.
5959
"""
6060

61+
6162
class SDLVersion(SoftwareVersion):
6263
"""
6364
SDL Version class.
6465
"""
6566

67+
6668
_sdl_tuple = get_sdl_version()
6769
SDL = SDLVersion(_sdl_tuple[0], _sdl_tuple[1], _sdl_tuple[2])
70+
71+
ver = __version__ # pylint: disable=invalid-name
72+
vernum = PygameVersion(*map(int, ver.split(".")[:3]))
73+
rev = "" # pylint: disable=invalid-name
74+
75+
__all__ = ["SDL", "ver", "vernum", "rev"]

test/version_test.py

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import os
22
import unittest
33

4+
from importlib.metadata import version
5+
6+
import pygame
47

58
pg_header = os.path.join("src_c", "include", "_pygame.h")
69

@@ -10,8 +13,6 @@ class VersionTest(unittest.TestCase):
1013
not os.path.isfile(pg_header), "Skipping because we cannot find _pygame.h"
1114
)
1215
def test_pg_version_consistency(self):
13-
from pygame import version
14-
1516
pgh_major = -1
1617
pgh_minor = -1
1718
pgh_patch = -1
@@ -34,14 +35,16 @@ def test_pg_version_consistency(self):
3435
m = patch_exp_search(line)
3536
if m:
3637
pgh_patch = int(m.group(1))
37-
self.assertEqual(pgh_major, version.vernum[0])
38-
self.assertEqual(pgh_minor, version.vernum[1])
39-
self.assertEqual(pgh_patch, version.vernum[2])
38+
self.assertEqual(pgh_major, pygame.version.vernum[0])
39+
self.assertEqual(pgh_minor, pygame.version.vernum[1])
40+
self.assertEqual(pgh_patch, pygame.version.vernum[2])
4041

4142
def test_sdl_version(self):
42-
from pygame import version
43+
self.assertEqual(len(pygame.version.SDL), 3)
4344

44-
self.assertEqual(len(version.SDL), 3)
45+
def test_installed_version_and_dunder(self):
46+
self.assertEqual(pygame.__version__, pygame.version.ver)
47+
self.assertEqual(pygame.__version__, version("pygame-ce"))
4548

4649

4750
if __name__ == "__main__":

0 commit comments

Comments
 (0)