Skip to content

Commit fae9ef6

Browse files
corona10aisk
authored andcommitted
pythongh-112205: Support @Getter annotation from AC (pythongh-112396)
1 parent 916c247 commit fae9ef6

File tree

5 files changed

+169
-68
lines changed

5 files changed

+169
-68
lines changed

Lib/test/clinic.test.c

+21
Original file line numberDiff line numberDiff line change
@@ -4951,6 +4951,27 @@ static PyObject *
49514951
Test_meth_coexist_impl(TestObj *self)
49524952
/*[clinic end generated code: output=808a293d0cd27439 input=2a1d75b5e6fec6dd]*/
49534953

4954+
/*[clinic input]
4955+
@getter
4956+
Test.property
4957+
[clinic start generated code]*/
4958+
4959+
#define TEST_PROPERTY_GETTERDEF \
4960+
{"property", (getter)Test_property_get, NULL, NULL},
4961+
4962+
static PyObject *
4963+
Test_property_get_impl(TestObj *self);
4964+
4965+
static PyObject *
4966+
Test_property_get(TestObj *self, void *Py_UNUSED(context))
4967+
{
4968+
return Test_property_get_impl(self);
4969+
}
4970+
4971+
static PyObject *
4972+
Test_property_get_impl(TestObj *self)
4973+
/*[clinic end generated code: output=892b6fb351ff85fd input=2d92b3449fbc7d2b]*/
4974+
49544975

49554976
/*[clinic input]
49564977
output push

Lib/test/test_clinic.py

+17-9
Original file line numberDiff line numberDiff line change
@@ -638,7 +638,7 @@ class C "void *" ""
638638
C.__init__ = C.meth
639639
[clinic start generated code]*/
640640
"""
641-
err = "'__init__' must be a normal method, not a class or static method"
641+
err = "'__init__' must be a normal method; got 'FunctionKind.CLASS_METHOD'!"
642642
self.expect_failure(block, err, lineno=8)
643643

644644
def test_validate_cloned_new(self):
@@ -2180,14 +2180,22 @@ class Foo "" ""
21802180
self.expect_failure(block, err, lineno=2)
21812181

21822182
def test_init_must_be_a_normal_method(self):
2183-
err = "'__init__' must be a normal method, not a class or static method!"
2184-
block = """
2185-
module foo
2186-
class Foo "" ""
2187-
@classmethod
2188-
Foo.__init__
2189-
"""
2190-
self.expect_failure(block, err, lineno=3)
2183+
err_template = "'__init__' must be a normal method; got 'FunctionKind.{}'!"
2184+
annotations = {
2185+
"@classmethod": "CLASS_METHOD",
2186+
"@staticmethod": "STATIC_METHOD",
2187+
"@getter": "GETTER",
2188+
}
2189+
for annotation, invalid_kind in annotations.items():
2190+
with self.subTest(annotation=annotation, invalid_kind=invalid_kind):
2191+
block = f"""
2192+
module foo
2193+
class Foo "" ""
2194+
{annotation}
2195+
Foo.__init__
2196+
"""
2197+
expected_error = err_template.format(invalid_kind)
2198+
self.expect_failure(block, expected_error, lineno=3)
21912199

21922200
def test_duplicate_coexist(self):
21932201
err = "Called @coexist twice"

Modules/_io/bufferedio.c

+33-48
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include "Python.h"
1111
#include "pycore_bytesobject.h" // _PyBytes_Join()
1212
#include "pycore_call.h" // _PyObject_CallNoArgs()
13-
#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION()
1413
#include "pycore_object.h" // _PyObject_GC_UNTRACK()
1514
#include "pycore_pyerrors.h" // _Py_FatalErrorFormat()
1615
#include "pycore_pylifecycle.h" // _Py_IsInterpreterFinalizing()
@@ -518,25 +517,20 @@ buffered_closed(buffered *self)
518517
return closed;
519518
}
520519

520+
/*[clinic input]
521+
@critical_section
522+
@getter
523+
_io._Buffered.closed
524+
[clinic start generated code]*/
525+
521526
static PyObject *
522-
buffered_closed_get_impl(buffered *self, void *context)
527+
_io__Buffered_closed_get_impl(buffered *self)
528+
/*[clinic end generated code: output=f08ce57290703a1a input=18eddefdfe4a3d2f]*/
523529
{
524530
CHECK_INITIALIZED(self)
525531
return PyObject_GetAttr(self->raw, &_Py_ID(closed));
526532
}
527533

528-
static PyObject *
529-
buffered_closed_get(buffered *self, void *context)
530-
{
531-
PyObject *return_value = NULL;
532-
533-
Py_BEGIN_CRITICAL_SECTION(self);
534-
return_value = buffered_closed_get_impl(self, context);
535-
Py_END_CRITICAL_SECTION();
536-
537-
return return_value;
538-
}
539-
540534
/*[clinic input]
541535
@critical_section
542536
_io._Buffered.close
@@ -662,44 +656,35 @@ _io__Buffered_writable_impl(buffered *self)
662656
return PyObject_CallMethodNoArgs(self->raw, &_Py_ID(writable));
663657
}
664658

659+
660+
/*[clinic input]
661+
@critical_section
662+
@getter
663+
_io._Buffered.name
664+
[clinic start generated code]*/
665+
665666
static PyObject *
666-
buffered_name_get_impl(buffered *self, void *context)
667+
_io__Buffered_name_get_impl(buffered *self)
668+
/*[clinic end generated code: output=d2adf384051d3d10 input=6b84a0e6126f545e]*/
667669
{
668670
CHECK_INITIALIZED(self)
669671
return PyObject_GetAttr(self->raw, &_Py_ID(name));
670672
}
671673

672-
static PyObject *
673-
buffered_name_get(buffered *self, void *context)
674-
{
675-
PyObject *return_value = NULL;
676-
677-
Py_BEGIN_CRITICAL_SECTION(self);
678-
return_value = buffered_name_get_impl(self, context);
679-
Py_END_CRITICAL_SECTION();
680-
681-
return return_value;
682-
}
674+
/*[clinic input]
675+
@critical_section
676+
@getter
677+
_io._Buffered.mode
678+
[clinic start generated code]*/
683679

684680
static PyObject *
685-
buffered_mode_get_impl(buffered *self, void *context)
681+
_io__Buffered_mode_get_impl(buffered *self)
682+
/*[clinic end generated code: output=0feb205748892fa4 input=0762d5e28542fd8c]*/
686683
{
687684
CHECK_INITIALIZED(self)
688685
return PyObject_GetAttr(self->raw, &_Py_ID(mode));
689686
}
690687

691-
static PyObject *
692-
buffered_mode_get(buffered *self, void *context)
693-
{
694-
PyObject *return_value = NULL;
695-
696-
Py_BEGIN_CRITICAL_SECTION(self);
697-
return_value = buffered_mode_get_impl(self, context);
698-
Py_END_CRITICAL_SECTION();
699-
700-
return return_value;
701-
}
702-
703688
/* Lower-level APIs */
704689

705690
/*[clinic input]
@@ -2541,9 +2526,9 @@ static PyMemberDef bufferedreader_members[] = {
25412526
};
25422527

25432528
static PyGetSetDef bufferedreader_getset[] = {
2544-
{"closed", (getter)buffered_closed_get, NULL, NULL},
2545-
{"name", (getter)buffered_name_get, NULL, NULL},
2546-
{"mode", (getter)buffered_mode_get, NULL, NULL},
2529+
_IO__BUFFERED_CLOSED_GETTERDEF
2530+
_IO__BUFFERED_NAME_GETTERDEF
2531+
_IO__BUFFERED_MODE_GETTERDEF
25472532
{NULL}
25482533
};
25492534

@@ -2601,9 +2586,9 @@ static PyMemberDef bufferedwriter_members[] = {
26012586
};
26022587

26032588
static PyGetSetDef bufferedwriter_getset[] = {
2604-
{"closed", (getter)buffered_closed_get, NULL, NULL},
2605-
{"name", (getter)buffered_name_get, NULL, NULL},
2606-
{"mode", (getter)buffered_mode_get, NULL, NULL},
2589+
_IO__BUFFERED_CLOSED_GETTERDEF
2590+
_IO__BUFFERED_NAME_GETTERDEF
2591+
_IO__BUFFERED_MODE_GETTERDEF
26072592
{NULL}
26082593
};
26092594

@@ -2719,9 +2704,9 @@ static PyMemberDef bufferedrandom_members[] = {
27192704
};
27202705

27212706
static PyGetSetDef bufferedrandom_getset[] = {
2722-
{"closed", (getter)buffered_closed_get, NULL, NULL},
2723-
{"name", (getter)buffered_name_get, NULL, NULL},
2724-
{"mode", (getter)buffered_mode_get, NULL, NULL},
2707+
_IO__BUFFERED_CLOSED_GETTERDEF
2708+
_IO__BUFFERED_NAME_GETTERDEF
2709+
_IO__BUFFERED_MODE_GETTERDEF
27252710
{NULL}
27262711
};
27272712

Modules/_io/clinic/bufferedio.c.h

+55-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)