Skip to content

Commit 2844323

Browse files
Only generate the version & build info once.
1 parent d8a5092 commit 2844323

File tree

6 files changed

+30
-9
lines changed

6 files changed

+30
-9
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ PyAPI_FUNC(int) _Py_IsLocaleCoercionTarget(const char *ctype_loc);
3333

3434
/* Various one-time initializers */
3535

36+
extern void _Py_InitVersion(void);
3637
extern PyStatus _PyImport_Init(void);
3738
extern PyStatus _PyFaulthandler_Init(int enable);
3839
extern int _PyTraceMalloc_Init(int enable);

Modules/getbuildinfo.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,18 @@
3131
#define GITBRANCH ""
3232
#endif
3333

34+
static int initialized = 0;
35+
static char buildinfo[50 + sizeof(GITVERSION) +
36+
((sizeof(GITTAG) > sizeof(GITBRANCH)) ?
37+
sizeof(GITTAG) : sizeof(GITBRANCH))];
38+
3439
const char *
3540
Py_GetBuildInfo(void)
3641
{
37-
static char buildinfo[50 + sizeof(GITVERSION) +
38-
((sizeof(GITTAG) > sizeof(GITBRANCH)) ?
39-
sizeof(GITTAG) : sizeof(GITBRANCH))];
42+
if (initialized) {
43+
return buildinfo;
44+
}
45+
initialized = 1;
4046
const char *revision = _Py_gitversion();
4147
const char *sep = *revision ? ":" : "";
4248
const char *gitid = _Py_gitidentifier();

Python/getversion.c

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55

66
#include "patchlevel.h"
77

8-
const char *
9-
Py_GetVersion(void)
8+
static int initialized = 0;
9+
static char version[250];
10+
11+
void _Py_InitVersion(void)
1012
{
11-
static char version[250];
13+
if (initialized) {
14+
return;
15+
}
16+
initialized = 1;
1217
PyOS_snprintf(version, sizeof(version), "%.80s (%.80s) %.80s",
1318
PY_VERSION, Py_GetBuildInfo(), Py_GetCompiler());
19+
}
20+
21+
const char *
22+
Py_GetVersion(void)
23+
{
24+
_Py_InitVersion();
1425
return version;
1526
}
1627

Python/pylifecycle.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,8 @@ pycore_init_runtime(_PyRuntimeState *runtime,
600600
*/
601601
_PyRuntimeState_SetFinalizing(runtime, NULL);
602602

603+
_Py_InitVersion();
604+
603605
status = _Py_HashRandomization_Init(config);
604606
if (_PyStatus_EXCEPTION(status)) {
605607
return status;

Tools/c-analyzer/cpython/globals-to-fix.tsv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,6 @@ Python/dtoa.c - freelist -
333333
Python/dtoa.c - private_mem -
334334

335335
# local buffer
336-
Python/getversion.c Py_GetVersion version -
337336
Python/suggestions.c levenshtein_distance buffer -
338337

339338
# linked list

Tools/c-analyzer/cpython/ignored.tsv

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ Python/import.c - PyImport_Inittab -
4040
#-----------------------
4141
# effectively const, initialized during init
4242

43+
Modules/getbuildinfo.c - buildinfo -
44+
Modules/getbuildinfo.c - initialized -
4345
Objects/floatobject.c - double_format -
4446
Objects/floatobject.c - float_format -
4547
Objects/unicodeobject.c - bloom_linebreak -
@@ -48,6 +50,8 @@ Python/bootstrap_hash.c - _Py_HashSecret_Initialized -
4850
Python/fileutils.c - _Py_open_cloexec_works -
4951
Python/fileutils.c - force_ascii -
5052
Python/fileutils.c set_inheritable ioctl_works -
53+
Python/getversion.c - initialized -
54+
Python/getversion.c - version -
5155
Python/import.c import_find_and_load header -
5256
Python/initconfig.c - orig_argv -
5357
Python/preconfig.c - Py_FileSystemDefaultEncoding -
@@ -98,8 +102,6 @@ Python/pathconfig.c - _Py_path_config -
98102
# XXX The analyzer should have ignored these.
99103
Modules/_io/_iomodule.c - _PyIO_Module -
100104
Modules/_sqlite/module.c - _sqlite3module -
101-
# a pre-allocated buffer with a very unlikely race
102-
Modules/getbuildinfo.c Py_GetBuildInfo buildinfo -
103105

104106

105107
##################################

0 commit comments

Comments
 (0)