-
-
Notifications
You must be signed in to change notification settings - Fork 31.9k
Derby #18: Convert 31 sites to Argument Clinic across 23 files #64385
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
Comments
This issue is part of the Great Argument Clinic Conversion Derby, This issue asks you to change the following bundle of files: Talk to me (larry) if you only want to attack part of a bundle. For instructions on how to convert a function to work with Argument |
Looking at _csv.c, I see a few functions using PyArg_UnpackTuple. They should be converted too, no? |
Attached part 1 of mathmodule (17 functions). I'm looking forward to a suggestion for handling the rest (see FUNC1/1A/2 macros :) |
Wow. I never knew about PyArg_UnpackTuple. You're right, those should be converted too. Hooray, more entry points to convert. I'll write something up for the howto about UnpackTuple. I just did a quick check, and there are 96 entry points (by my count) that use PyArg_UnpackTuple(). Shall I create Derby issues #19 and #20, or do you have a better idea?
/*[clinic input] docstring goes here You'd skip the parameters and the return annotation. You could only reuse functions from the current file. Would that be a big boon to you? |
One thing to note is that (at least in math) many instances of UnpackTuple could have been replaced by ParseTuple. See for example math_hypot: it uses UnpackTuple to get two objects, and then immediately calls PyFloat_AsDouble on them. I've converted these using 'd' and not 'O' specifiers.
Probably better to add them to the issues that cover their modules, otherwise people might get confused.
That sounds good. On the other hand, if clinic expanded cpp macros we could... *:-) |
OK, here's a patch for _csv. Two problems here: First problem is the __new__ method of the Dialect class:
I tried to hack something into clinic with a new decorator, but it may not be how you want it to look, take care. Second problem is the functions reader(), writer(), register_dialect(): they parse their *args but pass their **kwargs through to another class. BTW, for a module like _csv that is exported through a Python module named csv, should we use the "real" or the "nice" module name? |
Tried to tackle symtable -- it uses an O& converter. The clinic howto says
but Traceback (most recent call last):
File "Tools/clinic/clinic.py", line 2817, in <module>
sys.exit(main(sys.argv[1:]))
File "Tools/clinic/clinic.py", line 2813, in main
parse_file(filename, output=ns.output, verify=not ns.force)
File "Tools/clinic/clinic.py", line 1116, in parse_file
cooked = clinic.parse(raw)
File "Tools/clinic/clinic.py", line 1066, in parse
parser.parse(block)
File "Tools/clinic/clinic.py", line 2109, in parse
self.state(line)
File "Tools/clinic/clinic.py", line 2378, in state_parameter
converter = dict[name](parameter_name, self.function, value, **kwargs)
File "Tools/clinic/clinic.py", line 1403, in __init__
self.converter_init(**kwargs)
TypeError: converter_init() got an unexpected keyword argument 'converter' |
_tracemalloc converted. Its existing docstrings did use the func(arg: argtype) -> rettype convention. Is there a way in clinic to retain that? |
Here's _iomodule. _io.open has a whopping 100-line docstring, which is ... unfortunate ... to have duplicated in the file :) |
And _heapq. No problems there, except that it also used "->" return annotations in the docstring. |
And lsprof. |
OK, new patches coming in. |
Actually I put all I have in one. Rietveld doesn't care. The mathmodule still awaits some kind of solution for the macro atrocities. Objects will be attacked next. |
New changeset 060cfd049d14 by Stefan Krah in branch 'default': |
memoryobject.c is converted with a minimal patch (I would like to keep |
All the Derby patches should only go into trunk at this point. |
Attached is an updated patch for Modules/mathmodule.c. This is based on Georg's patch, updated to apply to current 3.5, with several improvements:
|
Should Argument Clinic conversion patches still be against the 'default' branch, and not 3.5, even though they don't include any functionality changes? |
Attached is an AC conversion patch for Objects/enumobject.c. Note that this file contains the implementations of the 'enumerate' and 'reversed' classes, but *not* the 'Enum' class. This is based on the 3.5 branch. |
Attached is a patch for all of _operator except for itemgetter, attrgetter and methodcaller. The entire test suite passes after applying this patch. Using AC has allowed the removal of all of the cryptic "spam*()" macros in the code, making it much more straightforward. In terms of readability, IMO this is a great improvement. I skipped itemgetter, attrgetter and methodcaller since they all support passing a variable number of arguments and treating those as a sequence of values. |
Is there any performance difference? |
I tried running the pystone benchmark, but the results were inconclusive. I saw very large differences (up to 20%) between runs at different times, with no clear differences with or without the patch. However, a quick search shows that the operator module is almost completely unused by the rest of the code and the stdlib. So I really don't think performance is an issue here. |
Attached a slightly revised patch thanks to Serhiy's review. In addition to Serhiy's remarks, I used "_operator._compare_digest = _operator.eq" to reduce a bit more boilerplate. |
The operator module is rarely used in the stdlib, but if it is used in user code (mainly with map(), reduce() or like) the performance often is important. You can use microbenchmarks like following (operator.add is twice faster than lambda x, y: x + y).
|
I just ran such microbenchmarks for operator.add and operator.not_, and saw no significant difference with or without the patch. |
Here's another complete conversion patch for _operator. As suggested by Serhiy, I changed the comparison operators to copy the signature from _operator.eq() instead of _operator.lt(), which is easier to understand. |
Here's another version of the _operator patch, with another small change after Serhiy's latest review. |
New changeset 112f27b8c8ea by Serhiy Storchaka in branch 'default': |
bpo-20186.mathmodule.v2.patch needed just synchronizing docstrings. |
bpo-20186.enumobject.patch LGTM too. |
New changeset e3db9bccff3f by Serhiy Storchaka in branch 'default': |
Sorry for committing patches for you Tal, but they were hanging so long time. |
Serhiy, no apology is required. On the contrary, thank you for the taking the time to review this and commit, I don't have time available for this these days. |
There are patches based on modules_issue20186.patch that convert to Argument
|
The application of AC to enumerate() lost information about the start argument and the signature of the call. We're going backwards. ---- New help ----------------------------------------------------- class enumerate(object)
| Return an enumerate object.
|
| iterable
| an object supporting iteration
|
| The enumerate object yields pairs containing a count (from start, which
| defaults to zero) and a value yielded by the iterable argument.
|
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), ... ---- Old help ----------------------------------------------------- class enumerate(object)
| enumerate(iterable[, start]) -> iterator for index, value of iterable
|
| Return an enumerate object. iterable must be another object that supports
| iteration. The enumerate object yields pairs containing a count (from
| start, which defaults to zero) and a value yielded by the iterable argument.
| enumerate is useful for obtaining an indexed list:
| (0, seq[0]), (1, seq[1]), (2, seq[2]), ... Also, reversed() lost the indication of its signature: And the help doesn't have the usual:
|
When reviewing AC patches, we should always compare the help() before and after. Also, if the code already had fast parsing like: When PyArg_NoKeywords was present, we need to verify that the AC version also precludes keyword arguments (to prevent the creation of unhelpful keyword args and to keep compatibility with other versions of Python). |
Microbenchmarks: $ ./python -m perf timeit --duplicate 100 "enumerate('abc')"
Unpatched: Median +- std dev: 1.76 us +- 0.10 us
Patched: Median +- std dev: 1.61 us +- 0.07 us
$ ./python -m perf timeit --duplicate 100 "enumerate('abc', 1)"
Unpatched: Median +- std dev: 2.14 us +- 0.09 us
Patched: Median +- std dev: 1.76 us +- 0.07 us
$ ./python -m perf timeit --duplicate 100 "reversed('abc')"
Unpatched: Median +- std dev: 1.20 us +- 0.06 us
Patched: Median +- std dev: 1.20 us +- 0.07 us enumerate() is 9-21% faster (due to avoiding of tuple creating), reversed() is not changed (Argument Clinic generates the same parsing code for it). |
Here is a patch that restores ol docstrings for enumerate() and reversed(). But it may be better to change pydoc so that it would output a text signature of class constructor if available. |
Argument Clinic generates incorrect parsing code for _csv.field_size_limit(). |
The problem with lsprof_clinic.patch is that it exposes default value of _lsprof.Profiler.enable() parameters as -1. Actually _lsprof.Profiler.enable() should accept boolean arguments without default value. |
New changeset 7f8a3eb3459e by Serhiy Storchaka in branch 'default': |
New changeset e1df73b46094 by Serhiy Storchaka in branch 'default': |
New changeset b0ef37ec83f337b4b77275b367288a5656a0682c by Serhiy Storchaka in branch 'master': New changeset 18a02e9d1f8e981b7b2f4287a4ed871021b13ade by Serhiy Storchaka in branch 'master': |
New changeset 8ccb3ad39ee4 by Serhiy Storchaka in branch 'default': |
I suspect change in this issue led to bpo-43413. |
…ller (#94591) These were intentionally skipped when operator was updated to use the argument clinic: #64385 (comment) However, by not using the argument clinic, they missed out on getting signatures. This is a narrow PR to update the docstrings so that `__text_signature__` can be extracted from them. Updating to use the argument clinic is beyond scope. `methodcaller` uses `*args, **kwargs` to match variadic names used elsewhere, including in `operator.call`.
…thodcaller (python#94591) These were intentionally skipped when operator was updated to use the argument clinic: python#64385 (comment) However, by not using the argument clinic, they missed out on getting signatures. This is a narrow PR to update the docstrings so that `__text_signature__` can be extracted from them. Updating to use the argument clinic is beyond scope. `methodcaller` uses `*args, **kwargs` to match variadic names used elsewhere, including in `operator.call`.
…ller These were intentionally skipped when operator was updated to use the argument clinic: python/cpython#64385 (comment) However, by not using the argument clinic, they missed out on getting signatures. This is a narrow PR to update the docstrings so that `__text_signature__` can be extracted from them. Updating to use the argument clinic is beyond scope. `methodcaller` uses `*args, **kwargs` to match variadic names used elsewhere, including in `operator.call`.
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: