10000 gh-116646: Add limited C API support to AC fildes converter (#116769) · python/cpython@d402872 · GitHub
[go: up one dir, main page]

Skip to content

Commit d402872

Browse files
authored
gh-116646: Add limited C API support to AC fildes converter (#116769)
Add tests on the "fildes" converter to _testclinic_limited.
1 parent a18c985 commit d402872

File tree

4 files changed

+98
-13
lines changed

4 files changed

+98
-13
lines changed

Lib/test/test_clinic.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3698,6 +3698,39 @@ def test_my_double_sum(self):
36983698
with self.assertRaises(TypeError):
36993699
func(1., "2")
37003700

3701+
def test_get_file_descriptor(self):
3702+
# test 'file descriptor' converter: call PyObject_AsFileDescriptor()
3703+
get_fd = _testclinic_limited.get_file_descriptor
3704+
3705+
class MyInt(int):
3706+
pass
3707+
3708+
class MyFile:
3709+
def __init__(self, fd):
3710+
self._fd = fd
3711+
def fileno(self):
3712+
return self._fd
3713+
3714+
for fd in (0, 1, 2, 5, 123_456):
3715+
self.assertEqual(get_fd(fd), fd)
3716+
3717+
myint = MyInt(fd)
3718+
self.assertEqual(get_fd(myint), fd)
3719+
3720+
myfile = MyFile(fd)
3721+
self.assertEqual(get_fd(myfile), fd)
3722+
3723+
with self.assertRaises(OverflowError):
3724+
get_fd(2**256)
3725+
with self.assertWarnsRegex(RuntimeWarning,
3726+
"bool is used as a file descriptor"):
3727+
get_fd(True)
3728+
with self.assertRaises(TypeError):
3729+
get_fd(1.0)
3730+
with self.assertRaises(TypeError):
3731+
get_fd("abc")
3732+
with self.assertRaises(TypeError):
3733+
get_fd(None)
37013734

37023735

37033736
class PermutationTests(unittest.TestCase):

Modules/_testclinic_limited.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,30 @@ my_double_sum_impl(PyObject *module, double x, double y)
105105
}
106106

107107

108+
/*[clinic input]
109+
get_file_descriptor -> int
110+
111+
file as fd: fildes
112+
/
113+
114+
Get a file descriptor.
115+
[clinic start generated code]*/
116+
117+
static int
118+
get_file_descriptor_impl(PyObject *module, int fd)
119+
/*[clinic end generated code: output=80051ebad54db8a8 input=82e2a1418848cd5b]*/
120+
{
121+
return fd;
122+
}
123+
124+
108125
static PyMethodDef tester_methods[] = {
109126
TEST_EMPTY_FUNCTION_METHODDEF
110127
MY_INT_FUNC_METHODDEF
111128
MY_INT_SUM_METHODDEF
112129
MY_FLOAT_SUM_METHODDEF
113130
MY_DOUBLE_SUM_METHODDEF
131+
GET_FILE_DESCRIPTOR_METHODDEF
114132
{NULL, NULL}
115133
};
116134

Modules/clinic/_testclinic_limited.c.h

Lines changed: 34 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Tools/clinic/clinic.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3800,18 +3800,19 @@ class fildes_converter(CConverter):
38003800
type = 'int'
38013801
converter = '_PyLong_FileDescriptor_Converter'
38023802

3803-
def converter_init(self, *, accept: TypeSet = {int, NoneType}) -> None:
3804-
self.add_include('pycore_fileutils.h',
3805-
'_PyLong_FileDescriptor_Converter()')
3806-
3807-
def _parse_arg(self, argname: str, displayname: str) -> str | None:
3808-
return self.format_code("""
3809-
{paramname} = PyObject_AsFileDescriptor({argname});
3810-
if ({paramname} == -1) {{{{
3811-
goto exit;
3812-
}}}}
3813-
""",
3814-
argname=argname)
3803+
def parse_arg(self, argname: str, displayname: str, *, limited_capi: bool) -> str | None:
3804+
if limited_capi:
3805+
return self.format_code("""
3806+
{paramname} = PyObject_AsFileDescriptor({argname});
3807+
if ({paramname} < 0) {{{{
3808+
goto exit;
3809+
}}}}
3810+
""",
3811+
argname=argname)
3812+
else:
3813+
self.add_include('pycore_fileutils.h',
3814+
'_PyLong_FileDescriptor_Converter()')
3815+
return super().parse_arg(argname, displayname, limited_capi=limited_capi)
38153816

38163817

38173818
class float_converter(CConverter):

0 commit comments

Comments
 (0)
0