Skip to content

__array_ufunc__ *args and 'out' always a tuple - #6

Merged
charris merged 2 commits into
charris:enable-numpy_ufuncfrom
mhvk:array_ufunc_star_args
Mar 23, 2017
Merged

charris merged 2 commits into
charris:enable-numpy_ufuncfrom
mhvk:array_ufunc_star_args

Conversation

@mhvk

@mhvk mhvk commented Mar 14, 2017

Copy link
Copy Markdown

(comments in main thread)

@mhvk
mhvk force-pushed the array_ufunc_star_args branch 2 times, most recently from eb07f7c to 84fed38 Compare March 15, 2017 02:37
Comment thread doc/source/reference/arrays.classes.rst Outdated

@charris charris Mar 16, 2017 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All ``out`` arguments, both positional and keyword, are passed as a :obj:`tuple` in *kwargs*.

Comment thread doc/source/reference/arrays.classes.rst Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and which may eventually be deprecated?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Furthermore, unless it is set to `None`,

Comment thread doc/source/reference/arrays.classes.rst Outdated

@charris charris Mar 16, 2017 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove ( and if your class....

Does this imply the subclasses cannot opt out? @

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With this ufuncs will treat your class..

@charris charris Mar 16, 2017 •

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I think it needs to be stated that for the python numeric ops this is equivalent to ndarray returning NotImplemented.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good comments; I'll implement them later.

For now, in answer to your question, I think the text is not quite right: for a proper subclass of ndarray, the right operators will be given preference by CPython already.

Comment thread doc/source/reference/arrays.classes.rst Outdated

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is correct. It is the calling class, ndarray and subclasses thereof, for which it is equivalent to calling NotImplemented. The effect is that unary ops fail and binary ops get passed to the corresponding right op. Or am I missing something?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll try to separate more clearly what happens for binary operators and inside ufuncs (I've gotten myself confused right now...).
p.s. Will do this over the weekend -- some horrible money proposal is taking all my time right now.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem. We all need to earn a living...

@charris
charris force-pushed the enable-numpy_ufunc branch from a3130f1 to 648e711 Compare March 17, 2017 16:15
@mhvk
mhvk force-pushed the array_ufunc_star_args branch from 84fed38 to 4bcb03e Compare March 17, 2017 21:43
@mhvk

mhvk commented Mar 17, 2017

Copy link
Copy Markdown
Author

@charris - rebased, and now with updated documentation (which I think may correspond to reality...)

@mhvk
mhvk force-pushed the array_ufunc_star_args branch 3 times, most recently from 16d89ca to c5fb325 Compare March 21, 2017 18:53
@mhvk
mhvk force-pushed the array_ufunc_star_args branch from c5fb325 to c3d20fe Compare March 21, 2017 18:57
Any class (ndarray subclass or not) can define this method to
override behavior of NumPy's ufuncs. This works quite similarly to
Python's ``__mul__`` and other binary operation routines.
Any class (ndarray subclass or not) can define this method (or set it to

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commas, not "(ndarray....)". I'd also remove the parenthesis around "or set...".

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll do the correction.

that you avoid confusion by neither setting
:func:`__array_ufunc__` to :obj:`None` (which seems to make no
sense for an array subclass), nor defining it and also defining
reverse methods (which will be called by ``CPython`` in

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove parenthesis.

ufunc for computation.

.. note:: It is hoped to eventually deprecate this method in favour of
:func:__array_ufunc__`.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing backtick.

continue;
}
else if (i == 1) {
if (i == 1) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could eventually turn these into a switch statement.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could even make a switch on nargs and complete by falling through in reverse order, thus avoiding the loop.

PyDict_DelItemString(normal_kwds, "out");
}
}
else if (out != Py_None) { /* not already a tuple and not None */

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put trailing comment on next line.

if(PyTuple_Check(out)) {
int all_none;
int i;
for (i==0; i<PyTuple_GET_SIZE(out); i++) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bank line after declarations.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also i==0 and spaces around operators.

Py_XDECREF(method_name);
Py_XDECREF(normal_args);
Py_XDECREF(normal_kwds);
Py_DECREF(override_args);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is safe?

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it isn't if an error check is performed.

}

Py_ssize_t len = PyTuple_GET_SIZE(normal_args);
PyObject *override_args = PyTuple_New(len + 2);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Really need to check for NULL here. Then you will want Py_XDECREF in failure path.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, no declarations except at top of blocks.

}
}
else if (out != Py_None) { /* not already a tuple and not None */
PyObject *out_tuple = PyTuple_New(1);

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need error check here.

@charris

charris commented Mar 23, 2017

Copy link
Copy Markdown
Owner

Do you want to correct the comments? If you are short on time I can do it after the merge.

@charris

charris commented Mar 23, 2017

Copy link
Copy Markdown
Owner

Note that the normalization routines still need error checking at some point.

@charris
charris merged this pull request into charris:enable-numpy_ufunc Mar 23, 2017
@charris

charris commented Mar 23, 2017

Copy link
Copy Markdown
Owner

Merged, I'll do a maintenance PR on top of this.

@mhvk

mhvk commented Mar 23, 2017

Copy link
Copy Markdown
Author

OK, thanks! I'll have a look at those just in case, and then rebase #5 on top.

@charris

charris commented Mar 23, 2017

Copy link
Copy Markdown
Owner

I merged a fixup with this as it doesn't compile as is.

@mhvk

mhvk commented Mar 23, 2017

Copy link
Copy Markdown
Author

Thanks for all the corrections; I'm amazed at how much I missed (and that it compiled...)

@mhvk
mhvk deleted the array_ufunc_star_args branch April 5, 2017 18:33
charris pushed a commit that referenced this pull request Nov 21, 2021
commit 9c833bed5879d77e625556260690c349de18b433
Author: Thomas Li <[email protected]>
Date:   Wed Nov 17 16:21:27 2021 -0800

    Add Windows config to GHA

    update script [wheel build]

    typo [wheel build]

    fix typo? [wheel build]

    fix linux builds? [wheel build]

    typo [wheel build]

    add license and pin to windows 2016

    skip tests [wheel build]

    pin to windows 2019 instead [wheel build]

    try to find out the error on windows [wheel build]

    maybe fix? [wheel build]

    maybe fix? [wheel build]

    fix? [wheel build]

    cleanup [wheel build]

    Add Windows config to GHA

    update script [wheel build]

    typo [wheel build]

    fix typo? [wheel build]

    fix linux builds? [wheel build]

    typo [wheel build]

    add license and pin to windows 2016

    skip tests [wheel build]

    pin to windows 2019 instead [wheel build]

    try to find out the error on windows [wheel build]

    maybe fix? [wheel build]

    maybe fix? [wheel build]

    fix? [wheel build]

    cleanup [wheel build]

    Update LICENSE_win32.txt

    Update LICENSE_win32.txt

    Add Windows config to GHA

    update script [wheel build]

    typo [wheel build]

    fix typo? [wheel build]

    fix linux builds? [wheel build]

    typo [wheel build]

    add license and pin to windows 2016

    skip tests [wheel build]

    pin to windows 2019 instead [wheel build]

    try to find out the error on windows [wheel build]

    maybe fix? [wheel build]

    maybe fix? [wheel build]

    fix? [wheel build]

    cleanup [wheel build]

    Update LICENSE_win32.txt

    Update LICENSE_win32.txt

    Update cibw_test_command.sh

commit 4bd12df
Author: Thomas Li <[email protected]>
Date:   Mon Nov 15 17:28:47 2021 -0800

    # This is a combination of 14 commits.
    # This is the 1st commit message:

    Add Windows config to GHA
    # This is the commit message #2:

    update script [wheel build]
    # This is the commit message #3:

    typo [wheel build]
    # This is the commit message #4:

    fix typo? [wheel build]
    # This is the commit message #5:

    fix linux builds? [wheel build]
    # This is the commit message #6:

    typo [wheel build]
    # This is the commit message #7:

    add license and pin to windows 2016
    # This is the commit message #8:

    skip tests [wheel build]
    # This is the commit message #9:

    pin to windows 2019 instead [wheel build]
    # This is the commit message #10:

    try to find out the error on windows [wheel build]
    # This is the commit message #11:

    maybe fix? [wheel build]
    # This is the commit message #12:

    maybe fix? [wheel build]
    # This is the commit message #13:

    fix? [wheel build]
    # This is the commit message #14:

    cleanup [wheel build]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants