summaryrefslogtreecommitdiff
path: root/pkg_resources/__init__.py
diff options
context:
space:
mode:
authorSteve Kowalik <steven@wedontsleep.org>2016-01-07 14:04:36 +1100
committerSteve Kowalik <steven@wedontsleep.org>2016-01-07 14:04:36 +1100
commit3bd5118eaa87b4f6598e0a473fec3623b6579d3b (patch)
tree22b9a1f65394dfee78f4327db2ee02cda2a14f55 /pkg_resources/__init__.py
parent7d63ebffa598f12b55d9fd0a41768ec88ff70528 (diff)
downloadpython-setuptools-git-3bd5118eaa87b4f6598e0a473fec3623b6579d3b.tar.gz
Destroy MarkerEvaluation for being pointless, fix two silly mistakes in tests and update packaging changes.
Diffstat (limited to 'pkg_resources/__init__.py')
-rw-r--r--pkg_resources/__init__.py181
1 files changed, 21 insertions, 160 deletions
diff --git a/pkg_resources/__init__.py b/pkg_resources/__init__.py
index d81430c7..aa221347 100644
--- a/pkg_resources/__init__.py
+++ b/pkg_resources/__init__.py
@@ -1407,170 +1407,31 @@ def to_filename(name):
return name.replace('-','_')
-class MarkerEvaluation(object):
- values = {
- 'os_name': lambda: os.name,
- 'sys_platform': lambda: sys.platform,
- 'python_full_version': platform.python_version,
- 'python_version': lambda: platform.python_version()[:3],
- 'platform_version': platform.version,
- 'platform_machine': platform.machine,
- 'platform_python_implementation': platform.python_implementation,
- 'python_implementation': platform.python_implementation,
- }
-
- @classmethod
- def is_invalid_marker(cls, text):
- """
- Validate text as a PEP 508 environment marker; return an exception
- if invalid or False otherwise.
- """
- try:
- cls.evaluate_marker(text)
- except packaging.markers.InvalidMarker as e:
- e.filename = None
- e.lineno = None
- return e
- return False
-
- @staticmethod
- def normalize_exception(exc):
- """
- Given a SyntaxError from a marker evaluation, normalize the error
- message:
- - Remove indications of filename and line number.
- - Replace platform-specific error messages with standard error
- messages.
- """
- subs = {
- 'unexpected EOF while parsing': 'invalid syntax',
- 'parenthesis is never closed': 'invalid syntax',
- }
- exc.filename = None
- exc.lineno = None
- exc.msg = subs.get(exc.msg, exc.msg)
- return exc
-
- @classmethod
- def and_test(cls, nodelist):
- # MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
- items = [
- cls.interpret(nodelist[i])
- for i in range(1, len(nodelist), 2)
- ]
- return functools.reduce(operator.and_, items)
-
- @classmethod
- def test(cls, nodelist):
- # MUST NOT short-circuit evaluation, or invalid syntax can be skipped!
- items = [
- cls.interpret(nodelist[i])
- for i in range(1, len(nodelist), 2)
- ]
- return functools.reduce(operator.or_, items)
-
- @classmethod
- def atom(cls, nodelist):
- t = nodelist[1][0]
- if t == token.LPAR:
- if nodelist[2][0] == token.RPAR:
- raise SyntaxError("Empty parentheses")
- return cls.interpret(nodelist[2])
- msg = "Language feature not supported in environment markers"
- raise SyntaxError(msg)
-
- @classmethod
- def comparison(cls, nodelist):
- if len(nodelist) > 4:
- msg = "Chained comparison not allowed in environment markers"
- raise SyntaxError(msg)
- comp = nodelist[2][1]
- cop = comp[1]
- if comp[0] == token.NAME:
- if len(nodelist[2]) == 3:
- if cop == 'not':
- cop = 'not in'
- else:
- cop = 'is not'
- try:
- cop = cls.get_op(cop)
- except KeyError:
- msg = repr(cop) + " operator not allowed in environment markers"
- raise SyntaxError(msg)
- return cop(cls.evaluate(nodelist[1]), cls.evaluate(nodelist[3]))
-
- @classmethod
- def get_op(cls, op):
- ops = {
- symbol.test: cls.test,
- symbol.and_test: cls.and_test,
- symbol.atom: cls.atom,
- symbol.comparison: cls.comparison,
- 'not in': lambda x, y: x not in y,
- 'in': lambda x, y: x in y,
- '==': operator.eq,
- '!=': operator.ne,
- '<': operator.lt,
- '>': operator.gt,
- '<=': operator.le,
- '>=': operator.ge,
- }
- if hasattr(symbol, 'or_test'):
- ops[symbol.or_test] = cls.test
- return ops[op]
-
- @classmethod
- def evaluate_marker(cls, text, extra=None):
- """
- Evaluate a PEP 508 environment marker on CPython 2.4+.
- Return a boolean indicating the marker result in this environment.
- Raise InvalidMarker if marker is invalid.
-
- This implementation uses the 'pyparsing' module.
- """
- marker = packaging.markers.Marker(text)
- return marker.evaluate()
+def invalid_marker(text):
+ """
+ Validate text as a PEP 508 environment marker; return an exception
+ if invalid or False otherwise.
+ """
+ try:
+ evaluate_marker(text)
+ except packaging.markers.InvalidMarker as e:
+ e.filename = None
+ e.lineno = None
+ return e
+ return False
- @classmethod
- def interpret(cls, nodelist):
- while len(nodelist)==2: nodelist = nodelist[1]
- try:
- op = cls.get_op(nodelist[0])
- except KeyError:
- raise SyntaxError("Comparison or logical expression expected")
- return op(nodelist)
- @classmethod
- def evaluate(cls, nodelist):
- while len(nodelist)==2: nodelist = nodelist[1]
- kind = nodelist[0]
- name = nodelist[1]
- if kind==token.NAME:
- try:
- op = cls.values[name]
- except KeyError:
- raise SyntaxError("Unknown name %r" % name)
- return op()
- if kind==token.STRING:
- s = nodelist[1]
- if not cls._safe_string(s):
- raise SyntaxError(
- "Only plain strings allowed in environment markers")
- return s[1:-1]
- msg = "Language feature not supported in environment markers"
- raise SyntaxError(msg)
+def evaluate_marker(text, extra=None):
+ """
+ Evaluate a PEP 508 environment marker.
+ Return a boolean indicating the marker result in this environment.
+ Raise InvalidMarker if marker is invalid.
- @staticmethod
- def _safe_string(cand):
- return (
- cand[:1] in "'\"" and
- not cand.startswith('"""') and
- not cand.startswith("'''") and
- '\\' not in cand
- )
+ This implementation uses the 'pyparsing' module.
+ """
+ marker = packaging.markers.Marker(text)
+ return marker.evaluate()
-invalid_marker = MarkerEvaluation.is_invalid_marker
-evaluate_marker = MarkerEvaluation.evaluate_marker
class NullProvider:
"""Try to implement resources and metadata for arbitrary PEP 302 loaders"""