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.
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 91456e9..13c4ecc 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -4167,10 +4167,10 @@
{"now", (PyCFunction)datetime_datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_datetime_now__doc__},
static PyObject *
-datetime_datetime_now_impl(PyObject *cls, PyObject *tz);
+datetime_datetime_now_impl(PyTypeObject *cls, PyObject *tz);
static PyObject *
-datetime_datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs)
+datetime_datetime_now(PyTypeObject *cls, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static char *_keywords[] = {"tz", NULL};
@@ -4187,8 +4187,8 @@
}
static PyObject *
-datetime_datetime_now_impl(PyObject *cls, PyObject *tz)
-/*[clinic checksum: cde1daca68c9b7dca6df51759db2de1d43a39774]*/
+datetime_datetime_now_impl(PyTypeObject *cls, PyObject *tz)
+/*[clinic checksum: 5e61647d5d1feaf1ab096c5406ccea17bb7b061c]*/
{
PyObject *self;
@@ -4198,7 +4198,7 @@
if (check_tzinfo_subclass(tz) < 0)
return NULL;
- self = datetime_best_possible(cls,
+ self = datetime_best_possible((PyObject *)cls,
tz == Py_None ? localtime : gmtime,
tz);
if (self != NULL && tz != Py_None) {
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
index 7772b49..10f872d 100644
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -43,6 +43,20 @@
static PyObject *DbmError;
+/*[clinic]
+module dbm
+class dbm.dbm
+[clinic]*/
+/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+
+/*[python]
+class dbmobject_converter(self_converter):
+ type = "dbmobject *"
+ def converter_init(self):
+ self.name = 'dp'
+[python]*/
+/*[python checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+
static PyObject *
newdbmobject(const char *file, int flags, int mode)
{
@@ -248,27 +262,77 @@
0, /* sq_inplace_repeat */
};
-static PyObject *
-dbm_get(dbmobject *dp, PyObject *args)
-{
- datum key, val;
- PyObject *defvalue = Py_None;
- char *tmp_ptr;
- Py_ssize_t tmp_size;
+/*[clinic]
- if (!PyArg_ParseTuple(args, "s#|O:get",
- &tmp_ptr, &tmp_size, &defvalue))
- return NULL;
- key.dptr = tmp_ptr;
- key.dsize = tmp_size;
+dbm.dbm.get
+
+ self: dbmobject
+
+ key: str(length=True)
+ [
+ default: object
+ ]
+ /
+
+Return the value for key if present, otherwise default.
+[clinic]*/
+
+PyDoc_STRVAR(dbm_dbm_get__doc__,
+"Return the value for key if present, otherwise default.\n"
+"\n"
+"dbm.dbm.get(key, [default])");
+
+#define DBM_DBM_GET_METHODDEF \
+ {"get", (PyCFunction)dbm_dbm_get, METH_VARARGS, dbm_dbm_get__doc__},
+
+static PyObject *
+dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value);
+
+static PyObject *
+dbm_dbm_get(PyObject *self, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ const char *key;
+ Py_ssize_clean_t key_length;
+ int group_right_1 = 0;
+ PyObject *default_value = NULL;
+
+ switch (PyTuple_Size(args)) {
+ case 1:
+ if (!PyArg_ParseTuple(args, "s#:get", &key, &key_length))
+ return NULL;
+ break;
+ case 2:
+ if (!PyArg_ParseTuple(args, "s#O:get", &key, &key_length, &default_value))
+ return NULL;
+ group_right_1 = 1;
+ break;
+ default:
+ PyErr_SetString(PyExc_TypeError, "dbm.dbm.get requires 1 to 2 arguments");
+ return NULL;
+ }
+ return_value = dbm_dbm_get_impl((dbmobject *)self, key, key_length, group_right_1, default_value);
+
+ return return_value;
+}
+
+static PyObject *
+dbm_dbm_get_impl(dbmobject *dp, const char *key, Py_ssize_clean_t key_length, int group_right_1, PyObject *default_value)
+/*[clinic checksum: 5b4265e66568f163ef0fc7efec09410eaf793508]*/
+{
+ datum dbm_key, val;
+
+ if (!group_right_1)
+ default_value = Py_None;
+ dbm_key.dptr = (char *)key;
+ dbm_key.dsize = key_length;
check_dbmobject_open(dp);
- val = dbm_fetch(dp->di_dbm, key);
+ val = dbm_fetch(dp->di_dbm, dbm_key);
if (val.dptr != NULL)
return PyBytes_FromStringAndSize(val.dptr, val.dsize);
- else {
- Py_INCREF(defvalue);
- return defvalue;
- }
+
+ Py_INCREF(default_value);
+ return default_value;
}
static PyObject *
@@ -333,9 +397,7 @@
"close()\nClose the database."},
{"keys", (PyCFunction)dbm_keys, METH_NOARGS,
"keys() -> list\nReturn a list of all keys in the database."},
- {"get", (PyCFunction)dbm_get, METH_VARARGS,
- "get(key[, default]) -> value\n"
- "Return the value for key if present, otherwise default."},
+ DBM_DBM_GET_METHODDEF
{"setdefault", (PyCFunction)dbm_setdefault, METH_VARARGS,
"setdefault(key[, default]) -> value\n"
"Return the value for key if present, otherwise default. If key\n"
@@ -379,7 +441,6 @@
/* ----------------------------------------------------------------- */
/*[clinic]
-module dbm
dbm.open as dbmopen
@@ -415,10 +476,10 @@
{"open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
static PyObject *
-dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode);
+dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode);
static PyObject *
-dbmopen(PyObject *module, PyObject *args)
+dbmopen(PyModuleDef *module, PyObject *args)
{
PyObject *return_value = NULL;
const char *filename;
@@ -436,8 +497,8 @@
}
static PyObject *
-dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode)
-/*[clinic checksum: 2b0ec9e3c6ecd19e06d16c9f0ba33848245cb1ab]*/
+dbmopen_impl(PyModuleDef *module, const char *filename, const char *flags, int mode)
+/*[clinic checksum: c1f2036017ec36a43ac6f59893732751e67c19d5]*/
{
int iflags;
diff --git a/Modules/_weakref.c b/Modules/_weakref.c
index 771639d..af845ff 100644
--- a/Modules/_weakref.c
+++ b/Modules/_weakref.c
@@ -25,10 +25,10 @@
{"getweakrefcount", (PyCFunction)_weakref_getweakrefcount, METH_O, _weakref_getweakrefcount__doc__},
static Py_ssize_t
-_weakref_getweakrefcount_impl(PyObject *module, PyObject *object);
+_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object);
static PyObject *
-_weakref_getweakrefcount(PyObject *module, PyObject *object)
+_weakref_getweakrefcount(PyModuleDef *module, PyObject *object)
{
PyObject *return_value = NULL;
Py_ssize_t _return_value;
@@ -42,8 +42,8 @@
}
static Py_ssize_t
-_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
-/*[clinic checksum: 05cffbc3a4b193a0b7e645da81be281748704f69]*/
+_weakref_getweakrefcount_impl(PyModuleDef *module, PyObject *object)
+/*[clinic checksum: 015113be0c9a0a8672d35df10c63e3642cc23da4]*/
{
PyWeakReference **list;
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 4c96204..9143fea 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -2460,10 +2460,10 @@
{"stat", (PyCFunction)os_stat, METH_VARARGS|METH_KEYWORDS, os_stat__doc__},
static PyObject *
-os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks);
+os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks);
static PyObject *
-os_stat(PyObject *module, PyObject *args, PyObject *kwargs)
+os_stat(PyModuleDef *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
@@ -2485,8 +2485,8 @@
}
static PyObject *
-os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
-/*[clinic checksum: 89390f78327e3f045a81974d758d3996e2a71f68]*/
+os_stat_impl(PyModuleDef *module, path_t *path, int dir_fd, int follow_symlinks)
+/*[clinic checksum: b08112eff0ceab3ec2c72352da95ce73f245d104]*/
{
return posix_do_stat("stat", path, dir_fd, follow_symlinks);
}
@@ -2600,10 +2600,10 @@
{"access", (PyCFunction)os_access, METH_VARARGS|METH_KEYWORDS, os_access__doc__},
static PyObject *
-os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks);
+os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks);
static PyObject *
-os_access(PyObject *module, PyObject *args, PyObject *kwargs)
+os_access(PyModuleDef *module, PyObject *args, PyObject *kwargs)
{
PyObject *return_value = NULL;
static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL};
@@ -2627,8 +2627,8 @@
}
static PyObject *
-os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
-/*[clinic checksum: aa3e145816a748172e62df8e44af74169c7e1247]*/
+os_access_impl(PyModuleDef *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
+/*[clinic checksum: b9f8ececb061d31b64220c29526bfee642d1b602]*/
{
PyObject *return_value = NULL;
@@ -2734,10 +2734,10 @@
{"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__},
static char *
-os_ttyname_impl(PyObject *module, int fd);
+os_ttyname_impl(PyModuleDef *module, int fd);
static PyObject *
-os_ttyname(PyObject *module, PyObject *args)
+os_ttyname(PyModuleDef *module, PyObject *args)
{
PyObject *return_value = NULL;
int fd;
@@ -2757,8 +2757,8 @@
}
static char *
-os_ttyname_impl(PyObject *module, int fd)
-/*[clinic checksum: c742dd621ec98d0f81d37d264e1d3c89c7a5fb1a]*/
+os_ttyname_impl(PyModuleDef *module, int fd)
+/*[clinic checksum: 61e4e525984cb293f949ccae6ae393c0011dfe8e]*/
{
char *ret;
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
index 8fdc239..b223aa7 100644
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -81,6 +81,13 @@
PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
}
+/*[clinic]
+module zlib
+class zlib.Compress
+class zlib.Decompress
+[clinic]*/
+/*[clinic checksum: da39a3ee5e6b4b0d3255bfef95601890afd80709]*/
+
PyDoc_STRVAR(compressobj__doc__,
"compressobj(level=-1, method=DEFLATED, wbits=15, memlevel=8,\n"
" strategy=Z_DEFAULT_STRATEGY[, zdict])\n"
@@ -157,32 +164,86 @@
PyMem_RawFree(ptr);
}
-PyDoc_STRVAR(compress__doc__,
-"compress(string[, level]) -- Returned compressed string.\n"
+/*[clinic]
+zlib.compress
+ bytes: Py_buffer
+ Binary data to be compressed.
+ [
+ level: int
+ Compression level, in 0-9.
+ ]
+ /
+
+Returns compressed string.
+
+[clinic]*/
+
+PyDoc_STRVAR(zlib_compress__doc__,
+"Returns compressed string.\n"
"\n"
-"Optional arg level is the compression level, in 0-9.");
+"zlib.compress(bytes, [level])\n"
+" bytes\n"
+" Binary data to be compressed.\n"
+" level\n"
+" Compression level, in 0-9.");
+
+#define ZLIB_COMPRESS_METHODDEF \
+ {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
static PyObject *
-PyZlib_compress(PyObject *self, PyObject *args)
+zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level);
+
+static PyObject *
+zlib_compress(PyModuleDef *module, PyObject *args)
+{
+ PyObject *return_value = NULL;
+ Py_buffer bytes;
+ int group_right_1 = 0;
+ int level = 0;
+
+ switch (PyTuple_Size(args)) {
+ case 1:
+ if (!PyArg_ParseTuple(args, "y*:compress", &bytes))
+ return NULL;
+ break;
+ case 2:
+ if (!PyArg_ParseTuple(args, "y*i:compress", &bytes, &level))
+ return NULL;
+ group_right_1 = 1;
+ break;
+ default:
+ PyErr_SetString(PyExc_TypeError, "zlib.compress requires 1 to 2 arguments");
+ return NULL;
+ }
+ return_value = zlib_compress_impl(module, &bytes, group_right_1, level);
+
+ /* Cleanup for bytes */
+ if (bytes.buf)
+ PyBuffer_Release(&bytes);
+
+ return return_value;
+}
+
+static PyObject *
+zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int group_right_1, int level)
+/*[clinic checksum: 03e857836db25448d4d572da537eb7faf7695d71]*/
{
PyObject *ReturnVal = NULL;
- Py_buffer pinput;
Byte *input, *output = NULL;
unsigned int length;
- int level=Z_DEFAULT_COMPRESSION, err;
+ int err;
z_stream zst;
- /* require Python string object, optional 'level' arg */
- if (!PyArg_ParseTuple(args, "y*|i:compress", &pinput, &level))
- return NULL;
+ if (!group_right_1)
+ level = Z_DEFAULT_COMPRESSION;
- if ((size_t)pinput.len > UINT_MAX) {
+ if ((size_t)bytes->len > UINT_MAX) {
PyErr_SetString(PyExc_OverflowError,
"Size does not fit in an unsigned int");
goto error;
}
- input = pinput.buf;
- length = (unsigned int)pinput.len;
+ input = bytes->buf;
+ length = (unsigned int)bytes->len;
zst.avail_out = length + length/1000 + 12 + 1;
@@ -239,7 +300,6 @@
zlib_error(zst, err, "while finishing compression");
error:
- PyBuffer_Release(&pinput);
PyMem_Free(output);
return ReturnVal;
@@ -682,10 +742,6 @@
}
/*[clinic]
-
-module zlib
-class zlib.Decompress
-
zlib.Decompress.decompress
data: Py_buffer
@@ -739,14 +795,15 @@
exit:
/* Cleanup for data */
- PyBuffer_Release(&data);
+ if (data.buf)
+ PyBuffer_Release(&data);
return return_value;
}
static PyObject *
zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, unsigned int max_length)
-/*[clinic checksum: 76ca9259e3f5ca86bae9da3d0e75637b5d492234]*/
+/*[clinic checksum: f83e91728d327462d7ccbee95299514f26b92253]*/
{
compobject *zself = (compobject *)self;
int err;
@@ -966,8 +1023,6 @@
#ifdef HAVE_ZLIB_COPY
/*[clinic]
-
-class zlib.Compress
zlib.Compress.copy
Return a copy of the compression object.
@@ -1295,8 +1350,7 @@
{
{"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,
adler32__doc__},
- {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,
- compress__doc__},
+ ZLIB_COMPRESS_METHODDEF
{"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS|METH_KEYWORDS,
compressobj__doc__},
{"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,