| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -2973,6 +2973,17 @@ def test_depr_star_new(self): | |||
| 2973 | 2973 | ac_tester.DeprStarNew(None) | |
| 2974 | 2974 | self.assertEqual(cm.filename, __file__) | |
| 2975 | 2975 | ||
| 2976 | + def test_depr_star_new_cloned(self): | ||
| 2977 | + regex = re.escape( | ||
| 2978 | + "Passing positional arguments to _testclinic.DeprStarNew.cloned() " | ||
| 2979 | + "is deprecated. Parameter 'a' will become a keyword-only parameter " | ||
| 2980 | + "in Python 3.14." | ||
| 2981 | + ) | ||
| 2982 | + obj = ac_tester.DeprStarNew(a=None) | ||
| 2983 | + with self.assertWarnsRegex(DeprecationWarning, regex) as cm: | ||
| 2984 | + obj.cloned(None) | ||
| 2985 | + self.assertEqual(cm.filename, __file__) | ||
| 2986 | + | ||
| 2976 | 2987 | def test_depr_star_init(self): | |
| 2977 | 2988 | regex = re.escape( | |
| 2978 | 2989 | "Passing positional arguments to _testclinic.DeprStarInit() is " | |
@@ -2983,6 +2994,17 @@ def test_depr_star_init(self): | |||
| 2983 | 2994 | ac_tester.DeprStarInit(None) | |
| 2984 | 2995 | self.assertEqual(cm.filename, __file__) | |
| 2985 | 2996 | ||
| 2997 | + def test_depr_star_init_cloned(self): | ||
| 2998 | + regex = re.escape( | ||
| 2999 | + "Passing positional arguments to _testclinic.DeprStarInit.cloned() " | ||
| 3000 | + "is deprecated. Parameter 'a' will become a keyword-only parameter " | ||
| 3001 | + "in Python 3.14." | ||
| 3002 | + ) | ||
| 3003 | + obj = ac_tester.DeprStarInit(a=None) | ||
| 3004 | + with self.assertWarnsRegex(DeprecationWarning, regex) as cm: | ||
| 3005 | + obj.cloned(None) | ||
| 3006 | + self.assertEqual(cm.filename, __file__) | ||
| 3007 | + | ||
| 2986 | 3008 | def test_depr_star_pos0_len1(self): | |
| 2987 | 3009 | fn = ac_tester.depr_star_pos0_len1 | |
| 2988 | 3010 | fn(a=None) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,2 @@ | |||
| 1 | + Argument Clinic can now clone :meth:`!__init__` and :meth:`!__new__` | ||
| 2 | + methods. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1230,12 +1230,29 @@ depr_star_new_impl(PyTypeObject *type, PyObject *a) | |||
| 1230 | 1230 | return type->tp_alloc(type, 0); | |
| 1231 | 1231 | } | |
| 1232 | 1232 | ||
| 1233 | + /*[clinic input] | ||
| 1234 | + _testclinic.DeprStarNew.cloned as depr_star_new_clone = _testclinic.DeprStarNew.__new__ | ||
| 1235 | + [clinic start generated code]*/ | ||
| 1236 | + | ||
| 1237 | + static PyObject * | ||
| 1238 | + depr_star_new_clone_impl(PyObject *type, PyObject *a) | ||
| 1239 | + /*[clinic end generated code: output=3b17bf885fa736bc input=ea659285d5dbec6c]*/ | ||
| 1240 | + { | ||
| 1241 | + Py_RETURN_NONE; | ||
| 1242 | + } | ||
| 1243 | + | ||
| 1244 | + static struct PyMethodDef depr_star_new_methods[] = { | ||
| 1245 | + DEPR_STAR_NEW_CLONE_METHODDEF | ||
| 1246 | + {NULL, NULL} | ||
| 1247 | + }; | ||
| 1248 | + | ||
| 1233 | 1249 | static PyTypeObject DeprStarNew = { | |
| 1234 | 1250 | PyVarObject_HEAD_INIT(NULL, 0) | |
| 1235 | 1251 | .tp_name = "_testclinic.DeprStarNew", | |
| 1236 | 1252 | .tp_basicsize = sizeof(PyObject), | |
| 1237 | 1253 | .tp_new = depr_star_new, | |
| 1238 | 1254 | .tp_flags = Py_TPFLAGS_DEFAULT, | |
| 1255 | + .tp_methods = depr_star_new_methods, | ||
| 1239 | 1256 | }; | |
| 1240 | 1257 | ||
| 1241 | 1258 | ||
@@ -1254,13 +1271,30 @@ depr_star_init_impl(PyObject *self, PyObject *a) | |||
| 1254 | 1271 | return 0; | |
| 1255 | 1272 | } | |
| 1256 | 1273 | ||
| 1274 | + /*[clinic input] | ||
| 1275 | + _testclinic.DeprStarInit.cloned as depr_star_init_clone = _testclinic.DeprStarInit.__init__ | ||
| 1276 | + [clinic start generated code]*/ | ||
| 1277 | + | ||
| 1278 | + static PyObject * | ||
| 1279 | + depr_star_init_clone_impl(PyObject *self, PyObject *a) | ||
| 1280 | + /*[clinic end generated code: output=ddfe8a1b5531e7cc input=561e103fe7f8e94f]*/ | ||
| 1281 | + { | ||
| 1282 | + Py_RETURN_NONE; | ||
| 1283 | + } | ||
| 1284 | + | ||
| 1285 | + static struct PyMethodDef depr_star_init_methods[] = { | ||
| 1286 | + DEPR_STAR_INIT_CLONE_METHODDEF | ||
| 1287 | + {NULL, NULL} | ||
| 1288 | + }; | ||
| 1289 | + | ||
| 1257 | 1290 | static PyTypeObject DeprStarInit = { | |
| 1258 | 1291 | PyVarObject_HEAD_INIT(NULL, 0) | |
| 1259 | 1292 | .tp_name = "_testclinic.DeprStarInit", | |
| 1260 | 1293 | .tp_basicsize = sizeof(PyObject), | |
| 1261 | 1294 | .tp_new = PyType_GenericNew, | |
| 1262 | 1295 | .tp_init = depr_star_init, | |
| 1263 | 1296 | .tp_flags = Py_TPFLAGS_DEFAULT, | |
| 1297 | + .tp_methods = depr_star_init_methods, | ||
| 1264 | 1298 | }; | |
| 1265 | 1299 | ||
| 1266 | 1300 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -4888,13 +4888,25 @@ def state_modulename_name(self, line: str) -> None: | |||
| 4888 | 4888 | function_name = fields.pop() | |
| 4889 | 4889 | module, cls = self.clinic._module_and_class(fields) | |
| 4890 | 4890 | ||
| 4891 | - if not (existing_function.kind is self.kind and existing_function.coexist == self.coexist): | ||
| 4892 | - fail("'kind' of function and cloned function don't match! " | ||
| 4893 | - "(@classmethod/@staticmethod/@coexist)") | ||
| 4894 | - function = existing_function.copy( | ||
| 4895 | - name=function_name, full_name=full_name, module=module, | ||
| 4896 | - cls=cls, c_basename=c_basename, docstring='' | ||
| 4897 | - ) | ||
| 4891 | + overrides: dict[str, Any] = { | ||
| 4892 | + "name": function_name, | ||
| 4893 | + "full_name": full_name, | ||
| 4894 | + "module": module, | ||
| 4895 | + "cls": cls, | ||
| 4896 | + "c_basename": c_basename, | ||
| 4897 | + "docstring": "", | ||
| 4898 | + } | ||
| 4899 | + if not (existing_function.kind is self.kind and | ||
| 4900 | + existing_function.coexist == self.coexist): | ||
| 4901 | + # Allow __new__ or __init__ methods. | ||
| 4902 | + if existing_function.kind.new_or_init: | ||
| 4903 | + overrides["kind"] = self.kind | ||
| 4904 | + # Future enhancement: allow custom return converters | ||
| 4905 | + overrides["return_converter"] = CReturnConverter() | ||
| 4906 | + else: | ||
| 4907 | + fail("'kind' of function and cloned function don't match! " | ||
| 4908 | + "(@classmethod/@staticmethod/@coexist)") | ||
| 4909 | + function = existing_function.copy(**overrides) | ||
| 4898 | 4910 | self.function = function | |
| 4899 | 4911 | self.block.signatures.append(function) | |
| 4900 | 4912 | (cls or module).functions.append(function) | |
| Back | FazBrowse Home | New Git URL |
0 commit comments