-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
Derby #4: Convert 53 sites to Argument Clinic across 5 files #64372
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 |
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 tuple But 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. |
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_, |
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). |
Here is the patch for sha256. It got the same issue as sha1. After being clinic-ed, the constructor accepts None value happily. |
Here is the patch for sha512. It got the same issue as sha1. After being clinic-ed, the constructor accepts None value happily. |
Here is the patch for md5. It got the same issue as sha1. After being clinic-ed, the constructor accepts None value happily. |
Here is the patch for _codecs module. |
So, as Guide said in https://mail.python.org/pipermail/python-dev/2014-January/131675.html: "In the sha1 example, however, accepting None and converting it to NULL I let the patches as they are. |
s/Guide/Guido. |
Here is the updated patch of sha1module, based on Larry's review. Thanks! |
Here is the updated patch of sha256module, based on Larry's review. Thanks! |
Here is the updated patch of sha512module, based on Larry's review. Thanks! |
Here is the updated patch of md5module, based on Larry's review. Thanks! |
Here is the updated patch of codecsmodule, based on Larry's review. Thanks! I didn't realize clinic releases pybuffer implicitly. I also turned _codecs.Codecs to _codecs since all of these methods are module methods. |
Here is the updated patch for sha1module after changes from Larry to clinic. |
Here is the updated patch for sha256module after changes from Larry to clinic. |
Here is the updated patch for sha512module after changes from Larry to clinic. |
Here is the updated patch for md5module after changes from Larry to clinic. |
Status: All of them are ready for Python 3.4. /* AC 3.5: optional positional arguments */ |
Here 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': |
The 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': |
Updated to the tip and committed. Included Larry's suggestion about sys.getdefaultencoding(). |
New changeset 39df27d97901 by Serhiy Storchaka in branch 'default': |
This 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:
The text was updated successfully, but these errors were encountered: