<commit_before># Tests that require installed backends go into
# sympy/test_external/test_autowrap

import os
import tempfile
import shutil
from StringIO import StringIO

from sympy.utilities.autowrap import autowrap, binary_function, CythonCodeWrapper,\
        ufuncify
from sympy.utilities.codegen import Routine, CCodeGen, CodeGenArgumentListError
from sympy.utilities.pytest import raises
from sympy.core import symbols, Eq

def get_string(dump_fn, routines, prefix="file", header=False, empty=False):
    """Wrapper for dump_fn. dump_fn writes its results to a stream object and
       this wrapper returns the contents of that stream as a string. This
       auxiliary function is used by many tests below.

       The header and the empty lines are not generator to facilitate the
       testing of the output.
    """
    output = StringIO()
    dump_fn(routines, output, prefix, header, empty)
    source = output.getvalue()
    output.close()
    return source

def test_cython_wrapper_scalar_function():
    x,y,z = symbols('xyz')
    expr = (x+y)*z
    routine = Routine("test", expr)
    code_gen = CythonCodeWrapper(CCodeGen())
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   double test(double x, double y, double z)\n'
            'def test_c(double x, double y, double z):\n'
            '   return test(x, y, z)\n'
    )
    assert source == expected

def test_cython_wrapper_outarg():
    from sympy import Equality
    x,y,z = symbols('xyz')
    code_gen = CythonCodeWrapper(CCodeGen())

    routine = Routine("test", Equality(z, x + y))
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   void test(double x, double y, double &z)\n'
            'def test_c(double x, double y):\n'
            '   cdef double z\n'
            '   test(x, y, z)\n'
            '   return z\n'
    )
    assert source == expected

def test_cython_wrapper_inoutarg():
    from sympy import Equality
    x,y,z = symbols('xyz')
    code_gen = CythonCodeWrapper(CCodeGen())
    routine = Routine("test", Equality(z, x + y + z))
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   void test(double x, double y, double &z)\n'
            'def test_c(double x, double y, double z):\n'
            '   test(x, y, z)\n'
            '   return z\n'
    )
    assert source == expected

def test_autowrap_dummy():
    x, y, z = symbols('x y z')

    # Uses DummyWrapper to test that codegen works as expected

    f = autowrap(x + y, backend='dummy', tempdir='/tmp/dummy')
    assert f() == str(x + y)<commit_msg>security: Fix a slightly dangerous file operation in test_autowrap.py<commit_after># Tests that require installed backends go into
# sympy/test_external/test_autowrap

import os
import tempfile
import shutil
from StringIO import StringIO

from sympy.utilities.autowrap import autowrap, binary_function, CythonCodeWrapper,\
        ufuncify
from sympy.utilities.codegen import Routine, CCodeGen, CodeGenArgumentListError
from sympy.utilities.pytest import raises
from sympy.core import symbols, Eq

def get_string(dump_fn, routines, prefix="file", header=False, empty=False):
    """Wrapper for dump_fn. dump_fn writes its results to a stream object and
       this wrapper returns the contents of that stream as a string. This
       auxiliary function is used by many tests below.

       The header and the empty lines are not generator to facilitate the
       testing of the output.
    """
    output = StringIO()
    dump_fn(routines, output, prefix, header, empty)
    source = output.getvalue()
    output.close()
    return source

def test_cython_wrapper_scalar_function():
    x,y,z = symbols('xyz')
    expr = (x+y)*z
    routine = Routine("test", expr)
    code_gen = CythonCodeWrapper(CCodeGen())
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   double test(double x, double y, double z)\n'
            'def test_c(double x, double y, double z):\n'
            '   return test(x, y, z)\n'
    )
    assert source == expected

def test_cython_wrapper_outarg():
    from sympy import Equality
    x,y,z = symbols('xyz')
    code_gen = CythonCodeWrapper(CCodeGen())

    routine = Routine("test", Equality(z, x + y))
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   void test(double x, double y, double &z)\n'
            'def test_c(double x, double y):\n'
            '   cdef double z\n'
            '   test(x, y, z)\n'
            '   return z\n'
    )
    assert source == expected

def test_cython_wrapper_inoutarg():
    from sympy import Equality
    x,y,z = symbols('xyz')
    code_gen = CythonCodeWrapper(CCodeGen())
    routine = Routine("test", Equality(z, x + y + z))
    source = get_string(code_gen.dump_pyx, [routine])
    expected = (
            'cdef extern from "file.h":\n'
            '   void test(double x, double y, double &z)\n'
            'def test_c(double x, double y, double z):\n'
            '   test(x, y, z)\n'
            '   return z\n'
    )
    assert source == expected

def test_autowrap_dummy():
    x, y, z = symbols('x y z')

    # Uses DummyWrapper to test that codegen works as expected

    f = autowrap(x + y, backend='dummy')
    assert f() == str(x + y)
{'fraction_non_alphanumeric': 0.10833333333333334, 'fraction_numerical': 0.0, 'mean_word_length': 2.7379823967501693, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3938780', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1762, 'n_words': 536}