8000 gh-97955: Migrate `zoneinfo` to Argument Clinic (#97958) · python/cpython@24a6645 · GitHub
[go: up one dir, main page]

Skip to content

Commit 24a6645

Browse files
authored
gh-97955: Migrate zoneinfo to Argument Clinic (#97958)
1 parent 002252c commit 24a6645

File tree

6 files changed

+258
-36
lines changed

6 files changed

+258
-36
lines changed

Include/internal/pycore_global_strings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ struct _Py_global_strings {
494494
STRUCT_FOR_ID( 8000 offset_src)
495495
STRUCT_FOR_ID(on_type_read)
496496
STRUCT_FOR_ID(onceregistry)
497+
STRUCT_FOR_ID(only_keys)
497498
STRUCT_FOR_ID(oparg)
498499
STRUCT_FOR_ID(opcode)
499500
STRUCT_FOR_ID(open)

Include/internal/pycore_runtime_init_generated.h

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Lib/test/test_zoneinfo/test_zoneinfo.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,6 +404,19 @@ def test_time_fixed_offset(self):
404404
class CZoneInfoTest(ZoneInfoTest):
405405
module = c_zoneinfo
406406

407+
def test_signatures(self):
408+
"""Ensure that C module has valid method signatures."""
409+
import inspect
410+
411+
must_have_signatures = (
412+
self.klass.clear_cache,
413+
self.klass.no_cache,
414+
self.klass.from_file,
415+
)
416+
for method in must_have_signatures:
417+
with self.subTest(method=method):
418+
inspect.Signature.from_callable(method)
419+
407420
def test_fold_mutate(self):
408421
"""Test that fold isn't mutated when no change is necessary.
409422
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Migrate :mod:`zoneinfo` to Argument Clinic.

Modules/_zoneinfo.c

Lines changed: 48 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@
1212

1313
#include "datetime.h"
1414

15+
#include "clinic/_zoneinfo.c.h"
16+
/*[clinic input]
17+
module zoneinfo
18+
class zoneinfo.ZoneInfo "PyObject *" "PyTypeObject *"
19+
[clinic start generated code]*/
20+
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=d12c73c0eef36df8]*/
21+
1522
// Imports
1623
static PyObject *io_open = NULL;
1724
static PyObject *_tzpath_find_tzfile = NULL;
@@ -338,20 +345,25 @@ zoneinfo_dealloc(PyObject *obj_self)
338345
Py_TYPE(self)->tp_free((PyObject *)self);
339346
}
340347

348+
/*[clinic input]
349+
@classmethod
350+
zoneinfo.ZoneInfo.from_file
351+
352+
file_obj: object
353+
/
354+
key: object = None
355+
356+
Create a ZoneInfo file from a file object.
357+
[clinic start generated code]*/
358+
341359
static PyObject *
342-
zoneinfo_from_file(PyTypeObject *type, PyObject *args, PyObject *kwargs)
360+
zoneinfo_ZoneInfo_from_file_impl(PyTypeObject *type, PyObject *file_obj,
361+
PyObject *key)
362+
/*[clinic end generated code: output=68ed2022404ae5be input=ccfe73708133d2e4]*/
343363
{
344-
PyObject *file_obj = NULL;
345364
PyObject *file_repr = NULL;
346-
PyObject *key = Py_None;
347365
PyZoneInfo_ZoneInfo *self = NULL;
348366

349-
static char *kwlist[] = {"", "key", NULL};
350-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|O", kwlist, &file_obj,
351-
&key)) {
352-
return NULL;
353-
}
354-
355367
PyObject *obj_self = (PyObject *)(type->tp_alloc(type, 0));
356368
self = (PyZoneInfo_ZoneInfo *)obj_self;
357369
if (self == NULL) {
@@ -379,35 +391,41 @@ zoneinfo_from_file(PyTypeObject *type, PyObject *args, PyObject *kwargs)
379391
return NULL;
380392
}
381393

394+
/*[clinic input]
395+
@classmethod
396+
zoneinfo.ZoneInfo.no_cache
397+
398+
key: object
399+
400+
Get a new instance of ZoneInfo, bypassing the cache.
401+
[clinic start generated code]*/
402+
382403
static PyObject *
383-
zoneinfo_no_cache(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
404+
zoneinfo_ZoneInfo_no_cache_impl(PyTypeObject *type, PyObject *key)
405+
/*[clinic end generated code: output=751c6894ad66f91b input=bb24afd84a80ba46]*/
384406
{
385-
static char *kwlist[] = {"key", NULL};
386-
PyObject *key = NULL;
387-
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O", kwlist, &key)) {
388-
return NULL;
389-
}
390-
391-
PyObject *out = zoneinfo_new_instance(cls, key);
407+
PyObject *out = zoneinfo_new_instance(type, key);
392408
if (out != NULL) {
393409
((PyZoneInfo_ZoneInfo *)out)->source = SOURCE_NOCACHE;
394410
}
395411

396412
return out;
397413
}
398414

399-
static PyObject *
400-
zoneinfo_clear_cache(PyObject *cls, PyObject *args, PyObject *kwargs)
401-
{
402-
PyObject *only_keys = NULL;
403-
static char *kwlist[] = {"only_keys", NULL};
415+
/*[clinic input]
416+
@classmethod
417+
zoneinfo.ZoneInfo.clear_cache
404418
405-
if (!(PyArg_ParseTupleAndKeywords(args, kwargs, "|$O", kwlist,
406-
&only_keys))) {
407-
return NULL;
408-
}
419+
*
420+
only_keys: object = None
409421
410-
PyTypeObject *type = (PyTypeObject *)cls;
422+
Clear the ZoneInfo cache.
423+
[clinic start generated code]*/
424+
425+
static PyObject *
426+
zoneinfo_ZoneInfo_clear_cache_impl(PyTypeObject *type, PyObject *only_keys)
427+
/*[clinic end generated code: output=eec0a3276f07bd90 input=8cff0182a95f295b]*/
428+
{
411429
PyObject *weak_cache = get_weak_cache(type);
412430

413431
if (only_keys == NULL || only_keys == Py_None) {
@@ -2545,15 +2563,9 @@ zoneinfo_init_subclass(PyTypeObject *cls, PyObject *args, PyObject **kwargs)
25452563
/////
25462564
// Specify the ZoneInfo type
25472565
static PyMethodDef zoneinfo_methods[] = {
2548-
{"clear_cache", (PyCFunction)(void (*)(void))zoneinfo_clear_cache,
2549-
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2550-
PyDoc_STR("Clear the ZoneInfo cache.")},
2551-
{"no_cache", (PyCFunction)(void (*)(void))zoneinfo_no_cache,
2552-
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2553-
PyDoc_STR("Get a new instance of ZoneInfo, bypassing the cache.")},
2554-
{"from_file", (PyCFunction)(void (*)(void))zoneinfo_from_file,
2555-
METH_VARARGS | METH_KEYWORDS | METH_CLASS,
2556-
PyDoc_STR("Create a ZoneInfo file from a file object.")},
2566+
ZONEINFO_ZONEINFO_CLEAR_CACHE_METHODDEF
2567+
ZONEINFO_ZONEINFO_NO_CACHE_METHODDEF
2568+
ZONEINFO_ZONEINFO_FROM_FILE_METHODDEF
25572569
{"utcoffset", (PyCFunction)zoneinfo_utcoffset, METH_O,
25582570
PyDoc_STR("Retrieve a timedelta representing the UTC offset in a zone at "
25592571
"the given datetime.")},

Modules/clinic/_zoneinfo.c.h

Lines changed: 188 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)
0