Skip to content

Commit

Permalink
ignore whitespace in malformed floats #960
Browse files Browse the repository at this point in the history
  • Loading branch information
mozman committed Nov 10, 2023
1 parent f48ad2a commit 0c01701
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
8 changes: 8 additions & 0 deletions src/ezdxf/recover.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Copyright (c) 2020-2023, Manfred Moitzi
# License: MIT License
from __future__ import annotations

import string
import typing
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -637,6 +639,11 @@ def synced_bytes_loader(stream: BinaryIO) -> Iterator[DXFTag]:
ACADVER = b"$ACADVER"


def _strip_whitespace(s: str) -> str:
ws = set(string.whitespace)
return "".join([c for c in s if c not in ws])


def detect_encoding(tags: Iterable[DXFTag]) -> str:
"""Detect text encoding from header variables $DWGCODEPAGE and $ACADVER
out of a stream of DXFTag objects.
Expand Down Expand Up @@ -721,6 +728,7 @@ def recover_int(s: Union[str, bytes]) -> int:
def recover_float(s: Union[str, bytes]) -> float:
if isinstance(s, bytes):
s = s.decode(encoding="utf8", errors="ignore")
s = _strip_whitespace(s)
value = _search_float(s)
msg = f'recovered invalid floating point value "{s}" near line {line} as "{value}"'
messages.append((AuditError.INVALID_FLOATING_POINT_VALUE, msg))
Expand Down
12 changes: 9 additions & 3 deletions tests/test_00_dxf_low_level_structs/test_055_recover_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,17 @@ def test_underlines_in_numbers_are_handled_by_python(self):
assert tags[1] == (70, 456000)
assert len(msg) == 0

def test_spaces_in_floats_returns_invalid_value(self):
loader = bytes_loader(BytesIO(b"50\n1.000 e20\n"))
@pytest.mark.parametrize("b", [
b"50\n1.000 e20\n",
b"50\n1.000 E20\n",
b"50\n1.000\te20\n",
b"50\n1.000\tE20\n",
])
def test_ignore_whitespace_in_floats(self, b):
loader = bytes_loader(BytesIO(b))
msg = []
tags = list(byte_tag_compiler(loader, messages=msg))
assert tags[0] == (50, 1), "invalid interpretation"
assert tags[0] == (50, 1e20)
assert msg[0][0] == AuditError.INVALID_FLOATING_POINT_VALUE


Expand Down

0 comments on commit 0c01701

Please sign in to comment.