-
-
Notifications
You must be signed in to change notification settings - Fork 11.8k
MAINT: refactor scalartypes.c.src to pure C++
#30361
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
- DEF_PYARRTYPE - DEF_BINOP
- DEF_BIT_COUNT - DEF_UN_OP
|
I think the helpful first step may to be convert the file to C++ to continue in steps from there. These custom macros seem a step in the wrong direction, since if we are to replace custom templating with C++ templating, we should use that. |
|
I'm going to do that in another PR because apparently is not as trivial as I thought. converting from edit: I pushed the changes to review, and because this will have a dependency on it. Just confirm me if you want this in separate PR for easy review and to avoid noise on this edit 2: well apparently for the migration to C++ in windows it requires C++ 20 :/. do you have a suggestion? I think it is not suitable to do it without designated initializers
edit 3: learning stuff: struct factories at top level and compile time. introduce edit 4: removed all designated initializers just trying to make it compile in MSVC so that I can mark as "completed" the migration to C++. |
> warning: ISO C++11 does not allow conversion from string literal to 'char *' in python >=3.13 they changed PyArg_ParseTupleAndKeywords to have char *const* in C++, instead of char** ref: https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTupleAndKeywords TODO: remove these (char **) castings when we drop support for 3.12
567fb25 to
69fa910
Compare
this should be reverted when moving to c++20 in MSVC
d4daaff to
b09125a
Compare
|
nicee. finally C++ compiles jaja! Just confirm me if you want the migration to C++ in a separate PR for easy review and to avoid noise on the removal of edit 5: cannot make factories for array of methods because a) trying to use static variables inside a constexpr is not allowed and b) returning a stack address associated with a local variable is not something we want. a) because of that, I will just repeat the definition of those edit 6: cannot use edit 7: cannot use |
I think they are allowed to be written as long as they are in the exactly correct order? I am not sure how many holes they have, this is one annoyance with C++. If there are few we might also just fill them in explicitly, otherwise, I guess this tricky is fine. Maybe it makes sense to keep going here for now and then see if we want to split out just the initial conversion for example. (A bit conflicting, I would prefer to split it out, but if we don't follow through with the full thing there is no point.) For what it's worth, I think the changes you did beyond the main change are not far reaching enough to be nice, we need to go all-in on templates for things to make sense. (Almost) anything repeated should be a template instead, the bufferprocs are a good example, that should just be something like EDIT: CC @athurdekoos for awareness (and maybe if you want to even review). |
you want instead of ? |
|
No, |
|
// that's why I do
static auto unicode_arrtype_as_buffer = generic_pybufferprocs<unicode_getbuffer>; // << top level
PyUnicodeArrType_Type.tp_as_buffer = &unicode_arrtype_as_buffer;
// instead of
PyUnicodeArrType_Type.tp_as_buffer = &make_pybufferprocs(unicode_getbuffer);and doing something like PyUnicodeArrType_Type.tp_as_buffer = &arrtype_as_buffer<unicode_getbuffer>I wasn't able to do it yet. Can you give an example for that? |
by adding dependent_false_v. see issue CWG2518 [1] [2] [3] [1]: https://cplusplus.github.io/CWG/issues/2518.html [2]: https://reviews.llvm.org/D144285 [3]: https://en.cppreference.com/w/cpp/language/if.html#Constexpr_If
I cherry picked the changes to be mostly if-else chains and function definitions
|
I think the PR can be taken a first look. Remarking the following points
ccing the top 3 commiters of |
| PyMem_Free(ip); | ||
| if (new_ == NULL) { | ||
| return NULL; | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| PyMem_Free(ip); | |
| if (new_ == NULL) { | |
| return NULL; | |
| } | |
| if (new_ == NULL) { | |
| PyMem_Free(ip); | |
| return NULL; | |
| } |
scalartypes.c.src to avoid using custom metaprogrammingscalartypes.c.src to pure C++
|
ping @athurdekoos as well |
ngoldbaum
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I skimmed over most of the changes in scalartypes.cpp and left a few comments. No careful review here.
| npy_type scalar = PyArrayScalar_VAL(self, ScalarName); \ | ||
| uint8_t count = npy_popcount_func(scalar); \ | ||
| return ConvertFrom(count); \ | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can't this be a template function?
| gentype_squeeze(PyObject *self, PyObject *args, PyObject *kwds) | ||
| { | ||
| return gentype_generic_method(self, args, kwds, "squeeze"); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given that it's trivial it's probably better to make this a macro though, it'll be a lot more concise.
| (PyCFunction)npy_int_bit_count, | ||
| METH_NOARGS, NULL}, | ||
| {NULL, NULL, 0, NULL} /* sentinel */ | ||
| }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like that these are all explicitly defined in the source now, it's OK that it's more verbose.
| else if constexpr (std::is_same_v<PT, PyCDoubleScalarObject>) { | ||
| return "Zd"; | ||
| } | ||
| else if constexpr (std::is_same_v<PT, PyCLongDoubleScalarObject>) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
general question about code like this from a non-expert in C++, code like this using constexpr is evaluated at compile-time, right?
Maybe check out what |
remove begin repeat and use C macros. work towards #29528
for now I'm just refactoring to use macros and whenever the c++ templates are needed I will change to it. moving to C macros still has it's advantages cause I'm aiming for grepability and thus LSP advantages:
Py@NAME@ArrType_TypevsPyNumberArrType_Typethe objective is to have a
scalartypes.corscalartypes.cppedit: the objective now is
scalartypes.cpp