Skip to content
Open
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
26 changes: 15 additions & 11 deletions arrow/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,24 +155,28 @@ class DateTimeParser:
)
_ESCAPE_RE: ClassVar[Pattern[str]] = re.compile(r"\[[^\[\]]*\]")

_ONE_OR_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{1,2}")
_ONE_OR_TWO_OR_THREE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{1,3}")
_ONE_OR_MORE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d+")
_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{2}")
_THREE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{3}")
_FOUR_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"\d{4}")
_TZ_Z_RE: ClassVar[Pattern[str]] = re.compile(r"([\+\-])(\d{2})(?:(\d{2}))?|Z")
_TZ_ZZ_RE: ClassVar[Pattern[str]] = re.compile(r"([\+\-])(\d{2})(?:\:(\d{2}))?|Z")
_ONE_OR_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]{1,2}")
_ONE_OR_TWO_OR_THREE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]{1,3}")
_ONE_OR_MORE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]+")
_TWO_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]{2}")
_THREE_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]{3}")
_FOUR_DIGIT_RE: ClassVar[Pattern[str]] = re.compile(r"[0-9]{4}")
_TZ_Z_RE: ClassVar[Pattern[str]] = re.compile(
r"([\+\-])([0-9]{2})(?:([0-9]{2}))?|Z"
)
_TZ_ZZ_RE: ClassVar[Pattern[str]] = re.compile(
r"([\+\-])([0-9]{2})(?:\:([0-9]{2}))?|Z"
)
_TZ_NAME_RE: ClassVar[Pattern[str]] = re.compile(r"\w[\w+\-/]+")
# NOTE: timestamps cannot be parsed from natural language strings (by removing the ^...$) because it will
# break cases like "15 Jul 2000" and a format list (see issue #447)
_TIMESTAMP_RE: ClassVar[Pattern[str]] = re.compile(r"^\-?\d+\.?\d+$")
_TIMESTAMP_EXPANDED_RE: ClassVar[Pattern[str]] = re.compile(r"^\-?\d+$")
_TIME_RE: ClassVar[Pattern[str]] = re.compile(
r"^(\d{2})(?:\:?(\d{2}))?(?:\:?(\d{2}))?(?:([\.\,])(\d+))?$"
r"^([0-9]{2})(?:\:?([0-9]{2}))?(?:\:?([0-9]{2}))?(?:([\.\,])([0-9]+))?$"
)
_WEEK_DATE_RE: ClassVar[Pattern[str]] = re.compile(
r"(?P<year>\d{4})[\-]?W(?P<week>\d{2})[\-]?(?P<day>\d)?"
r"(?P<year>[0-9]{4})[\-]?W(?P<week>[0-9]{2})[\-]?(?P<day>[0-9])?"
)

_BASE_INPUT_RE_MAP: ClassVar[Dict[_FORMAT_TYPE, Pattern[str]]] = {
Expand Down Expand Up @@ -888,7 +892,7 @@ class TzinfoParser:
"""

_TZINFO_RE: ClassVar[Pattern[str]] = re.compile(
r"^(?:\(UTC)*([\+\-])?(\d{2})(?:\:?(\d{2}))?"
r"^(?:\(UTC)*([\+\-])?([0-9]{2})(?:\:?([0-9]{2}))?"
)

@classmethod
Expand Down
14 changes: 14 additions & 0 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,20 @@ def test_parse_numbers(self):
== self.expected
)

def test_parse_non_ascii_digits_rejected(self):
# \d matches the whole Unicode decimal category, so non-ASCII numerals
# (fullwidth, Arabic-Indic, ...) used to slip through the numeric tokens.
with pytest.raises(ParserMatchError):
self.parser.parse("2021-01-02", "YYYY-MM-DD")

with pytest.raises(ParserMatchError):
self.parser.parse("٢٠٢١-٠١-٠٢", "YYYY-MM-DD")

with pytest.raises(ParserMatchError):
self.parser.parse(
"2021-01-02T12:30:45", "YYYY-MM-DDTHH:mm:ss"
)

def test_parse_am(self):
with pytest.raises(ParserMatchError):
self.parser.parse("2021-01-30 14:00:00 AM", "YYYY-MM-DD HH:mm:ss A")
Expand Down