-
Notifications
You must be signed in to change notification settings - Fork 284
Ensure proper close after open in export_od #469
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,29 +29,33 @@ def export_od(od, dest: Union[str, TextIO, None] = None, doc_type: Optional[str] | |
| :rtype: str or None | ||
| """ | ||
|
|
||
| doctypes = {"eds", "dcf"} | ||
| if isinstance(dest, str): | ||
| if doc_type is None: | ||
| for t in doctypes: | ||
| if dest.endswith(f".{t}"): | ||
| doc_type = t | ||
| break | ||
|
|
||
| if doc_type is None: | ||
| doc_type = "eds" | ||
| dest = open(dest, 'w') | ||
| assert doc_type in doctypes | ||
|
|
||
| if doc_type == "eds": | ||
| from canopen.objectdictionary import eds | ||
| return eds.export_eds(od, dest) | ||
| elif doc_type == "dcf": | ||
| from canopen.objectdictionary import eds | ||
| return eds.export_dcf(od, dest) | ||
|
|
||
| # If dest is opened in this fn, it should be closed | ||
| if type(dest) is str: | ||
| dest.close() | ||
| opened_here = False | ||
| try: | ||
| doctypes = {"eds", "dcf"} | ||
| if isinstance(dest, str): | ||
| if doc_type is None: | ||
| for t in doctypes: | ||
| if dest.endswith(f".{t}"): | ||
| doc_type = t | ||
| break | ||
|
|
||
| if doc_type is None: | ||
| doc_type = "eds" | ||
| dest = open(dest, 'w') | ||
| opened_here = True | ||
|
|
||
| if doc_type == "eds": | ||
| from canopen.objectdictionary import eds | ||
| return eds.export_eds(od, dest) | ||
| elif doc_type == "dcf": | ||
| from canopen.objectdictionary import eds | ||
| return eds.export_dcf(od, dest) | ||
| else: | ||
| raise NotImplementedError(f"No support for the {doc_type!r} format") | ||
|
Comment on lines
+52
to
+53
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, I noticed this a little bit too late... this is a little bit unconventional;
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right. Agreed. It's not always easy to "just fix" something like the close issue and then uncovering other things that needs fixing too. ;)
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, one fix quickly leads to the discovery of other issues :) I'll create an issue about this.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I created #475.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For the record; before this PR, this check was an |
||
| finally: | ||
| # If dest is opened in this fn, it should be closed | ||
| if opened_here: | ||
| dest.close() | ||
|
|
||
|
|
||
| def import_od( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.