示例#1
0
def test_invalid_arraysize():
    with pytest.raises(exceptions.E13):
        field = tree.Field(None,
                           name='broken',
                           datatype='char',
                           arraysize='foo')
        converters.get_converter(field)
示例#2
0
def test_parse_vowarning():
    config = {'verify': 'exception', 'filename': 'foo.xml'}
    pos = (42, 64)
    with pytest.warns(exceptions.W47) as w:
        field = tree.Field(None,
                           name='c',
                           datatype='char',
                           config=config,
                           pos=pos)
        converters.get_converter(field, config=config, pos=pos)

    parts = exceptions.parse_vowarning(str(w[0].message))

    match = {
        'number': 47,
        'is_exception': False,
        'nchar': 64,
        'warning': 'W47',
        'is_something': True,
        'message': 'Missing arraysize indicates length 1',
        'doc_url': 'io/votable/api_exceptions.html#w47',
        'nline': 42,
        'is_warning': True
    }
    assert parts == match
示例#3
0
def test_precision():
    config = {'pedantic': True}

    field = tree.Field(
        None, name='c', datatype='float', precision="E4",
        config=config)
    c = converters.get_converter(field, config=config)
    assert c.output(266.248, False) == '266.2'

    field = tree.Field(
        None, name='c', datatype='float', precision="F4",
        config=config)
    c = converters.get_converter(field, config=config)
    assert c.output(266.248, False) == '266.2480'
def test_bit_mask():
    config = {'pedantic': True}
    with catch_warnings(exceptions.W39) as w:
        field = tree.Field(None, name='c', datatype='bit', config=config)
        c = converters.get_converter(field, config=config)
        c.output(True, True)
    assert len(w) == 1
示例#5
0
def test_unicode_as_char():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='unicode_in_char',
                       datatype='char',
                       arraysize='*',
                       config=config)
    c = converters.get_converter(field, config=config)

    # Test parsing.
    c.parse('XYZ')  # ASCII succeeds
    with pytest.warns(
            exceptions.W55,
            match=
            r'FIELD \(unicode_in_char\) has datatype="char" but contains non-ASCII value'
    ):
        c.parse("zła")  # non-ASCII

    # Test output.
    c.output('XYZ', False)  # ASCII str succeeds
    c.output(b'XYZ', False)  # ASCII bytes succeeds
    value = 'zła'
    value_bytes = value.encode('utf-8')
    with pytest.warns(exceptions.E24,
                      match=r'E24: Attempt to write non-ASCII value'):
        c.output(value, False)  # non-ASCII str raises
    with pytest.warns(exceptions.E24,
                      match=r'E24: Attempt to write non-ASCII value'):
        c.output(value_bytes, False)  # non-ASCII bytes raises
示例#6
0
def test_double_array():
    config = {'verify': 'exception', 'version_1_3_or_later': True}
    field = tree.Field(None,
                       name='c',
                       datatype='double',
                       arraysize='3',
                       config=config)
    data = (1.0, 2.0, 3.0)
    c = converters.get_converter(field, config=config)
    assert c.output(1.0, False) == '1'
    assert c.output(1.0, [False, False]) == '1'
    assert c.output(data, False) == '1 2 3'
    assert c.output(data, [False, False, False]) == '1 2 3'
    assert c.output(data, [False, False, True]) == '1 2 NaN'
    assert c.output(data, [False, False]) == '1 2'

    a = c.parse("1 2 3", config=config)
    assert_array_equal(a[0], data)
    assert_array_equal(a[1], False)

    with pytest.raises(exceptions.E02):
        c.parse("1", config=config)

    with pytest.raises(AttributeError), pytest.warns(exceptions.E02):
        c.parse("1")

    with pytest.raises(exceptions.E02):
        c.parse("2 3 4 5 6", config=config)

    with pytest.warns(exceptions.E02):
        a = c.parse("2 3 4 5 6")

    assert_array_equal(a[0], [2, 3, 4])
    assert_array_equal(a[1], False)
示例#7
0
def test_complex_array_vararray():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='floatComplex', arraysize='2x3*',
        config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
示例#8
0
def test_integer_overflow():
    config = {'verify': 'exception'}

    field = tree.Field(None, name='c', datatype='int', config=config)
    c = converters.get_converter(field, config=config)
    with pytest.raises(exceptions.W51):
        c.parse('-2208988800', config=config)
示例#9
0
def test_bit():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='bit',
        config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("T")
示例#10
0
def test_float_mask_permissive():
    config = {'pedantic': False}
    field = tree.Field(
        None, name='c', datatype='float',
        config=config)
    c = converters.get_converter(field, config=config)
    assert c.parse('null') == (c.null, True)
示例#11
0
def test_unicode_mask():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='unicodeChar',
        config=config)
    c = converters.get_converter(field, config=config)
    assert c.output("Foo", True) == ''
示例#12
0
def test_integer_overflow():
    config = {'pedantic': True}

    field = tree.Field(
        None, name='c', datatype='int', config=config)
    c = converters.get_converter(field, config=config)
    c.parse('-2208988800', config=config)
示例#13
0
def test_wrong_number_of_elements():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='int', arraysize='2x3*',
        config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
示例#14
0
def test_float_mask():
    config = {'verify': 'exception'}
    field = tree.Field(None, name='c', datatype='float', config=config)
    c = converters.get_converter(field, config=config)
    assert c.parse('') == (c.null, True)
    with pytest.raises(ValueError):
        c.parse('null')
示例#15
0
def test_bit_mask():
    config = {'verify': 'exception'}
    with pytest.warns(exceptions.W39) as w:
        field = tree.Field(None, name='c', datatype='bit', config=config)
        c = converters.get_converter(field, config=config)
        c.output(True, True)
    assert len(w) == 1
示例#16
0
def test_boolean():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='boolean',
        config=config)
    c = converters.get_converter(field, config=config)
    c.parse('YES')
示例#17
0
def test_complex():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='doubleComplex',
        config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("1 2 3")
示例#18
0
def test_float_mask_permissive():
    config = {'verify': 'ignore'}
    field = tree.Field(None, name='c', datatype='float', config=config)

    # config needs to be also passed into parse() to work.
    # https://github.com/astropy/astropy/issues/8775
    c = converters.get_converter(field, config=config)
    assert c.parse('null', config=config) == (c.null, True)
示例#19
0
def test_boolean_array():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='boolean', arraysize='*',
        config=config)
    c = converters.get_converter(field, config=config)
    r, mask = c.parse('TRUE FALSE T F 0 1')
    assert_array_equal(r, [True, False, True, False, False, True])
示例#20
0
def test_complex_array_vararray2():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='floatComplex', arraysize='2x3*',
        config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("")
    assert len(x[0]) == 0
示例#21
0
def test_float_default_precision():
    config = {'pedantic': True}

    field = tree.Field(
        None, name='c', datatype='float', arraysize="4",
        config=config)
    c = converters.get_converter(field, config=config)
    assert (c.output([1, 2, 3, 8.9990234375], [False, False, False, False]) ==
            '1 2 3 8.9990234375')
示例#22
0
def test_bit_mask():
    config = {'pedantic': True}
    with catch_warnings(exceptions.W39) as w:
        field = tree.Field(
            None, name='c', datatype='bit',
            config=config)
        c = converters.get_converter(field, config=config)
        c.output(True, True)
    assert len(w) == 1
示例#23
0
def test_complex_vararray():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='doubleComplex', arraysize='*',
        config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("1 2 3 4")
    assert len(x) == 2
    assert x[0][0] == complex(1, 2)
示例#24
0
def test_complex_array_vararray3():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='doubleComplex', arraysize='2x3*',
        config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("1 2 3 4 5 6 7 8 9 10 11 12")
    assert len(x) == 2
    assert np.all(x[0][0][0] == complex(1, 2))
示例#25
0
def test_wrong_number_of_elements():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       datatype='int',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
示例#26
0
def test_complex_array_vararray():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       datatype='floatComplex',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
示例#27
0
def test_precision():
    config = {'verify': 'exception'}

    field = tree.Field(None,
                       name='c',
                       datatype='float',
                       precision="E4",
                       config=config)
    c = converters.get_converter(field, config=config)
    assert c.output(266.248, False) == '266.2'

    field = tree.Field(None,
                       name='c',
                       datatype='float',
                       precision="F4",
                       config=config)
    c = converters.get_converter(field, config=config)
    assert c.output(266.248, False) == '266.2480'
示例#28
0
def test_unicode_mask():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       arraysize='1',
                       datatype='unicodeChar',
                       config=config)
    c = converters.get_converter(field, config=config)
    assert c.output("Foo", True) == ''
def test_complex_array_vararray():
    config = {'pedantic': True}
    field = tree.Field(None,
                       name='c',
                       datatype='floatComplex',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
def test_wrong_number_of_elements():
    config = {'pedantic': True}
    field = tree.Field(None,
                       name='c',
                       datatype='int',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    c.parse("2 3 4 5 6")
示例#31
0
def test_oversize_char():
    config = {'verify': 'exception'}
    with pytest.warns(exceptions.W47) as w:
        field = tree.Field(None, name='c', datatype='char', config=config)
        c = converters.get_converter(field, config=config)
    assert len(w) == 1

    with pytest.warns(exceptions.W46) as w:
        c.parse("XXX")
    assert len(w) == 1
示例#32
0
def test_oversize_unicode():
    config = {'pedantic': True}
    with catch_warnings(exceptions.W46) as w:
        field = tree.Field(
            None, name='c2', datatype='unicodeChar',
            config=config)
        c = converters.get_converter(field, config=config)

        c.parse("XXX")
    assert len(w) == 1
def test_complex_array_vararray2():
    config = {'pedantic': True}
    field = tree.Field(None,
                       name='c',
                       datatype='floatComplex',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("")
    assert len(x[0]) == 0
def test_oversize_char():
    config = {'pedantic': True}
    with catch_warnings(exceptions.W47) as w:
        field = tree.Field(None, name='c', datatype='char', config=config)
        c = converters.get_converter(field, config=config)
    assert len(w) == 1

    with catch_warnings(exceptions.W46) as w:
        c.parse("XXX")
    assert len(w) == 1
示例#35
0
def test_boolean_array():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       datatype='boolean',
                       arraysize='*',
                       config=config)
    c = converters.get_converter(field, config=config)
    r, mask = c.parse('TRUE FALSE T F 0 1')
    assert_array_equal(r, [True, False, True, False, False, True])
示例#36
0
def test_float_default_precision():
    config = {'verify': 'exception'}

    field = tree.Field(None,
                       name='c',
                       datatype='float',
                       arraysize="4",
                       config=config)
    c = converters.get_converter(field, config=config)
    assert (c.output([1, 2, 3, 8.9990234375],
                     [False, False, False, False]) == '1 2 3 8.9990234375')
示例#37
0
def test_complex_vararray():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       datatype='doubleComplex',
                       arraysize='*',
                       config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("1 2 3 4")
    assert len(x) == 2
    assert x[0][0] == complex(1, 2)
示例#38
0
def test_complex_array_vararray3():
    config = {'verify': 'exception'}
    field = tree.Field(None,
                       name='c',
                       datatype='doubleComplex',
                       arraysize='2x3*',
                       config=config)
    c = converters.get_converter(field, config=config)
    x = c.parse("1 2 3 4 5 6 7 8 9 10 11 12")
    assert len(x) == 2
    assert np.all(x[0][0][0] == complex(1, 2))
示例#39
0
def test_oversize_unicode():
    config = {'verify': 'exception'}
    with pytest.warns(exceptions.W46) as w:
        field = tree.Field(None,
                           name='c2',
                           datatype='unicodeChar',
                           arraysize='1',
                           config=config)
        c = converters.get_converter(field, config=config)
        c.parse("XXX")
    assert len(w) == 1
示例#40
0
def test_unicode_as_char_binary():
    config = {'verify': 'exception'}

    field = tree.Field(None,
                       name='unicode_in_char',
                       datatype='char',
                       arraysize='*',
                       config=config)
    c = converters.get_converter(field, config=config)
    c._binoutput_var('abc', False)  # ASCII succeeds
    with pytest.raises(exceptions.E24,
                       match=r"E24: Attempt to write non-ASCII value"):
        c._binoutput_var('zła', False)

    field = tree.Field(None,
                       name='unicode_in_char',
                       datatype='char',
                       arraysize='3',
                       config=config)
    c = converters.get_converter(field, config=config)
    c._binoutput_fixed('xyz', False)
    with pytest.raises(exceptions.E24,
                       match=r"E24: Attempt to write non-ASCII value"):
        c._binoutput_fixed('zła', False)
示例#41
0
	def _end_dataType(self, name, attrs, content):
		content = content.strip()

		class _values(object):
			null = None

		class _field(object):
			datatype = content
			arraysize = attrs.get("arraysize") if content in (
				"char", "unicodeChar") else None
			precision = None
			width = None
			values = _values()


		converter = get_converter(_field())
		self.curDtype = np.dtype(converter.format)
		self.curDatatype = content
		self.curArraysize = attrs.get("arraysize")
示例#42
0
def test_parse_vowarning():
    config = {'pedantic': True,
              'filename': 'foo.xml'}
    pos = (42, 64)
    with catch_warnings(exceptions.W47) as w:
        field = tree.Field(
            None, name='c', datatype='char',
            config=config, pos=pos)
        c = converters.get_converter(field, config=config, pos=pos)

    parts = exceptions.parse_vowarning(str(w[0].message))

    match = {
        'number': 47,
        'is_exception': False,
        'nchar': 64,
        'warning': 'W47',
        'is_something': True,
        'message': 'Missing arraysize indicates length 1',
        'doc_url': 'io/votable/api_exceptions.html#w47',
        'nline': 42,
        'is_warning': True
        }
    assert parts == match
示例#43
0
def test_bit():
    config = {'verify': 'exception'}
    field = tree.Field(None, name='c', datatype='bit', config=config)
    c = converters.get_converter(field, config=config)
    with pytest.raises(exceptions.E04):
        c.parse("T")
示例#44
0
def test_float_mask_permissive():
    config = {'verify': 'ignore'}
    field = tree.Field(None, name='c', datatype='float', config=config)
    c = converters.get_converter(field, config=config)
    assert c.parse('null') == (c.null, True)
示例#45
0
def test_invalid_arraysize():
    field = tree.Field(
        None, name='broken', datatype='char', arraysize='foo')
    converters.get_converter(field)
示例#46
0
def test_complex():
    config = {'verify': 'exception'}
    field = tree.Field(None, name='c', datatype='doubleComplex', config=config)
    c = converters.get_converter(field, config=config)
    with pytest.raises(exceptions.E03):
        c.parse("1 2 3")
示例#47
0
def test_invalid_type():
    config = {'pedantic': True}
    field = tree.Field(
        None, name='c', datatype='foobar',
        config=config)
    c = converters.get_converter(field, config=config)
示例#48
0
def test_boolean():
    config = {'verify': 'exception'}
    field = tree.Field(None, name='c', datatype='boolean', config=config)
    c = converters.get_converter(field, config=config)
    with pytest.raises(exceptions.E05):
        c.parse('YES')
示例#49
0
def test_invalid_type():
    config = {'verify': 'exception'}
    with pytest.raises(exceptions.E06):
        field = tree.Field(None, name='c', datatype='foobar', config=config)
        converters.get_converter(field, config=config)