Repository: pypa/pip

Issue:
Version parse error
```
% sw_vers
ProductName:    Mac OS X
ProductVersion: 10.10.1
BuildVersion:   14B25
% python --version
Python 2.7.9
% pip --version
pip 6.0.3 from /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (python 2.7)
% pip install -e git+https://github.com/edx/edx-ora2.git@release-2014-12-17T15.53#egg=edx-ora2
Obtaining edx-ora2 from git+https://github.com/edx/edx-ora2.git@release-2014-12-17T15.53#egg=edx-ora2
  Updating /Users/singingwolfboy/.virtualenvs/edx-platform/src/edx-ora2 clone (to release-2014-12-17T15.53)
Collecting scipy==0.14.0 (from edx-ora2)
  Using cached scipy-0.14.0-cp27-none-macosx_10_6_intel.macosx_10_9_intel.macosx_10_9_x86_64.macosx_10_10_intel.macosx_10_10_x86_64.whl
Collecting django-extensions==1.2.5 (from edx-ora2)
  Using cached django-extensions-1.2.5.tar.gz
Collecting lxml==3.3.6 (from edx-ora2)
  Using cached lxml-3.3.6.tar.gz
    /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/distutils/dist.py:267: UserWarning: Unknown distribution option: 'bugtrack_url'
      warnings.warn(msg)
    Building lxml version 3.3.6.
    Building without Cython.
    Using build configuration of libxslt 1.1.28
Collecting voluptuous==0.8.5 (from edx-ora2)
  Using cached voluptuous-0.8.5.tar.gz
    WARNING: Could not locate pandoc, using Markdown long_description.
Collecting loremipsum==1.0.2 (from edx-ora2)
  Using cached loremipsum-1.0.2.tar.gz
    /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pkg_resources.py:2443: PEP440Warning: '0.6 (maintenance)' is being parsed as a legacy, non PEP 440, version. You may find odd behavior and sort order. In particular it will be sorted as less than 0.0. It is recommend to migrate to PEP 440 compatible versions.
      PEP440Warning,
    /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pkg_resources.py:2443: PEP440Warning: 'distribute (dev)' is being parsed as a legacy, non PEP 440, version. You may find odd behavior and sort order. In particular it will be sorted as less than 0.0. It is recommend to migrate to PEP 440 compatible versions.
      PEP440Warning,
    warning: install_lib: 'build/lib' does not exist -- no Python modules to install
    Installed /private/var/folders/0y/h8bkkgwj707_20n7lcfzdfjc0000gn/T/pip-build-HdgbgF/loremipsum/.eggs/distribute-0.7.3-py2.7.egg
Collecting pytz==2012h (from edx-ora2)
  Using cached pytz-2012h.tar.bz2
    /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/setuptools/dist.py:298: UserWarning: The version specified ('2012h') is an invalid version, this may not work as expected with newer versions of setuptools, pip, and PyPI. Please see PEP 440 for more details.
      "details." % self.metadata.version
Collecting nltk==2.0.4 (from edx-ora2)
  Using cached nltk-2.0.4.tar.gz
    Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.21.tar.gz
    Extracting in /var/folders/0y/h8bkkgwj707_20n7lcfzdfjc0000gn/T/tmpvygG41
    Now working in /var/folders/0y/h8bkkgwj707_20n7lcfzdfjc0000gn/T/tmpvygG41/distribute-0.6.21
    Building a Distribute egg in /private/var/folders/0y/h8bkkgwj707_20n7lcfzdfjc0000gn/T/pip-build-HdgbgF/nltk
    /private/var/folders/0y/h8bkkgwj707_20n7lcfzdfjc0000gn/T/pip-build-HdgbgF/nltk/distribute-0.6.21-py2.7.egg
Collecting dogapi==1.2.1 (from edx-ora2)
  Using cached dogapi-1.2.1.tar.gz
Collecting djangorestframework<2.4 (from edx-ora2)
  Using cached djangorestframework-2.3.14-py2.py3-none-any.whl
Collecting python-dateutil==2.1 (from edx-ora2)
  Using cached python-dateutil-2.1.tar.gz
Collecting python-memcached==1.48 (from edx-ora2)
  Using cached python-memcached-1.48.tar.gz
Collecting scikit-learn==0.12.1 (from edx-ora2)
  Using cached scikit-learn-0.12.1.tar.gz
    Partial import of sklearn during the build process.
    blas_opt_info:
      FOUND:
        extra_link_args = ['-Wl,-framework', '-Wl,Accelerate']
        define_macros = [('NO_ATLAS_INFO', 3)]
        extra_compile_args = ['-msse3', '-I/System/Library/Frameworks/vecLib.framework/Headers']
    build_src
    building library "libsvm-skl" sources
    building extension "sklearn.__check_build._check_build" sources
    building extension "sklearn.svm.libsvm" sources
    building extension "sklearn.svm.liblinear" sources
    building extension "sklearn.svm.libsvm_sparse" sources
    building extension "sklearn.datasets._svmlight_format" sources
    building extension "sklearn.cluster._hierarchical" sources
    building extension "sklearn.cluster._k_means" sources
    building extension "sklearn.ensemble._gradient_boosting" sources
    building extension "sklearn.utils.sparsetools._csgraph" sources
    building extension "sklearn.utils.arraybuilder" sources
    building extension "sklearn.utils.sparsefuncs" sources
    building extension "sklearn.utils.arrayfuncs" sources
    building extension "sklearn.utils.murmurhash" sources
    building extension "sklearn.utils.graph_shortest_path" sources
    building extension "sklearn.utils.seq_dataset" sources
    building extension "sklearn.utils.weight_vector" sources
    building extension "sklearn.neighbors.ball_tree" sources
    building extension "sklearn.tree._tree" sources
    building extension "sklearn._hmmc" sources
    building extension "sklearn.linear_model.cd_fast" sources
    building extension "sklearn.linear_model.sgd_fast" sources
    building extension "sklearn.utils.sparsetools._csgraph" sources
    building extension "sklearn.utils.arraybuilder" sources
    building extension "sklearn.utils.sparsefuncs" sources
    building extension "sklearn.utils.arrayfuncs" sources
    building extension "sklearn.utils.murmurhash" sources
    building extension "sklearn.utils.graph_shortest_path" sources
    building extension "sklearn.utils.seq_dataset" sources
    building extension "sklearn.utils.weight_vector" sources
    building data_files sources
    build_src: building npy-pkg config files
Collecting django-model-utils==1.4.0 (from edx-ora2)
  Using cached django-model-utils-1.4.0.tar.gz
Requirement already satisfied (use --upgrade to upgrade): django<1.5,>=1.4 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from edx-ora2)
Collecting django-celery==3.0.17 (from edx-ora2)
  Using cached django-celery-3.0.17.tar.gz
Collecting celery==3.0.19 (from edx-ora2)
  Using cached celery-3.0.19.tar.gz
    Upgrade: no old version found.
Requirement already satisfied (use --upgrade to upgrade): lazy==1.1 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from edx-ora2)
Collecting boto<3.0.0,>=2.13.0 (from edx-ora2)
  Using cached boto-2.34.0-py2.py3-none-any.whl
Collecting edx-submissions<0.1.0,>=0.0.8 (from edx-ora2)
  Using cached edx-submissions-0.0.8.tar.gz
Collecting defusedxml==0.4.1 (from edx-ora2)
  Using cached defusedxml-0.4.1.tar.gz
Collecting South==0.7.6 (from edx-ora2)
  Using cached South-0.7.6.tar.gz
Requirement already satisfied (use --upgrade to upgrade): numpy==1.6.2 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from edx-ora2)
Requirement already satisfied (use --upgrade to upgrade): six in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from django-extensions==1.2.5->edx-ora2)
Requirement already satisfied (use --upgrade to upgrade): setuptools>=0.6b1 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from voluptuous==0.8.5->edx-ora2)
Collecting distribute (from loremipsum==1.0.2->edx-ora2)
  Using cached distribute-0.7.3.zip
Requirement already satisfied (use --upgrade to upgrade): PyYAML>=3.09 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from nltk==2.0.4->edx-ora2)
Requirement already satisfied (use --upgrade to upgrade): simplejson>=2.0.9 in /Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages (from dogapi==1.2.1->edx-ora2)
Collecting decorator>=3.3.2 (from dogapi==1.2.1->edx-ora2)
  Using cached decorator-3.4.0.tar.gz
Collecting billiard<3.0,>=2.7.3.28 (from celery==3.0.19->edx-ora2)
  Using cached billiard-2.7.3.34.tar.gz
Collecting kombu<3.0,>=2.5.10 (from celery==3.0.19->edx-ora2)
  Using cached kombu-2.5.16.tar.gz
Collecting anyjson>=0.3.3 (from kombu<3.0,>=2.5.10->celery==3.0.19->edx-ora2)
  Using cached anyjson-0.3.3.tar.gz
Collecting amqp<1.1.0,>=1.0.13 (from kombu<3.0,>=2.5.10->celery==3.0.19->edx-ora2)
  Using cached amqp-1.0.13.tar.gz
Exception:
Traceback (most recent call last):
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/commands/install.py", line 347, in run
    root=options.root_path,
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/req/req_set.py", line 497, in install
    and req.installed_version in distribute_req):
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/_vendor/pkg_resources.py", line 2851, in __contains__
    return self.specifier.contains(item, prereleases=True)
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/_vendor/packaging/specifiers.py", line 651, in contains
    item = parse(item)
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/_vendor/packaging/version.py", line 41, in parse
    return Version(version)
  File "/Users/singingwolfboy/.virtualenvs/edx-platform/lib/python2.7/site-packages/pip/_vendor/packaging/version.py", line 202, in __init__
    match = self._regex.search(version)
TypeError: expected string or buffer
```

Fixes #2272
Returning None if distutils hasn't been installed will make the test `req.installed_version in distribute_req` fail with a TypeError



Discussion:

Thank you.

Is this an issue that should be contributed to the distribute package instead of providing the workaround here in pip?

Edit: Nevermind I forsee this can be an issue if the package doesn't properly specify it's version so perhaps this is still the right place to fix this regression.

:+1: @farvour , pip code is faulty here.

Nonetheless, we can clearly see this part of the code in pip is to workaround some behavior with distutils too. I'll see what I can do there.


Resolving patch:
diff --git a/pip/req/req_set.py b/pip/req/req_set.py
--- a/pip/req/req_set.py
+++ b/pip/req/req_set.py
@@ -494,6 +494,7 @@ def install(self, install_options, global_options=(), *args, **kwargs):
         distribute_req = pkg_resources.Requirement.parse("distribute>=0.7")
         for req in to_install:
             if (req.name == 'distribute'
+                    and req.installed_version is not None
                     and req.installed_version in distribute_req):
                 to_install.remove(req)
                 to_install.append(req)


Test patch: