Skip to content
Open
Changes from 1 commit
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
32 changes: 27 additions & 5 deletions Modules/_zoneinfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ static const int SOURCE_FILE = 2;

static const size_t ZONEINFO_STRONG_CACHE_MAX_SIZE = 8;

static const int MINYEAR = 1;
static const int MAXYEAR = 9999;

// Forward declarations
static int
load_data(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self,
Expand Down Expand Up @@ -2200,17 +2203,24 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
unsigned char fold;
if (PyDateTime_Check(dt)) {
fold = PyDateTime_DATE_GET_FOLD(dt);
} else {
}
else {
PyObject *fold_obj = PyObject_GetAttrString(dt, "fold");
if (fold_obj == NULL) {
return NULL;
}

fold = (unsigned char)PyLong_AsLong(fold_obj);
long fold_int = PyLong_AsLong(fold_obj);
Py_DECREF(fold_obj);
if (PyErr_Occurred()) {
return NULL;
}
if (fold_int < 0 || fold_int > 2) {
PyErr_Format(PyExc_ValueError,
"fold must be 0 or 1, got %d", fold_int);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
"fold must be 0 or 1, got %d", fold_int);
"fold must be 0 or 1, got %ld", fold_int);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Or use int and PyLong_AsLong() for fold_int.

return NULL;
}
fold = (unsigned char)fold_int;
Comment thread
serhiy-storchaka marked this conversation as resolved.
}

assert(fold < 2);
Expand All @@ -2224,20 +2234,32 @@ find_ttinfo(zoneinfo_state *state, PyZoneInfo_ZoneInfo *self, PyObject *dt)
int year;
if (PyDateTime_Check(dt)) {
year = PyDateTime_GET_YEAR(dt);
} else {
}
else {
PyObject *year_obj = PyObject_GetAttrString(dt, "year");
if (year_obj == NULL) {
return NULL;
}

year = PyLong_AsLong(year_obj);
year = PyLong_AsInt(year_obj);
Py_DECREF(year_obj);
if (PyErr_Occurred()) {
return NULL;
}

if (year < MINYEAR || year > 9999) {
PyErr_Format(PyExc_ValueError,
"year out of range, should be in (%d, %d) but got %d",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

See error messages in check_date_args(), datetime_date_fromisocalendar_impl(), utc_to_seconds().

MINYEAR,
MAXYEAR,
year
);
return NULL;
}
}
return find_tzrule_ttinfo(&(self->tzrule_after), ts, fold, year);
} else {
}
else {
size_t idx = _bisect(ts, local_transitions, self->num_transitions) - 1;
assert(idx < self->num_transitions);
return self->trans_ttinfos[idx];
Expand Down
Loading