sympy icon indicating copy to clipboard operation
sympy copied to clipboard

Test `sympy/parsing/tests/test_c_parser.py` fails on master

Open mbellitti opened this issue 3 years ago • 3 comments

I've just forked and cloned, and running bin/test test_c_parser fails. I'm on Fedora 37, the version of SymPy is this

Python 3.10.9 (main, Jan 11 2023, 15:21:40) [GCC 11.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sympy
>>> sympy.__version__
'1.13.dev'

I searched the issues but I could not find a similar error. Running the tests fails like this:

$ bin/test test_c_parser
===================================================================== test process starts =====================================================================
executable:         /home/ksrnk/bin/miniconda3/envs/sympy/bin/python  (3.10.9-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       python 
numpy:              1.23.5
random seed:        74424460
hash randomization: on (PYTHONHASHSEED=3724590675)

sympy/parsing/tests/test_c_parser.py[14] .XXXEEE.Ff...E                                                                                                  [FAIL]

________________________________________________________________________ xpassed tests ________________________________________________________________________
sympy/parsing/tests/test_c_parser.py: test_int
sympy/parsing/tests/test_c_parser.py: test_float
sympy/parsing/tests/test_c_parser.py: test_bool

_______________________________________________________________________________________________________________________________________________________________
_____________________________________________________ sympy/parsing/tests/test_c_parser.py:test_function ______________________________________________________
Traceback (most recent call last):
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/tests/test_c_parser.py", line 969, in test_function
    res2 = SymPyExpression(c_src2, 'c').return_expr()
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/sym_expr.py", line 108, in __init__
    self._expr = parse_c(source_code)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 1089, in parse_c
    src = converter.parse_str(source, flags = [])
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 197, in parse_str
    self._py_nodes.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 397, in transform_function_decl
    decl = self.transform(child)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 706, in transform_compound_stmt
    expr.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 687, in transform_return_stmt
    return Return(next(node.get_children()).spelling)
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 228, in __new__
    attrvals.append(cls._construct(attrname, argval))
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 211, in _construct
    return cls._get_constructor(attr)(arg)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 528, in _sympify
    return sympify(a, strict=True)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 449, in sympify
    raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: 'a'
_______________________________________________________________________________________________________________________________________________________________
____________________________________________________ sympy/parsing/tests/test_c_parser.py:test_parameters _____________________________________________________
Traceback (most recent call last):
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/tests/test_c_parser.py", line 1062, in test_parameters
    res2 = SymPyExpression(c_src2, 'c').return_expr()
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/sym_expr.py", line 108, in __init__
    self._expr = parse_c(source_code)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 1089, in parse_c
    src = converter.parse_str(source, flags = [])
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 197, in parse_str
    self._py_nodes.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 397, in transform_function_decl
    decl = self.transform(child)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 706, in transform_compound_stmt
    expr.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 687, in transform_return_stmt
    return Return(next(node.get_children()).spelling)
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 228, in __new__
    attrvals.append(cls._construct(attrname, argval))
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 211, in _construct
    return cls._get_constructor(attr)(arg)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 528, in _sympify
    return sympify(a, strict=True)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 449, in sympify
    raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: 'a'
_______________________________________________________________________________________________________________________________________________________________
___________________________________________________ sympy/parsing/tests/test_c_parser.py:test_function_call ___________________________________________________
Traceback (most recent call last):
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/tests/test_c_parser.py", line 1224, in test_function_call
    res1 = SymPyExpression(c_src1, 'c').return_expr()
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/sym_expr.py", line 108, in __init__
    self._expr = parse_c(source_code)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 1089, in parse_c
    src = converter.parse_str(source, flags = [])
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 197, in parse_str
    self._py_nodes.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 397, in transform_function_decl
    decl = self.transform(child)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 706, in transform_compound_stmt
    expr.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 687, in transform_return_stmt
    return Return(next(node.get_children()).spelling)
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 228, in __new__
    attrvals.append(cls._construct(attrname, argval))
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 211, in _construct
    return cls._get_constructor(attr)(arg)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 528, in _sympify
    return sympify(a, strict=True)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 449, in sympify
    raise SympifyError(a)
sympy.core.sympify.SympifyError: SympifyError: 'x'
_______________________________________________________________________________________________________________________________________________________________
____________________________________________________ sympy/parsing/tests/test_c_parser.py:test_while_stmt _____________________________________________________
Traceback (most recent call last):
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/tests/test_c_parser.py", line 5112, in test_while_stmt
    res4 = SymPyExpression(c_src4, 'c').return_expr()
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/sym_expr.py", line 108, in __init__
    self._expr = parse_c(source_code)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 1089, in parse_c
    src = converter.parse_str(source, flags = [])
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 197, in parse_str
    self._py_nodes.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 397, in transform_function_decl
    decl = self.transform(child)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 706, in transform_compound_stmt
    expr.append(self.transform(child))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 734, in transform_decl_stmt
    statement = self.transform(next(children))
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 228, in transform
    result = handler(node)
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/c/c_parser.py", line 321, in transform_var_decl
    return Variable(
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 1544, in as_Declaration
    return Declaration(self.func(**kw))
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 241, in __new__
    attrvals.append(cls._construct(attrname, argval))
  File "/home/ksrnk/Documents/projects/sympy/sympy/codegen/ast.py", line 211, in _construct
    return cls._get_constructor(attr)(arg)
  File "/home/ksrnk/Documents/projects/sympy/sympy/core/sympify.py", line 464, in sympify
    sympy_deprecation_warning(
  File "/home/ksrnk/Documents/projects/sympy/sympy/utilities/exceptions.py", line 207, in sympy_deprecation_warning
    warnings.warn(w, stacklevel=stacklevel)
sympy.utilities.exceptions.SymPyDeprecationWarning: 

The string fallback in sympify() is deprecated.

To explicitly convert the string form of an object, use
sympify(str(obj)). To add define sympify behavior on custom
objects, use sympy.core.sympify.converter or define obj._sympy_
(see the sympify() docstring).

sympify() performed the string fallback resulting in the following string:

'<built-in function sum>'

See https://docs.sympy.org/latest/explanation/active-deprecations.html#deprecated-sympify-string-fallback
for details.

This has been deprecated since SymPy version 1.6. It
will be removed in a future version of SymPy.


_______________________________________________________________________________________________________________________________________________________________
_________________________________________________ sympy/parsing/tests/test_c_parser.py:test_binary_operators __________________________________________________
Traceback (most recent call last):
  File "/home/ksrnk/Documents/projects/sympy/sympy/parsing/tests/test_c_parser.py", line 2514, in test_binary_operators
    assert res19[0] == FunctionDefinition(
AssertionError

==================== tests finished: 5 passed, 1 failed, 1 expected to fail, 3 expected to fail but passed, 4 exceptions, in 1.01 seconds =====================
DO *NOT* COMMIT!

mbellitti avatar Feb 27 '23 01:02 mbellitti

It looks like these tests aren't run in CI because they need clang to be installed. At some point I tried to ensure that no tests were being skipped in the optional dependency jobs. I guess I missed these because they don't show up as being skipped:

$ bin/test test_c_parser
======================================== test process starts ========================================
executable:         /home/oscar/current/sympy/sympy.git/38venv/bin/python  (3.8.9-final-0) [CPython]
architecture:       64-bit
cache:              yes
ground types:       gmpy 2.1.5
numpy:              1.22.1
random seed:        50712793
hash randomization: on (PYTHONHASHSEED=1219004996)

sympy/parsing/tests/test_c_parser.py[1] .                                                        [OK]

============================= tests finished: 1 passed, in 0.14 seconds =============================

That's because the test module doesn't even define the test functions if clang is not installed: https://github.com/sympy/sympy/blob/75691e5cbdb3dfda4c8164a217bac6a6434404f6/sympy/parsing/tests/test_c_parser.py#L7

oscarbenjamin avatar Feb 27 '23 15:02 oscarbenjamin

Thank you for responding. I still don't understand what's happening, can i do something to help?

mbellitti avatar Feb 27 '23 15:02 mbellitti

What is happening is that this code has not been tested properly because the tests only run if clang is installed and none of the jobs that run in CI have clang installed. Since the code was not tested changes have been made in the codebase and now this code does not work any more.

can i do something to help?

You might be able to fix whatever bugs cause the code to fail. I can't immediately install whatever is needed to get clang to work so I can't test this myself.

oscarbenjamin avatar Feb 27 '23 15:02 oscarbenjamin

@oscarbenjamin while working on this issue, I realized that the SymPyDeprecationWarning raised because in one of the test code snippets, a variable is named sum, which coincides with the Python built-in function.

What is the protocol regarding C code snippets where variables coincide with Python keywords/built-in functions?

For the offending code snippet, I just changed the variable name from sum to ans.

wermos avatar Mar 20 '23 18:03 wermos