Skip to content

Commit

Permalink
pythongh-108494: Argument Clinic: inline parsing code for positional-…
Browse files Browse the repository at this point in the history
…only parameters in the limited C API (pythonGH-108622)
  • Loading branch information
serhiy-storchaka authored Sep 3, 2023
1 parent 5584609 commit 1796c19
Show file tree
Hide file tree
Showing 13 changed files with 555 additions and 307 deletions.
6 changes: 1 addition & 5 deletions Modules/_io/clinic/bufferedio.c.h

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

3 changes: 1 addition & 2 deletions Modules/_io/clinic/bytesio.c.h

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

3 changes: 1 addition & 2 deletions Modules/_io/clinic/fileio.c.h

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

3 changes: 1 addition & 2 deletions Modules/_io/clinic/winconsoleio.c.h

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

9 changes: 5 additions & 4 deletions Modules/_multiprocessing/multiprocessing.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,17 @@ class HANDLE_converter(CConverter):
type = "HANDLE"
format_unit = '"F_HANDLE"'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsVoidPtr({argname});
if (!{paramname} && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=3e537d244034affb]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=3cf0318efc6a8772]*/

/*[clinic input]
module _multiprocessing
Expand Down
9 changes: 5 additions & 4 deletions Modules/_posixsubprocess.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,15 +87,16 @@ class pid_t_converter(CConverter):
type = 'pid_t'
format_unit = '" _Py_PARSE_PID "'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsPid({argname});
if ({paramname} == -1 && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=5af1c116d56cbb5a]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=c94349aa1aad151d]*/

#include "clinic/_posixsubprocess.c.h"

Expand Down
12 changes: 7 additions & 5 deletions Modules/_struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,18 +110,20 @@ class cache_struct_converter(CConverter):
c_default = "NULL"
broken_limited_capi = True
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
assert not limited_capi
return self.format_code("""
if (!{converter}(module, {argname}, &{paramname})) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.name,
converter=self.converter)
""",
argname=argname,
converter=self.converter)
def cleanup(self):
return "Py_XDECREF(%s);\n" % self.name
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=14e83804f599ed8f]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=c33b27d6b06006c6]*/

static int cache_struct_converter(PyObject *, PyObject *, PyStructObject **);

Expand Down
22 changes: 16 additions & 6 deletions Modules/clinic/_testclinic_limited.c.h

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

16 changes: 9 additions & 7 deletions Modules/overlapped.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@
class pointer_converter(CConverter):
format_unit = '"F_POINTER"'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsVoidPtr({argname});
if (!{paramname} && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
class OVERLAPPED_converter(pointer_converter):
type = 'OVERLAPPED *'
Expand All @@ -55,21 +56,22 @@ class HANDLE_converter(pointer_converter):
class ULONG_PTR_converter(pointer_converter):
type = 'ULONG_PTR'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = (uintptr_t)PyLong_AsVoidPtr({argname});
if (!{paramname} && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
class DWORD_converter(unsigned_long_converter):
type = 'DWORD'
class BOOL_converter(int_converter):
type = 'BOOL'
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=8a07ea3018f4cec8]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=436f4440630a304c]*/

/*[clinic input]
module _overlapped
Expand Down
9 changes: 5 additions & 4 deletions Modules/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ class pid_t_converter(CConverter):
type = 'pid_t'
format_unit = '" _Py_PARSE_PID "'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsPid({argname});
if ({paramname} == -1 && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=5af1c116d56cbb5a]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=c94349aa1aad151d]*/

#include "clinic/resource.c.h"

Expand Down
9 changes: 5 additions & 4 deletions PC/msvcrtmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ class HANDLE_converter(CConverter):
type = 'void *'
format_unit = '"_Py_PARSE_UINTPTR"'
def parse_arg(self, argname, displayname):
return """
def parse_arg(self, argname, displayname, *, limited_capi):
return self.format_code("""
{paramname} = PyLong_AsVoidPtr({argname});
if (!{paramname} && PyErr_Occurred()) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name)
""",
argname=argname)
class HANDLE_return_converter(CReturnConverter):
type = 'void *'
Expand Down Expand Up @@ -74,7 +75,7 @@ class wchar_t_return_converter(CReturnConverter):
data.return_conversion.append(
'return_value = PyUnicode_FromOrdinal(_return_value);\n')
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=1e8e9fa3538ec08f]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=ff031be44ab3250d]*/

/*[clinic input]
module msvcrt
Expand Down
18 changes: 10 additions & 8 deletions PC/winreg.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,15 @@ class HKEY_converter(CConverter):
converter = 'clinic_HKEY_converter'
broken_limited_capi = True
def parse_arg(self, argname, displayname):
return """
if (!{converter}(_PyModule_GetState(module), {argname}, &{paramname})) {{{{
goto exit;
}}}}
""".format(argname=argname, paramname=self.parser_name,
converter=self.converter)
def parse_arg(self, argname, displayname, *, limited_capi):
assert not limited_capi
return self.format_code("""
if (!{converter}(_PyModule_GetState(module), {argname}, &{paramname})) {{{{
goto exit;
}}}}
""",
argname=argname,
converter=self.converter)
class HKEY_return_converter(CReturnConverter):
type = 'HKEY'
Expand All @@ -250,7 +252,7 @@ class self_return_converter(CReturnConverter):
data.return_conversion.append(
'return_value = (PyObject *)_return_value;\n')
[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=f8cb7034338aeaba]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=4979f33998ffb6f8]*/

#include "clinic/winreg.c.h"

Expand Down
Loading

0 comments on commit 1796c19

Please sign in to comment.