This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Argument Clinic: support for simple expressions?
Type: enhancement Stage: resolved
Components: Versions: Python 3.4
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: larry Nosy List: georg.brandl, larry, meador.inge, python-dev, serhiy.storchaka, zach.ware
Priority: normal Keywords:

Created on 2014-01-12 10:29 by georg.brandl, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
larry.clinic.rollup.patch.two.diff.1.txt larry, 2014-01-15 03:40 review
larry.clinic.rollup.patch.two.diff.2.txt larry, 2014-01-16 08:41 review
larry.clinic.rollup.patch.two.diff.3.txt larry, 2014-01-16 17:12
larry.clinic.rollup.patch.two.diff.4.txt larry, 2014-01-16 17:23 review
Messages (24)
msg207946 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-01-12 10:29
Take for example select.epoll.__new__():

static PyObject *
pyepoll_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
{
    int flags = 0, sizehint = FD_SETSIZE - 1;
    static char *kwlist[] = {"sizehint", "flags", NULL};

    if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:epoll", kwlist,
                                     &sizehint, &flags))
        return NULL;

How should that be handled?  Currently I can't specify ``FD_SETSIZE - 1`` as the default value, but I don't want a magic ``-909`` either :)
msg207947 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-12 10:37
See also a discussion in issue20193. zlib needs this in many functions.
msg207956 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-12 13:58
Yes, this is supported, just undocumented (for now).  The magic you want is, for example in Modules/_sre.c:

endpos: Py_ssize_t(c_default="PY_SSIZE_T_MAX") = sys.maxsize
msg207957 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-01-12 14:03
Nice, thanks.
msg208135 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-15 03:40
The attached patch extends Argument Clinic and the inspect module: now you may use simple (!) expressions as default values.  So for example "sys.maxsize - 1" should now work fine.

* Names may be in the current module; as a backup they will also be looked up in sys.modules.
* You may look up attributes of names.
* You may subscript into names.
* You may use math operators, both unary and binary.
* int/float/string literals are (still) fine.
* You may not use: function calls, list/set/dict/tuple/bytes literals, generator expressions or comprehensions, if expressions, anything fun.

Other changes in this diff:
* "doc_default" is gone, it no longer makes sense.
* "py_default" and "c_default" handling is simpler and more predictable.
* Minor doc fix suggested on tracker.

The docs still need work.  More tests would be good too but I'm already overwhelmed by work--could one of you guys contribute some tests?
msg208139 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2014-01-15 04:11
Larry, nice.  I am verified that your latest patch fixes my third sqlite3
nit from issue20178.
msg208175 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-01-15 16:52
Just confirmed that this patch fixes the traceback I was getting from winreg in #20172, but this has a new oddity:

>>> help(winreg)
...
    ConnectRegistry(computer_name=<object object at 0x0190E148>, key=<object object at 0x0190E148>)
...

Every param of every function has this kind of thing, positional or keyword, default or no.  I'll try to look into it, but my hopes aren't high (much of clinic's inner workings are still mysterious to me).
msg208176 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-15 16:53
That's okay, email me a sample at

larry at hastings dot org

and I'll take a look at it.  Be sure to tell me which patch(es) were applied at the time.

(Note: all you need to send is the Clinic block.)
msg208177 - (view) Author: Zachary Ware (zach.ware) * (Python committer) Date: 2014-01-15 17:00
Actually, I can reproduce on tip with just this patch applied; os.chmod shows it.  And I was wrong, params that have a default are correct, it's just ones without.
msg208178 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-15 17:02
I'd prefer it if you emailed me as I asked.  That will help me get to it a lot quicker.
msg208253 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 08:08
Okay, I have a fix for the help(os.chmod) problem, that'll be in the next refreshed patch.
msg208254 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-16 08:11
Why not allow arbitrary string as py_default?
msg208255 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 08:12
Isn't that what it does?

I may actually remove py_default.  Nobody uses it, and I don't think you need it--you just specify what you want as the real default value.
msg208259 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 08:41
Second rollup patch.  More small fixes, many suggested by other folks.
msg208260 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-16 08:47
It would be better if you commit a patch which changes PyTuple_Size to PyTuple_GET_SIZE in separate commit.
msg208266 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-01-16 09:29
Excellent! I'm rewriting the zlib module and the code becomes much cleaner with this patch.

There is one problem left -- Py_buffer doesn't support default value at all.

Such code

/*[clinic input]
zlib.compressobj
    zdict: Py_buffer = unspecified
[clinic start generated code]*/

produces error:

When you specify a named constant ('unspecified') as your default value,
you MUST specify a valid c_default.

But this code

/*[clinic input]
zlib.compressobj
    zdict: Py_buffer(c_default="{NULL, NULL}") = unspecified
[clinic start generated code]*/

produces error too:

The only legal default value for Py_buffer is None.

And specifying c_default=None returns first error.
msg208288 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 16:56
Py_buffer is currently inflexible.  The only default value it accepts is None.  If you want it to be required, give it no default value.  If you want it to be optional, give it a default value of None.

Do you have a use case for any other default value?
msg208290 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 17:12
Third patch, changes based on Georg's review.  Thanks, Georg!
msg208291 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-01-16 17:22
Rietveld doesn't like your new patch?
msg208292 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 17:23
Sorry, forgot to update trunk, here you go.
msg208293 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2014-01-16 17:25
Thx! (without soundcheck)
msg208302 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-01-16 19:32
New changeset abf87e1fbc62 by Larry Hastings in branch 'default':
Issue #20226: Major improvements to Argument Clinic.
http://hg.python.org/cpython/rev/abf87e1fbc62
msg208303 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2014-01-16 19:33
Done!

Georg: Thanks for the suggestion, and for the reviews!  Argument Clinic is improved this day.
msg208314 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-01-16 22:15
New changeset cd3fdf21a6e4 by Larry Hastings in branch 'default':
Issue #20226: Added tests for new features and regressions.
http://hg.python.org/cpython/rev/cd3fdf21a6e4
History
Date User Action Args
2022-04-11 14:57:56adminsetgithub: 64425
2014-01-26 04:48:06larrysetstage: commit review -> resolved
2014-01-16 22:15:19python-devsetmessages: + msg208314
2014-01-16 19:33:41larrysetstatus: open -> closed
resolution: fixed
stage: patch review -> commit review
2014-01-16 19:33:31larrysetmessages: + msg208303
2014-01-16 19:32:14python-devsetnosy: + python-dev
messages: + msg208302
2014-01-16 17:25:41georg.brandlsetmessages: + msg208293
2014-01-16 17:23:58larrysetfiles: + larry.clinic.rollup.patch.two.diff.4.txt

messages: + msg208292
2014-01-16 17:22:43georg.brandlsetmessages: + msg208291
2014-01-16 17:12:56larrysetfiles: + larry.clinic.rollup.patch.two.diff.3.txt

messages: + msg208290
2014-01-16 16:56:30larrysetmessages: + msg208288
2014-01-16 09:29:17serhiy.storchakasetmessages: + msg208266
2014-01-16 08:47:32serhiy.storchakasetmessages: + msg208260
2014-01-16 08:41:32larrysetfiles: + larry.clinic.rollup.patch.two.diff.2.txt

messages: + msg208259
2014-01-16 08:12:53larrysetmessages: + msg208255
2014-01-16 08:11:41serhiy.storchakasetmessages: + msg208254
2014-01-16 08:08:10larrysetmessages: + msg208253
2014-01-15 20:47:31serhiy.storchakalinkissue20193 dependencies
2014-01-15 20:44:48zach.warelinkissue20273 superseder
2014-01-15 17:02:51larrysetmessages: + msg208178
2014-01-15 17:00:28zach.waresetmessages: + msg208177
2014-01-15 16:53:23larrysetmessages: + msg208176
2014-01-15 16:52:12zach.waresetnosy: + zach.ware
messages: + msg208175
2014-01-15 04:11:23meador.ingesetnosy: + meador.inge
messages: + msg208139
2014-01-15 03:40:43larrysetfiles: + larry.clinic.rollup.patch.two.diff.1.txt
type: enhancement
messages: + msg208135

stage: patch review
2014-01-12 14:03:39georg.brandlsetmessages: + msg207957
2014-01-12 13:58:07larrysetmessages: + msg207956
2014-01-12 10:37:41serhiy.storchakasetnosy: + serhiy.storchaka

messages: + msg207947
versions: + Python 3.4
2014-01-12 10:29:54georg.brandlsetassignee: larry

nosy: + larry
2014-01-12 10:29:49georg.brandlcreate