This resolves #2189.

> build isolation would be a bit difficult to use in our case, as for instance when building scipy we need the patched numpy on the host and not the numpy version specified in pyproject.toml (which would be unpatched)

This is indeed the case, certain packages cannot be isolated. My strategy is to make a list of packages that shouldn't be isolated and add symlinks from the isolated build environment into the `.artifacts` directory to "unisolate" them. Then we remove the unisolated package requirements from the list of packages to install, in case pesky constraints aren't satisfied. In particular, packages that expect to be used with `pypa/build` often feel free to put very specific constraints on their build dependencies (often asking them to be == to a particular version). Specific version constraints is good for build reproducibility and with build isolation doesn't cost anything. So we just ignore the constraints. Hopefully nothing goes wrong.

In particular, any package that does stuff both at build time and at runtime and requires synchronization between the build time and run time environments needs the unisolation. This includes cffi with `_cffi_backend.so`, and of course numpy and scipy. pycparser needs to be unisolated because it is a dependency of cffi.

Currently I have also unisolated pythran and cython, though these are build time only tools and do not really need to be unisolated. Cython I unisolated specifically because numcodecs needs it but it isn't in the numcodecs build dependencies. Pythran I unisolated because of a problem with the scipy build which I don't fully understand (some problem with long double feature detection). I believe both of these are removable, particularly once #2132 is finished.
Use pypa/build. This resolves #2189.

> build isolation would be a bit difficult to use in our case, as for instance when building scipy we need the patched numpy on the host and not the numpy version specified in pyproject.toml (which would be unpatched)

This is indeed the case, certain packages cannot be isolated. My strategy is to make a list of packages that shouldn't be isolated and add symlinks from the isolated build environment into the `.artifacts` directory to "unisolate" them. Then we remove the unisolated package requirements from the list of packages to install, in case pesky constraints aren't satisfied. In particular, packages that expect to be used with `pypa/build` often feel free to put very specific constraints on their build dependencies (often asking them to be == to a particular version). Specific version constraints is good for build reproducibility and with build isolation doesn't cost anything. So we just ignore the constraints. Hopefully nothing goes wrong.

In particular, any package that does stuff both at build time and at runtime and requires synchronization between the build time and run time environments needs the unisolation. This includes cffi with `_cffi_backend.so`, and of course numpy and scipy. pycparser needs to be unisolated because it is a dependency of cffi.

Currently I have also unisolated pythran and cython, though these are build time only tools and do not really need to be unisolated. Cython I unisolated specifically because numcodecs needs it but it isn't in the numcodecs build dependencies. Pythran I unisolated because of a problem with the scipy build which I don't fully understand (some problem with long double feature detection). I believe both of these are removable, particularly once #2132 is finished.
from pyodide_build.testing import run_in_pyodide


@run_in_pyodide(packages=["wrapt"])
def test_wrapt():
    import inspect
    import unittest

    import wrapt

    @wrapt.decorator
    def passthru_decorator(wrapped, instance, args, kwargs):
        return wrapped(*args, **kwargs)

    def function1(arg):
        """documentation"""
        return arg

    function1o = function1
    function1d = passthru_decorator(function1)
    assert function1d is not function1o

    class TestNamingFunction(unittest.TestCase):
        def test_object_name(self):
            # Test preservation of function __name__ attribute.

            self.assertEqual(function1d.__name__, function1o.__name__)

        def test_object_qualname(self):
            # Test preservation of function __qualname__ attribute.

            try:
                __qualname__ = function1o.__qualname__
            except AttributeError:
                pass
            else:
                self.assertEqual(function1d.__qualname__, __qualname__)

        def test_module_name(self):
            # Test preservation of function __module__ attribute.

            self.assertEqual(function1d.__module__, __name__)

        def test_doc_string(self):
            # Test preservation of function __doc__ attribute.

            self.assertEqual(function1d.__doc__, function1o.__doc__)

        def test_argspec(self):
            # Test preservation of function argument specification.

            function1o_argspec = inspect.getargspec(function1o)
            function1d_argspec = inspect.getargspec(function1d)
            self.assertEqual(function1o_argspec, function1d_argspec)

        def test_isinstance(self):
            # Test preservation of isinstance() checks.

            self.assertTrue(isinstance(function1d, type(function1o)))

    class TestCallingFunction(unittest.TestCase):
        def test_call_function(self):
            _args = (1, 2)
            _kwargs = {"one": 1, "two": 2}

            @wrapt.decorator
            def _decorator(wrapped, instance, args, kwargs):
                self.assertEqual(instance, None)
                self.assertEqual(args, _args)
                self.assertEqual(kwargs, _kwargs)
                return wrapped(*args, **kwargs)

            @_decorator
            def _function(*args, **kwargs):
                return args, kwargs

            result = _function(*_args, **_kwargs)

            self.assertEqual(result, (_args, _kwargs))

    # Run tests
    with unittest.TestCase().assertRaisesRegex(SystemExit, "False"):
        unittest.main()

from pyodide_build.testing import run_in_pyodide


@run_in_pyodide(packages=["wrapt"])
def test_wrapt():
    import inspect
    import unittest

    import wrapt

    @wrapt.decorator
    def passthru_decorator(wrapped, instance, args, kwargs):
        return wrapped(*args, **kwargs)

    def function1(arg):
        """documentation"""
        return arg

    function1o = function1
    function1d = passthru_decorator(function1)
    assert function1d is not function1o

    class TestNamingFunction(unittest.TestCase):
        def test_object_name(self):
            # Test preservation of function __name__ attribute.

            self.assertEqual(function1d.__name__, function1o.__name__)

        def test_object_qualname(self):
            # Test preservation of function __qualname__ attribute.

            try:
                __qualname__ = function1o.__qualname__
            except AttributeError:
                pass
            else:
                self.assertEqual(function1d.__qualname__, __qualname__)

        def test_module_name(self):
            # Test preservation of function __module__ attribute.

            self.assertEqual(function1d.__module__, __name__)

        def test_doc_string(self):
            # Test preservation of function __doc__ attribute.

            self.assertEqual(function1d.__doc__, function1o.__doc__)

        def test_argspec(self):
            # Test preservation of function argument specification.

            function1o_argspec = inspect.getargspec(function1o)
            function1d_argspec = inspect.getargspec(function1d)
            self.assertEqual(function1o_argspec, function1d_argspec)

        def test_isinstance(self):
            # Test preservation of isinstance() checks.

            self.assertTrue(isinstance(function1d, type(function1o)))

    class TestCallingFunction(unittest.TestCase):
        def test_call_function(self):
            _args = (1, 2)
            _kwargs = {"one": 1, "two": 2}

            @wrapt.decorator
            def _decorator(wrapped, instance, args, kwargs):
                self.assertEqual(instance, None)
                self.assertEqual(args, _args)
                self.assertEqual(kwargs, _kwargs)
                return wrapped(*args, **kwargs)

            @_decorator
            def _function(*args, **kwargs):
                return args, kwargs

            result = _function(*_args, **_kwargs)

            self.assertEqual(result, (_args, _kwargs))

    # Run tests
    with unittest.TestCase().assertRaisesRegex(SystemExit, "False"):
        unittest.main()
