8000 BUG: Disable hex(np.floating) and oct(np.floating) on python 2 by eric-wieser · Pull Request #10765 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Disable hex(np.floating) and oct(np.floating) on python 2 #10765

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

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 2 additions & 8 deletions numpy/core/src/multiarray/scalartypes.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1079,22 +1079,18 @@ static PyObject *

#if !defined(NPY_PY3K)

/**begin repeat1
* #name = int, hex, oct#
*/
static PyObject *
@char@longdoubletype_@name@(PyObject *self)
@char@longdoubletype_int(PyObject *self)
{
PyObject *ret;
PyObject *obj = @char@longdoubletype_long(self);
if (obj == NULL) {
return NULL;
}
ret = Py_TYPE(obj)->tp_as_number->nb_@name@(obj);
ret = Py_TYPE(obj)->tp_as_number->nb_int(obj);
Py_DECREF(obj);
return ret;
}
/**end repeat1**/

#endif /* !defined(NPY_PY3K) */

Expand Down Expand Up @@ -4370,8 +4366,6 @@ initialize_numeric_types(void)
#else
@char@longdoubletype_as_number.nb_int = @char@longdoubletype_int;
@char@longdoubletype_as_number.nb_long = @char@longdoubletype_long;
@char@longdoubletype_as_number.nb_hex = @char@longdoubletype_hex;
@char@longdoubletype_as_number.nb_oct = @char@longdoubletype_oct;
#endif

Py@CHAR@LongDoubleArrType_Type.tp_as_number = &@char@longdoubletype_as_number;
Expand Down
17 changes: 11 additions & 6 deletions numpy/core/src/umath/scalarmath.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -1464,12 +1464,10 @@ static NPY_INLINE PyObject *
/**begin repeat
*
* #name = (byte, ubyte, short, ushort, int, uint,
* long, ulong, longlong, ulonglong,
* half, float, double, longdouble,
* cfloat, cdouble, clongdouble)*2#
* #oper = oct*17, hex*17#
* #kind = (int*5, long*5, int*2, long*2, int, long*2)*2#
* #cap = (Int*5, Long*5, Int*2, Long*2, Int, Long*2)*2#
* long, ulong, longlong, ulonglong)*2#
* #oper = oct*10, hex*10#
* #kind = (int*5, long*5)*2#
* #cap = (Int*5, Long*5)*2#
*/
static PyObject *
@name@_@oper@(PyObject *obj)
Expand All @@ -1483,6 +1481,13 @@ static PyObject *
}
/**end repeat**/

/**begin repeat
* #name = (half, float, double, longdouble,
* cfloat, cdouble, clongdouble)*2#
* #oper = oct*7, hex*7#
*/
#define @name@_@oper@ NULL
Copy link
Member Author

Choose a reason for hiding this comment

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

There's precedent for this approach with #define @name@_invert NULL

/**end repeat**/
#endif

/**begin repeat
Expand Down
25 changes: 22 additions & 3 deletions numpy/core/tests/test_scalarprint.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
"""
from __future__ import division, absolute_import, print_function

import code, sys
import code
import sys
import platform
from tempfile import TemporaryFile

import pytest

from tempfile import TemporaryFile
import numpy as np
from numpy.testing import assert_, assert_equal, suppress_warnings
from numpy.testing import (
assert_, assert_equal, assert_raises, suppress_warnings
)

class TestRealScalars(object):
def test_str(self):
Expand Down Expand Up @@ -324,3 +328,18 @@ def float64_vs_python(self):
# gh-2643, gh-6136, gh-6908
assert_equal(repr(np.float64(0.1)), repr(0.1))
assert_(repr(np.float64(0.20000000000000004)) != repr(0.2))

@pytest.mark.parametrize('dt', [
np.half,
np.single,
np.double,
np.longdouble,
])
def test_hex_oct_float(self, dt):
"""
Test that hex and oct fail on numpy floats just like they do on
python ones
"""
f = dt(0.0)
assert_raises(TypeError, hex, f)
assert_raises(TypeError, oct, f)
0