simplify method_def generation (#100059)

simplify method_def generation

Summary:
This removes some duplication. This was originally done to streamline
a subsequent change, but that change turned out to be
misguided. Nevertheless, this is a nice simplification.

Test Plan:
This should change the code gen by removing some redundant
parentheses. Rely on CI.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/100059
Approved by: https://github.com/ezyang
This commit is contained in:
mikey dagitses
2023-04-26 18:46:53 +00:00
committed by PyTorch MergeBot
parent c778980fb8
commit cc628293bf

View File

@ -1011,24 +1011,20 @@ def method_def(
"""
pycname = get_pycname(name)
if name.dunder_method:
# PyMethodDef entry for binary op, throws not implemented error
pycname = f"TypeError_to_NotImplemented_<{pycname}>"
if is_noarg(overloads):
pyfunc_cast = ""
flags = "METH_NOARGS" if method else "METH_VARARGS | METH_KEYWORDS"
else:
pyfunc_cast = "castPyCFunctionWithKeywords"
pycname = f"castPyCFunctionWithKeywords({pycname})"
flags = "METH_VARARGS | METH_KEYWORDS"
if module == "torch":
flags += " | METH_STATIC"
if name.dunder_method:
# PyMethodDef entry for binary op, throws not implemented error
return f"""\
{{"{name}", {pyfunc_cast}(TypeError_to_NotImplemented_<{pycname}>), {flags}, NULL}},"""
else:
# PyMethodDef entry
return f"""\
{{"{name}", {pyfunc_cast}({pycname}), {flags}, NULL}},"""
return f'{{"{name}", {pycname}, {flags}, NULL}},'
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #