Skip to content

Commit 98ff4d5

Browse files
edison12apganssle
authored andcommitted
bpo-36782: Created C API wrappers and added missing tests for functions in the PyDateTimeAPI. (#13088)
* created a c API wrapper for pyDate_FromDate and added the test * 📜🤖 Added by blurb_it. * fixed auto-alignment by vscode * made changes as per PEP7 * Update 2019-05-04-21-25-19.bpo-36782.h3oPIb.rst * Refactored code as per requested changes * Remove Whitespace to Fix failed travis build * Update 2019-05-04-21-25-19.bpo-36782.h3oPIb.rst * Add a new line at end of ACKS * Added C API function for PyDateTime_FromDateAndTime * Added a test for the C API wrapper of PyDateTime_FromDateAndTime * Added C API function for PyDateTime_FromDateAndTime * Added a test for the C API wrapper of PyDateTime_FromDateAndTimeAndFold * Remove Whitespace using patchcheck * Added a C API function for PyTime_FromTime * Added a test for the C API wrapper of PyTime_FromTime * Added a C API function for PyTime_FromTimeAndFold * Added a test for the C API wrapper of PyTime_FromTimeAndFold * Added a C API function for PyDelta_FromDSU * Added a test for the C API wrapper of PyDelta_FromDSU * Refactor code, re-edit lines longer than 80 chars * Fix Whitespace issues in DatetimeTester * List all tests that were added in this PR * Update 2019-05-04-21-25-19.bpo-36782.h3oPIb.rst * Reformat code as per PEP7 guidelines * Remove unused varibles from another function * Added specific tests for the Fold Attribute * Update 2019-05-04-21-25-19.bpo-36782.h3oPIb.rst * Reformat code according to requested changes * Reformat code to PEP7 Guidelines * Reformat code to PEP7 Guidelines * Re-add name to blurb * Added a backtick to blurb file * Update 2019-05-04-21-25-19.bpo-36782.h3oPIb.rst * Remove the need to initialize mandatory parameters * Make the macro parameter mandatory * Re-arrange the order of unit-test args * Removed the need to initialize macro change all the int macro = 0 to int macro; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com> * Removed the need to initialize macro change all the `int macro = 0` to `int macro`; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com> * Removed the need to initialize macro change all the `int macro = 0` to `int macro`; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com> * Removed the need to initialize macro change all the `int macro = 0` to `int macro`; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com> * Removed the need to initialize macro change all the `int macro = 0` to `int macro`; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com> * Removed the need to initialize macro change all the `int macro = 0` to `int macro`; now that macro is required Co-Authored-By: Paul Ganssle <pganssle@users.noreply.github.com>
1 parent feac6cd commit 98ff4d5

File tree

4 files changed

+265
-4
lines changed

4 files changed

+265
-4
lines changed

Lib/test/datetimetester.py

Lines changed: 94 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6018,6 +6018,100 @@ class TZInfoSubclass(tzinfo):
60186018
with self.subTest(arg=arg, exact=exact):
60196019
self.assertFalse(is_tzinfo(arg, exact))
60206020

6021+
def test_date_from_date(self):
6022+
exp_date = date(1993, 8, 26)
6023+
6024+
for macro in [0, 1]:
6025+
with self.subTest(macro=macro):
6026+
c_api_date = _testcapi.get_date_fromdate(
6027+
macro,
6028+
exp_date.year,
6029+
exp_date.month,
6030+
exp_date.day)
6031+
6032+
self.assertEqual(c_api_date, exp_date)
6033+
6034+
def test_datetime_from_dateandtime(self):
6035+
exp_date = datetime(1993, 8, 26, 22, 12, 55, 99999)
6036+
6037+
for macro in [0, 1]:
6038+
with self.subTest(macro=macro):
6039+
c_api_date = _testcapi.get_datetime_fromdateandtime(
6040+
macro,
6041+
exp_date.year,
6042+
exp_date.month,
6043+
exp_date.day,
6044+
exp_date.hour,
6045+
exp_date.minute,
6046+
exp_date.second,
6047+
exp_date.microsecond)
6048+
6049+
self.assertEqual(c_api_date, exp_date)
6050+
6051+
def test_datetime_from_dateandtimeandfold(self):
6052+
exp_date = datetime(1993, 8, 26, 22, 12, 55, 99999)
6053+
6054+
for fold in [0, 1]:
6055+
for macro in [0, 1]:
6056+
with self.subTest(macro=macro, fold=fold):
6057+
c_api_date = _testcapi.get_datetime_fromdateandtimeandfold(
6058+
macro,
6059+
exp_date.year,
6060+
exp_date.month,
6061+
exp_date.day,
6062+
exp_date.hour,
6063+
exp_date.minute,
6064+
exp_date.second,
6065+
exp_date.microsecond,
6066+
exp_date.fold)
6067+
6068+
self.assertEqual(c_api_date, exp_date)
6069+
self.assertEqual(c_api_date.fold, exp_date.fold)
6070+
6071+
def test_time_from_time(self):
6072+
exp_time = time(22, 12, 55, 99999)
6073+
6074+
for macro in [0, 1]:
6075+
with self.subTest(macro=macro):
6076+
c_api_time = _testcapi.get_time_fromtime(
6077+
macro,
6078+
exp_time.hour,
6079+
exp_time.minute,
6080+
exp_time.second,
6081+
exp_time.microsecond)
6082+
6083+
self.assertEqual(c_api_time, exp_time)
6084+
6085+
def test_time_from_timeandfold(self):
6086+
exp_time = time(22, 12, 55, 99999)
6087+
6088+
for fold in [0, 1]:
6089+
for macro in [0, 1]:
6090+
with self.subTest(macro=macro, fold=fold):
6091+
c_api_time = _testcapi.get_time_fromtimeandfold(
6092+
macro,
6093+
exp_time.hour,
6094+
exp_time.minute,
6095+
exp_time.second,
6096+
exp_time.microsecond,
6097+
exp_time.fold)
6098+
6099+
self.assertEqual(c_api_time, exp_time)
6100+
self.assertEqual(c_api_time.fold, exp_time.fold)
6101+
6102+
def test_delta_from_dsu(self):
6103+
exp_delta = timedelta(26, 55, 99999)
6104+
6105+
for macro in [0, 1]:
6106+
with self.subTest(macro=macro):
6107+
c_api_delta = _testcapi.get_delta_fromdsu(
6108+
macro,
6109+
exp_delta.days,
6110+
exp_delta.seconds,
6111+
exp_delta.microseconds)
6112+
6113+
self.assertEqual(c_api_delta, exp_delta)
6114+
60216115
def test_date_from_timestamp(self):
60226116
ts = datetime(1995, 4, 12).timestamp()
60236117

@@ -6028,9 +6122,6 @@ def test_date_from_timestamp(self):
60286122
self.assertEqual(d, date(1995, 4, 12))
60296123

60306124
def test_datetime_from_timestamp(self):
6031-
ts0 = datetime(1995, 4, 12).timestamp()
6032-
ts1 = datetime(1995, 4, 12, 12, 30).timestamp()
6033-
60346125
cases = [
60356126
((1995, 4, 12), None, False),
60366127
((1995, 4, 12), None, True),

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1857,3 +1857,4 @@ Peter Åstrand
18571857
Zheao Li
18581858
Carsten Klein
18591859
Diego Rojas
1860+
Edison Abahurire
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add tests for several C API functions in the :mod:`datetime` module. Patch by Edison Abahurire.

Modules/_testcapimodule.c

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2340,6 +2340,168 @@ get_timezone_utc_capi(PyObject* self, PyObject *args) {
23402340
}
23412341
}
23422342

2343+
static PyObject *
2344+
get_date_fromdate(PyObject *self, PyObject *args)
2345+
{
2346+
PyObject *rv = NULL;
2347+
int macro;
2348+
int year, month, day;
2349+
2350+
if (!PyArg_ParseTuple(args, "piii", &macro, &year, &month, &day)) {
2351+
return NULL;
2352+
}
2353+
2354+
if (macro) {
2355+
rv = PyDate_FromDate(year, month, day);
2356+
}
2357+
else {
2358+
rv = PyDateTimeAPI->Date_FromDate(
2359+
year, month, day,
2360+
PyDateTimeAPI->DateType);
2361+
}
2362+
return rv;
2363+
}
2364+
2365+
static PyObject *
2366+
get_datetime_fromdateandtime(PyObject *self, PyObject *args)
2367+
{
2368+
PyObject *rv = NULL;
2369+
int macro;
2370+
int year, month, day;
2371+
int hour, minute, second, microsecond;
2372+
2373+
if (!PyArg_ParseTuple(args, "piiiiiii",
2374+
&macro,
2375+
&year, &month, &day,
2376+
&hour, &minute, &second, &microsecond)) {
2377+
return NULL;
2378+
}
2379+
2380+
if (macro) {
2381+
rv = PyDateTime_FromDateAndTime(
2382+
year, month, day,
2383+
hour, minute, second, microsecond);
2384+
}
2385+
else {
2386+
rv = PyDateTimeAPI->DateTime_FromDateAndTime(
2387+
year, month, day,
2388+
hour, minute, second, microsecond,
2389+
Py_None,
2390+
PyDateTimeAPI->DateTimeType);
2391+
}
2392+
return rv;
2393+
}
2394+
2395+
static PyObject *
2396+
get_datetime_fromdateandtimeandfold(PyObject *self, PyObject *args)
2397+
{
2398+
PyObject *rv = NULL;
2399+
int macro;
2400+
int year, month, day;
2401+
int hour, minute, second, microsecond, fold;
2402+
2403+
if (!PyArg_ParseTuple(args, "piiiiiiii",
2404+
&macro,
2405+
&year, &month, &day,
2406+
&hour, &minute, &second, &microsecond,
2407+
&fold)) {
2408+
return NULL;
2409+
}
2410+
2411+
if (macro) {
2412+
rv = PyDateTime_FromDateAndTimeAndFold(
2413+
year, month, day,
2414+
hour, minute, second, microsecond,
2415+
fold);
2416+
}
2417+
else {
2418+
rv = PyDateTimeAPI->DateTime_FromDateAndTimeAndFold(
2419+
year, month, day,
2420+
hour, minute, second, microsecond,
2421+
Py_None,
2422+
fold,
2423+
PyDateTimeAPI->DateTimeType);
2424+
}
2425+
return rv;
2426+
}
2427+
2428+
static PyObject *
2429+
get_time_fromtime(PyObject *self, PyObject *args)
2430+
{
2431+
PyObject *rv = NULL;
2432+
int macro;
2433+
int hour, minute, second, microsecond;
2434+
2435+
if (!PyArg_ParseTuple(args, "piiii",
2436+
&macro,
2437+
&hour, &minute, &second, &microsecond)) {
2438+
return NULL;
2439+
}
2440+
2441+
if (macro) {
2442+
rv = PyTime_FromTime(hour, minute, second, microsecond);
2443+
}
2444+
else {
2445+
rv = PyDateTimeAPI->Time_FromTime(
2446+
hour, minute, second, microsecond,
2447+
Py_None,
2448+
PyDateTimeAPI->TimeType);
2449+
}
2450+
return rv;
2451+
}
2452+
2453+
static PyObject *
2454+
get_time_fromtimeandfold(PyObject *self, PyObject *args)
2455+
{
2456+
PyObject *rv = NULL;
2457+
int macro;
2458+
int hour, minute, second, microsecond, fold;
2459+
2460+
if (!PyArg_ParseTuple(args, "piiiii",
2461+
&macro,
2462+
&hour, &minute, &second, &microsecond,
2463+
&fold)) {
2464+
return NULL;
2465+
}
2466+
2467+
if (macro) {
2468+
rv = PyTime_FromTimeAndFold(hour, minute, second, microsecond, fold);
2469+
}
2470+
else {
2471+
rv = PyDateTimeAPI->Time_FromTimeAndFold(
2472+
hour, minute, second, microsecond,
2473+
Py_None,
2474+
fold,
2475+
PyDateTimeAPI->TimeType);
2476+
}
2477+
return rv;
2478+
}
2479+
2480+
static PyObject *
2481+
get_delta_fromdsu(PyObject *self, PyObject *args)
2482+
{
2483+
PyObject *rv = NULL;
2484+
int macro;
2485+
int days, seconds, microseconds;
2486+
2487+
if (!PyArg_ParseTuple(args, "piii",
2488+
&macro,
2489+
&days, &seconds, &microseconds)) {
2490+
return NULL;
2491+
}
2492+
2493+
if (macro) {
2494+
rv = PyDelta_FromDSU(days, seconds, microseconds);
2495+
}
2496+
else {
2497+
rv = PyDateTimeAPI->Delta_FromDelta(
2498+
days, seconds, microseconds, 1,
2499+
PyDateTimeAPI->DeltaType);
2500+
}
2501+
2502+
return rv;
2503+
}
2504+
23432505
static PyObject *
23442506
get_date_fromtimestamp(PyObject* self, PyObject *args)
23452507
{
@@ -4826,7 +4988,7 @@ static PyMethodDef TestMethods[] = {
48264988
{"set_errno", set_errno, METH_VARARGS},
48274989
{"test_config", test_config, METH_NOARGS},
48284990
{"test_sizeof_c_types", test_sizeof_c_types, METH_NOARGS},
4829-
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
4991+
{"test_datetime_capi", test_datetime_capi, METH_NOARGS},
48304992
{"datetime_check_date", datetime_check_date, METH_VARARGS},
48314993
{"datetime_check_time", datetime_check_time, METH_VARARGS},
48324994
{"datetime_check_datetime", datetime_check_datetime, METH_VARARGS},
@@ -4835,6 +4997,12 @@ static PyMethodDef TestMethods[] = {
48354997
{"make_timezones_capi", make_timezones_capi, METH_NOARGS},
48364998
{"get_timezones_offset_zero", get_timezones_offset_zero, METH_NOARGS},
48374999
{"get_timezone_utc_capi", get_timezone_utc_capi, METH_VARARGS},
5000+
{"get_date_fromdate", get_date_fromdate, METH_VARARGS},
5001+
{"get_datetime_fromdateandtime", get_datetime_fromdateandtime, METH_VARARGS},
5002+
{"get_datetime_fromdateandtimeandfold", get_datetime_fromdateandtimeandfold, METH_VARARGS},
5003+
{"get_time_fromtime", get_time_fromtime, METH_VARARGS},
5004+
{"get_time_fromtimeandfold", get_time_fromtimeandfold, METH_VARARGS},
5005+
{"get_delta_fromdsu", get_delta_fromdsu, METH_VARARGS},
48385006
{"get_date_fromtimestamp", get_date_fromtimestamp, METH_VARARGS},
48395007
{"get_datetime_fromtimestamp", get_datetime_fromtimestamp, METH_VARARGS},
48405008
{"test_list_api", test_list_api, METH_NOARGS},

0 commit comments

Comments
 (0)