Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-112205: Support @setter annotation from AC #112922

Merged
merged 18 commits into from
Dec 13, 2023
Merged
34 changes: 31 additions & 3 deletions Lib/test/clinic.test.c
Original file line number Diff line number Diff line change
Expand Up @@ -4956,8 +4956,12 @@ Test_meth_coexist_impl(TestObj *self)
Test.property
[clinic start generated code]*/

#define TEST_PROPERTY_GETTERDEF \
{"property", (getter)Test_property_get, NULL, NULL},
#if defined(TEST_PROPERTY_GETSETDEF)
# undef TEST_PROPERTY_GETSETTDEF
# define TEST_PROPERTY_GETSETDEF {"property", (getter)Test_property_get, (setter)Test_property_set, NULL},
#else
# define TEST_PROPERTY_GETSETDEF {"property", (getter)Test_property_get, NULL, NULL},
#endif

static PyObject *
Test_property_get_impl(TestObj *self);
Expand All @@ -4970,8 +4974,32 @@ Test_property_get(TestObj *self, void *Py_UNUSED(context))

static PyObject *
Test_property_get_impl(TestObj *self)
/*[clinic end generated code: output=892b6fb351ff85fd input=2d92b3449fbc7d2b]*/
/*[clinic end generated code: output=c7f18e7848b00cfa input=2d92b3449fbc7d2b]*/

/*[clinic input]
@setter
Test.property
[clinic start generated code]*/

#if defined(TEST_PROPERTY_GETSETDEF)
# undef TEST_PROPERTY_GETSETDEF
# define TEST_PROPERTY_GETSETDEF {"property", (getter)Test_property_get, (setter)Test_property_set, NULL},
#else
# define TEST_PROPERTY_GETSETTERDEF {"property", NULL, (setter)Test_property_set, NULL},
#endif

static int
Test_property_set_impl(TestObj *self, PyObject *value);

static int
Test_property_set(TestObj *self, PyObject *value, void *Py_UNUSED(context))
{
return Test_property_set_impl(self, value);
}

static int
Test_property_set_impl(TestObj *self, PyObject *value)
/*[clinic end generated code: output=63e670882002b596 input=3bc3f46a23c83a88]*/

/*[clinic input]
output push
Expand Down
24 changes: 24 additions & 0 deletions Lib/test/test_clinic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2197,6 +2197,30 @@ class Foo "" ""
expected_error = err_template.format(invalid_kind)
self.expect_failure(block, expected_error, lineno=3)

def test_invalid_getset(self):
annotations = ["@getter", "@setter"]
for annotation in annotations:
with self.subTest(annotation=annotation):
block = f"""
module foo
class Foo "" ""
{annotation}
Foo.property -> int
"""
expected_error = "neither @getter nor @setter can define return type"
self.expect_failure(block, expected_error, lineno=3)

block = f"""
module foo
class Foo "" ""
{annotation}
Foo.property
obj: int
/
"""
expected_error = "neither @getter nor @setter can define parameters"
self.expect_failure(block, expected_error, lineno=0)

def test_duplicate_coexist(self):
err = "Called @coexist twice"
block = """
Expand Down
18 changes: 9 additions & 9 deletions Modules/_io/bufferedio.c
Original file line number Diff line number Diff line change
Expand Up @@ -2526,9 +2526,9 @@ static PyMemberDef bufferedreader_members[] = {
};

static PyGetSetDef bufferedreader_getset[] = {
_IO__BUFFERED_CLOSED_GETTERDEF
_IO__BUFFERED_NAME_GETTERDEF
_IO__BUFFERED_MODE_GETTERDEF
_IO__BUFFERED_CLOSED_GETSETDEF
_IO__BUFFERED_NAME_GETSETDEF
_IO__BUFFERED_MODE_GETSETDEF
{NULL}
};

Expand Down Expand Up @@ -2586,9 +2586,9 @@ static PyMemberDef bufferedwriter_members[] = {
};

static PyGetSetDef bufferedwriter_getset[] = {
_IO__BUFFERED_CLOSED_GETTERDEF
_IO__BUFFERED_NAME_GETTERDEF
_IO__BUFFERED_MODE_GETTERDEF
_IO__BUFFERED_CLOSED_GETSETDEF
_IO__BUFFERED_NAME_GETSETDEF
_IO__BUFFERED_MODE_GETSETDEF
{NULL}
};

Expand Down Expand Up @@ -2704,9 +2704,9 @@ static PyMemberDef bufferedrandom_members[] = {
};

static PyGetSetDef bufferedrandom_getset[] = {
_IO__BUFFERED_CLOSED_GETTERDEF
_IO__BUFFERED_NAME_GETTERDEF
_IO__BUFFERED_MODE_GETTERDEF
_IO__BUFFERED_CLOSED_GETSETDEF
_IO__BUFFERED_NAME_GETSETDEF
_IO__BUFFERED_MODE_GETSETDEF
{NULL}
};

Expand Down
26 changes: 19 additions & 7 deletions Modules/_io/clinic/bufferedio.c.h

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

26 changes: 19 additions & 7 deletions Modules/_io/clinic/stringio.c.h

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

46 changes: 45 additions & 1 deletion Modules/_io/clinic/textio.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 Modules/_io/stringio.c
Original file line number Diff line number Diff line change
Expand Up @@ -1037,15 +1037,15 @@ static struct PyMethodDef stringio_methods[] = {
};

static PyGetSetDef stringio_getset[] = {
_IO_STRINGIO_CLOSED_GETTERDEF
_IO_STRINGIO_NEWLINES_GETTERDEF
_IO_STRINGIO_CLOSED_GETSETDEF
_IO_STRINGIO_NEWLINES_GETSETDEF
/* (following comments straight off of the original Python wrapper:)
XXX Cruft to support the TextIOWrapper API. This would only
be meaningful if StringIO supported the buffer attribute.
Hopefully, a better solution, than adding these pseudo-attributes,
will be found.
*/
_IO_STRINGIO_LINE_BUFFERING_GETTERDEF
_IO_STRINGIO_LINE_BUFFERING_GETSETDEF
{NULL}
};

Expand Down
43 changes: 18 additions & 25 deletions Modules/_io/textio.c
Original file line number Diff line number Diff line change
Expand Up @@ -3238,33 +3238,37 @@ textiowrapper_errors_get(textio *self, void *context)
return result;
}

/*[clinic input]
@critical_section
@getter
_io.TextIOWrapper._CHUNK_SIZE
[clinic start generated code]*/

static PyObject *
textiowrapper_chunk_size_get_impl(textio *self, void *context)
_io_TextIOWrapper__CHUNK_SIZE_get_impl(textio *self)
/*[clinic end generated code: output=039925cd2df375bc input=e9715b0e06ff0fa6]*/
{
CHECK_ATTACHED(self);
return PyLong_FromSsize_t(self->chunk_size);
}

static PyObject *
textiowrapper_chunk_size_get(textio *self, void *context)
{
PyObject *result = NULL;
Py_BEGIN_CRITICAL_SECTION(self);
result = textiowrapper_chunk_size_get_impl(self, context);
Py_END_CRITICAL_SECTION();
return result;
}
/*[clinic input]
@critical_section
@setter
_io.TextIOWrapper._CHUNK_SIZE
[clinic start generated code]*/

static int
textiowrapper_chunk_size_set_impl(textio *self, PyObject *arg, void *context)
_io_TextIOWrapper__CHUNK_SIZE_set_impl(textio *self, PyObject *value)
/*[clinic end generated code: output=edb86d2db660a5ab input=32fc99861db02a0a]*/
{
Py_ssize_t n;
CHECK_ATTACHED_INT(self);
if (arg == NULL) {
if (value == NULL) {
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
return -1;
}
n = PyNumber_AsSsize_t(arg, PyExc_ValueError);
n = PyNumber_AsSsize_t(value, PyExc_ValueError);
if (n == -1 && PyErr_Occurred())
return -1;
if (n <= 0) {
Expand All @@ -3276,16 +3280,6 @@ textiowrapper_chunk_size_set_impl(textio *self, PyObject *arg, void *context)
return 0;
}

static int
textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
{
int result = 0;
Py_BEGIN_CRITICAL_SECTION(self);
result = textiowrapper_chunk_size_set_impl(self, arg, context);
Py_END_CRITICAL_SECTION();
return result;
}

static PyMethodDef incrementalnewlinedecoder_methods[] = {
_IO_INCREMENTALNEWLINEDECODER_DECODE_METHODDEF
_IO_INCREMENTALNEWLINEDECODER_GETSTATE_METHODDEF
Expand Down Expand Up @@ -3361,8 +3355,7 @@ static PyGetSetDef textiowrapper_getset[] = {
*/
{"newlines", (getter)textiowrapper_newlines_get, NULL, NULL},
{"errors", (getter)textiowrapper_errors_get, NULL, NULL},
{"_CHUNK_SIZE", (getter)textiowrapper_chunk_size_get,
(setter)textiowrapper_chunk_size_set, NULL},
_IO_TEXTIOWRAPPER__CHUNK_SIZE_GETSETDEF
{NULL}
};

Expand Down
Loading
Loading