parse PyCFunctions like pybind11 functions#256
Open
AaronOpfer wants to merge 1 commit intopybind:mainfrom
Open
parse PyCFunctions like pybind11 functions#256AaronOpfer wants to merge 1 commit intopybind:mainfrom
AaronOpfer wants to merge 1 commit intopybind:mainfrom
Conversation
PyCFunctions are surprisingly compatible with `inspect.getfullargspec` et. al.,
through a mechanism called `__text_signature__`. This means that
`pybind11-stubgen` will not activate its docstring-parsing fallback. This is
problematic for `pybind11-stubgen` however, because the `__text_signature__`
does not have any mechanism for including type hints or annotations.
This is relevant to `pybind11-stubgen` as I have been porting some C API
functions away from `pybind11` to CPython and hit a snag trying to do it
piecemeal, as `pybind11-stubgen` won't generate stubs for functions created in
the CPython way.
A test case could be as simple as:
```cpp
namespace {
static PyObject* python_time_ns(PyObject* /*self*/, PyObject* /*args*/) {
return PyLong_FromUnsignedLongLong(0); // obviously just for the purpose of testing
}
static PyMethodDef memberdef_time_ns{.ml_name = "time_ns",
.ml_meth = python_time_ns,
.ml_flags = METH_NOARGS,
.ml_doc = "time_ns() -> int\n"};
} // namespace
PyObject* create_time_ns_func(PyObject* modname) {
return PyCFunction_NewEx(&memberdef_time_ns, NULL, modname);
}
```
And then installing this onto a pybind11 module:
```cpp
m.attr("time_ns") = create_time_ns_func(m.attr("__name__").ptr());
```
Author
|
It was not clear to me how to run/augment the tests so I'm not sure if this PR breaks anything yet. I am also open to other suggestions or consideration of whether this change is appropriate since it's pretty clearly not pybind11-related. |
| func_name = Identifier(path[-1]) | ||
|
|
||
| try: | ||
| if type(func) is types.BuiltinFunctionType: |
Contributor
There was a problem hiding this comment.
Is there is reason why you are not using isinstance
Author
There was a problem hiding this comment.
Hmm, good question. it looks like using isinstance() would actually cover a few additional types, such as types.BuiltinMethodType, which is probably a good thing.
Contributor
|
Adding your tests to the test suite would be helpful |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PyCFunctions are surprisingly compatible with
inspect.getfullargspecet. al., through a mechanism called__text_signature__. This means thatpybind11-stubgenwill not activate its docstring-parsing fallback. This is problematic forpybind11-stubgenhowever, because the__text_signature__does not have any mechanism for including type hints or annotations.This is relevant to
pybind11-stubgenas I have been porting some C API functions away frompybind11to CPython and hit a snag trying to do it piecemeal, aspybind11-stubgenwon't generate stubs for functions created in the CPython way.A test case could be as simple as:
And then installing this onto a pybind11 module: