Repository: numpy/numpy

Issue:
Numpy 1.16.1 fails to run tests
Newly released NumPy 1.16.1 built and installed from source fails to run tests:

> python3 setup.py build
> sudo python3 setup.py install

```
import numpy
numpy.test('full')
NumPy version 1.16.1
NumPy relaxed strides checking option: True

================================================================================================================== ERRORS ===================================================================================================================
_____________________________________________________________________________________________ ERROR collecting core/_multiarray_module_test.py ______________________________________________________________________________________________
import file mismatch:
imported module 'numpy.core._multiarray_module_test' has this __file__ attribute:
  /usr/local/lib/python3.6/dist-packages/numpy-1.16.1-py3.6-linux-x86_64.egg/numpy/core/_multiarray_module_test.cpython-36m-x86_64-linux-gnu.so
which is not the same as the test file we want to collect:
  /usr/local/lib/python3.6/dist-packages/numpy-1.16.1-py3.6-linux-x86_64.egg/numpy/core/_multiarray_module_test.py
HINT: remove __pycache__ / .pyc files and/or use a unique basename for your test file modules
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 errors during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
1 error in 4.18 seconds
False
```


Discussion:
Where did the file `_multiarray_module_test.py` (with the `py` suffix) come from? It is not part of the numpy code base.
The use of `sudo` to install numpy  (`sudo python3 setup.py install`) will inevitably cause problems as well. Best practices strongly advise using a virtualenv  and `pip install .` or at least use the `--user` flag `pip install --user .` rather than `setup.py` directly
@mattip, sorry I can't tell where _multiarray_module_test.py comes from as I already downgraded to 1.16.0 where there is no such problem.
Actually the problem with building and installing from source was detected at first in docker container, running default root user.
And I have one more question - what is the way of system-wide building/installation of NumPy?
Since we started talking about failing tests, I have another question regarding one of them. When I install numpy 1.16.1 (system-wide from the repository) and run the tests, `test_multiarray_module()` fails. The documentation says  `# The expectation is that import will fail.`  Nevertheless,  the error I get is `PermissionError: [Errno 13] Permission denied: '/usr/lib/python3.7/site-packages/numpy/core/multiarray.cpython-37m-x86_64-linux-gnu.so'`
Is it what one should expect?
@V-Kh `numpy/core/multiarray.cpython-37m-x86_64-linux-gnu.so` is not part of 1.16.x. The problem is that it is already in your system library. How are you installing numpy? I have noticed that with some methods of installation you will need to delete the previous site-package, otherwise old files will remain after installation, with new files copied into the same directory.
@V-KH You will also want to clean the repo before install, `git -xdf > /dev/null` to make sure no old files are hanging around.

> @V-Kh `numpy/core/multiarray.cpython-37m-x86_64-linux-gnu.so` is not part of 1.16.x. The problem is that it is already in your system library. How are you installing numpy? I have noticed that with some methods of installation you will need to delete the previous site-package, otherwise old files will remain after installation, with new files copied into the same directory.

@charris This is awkward since I use pacman /makepkg to install system packages and thus should not have any old files. OK, I'm going to uninstall it and delete all the remainders manually; I didn't do that before due to other packages that depend on numpy. 

P.S. I use only stable releases to avoid any extra problems. 

@V-Kh That sounds painful. If you are installing from a local repo, might want to try cleaning that first.
@charris OK, this is actually quite funny.

After uninstalling numpy I couldn't find any leftovers. However, after installing it again I get exactly the same problem:
[test.log](https://github.com/numpy/numpy/files/2823761/test.log)
Files in that dir:
[files.log](https://github.com/numpy/numpy/files/2823770/files.log)
[That is how we build the package.](https://git.archlinux.org/svntogit/packages.git/tree/trunk/PKGBUILD?h=packages/python-numpy)
However, since I use pacman and install prebuilt packages the problem can be on the maintainer's side. 
Hmm..., there is definitely some funny stuff going on, I am see some new local weirdness also.
Investigating...
I think the failing test in the test.log is just a bad test. @mattip Could you take a look? I'd be happy to remove the test altogether...
#12916 may be related.
I can either try to fix the test or remove it, whichever you wish.
@mattip I'm inclined towards removal, the gain doesn't seem worth the complication. Here it looks like installing in a system directory leads to permission problems, and I see pytest problems when installing in `.local` due to the naming. Oddly enough, runtests works fine, I didn't test with a local install before release.

Resolving patch:
diff --git a/numpy/core/setup.py b/numpy/core/setup.py
--- a/numpy/core/setup.py
+++ b/numpy/core/setup.py
@@ -962,14 +962,6 @@ def generate_umath_c(ext, build_dir):
     config.add_extension('_operand_flag_tests',
                     sources=[join('src', 'umath', '_operand_flag_tests.c.src')])
 
-    #######################################################################
-    #                        _multiarray_module_test module               #
-    #######################################################################
-
-    config.add_extension('_multiarray_module_test',
-                    sources=[join('src', 'multiarray',
-                                         '_multiarray_module_test.c')])
-
     config.add_data_dir('tests')
     config.add_data_dir('tests/data')
 


Test patch:
diff --git a/numpy/core/src/multiarray/_multiarray_module_test.c b/numpy/core/src/multiarray/_multiarray_module_test.c
deleted file mode 100644
--- a/numpy/core/src/multiarray/_multiarray_module_test.c
+++ /dev/null
@@ -1,129 +0,0 @@
-#include "Python.h"
-
-/*
- * This is a dummy module. It will be used to ruin the import of multiarray
- * during testing. It exports two entry points, one to make the build happy,
- * and a multiarray one for the actual test. The content of the module is
- * irrelevant to the test.
- *
- * The code is from
- * https://docs.python.org/3/howto/cporting.html
- * or
- * https://github.com/python/cpython/blob/v3.7.0/Doc/howto/cporting.rst
- */
-
-#if defined _WIN32 || defined __CYGWIN__ || defined __MINGW32__
-  #if defined __GNUC__ || defined __clang__
-    #define DLL_PUBLIC __attribute__ ((dllexport))
-  #else
-    #define DLL_PUBLIC __declspec(dllexport)
-  #endif
-#elif defined __GNUC__  || defined __clang__
-  #define DLL_PUBLIC __attribute__ ((visibility ("default")))
-#else
-    /* Enhancement: error now instead ? */
-    #define DLL_PUBLIC
-#endif
-
-struct module_state {
-    PyObject *error;
-};
-
-#if PY_MAJOR_VERSION >= 3
-#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
-#else
-#define GETSTATE(m) (&_state)
-static struct module_state _state;
-#endif
-
-static PyObject *
-error_out(PyObject *m) {
-    struct module_state *st = GETSTATE(m);
-    PyErr_SetString(st->error, "something bad happened");
-    return NULL;
-}
-
-static PyMethodDef multiarray_methods[] = {
-    {"error_out", (PyCFunction)error_out, METH_NOARGS, NULL},
-    {NULL, NULL}
-};
-
-#if PY_MAJOR_VERSION >= 3
-
-static int multiarray_traverse(PyObject *m, visitproc visit, void *arg) {
-    Py_VISIT(GETSTATE(m)->error);
-    return 0;
-}
-
-static int multiarray_clear(PyObject *m) {
-    Py_CLEAR(GETSTATE(m)->error);
-    return 0;
-}
-
-
-static struct PyModuleDef moduledef = {
-        PyModuleDef_HEAD_INIT,
-        "multiarray",
-        NULL,
-        sizeof(struct module_state),
-        multiarray_methods,
-        NULL,
-        multiarray_traverse,
-        multiarray_clear,
-        NULL
-};
-
-#define INITERROR return NULL
-
-DLL_PUBLIC PyObject *
-PyInit_multiarray(void)
-
-#else
-#define INITERROR return
-
-void
-DLL_PUBLIC initmultiarray(void)
-#endif
-{
-#if PY_MAJOR_VERSION >= 3
-    PyObject *module = PyModule_Create(&moduledef);
-#else
-    PyObject *module = Py_InitModule("multiarray", multiarray_methods);
-#endif
-    struct module_state *st;
-    if (module == NULL)
-        INITERROR;
-    st = GETSTATE(module);
-
-    st->error = PyErr_NewException("multiarray.Error", NULL, NULL);
-    if (st->error == NULL) {
-        Py_DECREF(module);
-        INITERROR;
-    }
-
-#if PY_MAJOR_VERSION >= 3
-    return module;
-#endif
-}
-
-/*
- * Define a dummy entry point to make MSVC happy
- * Python's build system will export this function automatically
- */
-#if PY_MAJOR_VERSION >= 3
-
-PyObject *
-PyInit__multiarray_module_test(void)
-{
-    return PyInit_multiarray();
-}
-
-#else
-
-void
-init_multiarray_module_test(void)
-{
-    initmultiarray();
-}
-
-#endif                                                    
diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py
--- a/numpy/core/tests/test_multiarray.py
+++ b/numpy/core/tests/test_multiarray.py
@@ -8093,43 +8093,3 @@ def test_getfield():
     pytest.raises(ValueError, a.getfield, 'uint8', -1)
     pytest.raises(ValueError, a.getfield, 'uint8', 16)
     pytest.raises(ValueError, a.getfield, 'uint64', 0)
-
-def test_multiarray_module():
-    # gh-12736
-    # numpy 1.16 replaced the multiarray and umath c-extension modules with
-    # a single _multiarray_umath one. For backward compatibility, it added a
-    # pure-python multiarray.py and umath.py shim so people can still do
-    # from numpy.core.multirarray import something-public-api
-    # It turns out pip can leave old pieces of previous versions of numpy
-    # around when installing a newer version. If the old c-extension modules
-    # are found, they will be given precedence over the new pure-python ones.
-    #
-    # This test copies a multiarray c-extension in parallel with the pure-
-    # python one, and starts another python interpreter to load multiarray.
-    # The expectation is that import will fail.
-    import subprocess, shutil
-    core_dir = os.path.dirname(np.core.multiarray.__file__)
-    cextension = np.core._multiarray_umath.__file__
-    testfile = cextension.replace('_multiarray_umath', '_multiarray_module_test')
-    badfile = cextension.replace('_multiarray_umath', 'multiarray')
-    assert not os.path.exists(badfile), '%s exists, this numpy ' \
-                                    'installation is faulty' % badfile
-    try:
-        shutil.copy(testfile, badfile)
-        p = subprocess.Popen([sys.executable, '-c', 'import numpy' ],
-                stdout=subprocess.PIPE, stderr=subprocess.PIPE,
-                env=os.environ.copy())
-        stdout, stderr = p.communicate()
-        r = p.wait()
-        #print(stdout.decode())
-        #print(stderr.decode())
-        assert r != 0
-        assert b'ImportError' in stderr
-    finally:
-        if os.path.exists(badfile):
-            try:
-                # can this fail?
-                os.remove(badfile)
-            except:
-                print("Could not remove %s, remove it by hand" % badfile)
-                raise