Skip to content

gh-155496: Use Argument Clinic for more functions of the time module - #155513

Open
serhiy-storchaka wants to merge 5 commits into
python:mainfrom
serhiy-storchaka:time-clinic
Open

gh-155496: Use Argument Clinic for more functions of the time module#155513
serhiy-storchaka wants to merge 5 commits into
python:mainfrom
serhiy-storchaka:time-clinic

Conversation

@serhiy-storchaka

Copy link
Copy Markdown
Member

Convert all functions of the time module, except strftime() and strptime().
The docstring of strftime() is composed with the STRFTIME_FORMAT_CODES macro shared with strptime(), which Argument Clinic cannot express, and strptime() only forwards its arguments to _strptime._strptime_time().

time(), monotonic(), perf_counter(), process_time() and thread_time() now use the "double" return converter, which makes the _PyFloat_FromPyTime() helper redundant.

It needs a builtin function without a signature, but time.ctime() now
has one.
Any builtin function can get a signature, therefore the test uses the
function which exists for testing the lack of it.

@StanFromIreland StanFromIreland left a comment

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.

Please ensure that the parameters match what is documented.

Comment thread Modules/timemodule.c
return PyFloat_FromDouble(d);
}
/*[clinic input]
time.time -> double

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.

Clinic won't emit these type annotations?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

No, it is only for code generation.

Comment thread Modules/timemodule.c
/*[clinic input]
time.sleep

seconds as timeout_obj: object

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.

The documented parameter name is secs, and it is not positional only.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is named seconds in the current docstring, and it is positional-only (METH_O).

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.

Yes, but please make the documentation consistent.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is a separate documentation issue, which is not only about main. Should we change the docs or the docstrings? Should we explicitly document parameters as positional-only in docs? What other in the docs does not match the behavior?

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.

You're already changing the docstrings, so I suggest you make them match the documentation. It will make follow-ups simpler if they are consistent.

Comment thread Modules/timemodule.c Outdated

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.

This needs updating.

Comment thread Modules/timemodule.c
/*[clinic input]
time.localtime

seconds as ot: object = None

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.

Documented parameter is secs.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is seconds in the current docstring.

Comment thread Modules/timemodule.c Outdated
/*[clinic input]
time.asctime

time_tuple as tup: object = None

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.

This introduces a behaviour change, previously:

>>> import time
>>> time.asctime(None)
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    time.asctime(None)
    ~~~~~~~~~~~~^^^^^^
TypeError: Tuple or struct_time argument required

But with this patch, None falls back to localtime():

>>> import time
>>> time.asctime(None)
'Fri Aug 14 09:55:16 2026'

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good catch. Other functions accepted None.

Comment thread Modules/timemodule.c Outdated

Convert a time in seconds since the Epoch to a string in local time.

This is equivalent to asctime(localtime(seconds)). When the time tuple

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.

What time tuple?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This was in the current docstring. Guess it needs a correction.

Comment thread Modules/timemodule.c
/*[clinic input]
time.mktime

time_tuple as tm_tuple: object

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.

Documented parameter is t.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

It is simply tuple in the current docstring. And referred as a time tuple.

Restore the default of time.asctime() as NULL, so that passing None
raises a TypeError as before, and fix the docstring of time.ctime()
and the comment for parse_time_t_arg().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants