Skip to content

Commit

Permalink
Replace ValueErrors containing implicit contract about the structure …
Browse files Browse the repository at this point in the history
…of the message with a RequirementParseError explicitly joining arguments for the string representation.
  • Loading branch information
jaraco committed May 3, 2015
1 parent fb27525 commit cd79379
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions pkg_resources/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import io
import time
import re
import types
import imp
import zipfile
import zipimport
import warnings
Expand All @@ -39,12 +39,6 @@
import textwrap
from pkgutil import get_importer

try:
import _imp
except ImportError:
# Python 3.2 compatibility
import imp as _imp

PY3 = sys.version_info > (3,)
PY2 = not PY3

Expand Down Expand Up @@ -2169,7 +2163,7 @@ def _handle_ns(packageName, path_item):
return None
module = sys.modules.get(packageName)
if module is None:
module = sys.modules[packageName] = types.ModuleType(packageName)
module = sys.modules[packageName] = imp.new_module(packageName)
module.__path__ = []
_set_parent_ns(packageName)
elif not hasattr(module,'__path__'):
Expand All @@ -2188,7 +2182,7 @@ def _handle_ns(packageName, path_item):
def declare_namespace(packageName):
"""Declare that package 'packageName' is a namespace package"""

_imp.acquire_lock()
imp.acquire_lock()
try:
if packageName in _namespace_packages:
return
Expand All @@ -2215,18 +2209,18 @@ def declare_namespace(packageName):
_handle_ns(packageName, path_item)

finally:
_imp.release_lock()
imp.release_lock()

def fixup_namespace_packages(path_item, parent=None):
"""Ensure that previously-declared namespace packages include path_item"""
_imp.acquire_lock()
imp.acquire_lock()
try:
for package in _namespace_packages.get(parent,()):
subpath = _handle_ns(package, path_item)
if subpath:
fixup_namespace_packages(subpath, package)
finally:
_imp.release_lock()
imp.release_lock()

def file_ns_handler(importer, path_item, packageName, module):
"""Compute an ns-package subpath for a filesystem or zipfile importer"""
Expand Down Expand Up @@ -2859,6 +2853,11 @@ def issue_warning(*args,**kw):
warnings.warn(stacklevel=level + 1, *args, **kw)


class RequirementParseError(ValueError):
def __str__(self):
return ' '.join(self.args)


def parse_requirements(strs):
"""Yield ``Requirement`` objects for each specification in `strs`
Expand All @@ -2877,14 +2876,14 @@ def scan_list(ITEM, TERMINATOR, line, p, groups, item_name):
line = next(lines)
p = 0
except StopIteration:
raise ValueError(
raise RequirementParseError(
"\\ must not appear on the last nonblank line"
)

match = ITEM(line, p)
if not match:
msg = "Expected " + item_name + " in"
raise ValueError(msg, line, "at", line[p:])
raise RequirementParseError(msg, line, "at", line[p:])

items.append(match.group(*groups))
p = match.end()
Expand All @@ -2895,7 +2894,7 @@ def scan_list(ITEM, TERMINATOR, line, p, groups, item_name):
p = match.end()
elif not TERMINATOR(line, p):
msg = "Expected ',' or end-of-list in"
raise ValueError(msg, line, "at", line[p:])
raise RequirementParseError(msg, line, "at", line[p:])

match = TERMINATOR(line, p)
# skip the terminator, if any
Expand All @@ -2906,7 +2905,7 @@ def scan_list(ITEM, TERMINATOR, line, p, groups, item_name):
for line in lines:
match = DISTRO(line)
if not match:
raise ValueError("Missing distribution spec", line)
raise RequirementParseError("Missing distribution spec", line)
project_name = match.group(1)
p = match.end()
extras = []
Expand Down

0 comments on commit cd79379

Please sign in to comment.