Skip to main content
Join the official Python Developers Survey 2018 and win valuable prizes: Start the survey!

Safer way to sympify unsanitized input

Project description

Safer Sympify

Convert unsanitized user input into SymPy expression.

This does not prevent all possible attacks. However, this is much safer than using eval(), which SymPy still does.

See SymPy PR12524 for more details: https://github.com/sympy/sympy/pull/12524

Examples

    Create a SymPy expression from user input. 
    This uses pure Python syntax. 
    Whitelisted operators and functions only are allowed.
    >>> expr = SaferSympify().str2sympy('-sqrt(1 + a**b*b)/((a**b)*b+1)')
    >>> expr
    -1/sqrt(a**b*b + 1)

    Get free symbols:
    >>> sorted(expr.free_symbols, key=lambda x: str(x))
    [a, b]

    Evaluate expression:
    >>> expr.evalf(subs={'a': 1, 'b': 3, 'c': 5})  # Note extra values can be passed too
    -0.500000000000000

    Simplify expression:
    >>> expr.simplify()
    -1/sqrt(a**b*b + 1)

    Pretty-print expression as Latex (could be displayed in browser with MathJax)
    >>> sympy.latex(expr)
    '- \\frac{1}{\\sqrt{a^{b} b + 1}}'

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Filename, size & hash SHA256 hash help File type Python version Upload date
safersympify-0.1.11.tar.gz (2.6 kB) Copy SHA256 hash SHA256 Source None Aug 9, 2018

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page