Skip to content

ValueError exception for scientific notation with digits after . #449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
t20100 opened this issue Sep 13, 2023 · 2 comments · Fixed by #451
Closed

ValueError exception for scientific notation with digits after . #449

t20100 opened this issue Sep 13, 2023 · 2 comments · Fixed by #451

Comments

@t20100
Copy link
Contributor

t20100 commented Sep 13, 2023

With numexpr v2.8.6, the following code:

import numexpr
numexpr.NumExpr("4.0e-9*x")

raises an exception ValueError: Expression 4.0e-9*x has forbidden control characters.

Complete exception:
File ~/venvs/py310/lib/python3.10/site-packages/numexpr/necompiler.py:639, in NumExpr(ex, signature, sanitize, **kwargs)
    637 _frame_depth = 1
    638 context = getContext(kwargs, _frame_depth=_frame_depth)
--> 639 threeAddrProgram, inputsig, tempsig, constants, input_names = precompile(ex, signature, context, sanitize=sanitize)
    640 program = compileThreeAddrForm(threeAddrProgram)
    641 return interpreter.NumExpr(inputsig.encode('ascii'),
    642                            tempsig.encode('ascii'),
    643                            program, constants, input_names)

File ~/venvs/py310/lib/python3.10/site-packages/numexpr/necompiler.py:576, in precompile(ex, signature, context, sanitize)
    573 input_order = [name for (name, type_) in signature]
    575 if isinstance(ex, str):
--> 576     ex = stringToExpression(ex, types, context, sanitize)
    578 # the AST is like the expression, but the node objects don't have
    579 # any odd interpretations
    581 ast = expressionToAST(ex)

File ~/venvs/py310/lib/python3.10/site-packages/numexpr/necompiler.py:281, in stringToExpression(s, types, context, sanitize)
    279     no_whitespace = re.sub(r'\s+', '', s)
    280     if _blacklist_re.search(no_whitespace) is not None:
--> 281         raise ValueError(f'Expression {s} has forbidden control characters.')
    283 old_ctx = expressions._context.get_current_context()
    284 try:

ValueError: Expression 4.0e-9*x has forbidden control characters.

This is working with v2.8.5.

Also, numexpr.NumExpr("4.e-9*x") or numexpr.NumExpr("4.0*x") works fine with v2.8.6.

@t20100
Copy link
Contributor Author

t20100 commented Sep 13, 2023

Changing this regular expression:

_attr_pat = r'\.\b(?!(real|imag|[eE]?[+-]?\d+)\b)'

to:

_attr_pat = r'\.\b(?!(real|imag|\d*[eE]?[+-]?\d+)\b)'

makes it accept trailing zeros.

@t20100 t20100 changed the title ValueError exception for scientific notation with trailing 0 ValueError exception for scientific notation with digits after . Sep 13, 2023
@loichuder
Copy link

In fact, it was caught by @rebecca-palmer earlier (#442 (comment)).

They proposed a similar correction to the regexp that didn't make it in the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants