Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ebdcb50

Browse files
committed
Issue #19730: Argument Clinic now supports all the existing PyArg
"format units" as legacy converters, as well as two new features: "self converters" and the "version" directive.
1 parent 3a90797 commit ebdcb50

9 files changed

Lines changed: 466 additions & 155 deletions

File tree

Include/pyport.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@ typedef Py_ssize_t Py_hash_t;
188188
#define SIZEOF_PY_UHASH_T SIZEOF_SIZE_T
189189
typedef size_t Py_uhash_t;
190190

191+
/* Only used for compatibility with code that may not be PY_SSIZE_T_CLEAN. */
192+
#ifdef PY_SSIZE_T_CLEAN
193+
typedef Py_ssize_t Py_ssize_clean_t;
194+
#else
195+
typedef int Py_ssize_clean_t;
196+
#endif
197+
191198
/* Largest possible value of size_t.
192199
SIZE_MAX is part of C99, so it might be defined on some
193200
platforms. If it is not defined, (size_t)-1 is a portable

Misc/NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Projected release date: 2013-11-24
99

1010
Core and Builtins
1111
-----------------
12-
1312
- Use the repr of a module name in more places in import, especially
1413
exceptions.
1514

@@ -450,6 +449,9 @@ Build
450449

451450
Tools/Demos
452451
-----------
452+
- Issue #19730: Argument Clinic now supports all the existing PyArg
453+
"format units" as legacy converters, as well as two new features:
454+
"self converters" and the "version" directive.
453455

454456
- Issue #19552: pyvenv now bootstraps pip into virtual environments by
455457
default (pass --without-pip to request the old behaviour)

Modules/_datetimemodule.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,10 +4167,10 @@ PyDoc_STRVAR(datetime_datetime_now__doc__,
41674167
{"now", (PyCFunction)datetime_datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_datetime_now__doc__},
41684168

41694169
static PyObject *
4170-
datetime_datetime_now_impl(PyObject *cls, PyObject *tz);
4170+
datetime_datetime_now_impl(PyTypeObject *cls, PyObject *tz);
41714171

41724172
static PyObject *
4173-
datetime_datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs)
4173+
datetime_datetime_now(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
41744174
{
41754175
PyObject *return_value = NULL;
41764176
static char *_keywords[] = {"tz", NULL};
@@ -4187,8 +4187,8 @@ datetime_datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs)
41874187
}
41884188

41894189
static PyObject *
4190-
datetime_datetime_now_impl(PyObject *cls, PyObject *tz)
4191-
/*[clinic checksum: cde1daca68c9b7dca6df51759db2de1d43a39774]*/
4190+
datetime_datetime_now_impl(PyTypeObject *cls, PyObject *tz)
4191+
/*[clinic checksum: 5e61647d5d1feaf1ab096c5406ccea17bb7b061c]*/
41924192
{
41934193
PyObject *self;
41944194

@@ -4198,7 +4198,7 @@ datetime_datetime_now_impl(PyObject *cls, PyObject *tz)
41984198
if (check_tzinfo_subclass(tz) < 0)
41994199
return NULL;
42004200

4201-
self = datetime_best_possible(cls,
4201+
self = datetime_best_possible((PyObject *)cls,
42024202
tz == Py_None ? localtime : gmtime,
42034203
tz);
42044204
if (self != NULL && tz != Py_None) {

Modules/_dbmmodule.c

Lines changed: 84 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ static PyTypeObject Dbmtype;
4343

4444
static PyObject *DbmError;
4545

46+
/*[clinic]
47+
module dbm
48+
class dbm.dbm
49+
[clinic]*/
50+
/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
51+
52+
/*[python]
53+
class dbmobject_converter(self_converter):
54+
type = "dbmobject *"
55+
def converter_init(self):
56+
self.name = 'dp'
57+
[python]*/
58+
/*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
59+
4660
static PyObject *
4761
newdbmobject(const char *file, int flags, int mode)
4862
{
@@ -248,27 +262,77 @@ static PySequenceMethods dbm_as_sequence = {
248262
0, /* sq_inplace_repeat */
249263
};
250264

265+
/*[clinic]
266+
267+
dbm.dbm.get
268+
269+
self: dbmobject
270+
271+
key: str(length=True)
272+
[
273+
default: object
274+
]
275+
/
276+
277+
Return the value for key if present, otherwise default.
278+
[clinic]*/
279+
280+
PyDoc_STRVAR(dbm_dbm_get__doc__,
281+
"Return the value for key if present, otherwise default.\n"
282+
"\n"
283+
"dbm.dbm.get(key, [default])");
284+
285+
#define DBM_DBM_GET_METHODDEF \
286+
{"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__},
287+
288+
static PyObject *
289+
dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value);
290+
251291
static PyObject *
252-
dbm_get(dbmobject *dp, PyObject *args)
292+
dbm_dbm_get(PyObject *self, PyObject *args)
253293
{
254-
datum key, val;
255-
PyObject *defvalue = Py_None;
256-
char *tmp_ptr;
257-
Py_ssize_t tmp_size;
294+
PyObject *return_value = NULL;
295+
const char *key;
296+
Py_ssize_clean_t key_length;
297+
int group_right_1 = 0;
298+
PyObject *default_value = NULL;
299+
300+
switch (PyTuple_Size(args)) {
301+
case 1:
302+
if (!PyArg_ParseTuple(args, "s#:get", &key, &key_length))
303+
return NULL;
304+
break;
305+
case 2:
306+
if (!PyArg_ParseTuple(args, "s#O:get", &key, &key_length, &default_value))
307+
return NULL;
308+
group_right_1 = 1;
309+
break;
310+
default:
311+
PyErr_SetString(PyExc_TypeError, "dbm.dbm.get requires 1 to 2 arguments");
312+
return NULL;
313+
}
314+
return_value = dbm_dbm_get_impl((dbmobject *)self, key, key_length, group_right_1, default_value);
258315

259-
if (!PyArg_ParseTuple(args, "s#|O:get",
260-
&tmp_ptr, &tmp_size, &defvalue))
261-
return NULL;
262-
key.dptr = tmp_ptr;
263-
key.dsize = tmp_size;
316+
return return_value;
317+
}
318+
319+
static PyObject *
320+
dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value)
321+
/*[clinic checksum: 5b4265e66568f163ef0fc7efec09410eaf793508]*/
322+
{
323+
datum dbm_key, val;
324+
325+
if (!group_right_1)
326+
default_value = Py_None;
327+
dbm_key.dptr = (char *)key;
328+
dbm_key.dsize = key_length;
264329
check_dbmobject_open(dp);
265-
val = dbm_fetch(dp->di_dbm, key);
330+
val = dbm_fetch(dp->di_dbm, dbm_key);
266331
if (val.dptr != NULL)
267332
return PyBytes_FromStringAndSize(val.dptr, val.dsize);
268-
else {
269-
Py_INCREF(defvalue);
270-
return defvalue;
271-
}
333+
334+
Py_INCREF(default_value);
335+
return default_value;
272336
}
273337

274338
static PyObject *
@@ -333,9 +397,7 @@ static PyMethodDef dbm_methods[] = {
333397
"close()\nClose the database."},
334398
{"keys", (PyCFunction)dbm_keys, METH_NOARGS,
335399
"keys() -> list\nReturn a list of all keys in the database."},
336-
{"get", (PyCFunction)dbm_get, METH_VARARGS,
337-
"get(key[, default]) -> value\n"
338-
"Return the value for key if present, otherwise default."},
400+
DBM_DBM_GET_METHODDEF
339401
{"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
340402
"setdefault(key[, default]) -> value\n"
341403
"Return the value for key if present, otherwise default. If key\n"
@@ -379,7 +441,6 @@ static PyTypeObject Dbmtype = {
379441
/* ----------------------------------------------------------------- */
380442

381443
/*[clinic]
382-
module dbm
383444
384445
dbm.open as dbmopen
385446
@@ -415,10 +476,10 @@ PyDoc_STRVAR(dbmopen__doc__,
415476
{"open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
416477

417478
static PyObject *
418-
dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode);
479+
dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode);
419480

420481
static PyObject *
421-
dbmopen(PyObject *module, PyObject *args)
482+
dbmopen(PyModuleDef *module, PyObject *args)
422483
{
423484
PyObject *return_value = NULL;
424485
const char *filename;
@@ -436,8 +497,8 @@ dbmopen(PyObject *module, PyObject *args)
436497
}
437498

438499
static PyObject *
439-
dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode)
440-
/*[clinic checksum: 2b0ec9e3c6ecd19e06d16c9f0ba33848245cb1ab]*/
500+
dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode)
501+
/*[clinic checksum: c1f2036017ec36a43ac6f59893732751e67c19d5]*/
441502
{
442503
int iflags;
443504

Modules/_weakref.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ PyDoc_STRVAR(_weakref_getweakrefcount__doc__,
2525
{"getweakrefcount", (PyCFunction)_weakref_getweakrefcount, METH_O, _weakref_getweakrefcount__doc__},
2626

2727
static Py_ssize_t
28-
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object);
28+
_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object);
2929

3030
static PyObject *
31-
_weakref_getweakrefcount(PyObject *module, PyObject *object)
31+
_weakref_getweakrefcount(PyModuleDef *module, PyObject *object)
3232
{
3333
PyObject *return_value = NULL;
3434
Py_ssize_t _return_value;
@@ -42,8 +42,8 @@ _weakref_getweakrefcount(PyObject *module, PyObject *object)
4242
}
4343

4444
static Py_ssize_t
45-
_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
46-
/*[clinic checksum: 05cffbc3a4b193a0b7e645da81be281748704f69]*/
45+
_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object)
46+
/*[clinic checksum: 015113be0c9a0a8672d35df10c63e3642cc23da4]*/
4747
{
4848
PyWeakReference **list;
4949

Modules/posixmodule.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2460,10 +2460,10 @@ PyDoc_STRVAR(os_stat__doc__,
24602460
{"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__},
24612461

24622462
static PyObject *
2463-
os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks);
2463+
os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks);
24642464

24652465
static PyObject *
2466-
os_stat(PyObject *module, PyObject *args, PyObject *kwargs)
2466+
os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs)
24672467
{
24682468
PyObject *return_value = NULL;
24692469
static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
@@ -2485,8 +2485,8 @@ os_stat(PyObject *module, PyObject *args, PyObject *kwargs)
24852485
}
24862486

24872487
static PyObject *
2488-
os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
2489-
/*[clinic checksum: 89390f78327e3f045a81974d758d3996e2a71f68]*/
2488+
os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks)
2489+
/*[clinic checksum: b08112eff0ceab3ec2c72352da95ce73f245d104]*/
24902490
{
24912491
return posix_do_stat("stat", path, dir_fd, follow_symlinks);
24922492
}
@@ -2600,10 +2600,10 @@ PyDoc_STRVAR(os_access__doc__,
26002600
{"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__},
26012601

26022602
static PyObject *
2603-
os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks);
2603+
os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks);
26042604

26052605
static PyObject *
2606-
os_access(PyObject *module, PyObject *args, PyObject *kwargs)
2606+
os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs)
26072607
{
26082608
PyObject *return_value = NULL;
26092609
static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL};
@@ -2627,8 +2627,8 @@ os_access(PyObject *module, PyObject *args, PyObject *kwargs)
26272627
}
26282628

26292629
static PyObject *
2630-
os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
2631-
/*[clinic checksum: aa3e145816a748172e62df8e44af74169c7e1247]*/
2630+
os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
2631+
/*[clinic checksum: b9f8ececb061d31b64220c29526bfee642d1b602]*/
26322632
{
26332633
PyObject *return_value = NULL;
26342634

@@ -2734,10 +2734,10 @@ PyDoc_STRVAR(os_ttyname__doc__,
27342734
{"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__},
27352735

27362736
static char *
2737-
os_ttyname_impl(PyObject *module, int fd);
2737+
os_ttyname_impl(PyModuleDef *module, int fd);
27382738

27392739
static PyObject *
2740-
os_ttyname(PyObject *module, PyObject *args)
2740+
os_ttyname(PyModuleDef *module, PyObject *args)
27412741
{
27422742
PyObject *return_value = NULL;
27432743
int fd;
@@ -2757,8 +2757,8 @@ os_ttyname(PyObject *module, PyObject *args)
27572757
}
27582758

27592759
static char *
2760-
os_ttyname_impl(PyObject *module, int fd)
2761-
/*[clinic checksum: c742dd621ec98d0f81d37d264e1d3c89c7a5fb1a]*/
2760+
os_ttyname_impl(PyModuleDef *module, int fd)
2761+
/*[clinic checksum: 61e4e525984cb293f949ccae6ae393c0011dfe8e]*/
27622762
{
27632763
char *ret;
27642764

0 commit comments

Comments
 (0)