close
The Wayback Machine - https://web.archive.org/web/20201106012145/https://github.com/Toblerity/Fiona/issues/369
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

Catch BrokenPipeError in CLI and don't log exception #369

Open
geowurster opened this issue Jun 30, 2016 · 2 comments
Open

Catch BrokenPipeError in CLI and don't log exception #369

geowurster opened this issue Jun 30, 2016 · 2 comments

Comments

@geowurster
Copy link
Member

@geowurster geowurster commented Jun 30, 2016

When piping multiline outputs like feature sequences to another utility that does not fully consume the output an exception is raised and logged to stderr. If its easy, handling KeyboardInterrupt would be nice too. BrokenPipeError is ignored by default on Python 3, but Python 2 takes a bit of extra handling like:

try:
    # CLI logic
except BrokenPipeError as e:
    if sys.version_info[0] == 2 and 'broken pipe' in str(e):
        ctx.exit()  # Exit the click context
    else:
        raise
except Exception:
    logger.exception("Exception encountered ...")
    raise

For example:

$ fio cat tests/data/coutwildrnp.shp --precision 1 | head -1
{"bbox": [-111.8, 41.7, -111.6, 42.0], "geometry": {"coordinates": [[[-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.6, 41.9], [-111.6, 41.9], [-111.6, 41.9], [-111.6, 41.9], [-111.6, 41.8], [-111.6, 41.8], [-111.6, 41.8], [-111.6, 41.8], [-111.6, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.8, 41.8], [-111.8, 41.7], [-111.8, 41.8], [-111.8, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.8, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.7, 41.8], [-111.8, 41.8], [-111.8, 41.9], [-111.8, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.7, 41.9], [-111.8, 41.9], [-111.8, 41.9], [-111.7, 41.9], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0], [-111.7, 42.0]]], "type": "Polygon"}, "id": "0", "properties": {"AGBUR": "FS", "AREA": 0.0179264, "FEATURE1": "Wilderness", "FEATURE2": null, "NAME": "Mount Naomi Wilderness", "PERIMETER": 1.22107, "STATE": "UT", "STATE_FIPS": "49", "URL": "http://www.wilderness.net/index.cfm?fuse=NWPS&sec=wildView&wname=Mount%20Naomi", "WILDRNP020": 332}, "type": "Feature"}
ERROR:fio:Exception caught during processing
Traceback (most recent call last):
  File "/Users/wursterk/code/Fiona/fiona/fio/cat.py", line 85, in cat
    click.echo(json.dumps(feat, **dump_kwds))
  File "/Users/wursterk/code/Fiona/venv/lib/python3.5/site-packages/click/utils.py", line 260, in echo
    file.flush()
BrokenPipeError: [Errno 32] Broken pipe
Aborted!
Exception ignored in: <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>
BrokenPipeError: [Errno 32] Broken pipe
@sgillies
Copy link
Member

@sgillies sgillies commented Aug 17, 2017

@geowurster I'd like to keep punting on this one. The exception goes to stderr (you can verify with $ fio cat tests/data/coutwildrnp.shp --precision 1 2> /dev/null | head -1) and not stdout like the JSON data.

@geowurster
Copy link
Member Author

@geowurster geowurster commented Sep 3, 2017

@sgillies The note about the exception being emitted to stdout is a typo. I edited my initial comment. This definitely isn't a blocker and my mail goal was to eliminate confusion.

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

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.