Skip to content

Commit 54ba2c0

Browse files
committed
gh-158948: Add auto-generated Module/getbuildinfo.h
Merge Python/getcompiler.c, Python/getcopyright.c, Python/getplatform.c and Python/getversion.c into Modules/getbuildinfo.c. Add Tools/build/generate_getbuildinfo.py to auto-generate a new Module/getbuildinfo.h header file which contains build information strings.
1 parent 7d3fca4 commit 54ba2c0

14 files changed

Lines changed: 224 additions & 225 deletions

File tree

‎.gitignore‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,11 @@ Modules/Setup.config
9999
Modules/Setup.local
100100
Modules/Setup.stdlib
101101
Modules/config.c
102+
# Generated by Tools/build/generate_getbuildinfo.py
103+
Modules/getbuildinfo.h
102104
Modules/ld_so_aix
103105
Programs/_freeze_module
106+
Programs/_getcompiler
104107
Programs/_testembed
105108
PC/python_nt*.h
106109
PC/pythonnt_rc*.h

‎Include/internal/pycore_pylifecycle.h‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ extern int _Py_IsLocaleCoercionTarget(const char *ctype_loc);
2323

2424
/* Various one-time initializers */
2525

26-
extern PyStatus _Py_GetVersion_Init(void);
2726
extern PyStatus _PyFaulthandler_Init(int enable);
2827
extern PyObject * _PyBuiltin_Init(PyInterpreterState *interp);
2928
extern int _PyBuiltin_InitPythonFunctions(PyObject *dict);
@@ -43,7 +42,6 @@ extern PyStatus _PyDateTime_InitTypes(PyInterpreterState *interp);
4342

4443
/* Various internal finalizers */
4544

46-
extern void _Py_GetVersion_Fini(void);
4745
extern int _PySignal_Init(int install_signal_handlers);
4846
extern void _PySignal_Fini(void);
4947

‎Makefile.pre.in‎

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -461,10 +461,6 @@ PYTHON_OBJS= \
461461
Python/gc_free_threading.o \
462462
Python/gc_gil.o \
463463
Python/getargs.o \
464-
Python/getcompiler.o \
465-
Python/getcopyright.o \
466-
Python/getplatform.o \
467-
Python/getversion.o \
468464
Python/ceval_gil.o \
469465
Python/hamt.o \
470466
Python/hashtable.o \
@@ -1617,6 +1613,9 @@ regen-re: $(BUILDPYTHON)
16171613
Programs/_testembed: Programs/_testembed.o $(LINK_PYTHON_DEPS)
16181614
$(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) -o $@ Programs/_testembed.o $(LINK_PYTHON_OBJS) $(LIBS) $(MODLIBS) $(SYSLIBS)
16191615

1616+
Programs/_getcompiler: Programs/_getcompiler.c
1617+
$(LINKCC) $(PY_CORE_EXE_LDFLAGS) $(LINKFORSHARED) $(PY_CPPFLAGS) -o $@ $<
1618+
16201619
############################################################################
16211620
# "Bootstrap Python" used to run Programs/_freeze_module.py
16221621

@@ -1867,18 +1866,23 @@ regen-all: regen-cases regen-slots \
18671866
############################################################################
18681867
# Special rules for object files
18691868

1870-
Modules/getbuildinfo.o: $(PARSER_OBJS) \
1871-
$(OBJECT_OBJS) \
1872-
$(PYTHON_OBJS) \
1873-
$(MODULE_OBJS) \
1874-
$(MODOBJS) \
1875-
$(DTRACE_OBJS) \
1876-
$(srcdir)/Modules/getbuildinfo.c
1877-
$(CC) -c $(PY_CORE_CFLAGS) \
1878-
-DGITVERSION="\"`LC_ALL=C $(GITVERSION)`\"" \
1879-
-DGITTAG="\"`LC_ALL=C $(GITTAG)`\"" \
1880-
-DGITBRANCH="\"`LC_ALL=C $(GITBRANCH)`\"" \
1881-
-o $@ $(srcdir)/Modules/getbuildinfo.c
1869+
Modules/getbuildinfo.h: $(PARSER_OBJS) \
1870+
$(OBJECT_OBJS) \
1871+
$(PYTHON_OBJS) \
1872+
$(MODULE_OBJS) \
1873+
$(MODOBJS) \
1874+
$(DTRACE_OBJS) \
1875+
Programs/_getcompiler \
1876+
$(srcdir)/Tools/build/generate_getbuildinfo.py
1877+
$(PYTHON_FOR_REGEN) $(srcdir)/Tools/build/generate_getbuildinfo.py $@ \
1878+
"$(MACHDEP)" \
1879+
`LC_ALL=C $(GITVERSION)` \
1880+
`LC_ALL=C $(GITTAG)` \
1881+
`LC_ALL=C $(GITBRANCH)` \
1882+
"`Programs/_getcompiler`"
1883+
1884+
Modules/getbuildinfo.o: $(srcdir)/Modules/getbuildinfo.c Modules/getbuildinfo.h
1885+
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $(srcdir)/Modules/getbuildinfo.c
18821886

18831887
Modules/getpath.o: $(srcdir)/Modules/getpath.c Python/frozen_modules/getpath.h Makefile $(PYTHON_HEADERS)
18841888
$(CC) -c $(PY_CORE_CFLAGS) -DPYTHONPATH='"$(PYTHONPATH)"' \
@@ -2028,9 +2032,6 @@ regen-sre:
20282032

20292033
Python/compile.o Python/codegen.o Python/symtable.o Python/ast_unparse.o Python/ast.o Python/future.o: $(srcdir)/Include/internal/pycore_ast.h $(srcdir)/Include/internal/pycore_ast.h
20302034

2031-
Python/getplatform.o: $(srcdir)/Python/getplatform.c
2032-
$(CC) -c $(PY_CORE_CFLAGS) -DPLATFORM='"$(MACHDEP)"' -o $@ $(srcdir)/Python/getplatform.c
2033-
20342035
Python/importdl.o: $(srcdir)/Python/importdl.c
20352036
$(CC) -c $(PY_CORE_CFLAGS) -I$(DLINCLDIR) -o $@ $(srcdir)/Python/importdl.c
20362037

‎Modules/getbuildinfo.c‎

Lines changed: 47 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,40 @@
1-
#ifndef Py_BUILD_CORE_BUILTIN
2-
# define Py_BUILD_CORE_MODULE 1
3-
#endif
1+
/* Functions to get information about the Python build */
42

53
#include "Python.h"
6-
#include "pycore_pylifecycle.h" // _Py_gitidentifier()
74

8-
#ifndef DONT_HAVE_STDIO_H
9-
#include <stdio.h>
10-
#endif
5+
#include "getbuildinfo.h"
116

12-
#ifndef DATE
13-
#ifdef __DATE__
14-
#define DATE __DATE__
15-
#else
16-
#define DATE "Jan 01 1970"
17-
#endif
18-
#endif
7+
static const char copyright[] =
8+
"\
9+
Copyright (c) 2001 Python Software Foundation.\n\
10+
All Rights Reserved.\n\
11+
\n\
12+
Copyright (c) 2000 BeOpen.com.\n\
13+
All Rights Reserved.\n\
14+
\n\
15+
Copyright (c) 1995-2001 Corporation for National Research Initiatives.\n\
16+
All Rights Reserved.\n\
17+
\n\
18+
Copyright (c) 1991-1995 Stichting Mathematisch Centrum, Amsterdam.\n\
19+
All Rights Reserved.";
1920

20-
#ifndef TIME
21-
#ifdef __TIME__
22-
#define TIME __TIME__
23-
#else
24-
#define TIME "00:00:00"
25-
#endif
26-
#endif
2721

28-
/* XXX Only unix build process has been tested */
29-
#ifndef GITVERSION
30-
#define GITVERSION ""
31-
#endif
32-
#ifndef GITTAG
33-
#define GITTAG ""
34-
#endif
35-
#ifndef GITBRANCH
36-
#define GITBRANCH ""
37-
#endif
22+
const char *
23+
Py_GetCopyright(void)
24+
{
25+
return copyright;
26+
}
3827

39-
static int initialized = 0;
40-
static char buildinfo[50 + sizeof(GITVERSION) +
41-
((sizeof(GITTAG) > sizeof(GITBRANCH)) ?
42-
sizeof(GITTAG) : sizeof(GITBRANCH))];
28+
const char *
29+
Py_GetCompiler(void)
30+
{
31+
return COMPILER;
32+
}
4333

4434
const char *
45-
Py_GetBuildInfo(void)
35+
Py_GetPlatform(void)
4636
{
47-
if (initialized) {
48-
return buildinfo;
49-
}
50-
initialized = 1;
51-
const char *revision = _Py_gitversion();
52-
const char *sep = *revision ? ":" : "";
53-
const char *gitid = _Py_gitidentifier();
54-
if (!(*gitid)) {
55-
gitid = "main";
56-
}
57-
PyOS_snprintf(buildinfo, sizeof(buildinfo),
58-
"%s%s%s, %.20s, %.9s", gitid, sep, revision,
59-
DATE, TIME);
60-
return buildinfo;
37+
return PLATFORM;
6138
}
6239

6340
const char *
@@ -69,13 +46,26 @@ _Py_gitversion(void)
6946
const char *
7047
_Py_gitidentifier(void)
7148
{
72-
const char *gittag, *gitid;
73-
gittag = GITTAG;
74-
if ((*gittag) && strcmp(gittag, "undefined") != 0)
75-
gitid = gittag;
76-
else
77-
gitid = GITBRANCH;
78-
return gitid;
49+
return GIT_IDENTIFIER;
50+
}
51+
52+
const char *
53+
Py_GetBuildInfo(void)
54+
{
55+
return BUILDINFO;
56+
}
57+
58+
// Export the Python hex version as a constant.
59+
const unsigned long Py_Version = PY_VERSION_HEX;
60+
61+
// Keep the 'version' variable for backward compatibility.
62+
// Some debuggers inspect directly the variable.
63+
static const char *version = GET_VERSION;
64+
65+
const char *
66+
Py_GetVersion(void)
67+
{
68+
return version;
7969
}
8070

8171

‎Modules/main.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,6 @@ pymain_free(void)
847847
_PyPathConfig_ClearGlobal();
848848
_Py_ClearArgcArgv();
849849
_PyRuntime_Finalize();
850-
_Py_GetVersion_Fini();
851850
}
852851

853852

‎Objects/bytesobject.c‎

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3831,13 +3831,23 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t new_size, int resize)
38313831
}
38323832

38333833
Py_ssize_t alloc = new_size;
3834-
if (resize && writer->overallocate) {
3835-
if (alloc <= (PY_SSIZE_T_MAX - alloc / OVERALLOCATE_FACTOR)) {
3836-
alloc += alloc / OVERALLOCATE_FACTOR;
3837-
}
3838-
}
38393834

38403835
if (writer->obj != NULL) {
3836+
if (resize && writer->overallocate) {
3837+
if (alloc <= (PY_SSIZE_T_MAX - alloc / OVERALLOCATE_FACTOR)) {
3838+
alloc += alloc / OVERALLOCATE_FACTOR;
3839+
}
3840+
}
3841+
3842+
if (new_size <= old_allocated + old_allocated / 8) {
3843+
/* Moderate upsize; overallocate similar to list_resize() */
3844+
alloc = new_size + (new_size >> 3);
3845+
}
3846+
else {
3847+
/* Major upsize; resize up to exact size */
3848+
alloc = new_size;
3849+
}
3850+
38413851
if (writer->use_bytearray) {
38423852
if (PyByteArray_Resize(writer->obj, alloc)) {
38433853
#ifdef Py_DEBUG
@@ -3860,6 +3870,9 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t new_size, int resize)
38603870
assert(writer->obj != NULL);
38613871
}
38623872
else {
3873+
// Do not overallocate when the first bytes/bytearray object is
3874+
// allocated
3875+
38633876
char *data;
38643877
if (writer->use_bytearray) {
38653878
writer->obj = PyByteArray_FromStringAndSize(NULL, alloc);

‎PCbuild/pythoncore.vcxproj‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -649,11 +649,7 @@
649649
<ClCompile Include="..\Python\gc_free_threading.c" />
650650
<ClCompile Include="..\Python\gc_gil.c" />
651651
<ClCompile Include="..\Python\getargs.c" />
652-
<ClCompile Include="..\Python\getcompiler.c" />
653-
<ClCompile Include="..\Python\getcopyright.c" />
654652
<ClCompile Include="..\Python\getopt.c" />
655-
<ClCompile Include="..\Python\getplatform.c" />
656-
<ClCompile Include="..\Python\getversion.c" />
657653
<ClCompile Include="..\Python\ceval_gil.c" />
658654
<ClCompile Include="..\Python\hamt.c" />
659655
<ClCompile Include="..\Python\hashtable.c" />

‎PCbuild/pythoncore.vcxproj.filters‎

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1469,21 +1469,9 @@
14691469
<ClCompile Include="..\Python\getargs.c">
14701470
<Filter>Python</Filter>
14711471
</ClCompile>
1472-
<ClCompile Include="..\Python\getcompiler.c">
1473-
<Filter>Python</Filter>
1474-
</ClCompile>
1475-
<ClCompile Include="..\Python\getcopyright.c">
1476-
<Filter>Python</Filter>
1477-
</ClCompile>
14781472
<ClCompile Include="..\Python\getopt.c">
14791473
<Filter>Python</Filter>
14801474
</ClCompile>
1481-
<ClCompile Include="..\Python\getplatform.c">
1482-
<Filter>Python</Filter>
1483-
</ClCompile>
1484-
<ClCompile Include="..\Python\getversion.c">
1485-
<Filter>Python</Filter>
1486-
</ClCompile>
14871475
<ClCompile Include="..\Python\ceval_gil.c">
14881476
<Filter>Python</Filter>
14891477
</ClCompile>
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
2-
/* Return the compiler identification, if possible. */
3-
41
#include "Python.h"
2+
#include <stdio.h> // printf()
53

64
// PC/pyconfig.h defines _Py_COMPILER
75
#ifdef _Py_COMPILER
@@ -25,8 +23,9 @@
2523

2624
#endif /* !COMPILER */
2725

28-
const char *
29-
Py_GetCompiler(void)
26+
int main()
3027
{
31-
return COMPILER;
28+
const char *compiler = COMPILER;
29+
printf("%s\n", compiler);
30+
return 0;
3231
}

‎Python/getcopyright.c‎

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)