Repository navigation
Derby #4: Convert 53 sites to Argument Clinic across 5 files #64372
Description
Activity
This issue is part of the Great Argument Clinic Conversion Derby,
where we're trying to convert as much of Python 3.4 to use
Argument Clinic as we can before Release Candidate 1 on January 19.This issue asks you to change the following bundle of files:
Modules/_codecsmodule.c: 43 sites
Modules/sha1module.c: 2 sites
Modules/sha256module.c: 3 sites
Modules/sha512module.c: 3 sites
Modules/md5module.c: 2 sitesTalk 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
Clinic, read the "howto":
http://docs.python.org/dev/howto/clinic.html- addedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or errorextension-modulesC modules in the Modules dirC modules in the Modules dirtype-featureA feature request or enhancementA feature request or enhancementand removedtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
on Jan 7, 2014 Okay, here is my first attempt. I only worked on one file (Modules/sha1module.c). I need to see whether I hit the mark or not. If yes, I can do the other files as well. Anyway, handling the keyword argument was kinda tough. There was no example so I had to shoot in the dark.
Just one comment on your patch. The documentation already tells you how to handle keyword arguments (section 8 tells you how to handle default values, section 9 tells you how to handle | in the format string). If you have any suggestions on how I could improve the documentation I'd be happy to try it.
An example how to convert keyword argument would be very helpful. I searched the example from existing code but nothing shows up.
To be precise: a "keyword argument" is something that happens on the caller side. What you're talking about is a "positional-or-keyword parameter". And parameters are positional-or-keyword by default.
Okay, this is my second attempt. I want to get METH_VARGS but I always get METH_O for positional parameters. Is there any way to circumvent this? The documentation does not cover this.
That METH_O is working perfectly. You seem to be confused by it.
The original code was kind of dumb. The function only takes two parameters: self, and a second "obj" parameter which can be any kind of object. CPython has special support for exactly this kind of function, which is METH_O. This will actually be slightly faster. In any case, that part of your conversion was correct. Don't sweat it.
http://docs.python.org/3.4/c-api/structures.html?highlight=meth_o#METH_O
But METH_O makes the update method does not work.
>>> _sha1.sha1().update(b'a') Traceback (most recent call last): File "<stdin>", line 1, in <module> SystemError: new style getargs format but argument is not a tupleBut if you change METH_O to METH_VARARGS, it works again. I'll chip Christian Heimes. Maybe he has something to say about this.
Other than that, I need to update my patch because currently with my patch, the sha1 constructor accepts None value. The default behaviour is to reject None value. So I need to use advanced feature of clinic for this purpose.
I think I can convert all sha modules (including md5) in this weekend. However, _codecsmodule is totally different beast. 43 sites.... >.<
Right, it doesn't work because you left the PyArg_ParseTuple call in your impl function. Remove that and it should work.
Rule 1: Argument Clinic handles all argument parsing for you. Your "impl" function should never call PyArg_ParseTuple or PyArg_ParseTupleAndKeywords.
Rule 2: Never modify the output of Argument Clinic by hand.This is the current behaviour of sha1 constructor. It rejects None value.
>>> import _sha1 >>> _sha1.sha1() <_sha1.sha1 object at 0x7f7fa7f0dea0> >>> _sha1.sha1(None) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object supporting the buffer API required >>> _sha1.sha1(string=None) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: object supporting the buffer API required
Then when I clinic it, what about the doc? This doesn't seem right.
+PyDoc_STRVAR(sha1_SHA1_sha1__doc_,
+"sha1(string=None)\n"
"Return a new SHA1 hash object; optionally initialized with a string.");Rule 1: Argument Clinic handles all argument parsing for you. Your "impl" function should never call PyArg_ParseTuple or PyArg_ParseTupleAndKeywords.
Ah, got it. Here is the third patch. Everything works fine except for the issue whether sha1 constructor should accept None value or not. Right now, after clinic, it accepts it. So _sha1.sha1() is same as _sha1.sha1(string=None).
15 remaining items
Status:
Modules/sha1module.c: done
Modules/sha256module.c: done
Modules/sha512module.c: done
Modules/md5module.c: doneAll of them are ready for Python 3.4.
/* AC 3.5: optional positional arguments */
Modules/_codecsmoduleHere is the updated patch after Larry's commit to clinic. Everything is included except codecsmodule.
New changeset 5a2ec0a15017 by Martin v. Löwis in branch 'default':
Issue bpo-20173: Convert sha1, sha256, sha512 and md5 to ArgumentClinic.
http://hg.python.org/cpython/rev/5a2ec0a15017The codecsmodule still remains to be done.
BTW, Vajrasky: Thanks for the patch!
All the Derby patches should only go into trunk at this point.
Here is reworked patch for the _codecs module. No optional groups are used, all parameters have sensible defaults. Default encoding is "utf-8", default errors is "strict" or None (if function accepts None), default mapping is None. Unified names and coding style, improved docstrings.
The patch updated to the tip.
New changeset 031df83dffb4 by Serhiy Storchaka in branch 'default':
Issue bpo-20173: Converted the _codecs module to Argument Clinic.
https://hg.python.org/cpython/rev/031df83dffb4Updated to the tip and committed. Included Larry's suggestion about sys.getdefaultencoding().
New changeset 39df27d97901 by Serhiy Storchaka in branch 'default':
Fixed compilation on Windows for issue bpo-20173.
https://hg.python.org/cpython/rev/39df27d97901This appears to be finished, please reopen if I'm mistaken.
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: