[Python-checkins] cpython: Argument Clinic: rename "self" to "module" for module-level functions.

larry.hastings python-checkins at python.org
Mon Nov 18 18:32:31 CET 2013


http://hg.python.org/cpython/rev/64c853a88a10
changeset:   87245:64c853a88a10
user:        Larry Hastings <larry at hastings.org>
date:        Mon Nov 18 09:32:13 2013 -0800
summary:
  Argument Clinic: rename "self" to "module" for module-level functions.

files:
  Modules/_cursesmodule.c   |   1 -
  Modules/_datetimemodule.c |  23 +++++++-------
  Modules/_dbmmodule.c      |  10 +++---
  Modules/_weakref.c        |  10 +++---
  Modules/posixmodule.c     |  42 +++++++++++++-------------
  Modules/unicodedata.c     |  23 +++++++-------
  Modules/zlibmodule.c      |  40 +++++++++++++------------
  Objects/dictobject.c      |   2 +-
  Objects/unicodeobject.c   |   2 +-
  Tools/clinic/clinic.py    |  26 ++++++++++------
  10 files changed, 94 insertions(+), 85 deletions(-)


diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c
--- a/Modules/_cursesmodule.c
+++ b/Modules/_cursesmodule.c
@@ -551,7 +551,6 @@
 
 /*[clinic]
 module curses
-
 class curses.window
 
 curses.window.addch
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -4141,9 +4141,10 @@
 
 /*[clinic]
 module datetime
+class datetime.datetime
 
 @classmethod
-datetime.now
+datetime.datetime.now
 
     tz: object = None
         Timezone object.
@@ -4153,23 +4154,23 @@
 If no tz is specified, uses local timezone.
 [clinic]*/
 
-PyDoc_STRVAR(datetime_now__doc__,
+PyDoc_STRVAR(datetime_datetime_now__doc__,
 "Returns new datetime object representing current time local to tz.\n"
 "\n"
-"datetime.now(tz=None)\n"
+"datetime.datetime.now(tz=None)\n"
 "  tz\n"
 "    Timezone object.\n"
 "\n"
 "If no tz is specified, uses local timezone.");
 
-#define DATETIME_NOW_METHODDEF    \
-    {"now", (PyCFunction)datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_now__doc__},
+#define DATETIME_DATETIME_NOW_METHODDEF    \
+    {"now", (PyCFunction)datetime_datetime_now, METH_VARARGS|METH_KEYWORDS|METH_CLASS, datetime_datetime_now__doc__},
 
 static PyObject *
-datetime_now_impl(PyObject *cls, PyObject *tz);
+datetime_datetime_now_impl(PyObject *cls, PyObject *tz);
 
 static PyObject *
-datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs)
+datetime_datetime_now(PyObject *cls, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
     static char *_keywords[] = {"tz", NULL};
@@ -4179,15 +4180,15 @@
         "|O:now", _keywords,
         &tz))
         goto exit;
-    return_value = datetime_now_impl(cls, tz);
+    return_value = datetime_datetime_now_impl(cls, tz);
 
 exit:
     return return_value;
 }
 
 static PyObject *
-datetime_now_impl(PyObject *cls, PyObject *tz)
-/*[clinic checksum: 328b54387f4c2f8cb534997e1bd55f8cb38c4992]*/
+datetime_datetime_now_impl(PyObject *cls, PyObject *tz)
+/*[clinic checksum: cde1daca68c9b7dca6df51759db2de1d43a39774]*/
 {
     PyObject *self;
 
@@ -5037,7 +5038,7 @@
 
     /* Class methods: */
 
-    DATETIME_NOW_METHODDEF
+    DATETIME_DATETIME_NOW_METHODDEF
 
     {"utcnow",         (PyCFunction)datetime_utcnow,
      METH_NOARGS | METH_CLASS,
diff --git a/Modules/_dbmmodule.c b/Modules/_dbmmodule.c
--- a/Modules/_dbmmodule.c
+++ b/Modules/_dbmmodule.c
@@ -415,10 +415,10 @@
     {"open", (PyCFunction)dbmopen, METH_VARARGS, dbmopen__doc__},
 
 static PyObject *
-dbmopen_impl(PyObject *self, const char *filename, const char *flags, int mode);
+dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode);
 
 static PyObject *
-dbmopen(PyObject *self, PyObject *args)
+dbmopen(PyObject *module, PyObject *args)
 {
     PyObject *return_value = NULL;
     const char *filename;
@@ -429,15 +429,15 @@
         "s|si:open",
         &filename, &flags, &mode))
         goto exit;
-    return_value = dbmopen_impl(self, filename, flags, mode);
+    return_value = dbmopen_impl(module, filename, flags, mode);
 
 exit:
     return return_value;
 }
 
 static PyObject *
-dbmopen_impl(PyObject *self, const char *filename, const char *flags, int mode)
-/*[clinic checksum: 61007c796d38af85c8035afa769fb4bb453429ee]*/
+dbmopen_impl(PyObject *module, const char *filename, const char *flags, int mode)
+/*[clinic checksum: 2b0ec9e3c6ecd19e06d16c9f0ba33848245cb1ab]*/
 {
     int iflags;
 
diff --git a/Modules/_weakref.c b/Modules/_weakref.c
--- a/Modules/_weakref.c
+++ b/Modules/_weakref.c
@@ -25,14 +25,14 @@
     {"getweakrefcount", (PyCFunction)_weakref_getweakrefcount, METH_O, _weakref_getweakrefcount__doc__},
 
 static Py_ssize_t
-_weakref_getweakrefcount_impl(PyObject *self, PyObject *object);
+_weakref_getweakrefcount_impl(PyObject *module, PyObject *object);
 
 static PyObject *
-_weakref_getweakrefcount(PyObject *self, PyObject *object)
+_weakref_getweakrefcount(PyObject *module, PyObject *object)
 {
     PyObject *return_value = NULL;
     Py_ssize_t _return_value;
-    _return_value = _weakref_getweakrefcount_impl(self, object);
+    _return_value = _weakref_getweakrefcount_impl(module, object);
     if ((_return_value == -1) && PyErr_Occurred())
         goto exit;
     return_value = PyLong_FromSsize_t(_return_value);
@@ -42,8 +42,8 @@
 }
 
 static Py_ssize_t
-_weakref_getweakrefcount_impl(PyObject *self, PyObject *object)
-/*[clinic checksum: 0b7e7ddd87d483719ebac0fba364fff0ed0182d9]*/
+_weakref_getweakrefcount_impl(PyObject *module, PyObject *object)
+/*[clinic checksum: 05cffbc3a4b193a0b7e645da81be281748704f69]*/
 {
     PyWeakReference **list;
 
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
--- 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 *self, path_t *path, int dir_fd, int follow_symlinks);
-
-static PyObject *
-os_stat(PyObject *self, PyObject *args, PyObject *kwargs)
+os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks);
+
+static PyObject *
+os_stat(PyObject *module, PyObject *args, PyObject *kwargs)
 {
     PyObject *return_value = NULL;
     static char *_keywords[] = {"path", "dir_fd", "follow_symlinks", NULL};
@@ -2475,7 +2475,7 @@
         "O&|$O&p:stat", _keywords,
         path_converter, &path, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &follow_symlinks))
         goto exit;
-    return_value = os_stat_impl(self, &path, dir_fd, follow_symlinks);
+    return_value = os_stat_impl(module, &path, dir_fd, follow_symlinks);
 
 exit:
     /* Cleanup for path */
@@ -2485,8 +2485,8 @@
 }
 
 static PyObject *
-os_stat_impl(PyObject *self, path_t *path, int dir_fd, int follow_symlinks)
-/*[clinic checksum: 9d9af08e8cfafd12f94e73ea3065eb3056f99515]*/
+os_stat_impl(PyObject *module, path_t *path, int dir_fd, int follow_symlinks)
+/*[clinic checksum: 89390f78327e3f045a81974d758d3996e2a71f68]*/
 {
     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 *self, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks);
-
-static PyObject *
-os_access(PyObject *self, PyObject *args, PyObject *kwargs)
+os_access_impl(PyObject *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)
 {
     PyObject *return_value = NULL;
     static char *_keywords[] = {"path", "mode", "dir_fd", "effective_ids", "follow_symlinks", NULL};
@@ -2617,7 +2617,7 @@
         "O&i|$O&pp:access", _keywords,
         path_converter, &path, &mode, OS_STAT_DIR_FD_CONVERTER, &dir_fd, &effective_ids, &follow_symlinks))
         goto exit;
-    return_value = os_access_impl(self, &path, mode, dir_fd, effective_ids, follow_symlinks);
+    return_value = os_access_impl(module, &path, mode, dir_fd, effective_ids, follow_symlinks);
 
 exit:
     /* Cleanup for path */
@@ -2627,8 +2627,8 @@
 }
 
 static PyObject *
-os_access_impl(PyObject *self, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
-/*[clinic checksum: 0147557eb43243df57ba616cc7c35f232c69bc6a]*/
+os_access_impl(PyObject *module, path_t *path, int mode, int dir_fd, int effective_ids, int follow_symlinks)
+/*[clinic checksum: aa3e145816a748172e62df8e44af74169c7e1247]*/
 {
     PyObject *return_value = NULL;
 
@@ -2734,10 +2734,10 @@
     {"ttyname", (PyCFunction)os_ttyname, METH_VARARGS, os_ttyname__doc__},
 
 static char *
-os_ttyname_impl(PyObject *self, int fd);
-
-static PyObject *
-os_ttyname(PyObject *self, PyObject *args)
+os_ttyname_impl(PyObject *module, int fd);
+
+static PyObject *
+os_ttyname(PyObject *module, PyObject *args)
 {
     PyObject *return_value = NULL;
     int fd;
@@ -2747,7 +2747,7 @@
         "i:ttyname",
         &fd))
         goto exit;
-    _return_value = os_ttyname_impl(self, fd);
+    _return_value = os_ttyname_impl(module, fd);
     if (_return_value == NULL)
         goto exit;
     return_value = PyUnicode_DecodeFSDefault(_return_value);
@@ -2757,8 +2757,8 @@
 }
 
 static char *
-os_ttyname_impl(PyObject *self, int fd)
-/*[clinic checksum: ea680155d87bb733f542d67653eca732dd0981a8]*/
+os_ttyname_impl(PyObject *module, int fd)
+/*[clinic checksum: c742dd621ec98d0f81d37d264e1d3c89c7a5fb1a]*/
 {
     char *ret;
 
diff --git a/Modules/unicodedata.c b/Modules/unicodedata.c
--- a/Modules/unicodedata.c
+++ b/Modules/unicodedata.c
@@ -109,7 +109,8 @@
 
 /*[clinic]
 module unicodedata
-unicodedata.decimal
+class unicodedata.UCD
+unicodedata.UCD.decimal
 
     unichr: object(type='str')
     default: object=NULL
@@ -122,23 +123,23 @@
 not given, ValueError is raised.
 [clinic]*/
 
-PyDoc_STRVAR(unicodedata_decimal__doc__,
+PyDoc_STRVAR(unicodedata_UCD_decimal__doc__,
 "Converts a Unicode character into its equivalent decimal value.\n"
 "\n"
-"unicodedata.decimal(unichr, default=None)\n"
+"unicodedata.UCD.decimal(unichr, default=None)\n"
 "\n"
 "Returns the decimal value assigned to the Unicode character unichr\n"
 "as integer. If no such value is defined, default is returned, or, if\n"
 "not given, ValueError is raised.");
 
-#define UNICODEDATA_DECIMAL_METHODDEF    \
-    {"decimal", (PyCFunction)unicodedata_decimal, METH_VARARGS, unicodedata_decimal__doc__},
+#define UNICODEDATA_UCD_DECIMAL_METHODDEF    \
+    {"decimal", (PyCFunction)unicodedata_UCD_decimal, METH_VARARGS, unicodedata_UCD_decimal__doc__},
 
 static PyObject *
-unicodedata_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value);
+unicodedata_UCD_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value);
 
 static PyObject *
-unicodedata_decimal(PyObject *self, PyObject *args)
+unicodedata_UCD_decimal(PyObject *self, PyObject *args)
 {
     PyObject *return_value = NULL;
     PyObject *unichr;
@@ -148,15 +149,15 @@
         "O!|O:decimal",
         &PyUnicode_Type, &unichr, &default_value))
         goto exit;
-    return_value = unicodedata_decimal_impl(self, unichr, default_value);
+    return_value = unicodedata_UCD_decimal_impl(self, unichr, default_value);
 
 exit:
     return return_value;
 }
 
 static PyObject *
-unicodedata_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value)
-/*[clinic checksum: 76c8d1c3dbee495d4cfd86ca6829543a3129344a]*/
+unicodedata_UCD_decimal_impl(PyObject *self, PyObject *unichr, PyObject *default_value)
+/*[clinic checksum: a0980c387387287e2ac230c37d95b26f6903e0d2]*/
 {
     PyUnicodeObject *v = (PyUnicodeObject *)unichr;
     int have_old = 0;
@@ -1288,7 +1289,7 @@
 /* XXX Add doc strings. */
 
 static PyMethodDef unicodedata_functions[] = {
-    UNICODEDATA_DECIMAL_METHODDEF
+    UNICODEDATA_UCD_DECIMAL_METHODDEF
     {"digit", unicodedata_digit, METH_VARARGS, unicodedata_digit__doc__},
     {"numeric", unicodedata_numeric, METH_VARARGS, unicodedata_numeric__doc__},
     {"category", unicodedata_category, METH_VARARGS,
diff --git a/Modules/zlibmodule.c b/Modules/zlibmodule.c
--- a/Modules/zlibmodule.c
+++ b/Modules/zlibmodule.c
@@ -630,8 +630,9 @@
 /*[clinic]
 
 module zlib
+class zlib.Decompress
 
-zlib.decompress
+zlib.Decompress.decompress
 
     data: Py_buffer
         The binary data to decompress.
@@ -648,10 +649,10 @@
 Call the flush() method to clear these buffers.
 [clinic]*/
 
-PyDoc_STRVAR(zlib_decompress__doc__,
+PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
 "Return a string containing the decompressed version of the data.\n"
 "\n"
-"zlib.decompress(data, max_length=0)\n"
+"zlib.Decompress.decompress(data, max_length=0)\n"
 "  data\n"
 "    The binary data to decompress.\n"
 "  max_length\n"
@@ -663,14 +664,14 @@
 "internal buffers for later processing.\n"
 "Call the flush() method to clear these buffers.");
 
-#define ZLIB_DECOMPRESS_METHODDEF    \
-    {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__},
+#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF    \
+    {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
 
 static PyObject *
-zlib_decompress_impl(PyObject *self, Py_buffer *data, int max_length);
+zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, int max_length);
 
 static PyObject *
-zlib_decompress(PyObject *self, PyObject *args)
+zlib_Decompress_decompress(PyObject *self, PyObject *args)
 {
     PyObject *return_value = NULL;
     Py_buffer data;
@@ -680,7 +681,7 @@
         "y*|i:decompress",
         &data, &max_length))
         goto exit;
-    return_value = zlib_decompress_impl(self, &data, max_length);
+    return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
 
 exit:
     /* Cleanup for data */
@@ -690,8 +691,8 @@
 }
 
 static PyObject *
-zlib_decompress_impl(PyObject *self, Py_buffer *data, int max_length)
-/*[clinic checksum: 168d093d400739dde947cca1f4fb0f9d51cdc2c9]*/
+zlib_Decompress_decompress_impl(PyObject *self, Py_buffer *data, int max_length)
+/*[clinic checksum: bfac7a0f07e891869d87c665a76dc2611014420f]*/
 {
     compobject *zself = (compobject *)self;
     int err;
@@ -907,22 +908,23 @@
 
 /*[clinic]
 
-zlib.copy
+class zlib.Compress
+zlib.Compress.copy
 
 Return a copy of the compression object.
 [clinic]*/
 
-PyDoc_STRVAR(zlib_copy__doc__,
+PyDoc_STRVAR(zlib_Compress_copy__doc__,
 "Return a copy of the compression object.\n"
 "\n"
-"zlib.copy()");
+"zlib.Compress.copy()");
 
-#define ZLIB_COPY_METHODDEF    \
-    {"copy", (PyCFunction)zlib_copy, METH_NOARGS, zlib_copy__doc__},
+#define ZLIB_COMPRESS_COPY_METHODDEF    \
+    {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
 
 static PyObject *
-zlib_copy(PyObject *self)
-/*[clinic checksum: 7b648de2c1f933ba2b9fa17331ff1a44d9a4a740]*/
+zlib_Compress_copy(PyObject *self)
+/*[clinic checksum: 2551952e72329f0f2beb48a1dde3780e485a220b]*/
 {
     compobject *zself = (compobject *)self;
     compobject *retval = NULL;
@@ -1118,14 +1120,14 @@
     {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,
               comp_flush__doc__},
 #ifdef HAVE_ZLIB_COPY
-    ZLIB_COPY_METHODDEF
+    ZLIB_COMPRESS_COPY_METHODDEF
 #endif
     {NULL, NULL}
 };
 
 static PyMethodDef Decomp_methods[] =
 {
-    ZLIB_DECOMPRESS_METHODDEF
+    ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
     {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,
               decomp_flush__doc__},
 #ifdef HAVE_ZLIB_COPY
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2161,7 +2161,7 @@
 }
 
 /*[clinic]
-module dict
+class dict
 
 @coexist
 dict.__contains__
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -12722,7 +12722,7 @@
 }
 
 /*[clinic]
-module str
+class str
 
 @staticmethod
 str.maketrans as unicode_maketrans
diff --git a/Tools/clinic/clinic.py b/Tools/clinic/clinic.py
--- a/Tools/clinic/clinic.py
+++ b/Tools/clinic/clinic.py
@@ -127,13 +127,13 @@
 def is_legal_py_identifier(s):
     return all(is_legal_c_identifier(field) for field in s.split('.'))
 
-# added "self", "cls", and "null" just to be safe
+# added "module", "self", "cls", and "null" just to be safe
 # (clinic will generate variables with these names)
 c_keywords = set("""
 asm auto break case char cls const continue default do double
-else enum extern float for goto if inline int long null register
-return self short signed sizeof static struct switch typedef
-typeof union unsigned void volatile while
+else enum extern float for goto if inline int long module null
+register return self short signed sizeof static struct switch
+typedef typeof union unsigned void volatile while
 """.strip().split())
 
 def ensure_legal_c_identifier(s):
@@ -620,7 +620,7 @@
         else:
             if f.kind == CALLABLE:
                 meth_flags = ''
-                self_name = "self"
+                self_name = "self" if f.cls else "module"
             elif f.kind == CLASS_METHOD:
                 meth_flags = 'METH_CLASS'
                 self_name = "cls"
@@ -1028,6 +1028,7 @@
         self.verify = verify
         self.filename = filename
         self.modules = collections.OrderedDict()
+        self.classes = collections.OrderedDict()
 
         global clinic
         clinic = self
@@ -1064,7 +1065,7 @@
             if not in_classes:
                 child = parent.modules.get(field)
                 if child:
-                    module = child
+                    parent = module = child
                     continue
                 in_classes = True
             if not hasattr(parent, 'classes'):
@@ -1129,6 +1130,9 @@
         self.classes = collections.OrderedDict()
         self.functions = []
 
+    def __repr__(self):
+        return "<clinic.Module " + repr(self.name) + " at " + str(id(self)) + ">"
+
 class Class:
     def __init__(self, name, module=None, cls=None):
         self.name = name
@@ -1139,6 +1143,10 @@
         self.classes = collections.OrderedDict()
         self.functions = []
 
+    def __repr__(self):
+        return "<clinic.Class " + repr(self.name) + " at " + str(id(self)) + ">"
+
+
 DATA, CALLABLE, METHOD, STATIC_METHOD, CLASS_METHOD = range(5)
 
 class Function:
@@ -1808,13 +1816,11 @@
         so_far = []
         module, cls = self.clinic._module_and_class(fields)
 
-        if not module:
-            fail("You must explicitly specify the module for the class.")
-
         c = Class(name, module, cls)
-        module.classes[name] = c
         if cls:
             cls.classes[name] = c
+        else:
+            module.classes[name] = c
         self.block.signatures.append(c)
 
     def at_classmethod(self):

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list