Patchwork [1,of,6] setup: split "hasfunction" to test arbitrary code

login
register
mail settings
Submitter Jun Wu
Date March 21, 2017, 12:04 a.m.
Message ID <272f2d2a56cad143c899.1490054674@localhost.localdomain>
Download mbox | patch
Permalink /patch/19479/
State Accepted
Headers show

Comments

Jun Wu - March 21, 2017, 12:04 a.m.
# HG changeset patch
# User Jun Wu <quark@fb.com>
# Date 1490048888 25200
#      Mon Mar 20 15:28:08 2017 -0700
# Node ID 272f2d2a56cad143c899d4beb4b36610f68e6310
# Parent  44c591f634584c721778c5a77edeb04cd919ac43
# Available At https://bitbucket.org/quark-zju/hg-draft
#              hg pull https://bitbucket.org/quark-zju/hg-draft -r 272f2d2a56ca
setup: split "hasfunction" to test arbitrary code

The next patch wants to test include files.
Ryan McElroy - March 21, 2017, 6:07 p.m.
On 3/21/17 12:04 AM, Jun Wu wrote:
> # HG changeset patch
> # User Jun Wu <quark@fb.com>
> # Date 1490048888 25200
> #      Mon Mar 20 15:28:08 2017 -0700
> # Node ID 272f2d2a56cad143c899d4beb4b36610f68e6310
> # Parent  44c591f634584c721778c5a77edeb04cd919ac43
> # Available At https://urldefense.proofpoint.com/v2/url?u=https-3A__bitbucket.org_quark-2Dzju_hg-2Ddraft&d=DwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=Jw8rundaE7TbmqBYd1txIQ&m=VwA1xy5i20etAATQYp9kmp3q6Wlza8LdY_xR9E7o6L0&s=4tA_HLugojggt3qE_LszOptNukHRTTSWv8it6P3XdJk&e=
> #              hg pull https://urldefense.proofpoint.com/v2/url?u=https-3A__bitbucket.org_quark-2Dzju_hg-2Ddraft&d=DwIGaQ&c=5VD0RTtNlTh3ycd41b3MUw&r=Jw8rundaE7TbmqBYd1txIQ&m=VwA1xy5i20etAATQYp9kmp3q6Wlza8LdY_xR9E7o6L0&s=4tA_HLugojggt3qE_LszOptNukHRTTSWv8it6P3XdJk&e=  -r 272f2d2a56ca
> setup: split "hasfunction" to test arbitrary code
>
> The next patch wants to test include files.
>
> diff --git a/setup.py b/setup.py
> --- a/setup.py
> +++ b/setup.py
> @@ -95,15 +95,11 @@ if os.name == 'nt':
>       scripts.append('contrib/win32/hg.bat')
>   
> -# simplified version of distutils.ccompiler.CCompiler.has_function
> -# that actually removes its temporary files.
> -def hasfunction(cc, funcname):
> +def cancompile(cc, code):
>       tmpdir = tempfile.mkdtemp(prefix='hg-install-')
>       devnull = oldstderr = None
>       try:
> -        fname = os.path.join(tmpdir, 'funcname.c')
> +        fname = os.path.join(tmpdir, 'testcomp.c')
>           f = open(fname, 'w')
> -        f.write('int main(void) {\n')
> -        f.write('    %s();\n' % funcname)
> -        f.write('}\n')
> +        f.write(code)
>           f.close()
>           # Redirect stderr to /dev/null to hide any error messages
> @@ -126,4 +122,10 @@ def hasfunction(cc, funcname):
>           shutil.rmtree(tmpdir)
>   
> +# simplified version of distutils.ccompiler.CCompiler.has_function
> +# that actually removes its temporary files.
> +def hasfunction(cc, funcname):
> +    code = 'int main(void) { %s(); }\n' % funcname
> +    return cancompile(cc, code)
> +

Can you "minimize" this patch by splitting this the "other way" (eg put 
hasfunction on top of cancompile)?

Otherwise, this change seems fine to me.

>   # py2exe needs to be installed to work
>   try:
>

Patch

diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -95,15 +95,11 @@  if os.name == 'nt':
     scripts.append('contrib/win32/hg.bat')
 
-# simplified version of distutils.ccompiler.CCompiler.has_function
-# that actually removes its temporary files.
-def hasfunction(cc, funcname):
+def cancompile(cc, code):
     tmpdir = tempfile.mkdtemp(prefix='hg-install-')
     devnull = oldstderr = None
     try:
-        fname = os.path.join(tmpdir, 'funcname.c')
+        fname = os.path.join(tmpdir, 'testcomp.c')
         f = open(fname, 'w')
-        f.write('int main(void) {\n')
-        f.write('    %s();\n' % funcname)
-        f.write('}\n')
+        f.write(code)
         f.close()
         # Redirect stderr to /dev/null to hide any error messages
@@ -126,4 +122,10 @@  def hasfunction(cc, funcname):
         shutil.rmtree(tmpdir)
 
+# simplified version of distutils.ccompiler.CCompiler.has_function
+# that actually removes its temporary files.
+def hasfunction(cc, funcname):
+    code = 'int main(void) { %s(); }\n' % funcname
+    return cancompile(cc, code)
+
 # py2exe needs to be installed to work
 try: