strictyaml icon indicating copy to clipboard operation
strictyaml copied to clipboard

Bug: AttributeError raised while parsing unacceptable characters

Open Quibi opened this issue 6 years ago • 2 comments

Hi,

I have found a weird quirk on strictyaml that I think can be a minor bug.

If unacceptable characters are parsed, strictyaml raises —I think properly— a ruamel.yaml.reader.ReaderError consisting in unacceptable character #x[character]: special characters are not allowed.

However, another exception is raised while handling ruamel.yaml.reader.ReaderError: an AttributeError: 'ReaderError' object has no attribute 'context_mark' which I suspect that could be a non-intended behaviour.

For example, strictyaml.load('\0x00') raises:

Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 275, in generic_load document = ruamelyaml.load(yaml_string, Loader=DynamicStrictYAMLLoader) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/main.py", line 933, in load loader = Loader(stream, version, preserve_quotes=preserve_quotes) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 240, in init Reader.init(self, stream, loader=self) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 85, in init self.stream = stream # type: Any # as .read is called File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 117, in stream self.check_printable(val) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 255, in check_printable 'special characters are not allowed', ruamel.yaml.reader.ReaderError: unacceptable character #x0000: special characters are not allowed in "", position 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 313, in load return generic_load(yaml_string, schema=schema, label=label) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 277, in generic_load if parse_error.context_mark is not None: AttributeError: 'ReaderError' object has no attribute 'context_mark'

I think, maybe wrongly, that the AttributeError is a bug. Obviously, parse_error (<class 'ruamel.yaml.reader.ReaderError'>) does not have the attribute context_mark at this point (strictyaml/parser.py, line 277) —nor problem_mark attribute.

The problem is that the method generic_load() in strictyaml/parser.py is maybe expecting a MarkedYAMLError here but got a ReaderError —without neither context_mark nor problem_mark specific MarkedYAMLError's attribute.

So, I imagine that simply checking that the exception raised is the proper one (a MarkedYAMLError one) before going for context_mark and problem_mark will be enough to avoid this bug (if it is really a bug). For instance:

        if hasattr(parse_error, 'context_mark') and parse_error.context_mark is not None:
            parse_error.context_mark.name = label
        if hasattr(parse_error, 'problem_mark') and parse_error.problem_mark is not None:
            parse_error.problem_mark.name = label

Running on:

  • Python 3.7.4
  • strictyaml 1.0.2
  • ruamel.yaml 0.15.099

Thanks a lot. Sorry for bother you with this minor inconvenience.

Quibi avatar Jul 11 '19 07:07 Quibi

Yep, that looks like a bug. Thanks for reporting!

It might be with the underlying library (ruamel.yaml) but I'll see what I can find.

On Thu, 11 Jul 2019, 08:10 Quibizo, [email protected] wrote:

Hi,

I have found a weird quirk on strictyaml that I think can be a minor bug.

If unacceptable character are parsed, strictyaml raises —I think properly— a ruamel.yaml.reader.ReaderError consisting in unacceptable character #x[character]: special characters are not allowed.

However, another exception is raised while handling ruamel.yaml.reader.ReaderError: an AttributeError: 'ReaderError' object has no attribute 'context_mark' which I suspect that could be a non-intended behaviour.

For example, strictyaml.load('\0x00') raises:

Traceback (most recent call last): File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 275, in generic_load document = ruamelyaml.load(yaml_string, Loader=DynamicStrictYAMLLoader) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/main.py", line 933, in load loader = Loader(stream, version, preserve_quotes=preserve_quotes) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 240, in init Reader.init(self, stream, loader=self) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 85, in init self.stream = stream # type: Any # as .read is called File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 117, in stream self.check_printable(val) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/ruamel/yaml/reader.py", line 255, in check_printable 'special characters are not allowed', ruamel.yaml.reader.ReaderError: unacceptable character #x0000: special characters are not allowed in "", position 0

During handling of the above exception, another exception occurred:

Traceback (most recent call last): File "", line 1, in File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 313, in load return generic_load(yaml_string, schema=schema, label=label) File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/strictyaml/parser.py", line 277, in generic_load if parse_error.context_mark is not None: AttributeError: 'ReaderError' object has no attribute 'context_mark'

I think, maybe wrongly, that the AttributeError is a bug. Obviously, parse_error (<class 'ruamel.yaml.reader.ReaderError'>) does not have the attribute context_mark at this point (strictyaml/parser.py, line 277) —nor problem_mark attribute.

The problem is that the method generic_load() in strictyaml/parser.py is maybe expecting a MarkedYAMLError here but got a ReaderError —without neither context_mark nor problem_mark specific MarkedYAMLError's attribute.

So, I imagine that simply checking that the exception raised is the proper one (a MarkedYAMLError one) before going for context_mark and problem_mark will be enough to avoid this bug (if it is really a bug). For instance:

    if hasattr(parse_error, 'context_mark') and parse_error.context_mark is not None:

        parse_error.context_mark.name = label

    if hasattr(parse_error, 'problem_mark') and parse_error.problem_mark is not None:

        parse_error.problem_mark.name = label

Thanks a lot. Sorry for bother you with this minor inconvenience.

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/crdoconnor/strictyaml/issues/65?email_source=notifications&email_token=ABOJKNP5K3TR7VALG6WBDFTP63MHBA5CNFSM4IAXRPCKYY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4G6RLH7Q, or mute the thread https://github.com/notifications/unsubscribe-auth/ABOJKNMDIIA3KKGDRZLQCGLP63MHBANCNFSM4IAXRPCA .

crdoconnor avatar Jul 11 '19 07:07 crdoconnor

Thanks. I forgot to mention that the proposed solution is just a workaround (in my case, to pass tests). It really does not respect the label argument in strictyaml.load().

Quibi avatar Jul 11 '19 08:07 Quibi