# Buggy code
def setup_package():
    # Perform 2to3 if needed
    local_path = os.path.dirname(os.path.abspath(sys.argv[0]))
    src_path = local_path

    if sys.version_info[0] == 3:
        src_path = os.path.join(local_path, "build", "py3k")
        sys.path.insert(0, os.path.join(local_path, "tools"))
        import py3tool

        print("Converting to Python3 via 2to3...")
        py3tool.sync_2to3("numpy", os.path.join(src_path, "numpy"))

        site_cfg = os.path.join(local_path, "site.cfg")
        if os.path.isfile(site_cfg):
            shutil.copy(site_cfg, src_path)

    old_path = os.getcwd()
    os.chdir(src_path)
    sys.path.insert(0, src_path)

    # Rewrite the version file everytime
    write_version_py()

    # Run build
    from numpy.distutils.core import setup

    try:
        setup(
            name=NAME,
            maintainer=MAINTAINER,
            maintainer_email=MAINTAINER_EMAIL,
            description=DESCRIPTION,
            long_description=LONG_DESCRIPTION,
            url=URL,
            download_url=DOWNLOAD_URL,
            license=LICENSE,
            classifiers=CLASSIFIERS,
            author=AUTHOR,
            author_email=AUTHOR_EMAIL,
            platforms=PLATFORMS,
            configuration=configuration,
        )
    finally:
        del sys.path[0]
        os.chdir(old_path)
    return


# Traceback (AssertionError)
x = np.array([-1j], '<c8')
>>> x.tostring().encode('hex')
'00000000000080bf'
# This is a little-endian representation, in the order (real, imag)

# When I swap the whole array, it swaps each of the (real, imag) parts
separately
>>> y = x.byteswap()
>>> y.tostring().encode('hex')
'00000000bf800000'
# and this round-trips fine
>>> z = np.fromstring(y.tostring(), dtype='>c8')
>>> assert z[0] == -1j
>>>

# When I swap the scalar, it seems to swap the entire 8 bytes
>>> y = x[0].byteswap()
>>> y.tostring().encode('hex')
'bf80000000000000'
# ...and this doesn't round-trip
>>> z = np.fromstring(y.tostring(), dtype='>c8')
>>> assert z[0] == -1j
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AssertionError
>>>

# Fixed code
def setup_package():
    # Perform 2to3 if needed
    local_path = os.path.dirname(os.path.abspath(sys.argv[0]))
    src_path = local_path

    if sys.version_info[0] == 3:
        src_path = os.path.join(local_path, "build", "py3k")
        sys.path.insert(0, os.path.join(local_path, "tools"))
        import py3tool

        print("Converting to Python3 via 2to3...")
        py3tool.sync_2to3("numpy", os.path.join(src_path, "numpy"))

        site_cfg = os.path.join(local_path, "site.cfg")
        if os.path.isfile(site_cfg):
            shutil.copy(site_cfg, src_path)

        # Ugly hack to make pip work with Python 3, see #1857.
        # Explanation: pip messes with __file__ which interacts badly with the
        # change in directory due to the 2to3 conversion.  Therefore we restore
        # __file__ to what it would have been otherwise.
        global __file__
        __file__ = os.path.join(os.curdir, os.path.basename(__file__))
        if "--egg-base" in sys.argv:
            # Change pip-egg-info entry to absolute path, so pip can find it
            # after changing directory.
            idx = sys.argv.index("--egg-base")
            if sys.argv[idx + 1] == "pip-egg-info":
                sys.argv[idx + 1] = os.path.join(local_path, "pip-egg-info")

    old_path = os.getcwd()
    os.chdir(src_path)
    sys.path.insert(0, src_path)

    # Rewrite the version file everytime
    write_version_py()

    # Run build
    from numpy.distutils.core import setup

    try:
        setup(
            name=NAME,
            maintainer=MAINTAINER,
            maintainer_email=MAINTAINER_EMAIL,
            description=DESCRIPTION,
            long_description=LONG_DESCRIPTION,
            url=URL,
            download_url=DOWNLOAD_URL,
            license=LICENSE,
            classifiers=CLASSIFIERS,
            author=AUTHOR,
            author_email=AUTHOR_EMAIL,
            platforms=PLATFORMS,
            configuration=configuration,
        )
    finally:
        del sys.path[0]
        os.chdir(old_path)
    return