Skip to content

Commit

Permalink
Bump pyparsing, update usages and update UT
Browse files Browse the repository at this point in the history
Support `pyparsing` 2 and 3.

Related changes in `pyparsing==3.0.0`:
- `originalTextFor` -> `original_text_for`
- `stringEnd` -> `string_end`
- `stringStart` -> `string_start`

Implemented according to this suggestion:
pypa#480 (comment)

Determine which attributres to import according to
`pyparsing.__version__`, and import pyparsing 2 attributes as 3.

Update UT: `test_parseexception_error_msg` to check for either
`string_end` or `stringEnd` in exception message.
  • Loading branch information
moyiz committed Oct 30, 2021
1 parent afab307 commit 1c995fa
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
18 changes: 15 additions & 3 deletions packaging/markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import operator
import os
import platform
import pyparsing
import sys
from typing import Any, Callable, Dict, List, Optional, Tuple, Union

Expand All @@ -16,10 +17,21 @@
ParseResults,
QuotedString,
ZeroOrMore,
stringEnd,
stringStart,
)

# To support pyparsing 2 and 3
if pyparsing.__version__.startswith("2."):
from pyparsing import (
stringEnd as string_end,
stringStart as string_start,
)
else:
from pyparsing import (
string_end,
string_start,
)


from .specifiers import InvalidSpecifier, Specifier

__all__ = [
Expand Down Expand Up @@ -135,7 +147,7 @@ def serialize(self) -> str:
MARKER_ATOM = MARKER_ITEM | Group(LPAREN + MARKER_EXPR + RPAREN)
MARKER_EXPR << MARKER_ATOM + ZeroOrMore(BOOLOP + MARKER_EXPR)

MARKER = stringStart + MARKER_EXPR + stringEnd
MARKER = string_start + MARKER_EXPR + string_end


def _coerce_parse_result(results: Union[ParseResults, List[Any]]) -> List[Any]:
Expand Down
24 changes: 18 additions & 6 deletions packaging/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# 2.0, and the BSD License. See the LICENSE file in the root of this repository
# for complete details.

import pyparsing
import re
import string
import urllib.parse
Expand All @@ -15,14 +16,25 @@
Regex,
Word,
ZeroOrMore,
originalTextFor,
stringEnd,
stringStart,
)

from .markers import MARKER_EXPR, Marker
from .specifiers import LegacySpecifier, Specifier, SpecifierSet

# To support pyparsing 2 and 3
if pyparsing.__version__.startswith("2."):
from pyparsing import (
originalTextFor as original_text_for,
stringEnd as string_end,
stringStart as string_start,
)
else:
from pyparsing import (
original_text_for,
string_end,
string_start,
)


class InvalidRequirement(ValueError):
"""
Expand Down Expand Up @@ -63,10 +75,10 @@ class InvalidRequirement(ValueError):
_VERSION_SPEC = Optional((LPAREN + VERSION_MANY + RPAREN) | VERSION_MANY)
_VERSION_SPEC.setParseAction(lambda s, l, t: t._raw_spec or "")

VERSION_SPEC = originalTextFor(_VERSION_SPEC)("specifier")
VERSION_SPEC = original_text_for(_VERSION_SPEC)("specifier")
VERSION_SPEC.setParseAction(lambda s, l, t: t[1])

MARKER_EXPR = originalTextFor(MARKER_EXPR())("marker")
MARKER_EXPR = original_text_for(MARKER_EXPR())("marker")
MARKER_EXPR.setParseAction(
lambda s, l, t: Marker(s[t._original_start : t._original_end])
)
Expand All @@ -78,7 +90,7 @@ class InvalidRequirement(ValueError):

NAMED_REQUIREMENT = NAME + Optional(EXTRAS) + (URL_AND_MARKER | VERSION_AND_MARKER)

REQUIREMENT = stringStart + NAMED_REQUIREMENT + stringEnd
REQUIREMENT = string_start + NAMED_REQUIREMENT + string_end
# pyparsing isn't thread safe during initialization, so we do it eagerly, see
# issue #104
REQUIREMENT.parseString("x[]")
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
author=about["__author__"],
author_email=about["__email__"],
python_requires=">=3.6",
install_requires=["pyparsing>=2.0.2,<3"], # Needed to avoid issue #91
install_requires=["pyparsing>=2.0.2"], # Needed to avoid issue #91
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
Expand Down
2 changes: 1 addition & 1 deletion tests/test_requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,4 @@ def test_sys_platform_linux_in(self):
def test_parseexception_error_msg(self):
with pytest.raises(InvalidRequirement) as e:
Requirement("toto 42")
assert "Expected stringEnd" in str(e.value)
assert "Expected string_end" in str(e.value)

0 comments on commit 1c995fa

Please sign in to comment.