Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Include/internal/pycore_modsupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ PyAPI_FUNC(int) _PyArg_NoPositional(const char *funcname, PyObject *args);
#define _PyArg_NoPositional(funcname, args) \
((args) == NULL || _PyArg_NoPositional((funcname), (args)))

// Export for Argument Clinic generated code in shared extensions
PyAPI_FUNC(int) _PyArg_NoPositionalStack(const char *funcname,
Py_ssize_t nargs);
#define _PyArg_NoPositionalStack(funcname, nargs) \
((nargs) == 0 || _PyArg_NoPositionalStack((funcname), (nargs)))

// Export for '_asyncio' shared extension
PyAPI_FUNC(int) _PyArg_NoKeywords(const char *funcname, PyObject *kwargs);
#define _PyArg_NoKeywords(funcname, kwargs) \
Expand Down
36 changes: 35 additions & 1 deletion Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4946,6 +4946,7 @@ Test_cls_with_param_impl(TestObj *self, PyTypeObject *cls, int a)


/*[clinic input]
@vectorcall
Test.__init__
Empty init method.
[clinic start generated code]*/
Expand Down Expand Up @@ -4981,9 +4982,42 @@ Test___init__(PyObject *self, PyObject *args, PyObject *kwargs)
return return_value;
}

static PyObject *
Test_vectorcall(PyObject *type, PyObject *const *args,
size_t nargsf, PyObject *kwnames)
{
PyObject *return_value = NULL;
Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
PyObject *self;
int _result;

assert(_PyType_CAST(type)->tp_init == Test___init__);
/* Make sure the type object is immutable: the generated
* vectorcall doesn't deal e.g. with users reassigning __init__. */
assert(PyType_HasFeature(_PyType_CAST(type), Py_TPFLAGS_IMMUTABLETYPE));
if (!_PyArg_NoPositionalStack("Test", nargs) ||
!_PyArg_NoKwnames("Test", kwnames)) {
goto exit;
}
self = _PyType_CAST(type)->tp_new(_PyType_CAST(type),
(PyObject *)&_Py_SINGLETON(tuple_empty), NULL);
if (self == NULL) {
goto exit;
}
_result = Test___init___impl((TestObj *)self);
if (_result != 0) {
Py_DECREF(self);
goto exit;
}
return_value = self;

exit:
return return_value;
}

static int
Test___init___impl(TestObj *self)
/*[clinic end generated code: output=f6a35c85bc5b408f input=4ea79fee54d0c3ff]*/
/*[clinic end generated code: output=ce04b9d7beacd894 input=645fe693f6c6b9b6]*/


/*[clinic input]
Expand Down
8 changes: 4 additions & 4 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -3500,8 +3500,6 @@ def test_vectorcall_on_init(self):
class Foo "FooObject *" "Foo_Type"
@vectorcall
Foo.__init__
iterable: object = NULL
/
"""
func = self.parse_function(block, signatures_in_block=3,
function_index=2)
Expand Down Expand Up @@ -3532,14 +3530,16 @@ class Foo "FooObject *" "Foo_Type"
self.expect_failure(block, err, lineno=2)

def test_vectorcall_without_type_object(self):
err = "@vectorcall requires the type object of 'Foo'"
# Heap types have no C pointer to name, so the type object is optional.
block = """
module m
class Foo "FooObject *" ""
@vectorcall
Foo.__init__
"""
self.expect_failure(block, err, lineno=3)
func = self.parse_function(block, signatures_in_block=3,
function_index=2)
self.assertTrue(func.vectorcall)

def test_vectorcall_unsupported_converter(self):
# str(encoding=...) has no parse_arg() implementation.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Extend Argument Clinic :ref:`vectorcall` to support :ref:`heap-types` with
zero argument construction.
29 changes: 17 additions & 12 deletions Modules/_testclinic.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ custom_converter(PyObject *obj, custom_t *val)
/* Forward declarations for vectorcall types, needed because
* clinic/_testclinic.c.h is included before the type definitions. */
static PyTypeObject VcNew_Type;
static PyTypeObject VcInit_Type;
static PyTypeObject VcNewBase_Type;
static PyTypeObject VcKwOnly_Type;
#include "clinic/_testclinic.c.h"
Expand Down Expand Up @@ -2539,7 +2538,7 @@ static PyTypeObject VcNew_Type = {
* in both the helper body and the vectorcall fast-path inner block. */

/*[clinic input]
class _testclinic.VcInit "PyObject *" "&VcInit_Type"
class _testclinic.VcInit "PyObject *" ""
@vectorcall
@critical_section
_testclinic.VcInit.__init__ as vc_posorkw_init
Expand All @@ -2550,19 +2549,24 @@ _testclinic.VcInit.__init__ as vc_posorkw_init

static int
vc_posorkw_init_impl(PyObject *self, PyObject *a, PyObject *b)
/*[clinic end generated code: output=6018424ba9fb0744 input=7a4513f78dd42b57]*/
/*[clinic end generated code: output=6018424ba9fb0744 input=f0c7a84428d5b33d]*/
{
return 0;
}

static PyTypeObject VcInit_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_testclinic.VcInit",
.tp_basicsize = sizeof(PyObject),
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = vc_posorkw_init,
.tp_vectorcall = vc_posorkw_vectorcall,
/* Heap type: the generated vectorcall can't name the type object. */
static PyType_Slot VcInit_slots[] = {
{Py_tp_new, PyType_GenericNew},
{Py_tp_init, vc_posorkw_init},
{Py_tp_vectorcall, vc_posorkw_vectorcall},
{0, NULL},
};

static PyType_Spec VcInit_spec = {
.name = "_testclinic.VcInit",
.basicsize = sizeof(PyObject),
.flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_IMMUTABLETYPE,
.slots = VcInit_slots,
};


Expand Down Expand Up @@ -2880,7 +2884,8 @@ PyInit__testclinic(void)
if (PyModule_AddType(m, &VcNew_Type) < 0) {
goto error;
}
if (PyModule_AddType(m, &VcInit_Type) < 0) {
if (PyModule_Add(m, "VcInit",
PyType_FromModuleAndSpec(m, &VcInit_spec, NULL)) < 0) {
goto error;
}
if (PyModule_AddType(m, &VcNewBase_Type) < 0) {
Expand Down
10 changes: 5 additions & 5 deletions Modules/clinic/_testclinic.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/clinic/bytearrayobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/clinic/bytesobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Objects/clinic/enumobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/clinic/floatobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Objects/clinic/tupleobject.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion Python/getargs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2921,6 +2921,7 @@ _PyArg_UnpackStack(PyObject *const *args, Py_ssize_t nargs, const char *name,
#undef _PyArg_NoKeywords
#undef _PyArg_NoKwnames
#undef _PyArg_NoPositional
#undef _PyArg_NoPositionalStack

/* For type constructors that don't take keyword args
*
Expand Down Expand Up @@ -2955,8 +2956,15 @@ _PyArg_NoPositional(const char *funcname, PyObject *args)
PyErr_BadInternalCall();
return 0;
}
if (PyTuple_GET_SIZE(args) == 0)
return _PyArg_NoPositionalStack(funcname, PyTuple_GET_SIZE(args));
}

int
_PyArg_NoPositionalStack(const char *funcname, Py_ssize_t nargs)
{
if (nargs == 0) {
return 1;
}

PyErr_Format(PyExc_TypeError, "%.200s() takes no positional arguments",
funcname);
Expand Down
1 change: 0 additions & 1 deletion Tools/c-analyzer/cpython/globals-to-fix.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,6 @@ Modules/_testclinic.c - DeprKwdInit -
Modules/_testclinic.c - DeprKwdInitNoInline -
Modules/_testclinic.c - DeprKwdNew -
Modules/_testclinic.c - TestClass -
Modules/_testclinic.c - VcInit_Type -
Modules/_testclinic.c - VcKwOnly_Type -
Modules/_testclinic.c - VcNew_Type -
Modules/_testclinic.c - VcNewBase_Type -
Expand Down
5 changes: 0 additions & 5 deletions Tools/clinic/libclinic/dsl_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,11 +648,6 @@ def normalize_function_kind(self, fullname: str) -> None:
if not self.kind.new_or_init:
fail("@vectorcall can only be used with __init__ and __new__ "
"methods currently")
# Guaranteed by the __new__ / __init__ checks above.
assert cls is not None
if not cls.type_object:
fail(f"@vectorcall requires the type object of {cls.name!r}, "
f"which was declared without one")

def resolve_return_converter(
self, full_name: str, forced_converter: str
Expand Down
Loading
Loading