Skip to content
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

[stdune] add scanf module #2820

Merged
merged 2 commits into from
Nov 5, 2019
Merged

[stdune] add scanf module #2820

merged 2 commits into from
Nov 5, 2019

Conversation

rgrinberg
Copy link
Member

This is a slightly safer version that only catches 2 exceptions that
scanf throws

@rgrinberg rgrinberg requested review from emillon, a user and aalekseyev October 30, 2019 07:08
@rgrinberg rgrinberg requested a review from dra27 as a code owner October 30, 2019 07:08
src/dune/build.ml Outdated Show resolved Hide resolved
@rgrinberg
Copy link
Member Author

rgrinberg commented Oct 30, 2019 via email


let unescaped x =
match Scanf.unescaped x with
| exception End_of_file -> Error ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why only these two? The doc seems to say:

Raise Scanf.Scan_failure if the input does not match the format.
Raise Failure if a conversion to a number is not possible.
Raise End_of_file if the end of input is encountered while some more characters are needed to read the current conversion specification.
Raise Invalid_argument if the format string is invalid.

(but I don't know when the other two are raised)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll catch failure as well. However, I think that we shouldn't catch Invalid_argument _. A bad formatting string is just a bug.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, this does make the function a bit more dangerous to use as f could easily raise Failure for other reasons.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aalekseyev is away this week, I'll finish the review for him.

Of course, this does make the function a bit more dangerous to use as f could easily raise Failure for other reasons.

What about Scanf.ksscanf? It seems to be exactly for this case

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, if we use ksscanf I'd say we don't even need to inspect the exception

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I pushed a commit that uses ksscanf. However, I'm unable to accomplish this without this private exception I've defined. Perhaps you can think of a way to do it without this?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The method with a private exception looks good to me

@ghost
Copy link

ghost commented Oct 30, 2019

That would indeed be preferable. I just tried to preserve existing behaviour as much as possible. I agree we should be a lot more careful with all these exceptions.

The existing behaviour seems wrong though. Since we are touching this code, we might as well clean it up!

@rgrinberg rgrinberg force-pushed the stdune-scanf branch 2 times, most recently from 8ae5880 to 171a297 Compare November 4, 2019 14:22
ghost
ghost previously requested changes Nov 4, 2019
src/stdune/scanf.ml Outdated Show resolved Hide resolved
@rgrinberg
Copy link
Member Author

@diml now it's a toplevel exception

This is a slightly safer version that only catches 2 exceptions that
scanf throws

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
Signed-off-by: Rudi Grinberg <me@rgrinberg.com>
@rgrinberg rgrinberg dismissed ghost ’s stale review November 5, 2019 03:04

Review comments are addressed

@rgrinberg rgrinberg merged commit d49e16e into ocaml:master Nov 5, 2019
@rgrinberg rgrinberg deleted the stdune-scanf branch November 5, 2019 03:04
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 this pull request may close these issues.

2 participants