Repository: pypa/pip

Issue:
Fix per-requirement global options. fixes #3830
This fixes the problem described in #3830 but I am not sure if that problem should be fixed.
However, something is definitely wrong with per-requirement `--global-option`'s:
1) If it is intended to add a custom option to one and only one requirement, then #3830 needs to be solved, but the name "global option" is a bit confusing because it is not actually global, it's per-requirement.
2) If global options are intended to be applied to all requirements, then they should be listed separately from any particular requirement and also there is a bug, because global options are applied only to requirements that are listed after. So global options are not really global again.

In this pull request I address the first problem and make global options from different requirements not to interfere with each other.

<!-- Reviewable:start -->
---
This change is [<img src="https://reviewable.io/review_button.svg" height="34" align="absmiddle" alt="Reviewable"/>](https://reviewable.io/reviews/pypa/pip/4217)
<!-- Reviewable:end -->

Requirements File Providing global-option to Incorrect Requirements
- Pip version: 8.1.2
- Python version: 3.5.0
- Operating System: Travis
### Description:

A requirements file containing a --global-option passes that option to requirements other than that on which it was specified. This file will therefore fail:

```
pyyaml==3.11 --global-option='--without-libyaml'
requests==2.10.0
```

With the error:

```
Running setup.py install for requests ... error
    Complete output from command /home/travis/virtualenv/python3.5.0/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-aqgoiffd/requests/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" --without-libyaml install --record /tmp/pip-o_9poof_-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/travis/virtualenv/python3.5.0/include/site/python3.5/requests:
    usage: -c [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
       or: -c --help [cmd1 cmd2 ...]
       or: -c --help-commands
       or: -c cmd --help

    error: option --without-libyaml not recognized
```
### What I've Run:

I have created a small repository to reproduce the error:

https://github.com/jayfo/pip-requirements-bug

The current state of the repository is trivial. I have submitted two pull requests.

The pull which lists the global-option requirement first, as above, fails:

https://github.com/jayfo/pip-requirements-bug/pull/2

Swapping the order is enough in this case, as this passes:

https://github.com/jayfo/pip-requirements-bug/pull/1



Discussion:
You are correct in that the first option is the correct one, the name ``--global-option`` means it applies to every invocation of ``setup.py`` *for that requirement*, not just the ``setup.py install`` command.
This change looks good to me, however it needs a news file entry, you can see how to do that in our [documentation](https://pip.pypa.io/en/latest/development/#adding-a-news-entry). Since this fixes 3830 you can use the filename ``news/3830.bugfix``.
Related to #3763

Resolving patch:
diff --git a/pip/req/req_install.py b/pip/req/req_install.py
--- a/pip/req/req_install.py
+++ b/pip/req/req_install.py
@@ -720,8 +720,9 @@ def match_markers(self, extras_requested=None):
         else:
             return True
 
-    def install(self, install_options, global_options=[], root=None,
+    def install(self, install_options, global_options=None, root=None,
                 prefix=None):
+        global_options = global_options if global_options is not None else []
         if self.editable:
             self.install_editable(
                 install_options, global_options, prefix=prefix)


Test patch: