-
Notifications
You must be signed in to change notification settings - Fork 1.6k
MAINT: Replace try/except/pass with contextlib.suppress (SIM105) #3767
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
base: main
Are you sure you want to change the base?
Changes from all 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 |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| # POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| import contextlib | ||
| import os | ||
| import re | ||
| import sys | ||
|
|
@@ -893,14 +894,10 @@ def _read_standard_xref_table(self, stream: StreamType) -> None: | |
| else: | ||
| if entry_type_b == b"n": | ||
| self.xref[generation][num] = offset | ||
| try: | ||
| with contextlib.suppress(Exception): | ||
| self.xref_free_entry[generation][num] = entry_type_b == b"f" | ||
| except Exception: | ||
| pass | ||
| try: | ||
| with contextlib.suppress(Exception): | ||
|
Collaborator
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. While we are at it, I suggest tightening the suppressed exceptions here. |
||
| self.xref_free_entry[65535][num] = entry_type_b == b"f" | ||
| except Exception: | ||
| pass | ||
| cnt += 1 | ||
| num += 1 | ||
| read_non_whitespace(stream) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| # POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| import contextlib | ||
| import math | ||
| from typing import Any, Callable, Optional, Union | ||
|
|
||
|
|
@@ -300,10 +301,8 @@ def _handle_tf(self, operands: list[Any]) -> None: | |
| ) | ||
|
|
||
| self._space_width = self.font.space_width / 2 # Actually the width of _half_ a space... | ||
| try: | ||
| with contextlib.suppress(Exception): # keep previous size | ||
|
Collaborator
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. While we are at it, I suggest tightening the suppressed exceptions here. |
||
| self.font_size = float(operands[1]) | ||
| except Exception: | ||
| pass # keep previous size | ||
|
|
||
| def _handle_td(self, operands: list[Any]) -> float: | ||
| """Handle Td (Move text position) operation - Table 5.5 page 406.""" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,6 +27,7 @@ | |
| # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | ||
| # POSSIBILITY OF SUCH DAMAGE. | ||
|
|
||
| import contextlib | ||
| import decimal | ||
| import enum | ||
| import hashlib | ||
|
|
@@ -362,10 +363,8 @@ def _info(self) -> Optional[DictionaryObject]: | |
| @_info.setter | ||
| def _info(self, value: Optional[Union[IndirectObject, DictionaryObject]]) -> None: | ||
| if value is None: | ||
| try: | ||
| with contextlib.suppress(KeyError, AttributeError): | ||
| self._objects[self._info_obj.indirect_reference.idnum - 1] = None # type: ignore | ||
| except (KeyError, AttributeError): | ||
| pass | ||
| self._info_obj = None | ||
| else: | ||
| if self._info_obj is None: | ||
|
|
@@ -518,12 +517,10 @@ def _add_page( | |
| # page; therefore in order to add multiple copies of the same | ||
| # page, we need to create a new dictionary for the page, however the | ||
| # objects below (including content) are not duplicated: | ||
| try: # delete an already existing page | ||
| with contextlib.suppress(Exception): # delete an already existing page | ||
| del self._id_translated[id(page_org.indirect_reference.pdf)][ # type: ignore | ||
| page_org.indirect_reference.idnum # type: ignore | ||
| ] | ||
| except Exception: | ||
| pass | ||
|
|
||
| page = cast( | ||
| "PageObject", page_org.clone(self, False, excluded_keys).get_object() | ||
|
|
@@ -751,10 +748,8 @@ def open_destination( | |
| @open_destination.setter | ||
| def open_destination(self, dest: Union[None, str, Destination, PageObject]) -> None: | ||
| if dest is None: | ||
| try: | ||
| with contextlib.suppress(KeyError): | ||
| del self._root_object["/OpenAction"] | ||
| except KeyError: | ||
| pass | ||
| elif isinstance(dest, str): | ||
| self._root_object[NameObject("/OpenAction")] = TextStringObject(dest) | ||
| elif isinstance(dest, Destination): | ||
|
|
@@ -1255,10 +1250,8 @@ def clone_document_from_reader( | |
| ) | ||
| # else: _info_obj = None done in clone_reader_document_root() | ||
|
|
||
| try: | ||
| with contextlib.suppress(AttributeError): | ||
| self._ID = cast(ArrayObject, reader._ID).clone(self) | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| if callable(after_page_append): | ||
| for page in cast( | ||
|
|
@@ -1704,10 +1697,8 @@ def replace_in_obj( | |
| if not is_null_or_none(self._info): | ||
| unreferenced[self._info.indirect_reference.idnum - 1] = False # type: ignore | ||
|
|
||
| try: | ||
| with contextlib.suppress(AttributeError): | ||
| unreferenced[self._ID.indirect_reference.idnum - 1] = False # type: ignore | ||
| except AttributeError: | ||
| pass | ||
|
|
||
| for i in compress(range(len(self._objects)), unreferenced): | ||
| self._objects[i] = None | ||
|
|
@@ -2156,10 +2147,8 @@ def _remove_objects_from_page__clean_forms( | |
| if k1 not in ["/Length", "/Filter", "/DecodeParms"] | ||
| } | ||
| ) | ||
| try: | ||
| with contextlib.suppress(AttributeError): # pragma: no cover | ||
|
Collaborator
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. Keeping the pragma seems wrong, as this will not report coverage issues with the body. |
||
| content.indirect_reference = o.indirect_reference | ||
| except AttributeError: # pragma: no cover | ||
| pass | ||
| stack.append(elt) | ||
|
|
||
| # clean subforms | ||
|
|
@@ -3183,15 +3172,11 @@ def reset_translation( | |
| if reader is None: | ||
| self._id_translated = {} | ||
| elif isinstance(reader, PdfReader): | ||
| try: | ||
| with contextlib.suppress(Exception): | ||
|
Collaborator
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. While we are at it, I suggest tightening the suppressed exceptions here. |
||
| del self._id_translated[id(reader)] | ||
| except Exception: | ||
| pass | ||
| elif isinstance(reader, IndirectObject): | ||
| try: | ||
| with contextlib.suppress(Exception): | ||
|
Collaborator
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. While we are at it, I suggest tightening the suppressed exceptions here. |
||
| del self._id_translated[id(reader.pdf)] | ||
| except Exception: | ||
| pass | ||
| else: | ||
| raise Exception("invalid parameter {reader}") | ||
|
stefan6419846 marked this conversation as resolved.
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| """Test the pypdf._reader module.""" | ||
| import contextlib | ||
| import io | ||
| import struct | ||
| import sys | ||
|
|
@@ -513,10 +514,8 @@ def test_read_malformed_header(caplog): | |
| PdfReader(io.BytesIO(b"foo"), strict=True) | ||
| assert exc.value.args[0] == "PDF starts with 'foo', but '%PDF-' expected" | ||
| caplog.clear() | ||
| try: | ||
| with contextlib.suppress(Exception): | ||
|
Collaborator
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. While we are at it, I suggest tightening the suppressed exceptions here. |
||
| PdfReader(io.BytesIO(b"foo"), strict=False) | ||
| except Exception: | ||
| pass | ||
| assert caplog.messages[0].startswith("invalid pdf header") | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While we are at it, I suggest tightening the suppressed exceptions here.