# Buggy code
def get_ethtool_macro():
    # see: https://github.com/giampaolo/psutil/issues/659
    from distutils.unixccompiler import UnixCCompiler
    from distutils.errors import CompileError

    with tempfile.NamedTemporaryFile(suffix=".c", delete=False, mode="wt") as f:
        f.write("#include <linux/ethtool.h>")
    atexit.register(os.remove, f.name)
    compiler = UnixCCompiler()
    try:
        with captured_output("stderr"):
            with captured_output("stdout"):
                compiler.compile([f.name])
    except CompileError:
        return ("PSUTIL_ETHTOOL_MISSING_TYPES", 1)
    else:
        return None


# Traceback (TypeError)
root@ip-10-0-2-186:/home/ubuntu# easy_install psutil==3.2.0
Searching for psutil==3.2.0
Reading https://pypi.python.org/simple/psutil/
Best match: psutil 3.2.0
Downloading https://pypi.python.org/packages/source/p/psutil/psutil-3.2.0.tar.gz#md5=224c2bb432003d74d022ced4409df1bc
Processing psutil-3.2.0.tar.gz
Writing /tmp/easy_install-IRRDCd/psutil-3.2.0/setup.cfg
Running psutil-3.2.0/setup.py -q bdist_egg --dist-dir /tmp/easy_install-IRRDCd/psutil-3.2.0/egg-dist-tmp-3ewURn
Traceback (most recent call last):
File "/usr/bin/easy_install", line 9, in <module>
load_entry_point('setuptools==3.3', 'console_scripts', 'easy_install')()
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 1963, in main
with_ei_usage(lambda:
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 1950, in with_ei_usage
return f()
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 1967, in <lambda>
distclass=DistributionWithoutHelpCommands, **kw
File "/usr/lib/python2.7/distutils/core.py", line 151, in setup
dist.run_commands()
File "/usr/lib/python2.7/distutils/dist.py", line 953, in run_commands
self.run_command(cmd)
File "/usr/lib/python2.7/distutils/dist.py", line 972, in run_command
cmd_obj.run()
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 381, in run
self.easy_install(spec, not self.no_deps)
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 616, in easy_install
return self.install_item(spec, dist.location, tmpdir, deps)
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 646, in install_item
dists = self.install_eggs(spec, download, tmpdir)
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 834, in install_eggs
return self.build_and_install(setup_script, setup_base)
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 1040, in build_and_install
self.run_setup(setup_script, setup_base, args)
File "/usr/lib/python2.7/dist-packages/setuptools/command/easy_install.py", line 1025, in run_setup
run_setup(setup_script, args)
File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 50, in run_setup
lambda: execfile(
File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 100, in run
return func()
File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 52, in <lambda>
{'__file__':setup_script, '__name__':'__main__'}
File "setup.py", line 150, in <module>
File "setup.py", line 144, in get_ethtool_macro
File "/usr/lib/python2.7/distutils/ccompiler.py", line 566, in compile
depends, extra_postargs)
File "/usr/lib/python2.7/distutils/ccompiler.py", line 362, in _setup_compile
self.mkpath(os.path.dirname(obj))
File "/usr/lib/python2.7/distutils/ccompiler.py", line 881, in mkpath
mkpath(name, mode, dry_run=self.dry_run)
File "/usr/lib/python2.7/distutils/dir_util.py", line 68, in mkpath
log.info("creating %s", head)
File "/usr/lib/python2.7/distutils/log.py", line 40, in info
self._log(INFO, msg, args)
File "/usr/lib/python2.7/distutils/log.py", line 30, in _log
stream.write('%s\n' % msg)
TypeError: unicode argument expected, got 'str'
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 124, in wrap
return original(path,*args,**kw)
OSError: [Errno 2] No such file or directory: '/tmp/easy_install-IRRDCd/psutil-3.2.0/temp/tmpzcgy03.c'
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/dist-packages/setuptools/sandbox.py", line 124, in wrap
return original(path,*args,**kw)
OSError: [Errno 2] No such file or directory: '/tmp/easy_install-IRRDCd/psutil-3.2.0/temp/tmpzcgy03.c'

# Fixed code
def get_ethtool_macro():
    # see: https://github.com/giampaolo/psutil/issues/659
    from distutils.unixccompiler import UnixCCompiler
    from distutils.errors import CompileError

    with tempfile.NamedTemporaryFile(suffix=".c", delete=False, mode="wt") as f:
        f.write("#include <linux/ethtool.h>")

    @atexit.register
    def on_exit():
        try:
            os.remove(f.name)
        except OSError:
            pass

    compiler = UnixCCompiler()
    try:
        with silenced_output("stderr"):
            with silenced_output("stdout"):
                compiler.compile([f.name])
    except CompileError:
        return ("PSUTIL_ETHTOOL_MISSING_TYPES", 1)
    else:
        return None