Skip to content

Commit

Permalink
Merge pull request #744 from astrofrog/unit/fix-quantity-big-endian-bug
Browse files Browse the repository at this point in the history
Issues with big endian Numpy arrays in Quantity
  • Loading branch information
astrofrog committed Feb 8, 2013
2 parents b0475d5 + bf72891 commit 61ba977
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
3 changes: 1 addition & 2 deletions astropy/units/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,7 @@ def _condition_arg(value):
else:
try:
avalue = np.array(value)
dt = str(avalue.dtype)
if not (dt.startswith('int') or dt.startswith('float')):
if not avalue.dtype.kind in ['i', 'f']:
raise ValueError("Must be convertable to int or float array")
if ma.isMaskedArray(value):
return value
Expand Down
21 changes: 21 additions & 0 deletions astropy/units/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

import warnings

import numpy as np

from ...tests.helper import pytest, raises
from ...tests.compat import assert_allclose
from ...utils.compat.fractions import Fraction
Expand Down Expand Up @@ -397,9 +399,28 @@ def test_compose_no_duplicates():
composed = new.compose(units=u.cgs.bases)
assert len(composed) == 1


def test_long_int():
"""
Issue #672
"""
sigma = 10 ** 21 * u.M_p / u.cm ** 2
sigma.to(u.M_sun / u.pc ** 2)


def test_endian_independence():
"""
Regression test for #744
A logic issue in the units code meant that big endian arrays could not be
converted because the dtype is '>f4', not 'float32', and the code was
looking for the strings 'float' or 'int'.
"""
for endian in ['<', '>']:
for ntype in ['i', 'f']:
for byte in ['4', '8']:
# Note, we have to use encode() because we've imported
# unicode_literals from __future__, and Numpy 1.4.1 crashes if
# a unicode dtype is passed.
x = np.array([1,2,3], dtype=(endian + ntype + byte).encode('ascii'))
u.m.to(u.cm, x)

0 comments on commit 61ba977

Please sign in to comment.