8000 gh-123681: Check NORMALIZE_CENTURY behavior at runtime; require C99 by encukou · Pull Request #136022 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-123681: Check NORMALIZE_CENTURY behavior at runtime; require C99 #136022

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions Lib/test/datetimetester.py
Original file line number Diff line number Diff line change
Expand Up @@ -1807,7 +1807,7 @@ def test_bool(self):
self.assertTrue(self.theclass.min)
self.assertTrue(self.theclass.max)

def test_strftime_y2k(self):
def check_strftime_y2k(self, specifier):
# Test that years less than 1000 are 0-padded; note that the beginning
# of an ISO 8601 year may fall in an ISO week of the year before, and
# therefore needs an offset of -1 when formatting with '%G'.
Expand All @@ -1821,22 +1821,28 @@ def test_strftime_y2k(self):
(1000, 0),
(1970, 0),
)
specifiers = 'YG'
if _time.strftime('%F', (1900, 1, 1, 0, 0, 0, 0, 1, 0)) == '1900-01-01':
specifiers += 'FC'
for year, g_offset in dataset:
for specifier in specifiers:
with self.subTest(year=year, specifier=specifier):
d = self.theclass(year, 1, 1)
if specifier == 'G':
year += g_offset
if specifier == 'C':
expected = f"{year // 100:02d}"
else:
expected = f"{year:04d}"
if specifier == 'F':
expected += f"-01-01"
self.assertEqual(d.strftime(f"%{specifier}"), expected)
with self.subTest(year=year, specifier=specifier):
d = self.theclass(year, 1, 1)
if specifier == 'G':
year += g_offset
if specifier == 'C':
expected = f"{year // 100:02d}"
else:
expected = f"{year:04d}"
if specifier == 'F':
expected += f"-01-01"
self.assertEqual(d.strftime(f"%{specifier}"), expected)

def test_strftime_y2k(self):
self.check_strftime_y2k('Y')
self.check_strftime_y2k('G')

def test_strftime_y2k_c99(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should not it be decorated with cpython_only?

# CPython requires C11; specifiers new in C99 must work.
# (Other implementations may want to disable this test.)
self.check_strftime_y2k('F')
self.check_strftime_y2k('C')

def test_replace(self):
cls = self.theclass
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Check the ``strftime()`` behavior at runtime instead of at the compile time
to support cross-compiling. Remove macros Py_NORMALIZE_CENTURY and
Py_STRFTIME_C99_SUPPORT.
26 changes: 21 additions & 5 deletions Modules/_datetimemodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1763,6 +1763,24 @@ format_utcoffset(char *buf, size_t buflen, const char *sep,
return 0;
}

/* Check whether year with century should be normalized for strftime. */
inline static int
normalize_century(void)
{
static int cache = -1;
if (cache < 0) {
char year[5];
struct tm date = {
.tm_year = -1801,
.tm_mon = 0,
.tm_mday = 1
};
cache = (strftime(year, sizeof(year), "%Y", &date) &&
strcmp(year, "0099") != 0);
}
return cache;
}

static PyObject *
make_somezreplacement(PyObject *object, char *sep, PyObject *tzinfoarg)
{
Expand Down Expand Up @@ -1934,10 +1952,9 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
}
replacement = freplacement;
}
#ifdef Py_NORMALIZE_CENTURY
else if (ch == 'Y' || ch == 'G'
|| ch == 'F' || ch == 'C'
) {
else if (normalize_century()
&& (ch == 'Y' || ch == 'G' || ch == 'F' || ch == 'C'))
{
/* 0-pad year with century as necessary */
PyObject *item = PySequence_GetItem(timetuple, 0);
if (item == NULL) {
Expand Down Expand Up @@ -1988,7 +2005,6 @@ wrap_strftime(PyObject *object, PyObject *format, PyObject *timetuple,
}
continue;
}
#endif
else {
/* percent followed by something else */
continue;
Expand Down
1 change: 1 addition & 0 deletions Tools/c-analyzer/cpython/ignored.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ Modules/_datetimemodule.c datetime_isoformat specs -
Modules/_datetimemodule.c parse_hh_mm_ss_ff correction -
Modules/_datetimemodule.c time_isoformat specs -
Modules/_datetimemodule.c - capi_types -
Modules/_datetimemodule.c normalize_century cache -
Modules/_decimal/_decimal.c - cond_map_template -
Modules/_decimal/_decimal.c - dec_signal_string -
Modules/_decimal/_decimal.c - dflt_ctx -
Expand Down
104 changes: 0 additions & 104 deletions configure

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 0 additions & 51 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -6789,57 +6789,6 @@ then
[Define if you have struct stat.st_mtimensec])
fi

AC_CACHE_CHECK([whether year with century should be normalized for strftime], [ac_cv_normalize_century], [
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <time.h>
#include <string.h>

int main(void)
{
char year[5];
struct tm date = {
.tm_year = -1801,
.tm_mon = 0,
.tm_mday = 1
};
if (strftime(year, sizeof(year), "%Y", &date) && !strcmp(year, "0099")) {
return 1;
}
return 0;
}
]])],
[ac_cv_normalize_century=yes],
[ac_cv_normalize_century=no],
[ac_cv_normalize_century=yes])])
if test "$ac_cv_normalize_century" = yes
then
AC_DEFINE([Py_NORMALIZE_CENTURY], [1],
[Define if year with century should be normalized for strftime.])
fi

AC_CACHE_CHECK([whether C99-compatible strftime specifiers are supported], [ac_cv_strftime_c99_support], [
AC_RUN_IFELSE([AC_LANG_SOURCE([[
#include <time.h>
#include <string.h>

int main(void)
{
char full_date[11];
struct tm date = {
.tm_year = 0,
.tm_mon = 0,
.tm_mday = 1
};
if (strftime(full_date, sizeof(full_date), "%F", &date) && !strcmp(full_date, "1900-01-01")) {
return 0;
}
return 1;
}
]])],
[ac_cv_strftime_c99_support=yes],
[AC_MSG_ERROR([Python requires C99-compatible strftime specifiers])],
[ac_cv_strftime_c99_support=])])

dnl check for ncursesw/ncurses and panelw/panel
dnl NOTE: old curses is not detected.
dnl have_curses=[no, yes]
Expand Down
3 changes: 0 additions & 3 deletions pyconfig.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -1743,9 +1743,6 @@
SipHash13: 3, externally defined: 0 */
#undef Py_HASH_ALGORITHM

/* Define if year with century should be normalized for strftime. */
#undef Py_NORMALIZE_CENTURY

/* Define if you want to enable remote debugging support. */
#undef Py_REMOTE_DEBUG

Expand Down
Loading
0