chimpler / pyhocon

HOCON parser for Python
Apache License 2.0
508 stars 121 forks source link

Appending to a nested array causes an exception in pyparsing due to wrong type #103

Open movermeyer opened 8 years ago

movermeyer commented 8 years ago

pyparsing.lineno expects the second parameter to be a string.

However, in 2 locations within config_parser.py (here, and here), you set the instring parameter for a ConfigSubstitution to a boolean instead of a string, causing pyhocon to crash (instead of giving the expected error message) in cases where the key cannot be resolved.

Example:

foo: {bar: []}
foo.bar += []

And code:

import sys
from pyhocon import ConfigFactory

input = sys.stdin.read()
config = ConfigFactory.parse_string(input)

Causes:

Traceback (most recent call last):
  File "test.py", line 6, in <module>
    config = ConfigFactory.parse_string(input)
  File "<redacted>/pyhocon/config_parser.py", line 90, in parse_string
    return ConfigParser().parse(content, basedir, resolve)
  File "<redacted>/pyhocon/config_parser.py", line 275, in parse
    ConfigParser.resolve_substitutions(config)
  File "<redacted>/pyhocon/config_parser.py", line 446, in resolve_substitutions
    col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))
  File "<redacted>/pyhocon/config_parser.py", line 446, in <genexpr>
    col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))
  File "<redacted>/env/lib/python2.7/site-packages/pyparsing.py", line 968, in lineno
    return strg.count("\n",0,loc) + 1
AttributeError: 'bool' object has no attribute 'count'

_Aside_ Further, I'm not sure that this should be raising an exception anyway. For example:

a = []
a += []

is allowed in the HOCON spec. It seems to me that pyhocon should be able to resolve foo.bar. However that is another issue (possibly #97), and fixing this issue will at least make the code consistent with this case (which the HOCON spec says is equivalent):

foo: {bar: []}
foo.bar = ${?foo.bar} []

A shout out to American Fuzzy Lop which helped me find these issues.

movermeyer commented 8 years ago

@sanzinger, this is your code. Perhaps you could help out with this one?

raghukumarc commented 6 years ago

i was trying to scan a play config file with below values in it:

play.filters.disabled+=play.filters.csrf.CSRFFilter
play.filters.disabled+=play.filters.headers.SecurityHeadersFilter
play.filters.disabled+=play.filters.hosts.AllowedHostsFilter

I got the same error as above.

cat /tmp/application.conf| pyhocon -f properties
Traceback (most recent call last):
  File "/usr/local/bin/pyhocon", line 11, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/site-packages/pyhocon/tool.py", line 268, in main
    HOCONConverter.convert_from_file(args.input, args.output, args.format.lower(), args.indent, args.compact)
  File "/usr/local/lib/python3.6/site-packages/pyhocon/tool.py", line 233, in convert_from_file
    config = ConfigFactory.parse_string(content)
  File "/usr/local/lib/python3.6/site-packages/pyhocon/config_parser.py", line 94, in parse_string
    return ConfigParser().parse(content, basedir, resolve)
  File "/usr/local/lib/python3.6/site-packages/pyhocon/config_parser.py", line 293, in parse
    ConfigParser.resolve_substitutions(config)
  File "/usr/local/lib/python3.6/site-packages/pyhocon/config_parser.py", line 457, in resolve_substitutions
    col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))
  File "/usr/local/lib/python3.6/site-packages/pyhocon/config_parser.py", line 457, in <genexpr>
    col=col(substitution.loc, substitution.instring)) for substitution in substitutions)))
  File "/usr/local/lib/python3.6/site-packages/pyparsing.py", line 968, in lineno
    return strg.count("\n",0,loc) + 1
AttributeError: 'bool' object has no attribute 'count'

it looks like += is not being parsed correctly.

nmz787-intel commented 2 months ago

not sure if it's the same root cause, but I'm hitting the same exception with a += operation in 0.3.60"

    ConfigParser.resolve_substitutions(settings)
../.virtualenvs/myVenv/lib/python3.9/site-packages/pyhocon/config_parser.py:696: in resolve_substitutions
    variables=', '.join('${{{variable}}}: (line: {line}, col: {col})'.format(
../.virtualenvs/myVenv/lib/python3.9/site-packages/pyhocon/config_parser.py:698: in <genexpr>
    line=lineno(substitution.loc, substitution.instring),
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

loc = 52, strg = True

    def lineno(loc, strg):
        """Returns current line number within a string, counting newlines as line separators.
        The first line is number 1.

        Note - the default parsing behavior is to expand tabs in the input string
        before starting the parsing process.  See :class:`ParserElement.parseString`
        for more information on parsing strings containing ``<TAB>`` s, and
        suggested methods to maintain a consistent view of the parsed string, the
        parse location, and line and column positions within the parsed string.
        """
>       return strg.count("\n", 0, loc) + 1
E       AttributeError: 'bool' object has no attribute 'count'

../.virtualenvs/myVenv/lib/python3.9/site-packages/pyparsing.py:1235: AttributeError