Git Product home page Git Product logo

Comments (4)

deliciouslytyped avatar deliciouslytyped commented on August 17, 2024

Edit: So to restate this in a clearer manner: As far as I can tell, python doesn't let you catch exceptions raised from inside a generator, because they are converted into stopping the generator.

All the stackoverflow posts I've looked at so far suggest yielding an exception object from a generator instead of having it raised, which I don't really like, and might require rearchitecting parsy?

I'm going to try to see if I can write a wrapper function that will catch exceptions and return them in this form, so at least it can be done on a case by case basis, but will look less nice.

from parsy.

deliciouslytyped avatar deliciouslytyped commented on August 17, 2024

To be clear, help on the above would be appreciated, I'm winging this.

from parsy.

deliciouslytyped avatar deliciouslytyped commented on August 17, 2024

If the above is true I'm not quite sure I get how the normal case works, but being unable to figure out how to implement something better, I found a workaround:

I think I've temporarily worked around this by modifying the peek implementation to signal the end of the stream with None (I should probably create a more descriptive symbol instead);

def peek_with_none(parser): #To work around the cant raise exception from yield issue
    @Parser
    def peek_parser(stream, index):
        if index == len(stream):
            #bail early instead of raising a ParseError, or something
            return Result.success(index, None)

        result = parser(stream, index)
        if result.status:
            return Result.success(index, result.value)
        else:
            return result
    return peek_parser

from parsy.

deliciouslytyped avatar deliciouslytyped commented on August 17, 2024

I completely forgot I ran into this exact issue and did it again.
I think the problem is that wrapping a yield in a try/except doesn't catch the error because it's not actually "running" in the context where the try/except is. It's returning a value which gets run somewhere else.

I haven't actually figured out how to make this work but it probably involves writing a @parser. I worked around my issue temporarily by using a peek that wouldnt fail and .map()-ing a conditional function over the result to check if continuing is ok.

from parsy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.