diff --git a/weasyprint/css/__init__.py b/weasyprint/css/__init__.py index 1cb96629..c65c9e9e 100644 --- a/weasyprint/css/__init__.py +++ b/weasyprint/css/__init__.py @@ -123,13 +123,13 @@ class StyleFor: if ('table' in style['display'] and style['border_collapse'] == 'collapse'): # Padding do not apply - for side in ['top', 'bottom', 'left', 'right']: + for side in ('top', 'bottom', 'left', 'right'): style[f'padding_{side}'] = computed_values.ZERO_PIXELS if (len(style['display']) == 1 and style['display'][0].startswith('table-') and style['display'][0] != 'table-caption'): # Margins do not apply - for side in ['top', 'bottom', 'left', 'right']: + for side in ('top', 'bottom', 'left', 'right'): style[f'margin_{side}'] = computed_values.ZERO_PIXELS return style diff --git a/weasyprint/css/computed_values.py b/weasyprint/css/computed_values.py index 81fc873e..77c1af43 100644 --- a/weasyprint/css/computed_values.py +++ b/weasyprint/css/computed_values.py @@ -1,6 +1,7 @@ """Convert specified property values into computed values.""" from collections import OrderedDict +from contextlib import suppress from math import pi from urllib.parse import unquote @@ -228,11 +229,9 @@ def compute_variable(value, name, computed, base_url, parent_style): # See https://drafts.csswg.org/css-variables/#invalid-variables if new_value is None: - try: + with suppress(BaseException): computed_value = ''.join( token.serialize() for token in computed_value) - except BaseException: - pass LOGGER.warning( 'Unsupported computed value "%s" set in variable %r ' 'for property %r.', computed_value, diff --git a/weasyprint/css/validation/descriptors.py b/weasyprint/css/validation/descriptors.py index b61a6e0a..096231d5 100644 --- a/weasyprint/css/validation/descriptors.py +++ b/weasyprint/css/validation/descriptors.py @@ -167,7 +167,7 @@ def font_weight(token): if keyword in ('normal', 'bold'): return keyword if token.type == 'number' and token.int_value is not None: - if token.int_value in [100, 200, 300, 400, 500, 600, 700, 800, 900]: + if token.int_value in (100, 200, 300, 400, 500, 600, 700, 800, 900): return token.int_value diff --git a/weasyprint/formatting_structure/boxes.py b/weasyprint/formatting_structure/boxes.py index d761550d..4799d7e8 100644 --- a/weasyprint/formatting_structure/boxes.py +++ b/weasyprint/formatting_structure/boxes.py @@ -122,7 +122,7 @@ class Box: """ # Overridden in ParentBox to also translate children, if any. - if dx == 0 and dy == 0: + if dx == dy == 0: return self.position_x += dx self.position_y += dy @@ -224,12 +224,12 @@ class Box: # See https://www.w3.org/TR/css-backgrounds-3/#corner-overlap ratio = min([1] + [ extent / sum_radii - for extent, sum_radii in [ + for extent, sum_radii in ( (width, tlrx + trrx), (width, blrx + brrx), (height, tlry + blry), (height, trry + brry), - ] + ) if sum_radii > 0 ]) return ( diff --git a/weasyprint/layout/absolute.py b/weasyprint/layout/absolute.py index 6640c09c..8bf14ea2 100644 --- a/weasyprint/layout/absolute.py +++ b/weasyprint/layout/absolute.py @@ -21,7 +21,7 @@ class AbsolutePlaceholder: object.__setattr__(self, '_layout_done', True) def translate(self, dx=0, dy=0, ignore_floats=False): - if dx == 0 and dy == 0: + if dx == dy == 0: return if self._layout_done: self._box.translate(dx, dy, ignore_floats) diff --git a/weasyprint/layout/block.py b/weasyprint/layout/block.py index 8d52b2b9..a12c887d 100644 --- a/weasyprint/layout/block.py +++ b/weasyprint/layout/block.py @@ -185,7 +185,7 @@ def block_level_width(box, containing_block): width = box.width = cb_width - ( paddings_plus_borders + margin_l + margin_r) margin_sum = cb_width - paddings_plus_borders - width - if margin_l == 'auto' and margin_r == 'auto': + if margin_l == margin_r == 'auto': box.margin_left = margin_sum / 2 box.margin_right = margin_sum / 2 elif margin_l == 'auto' and margin_r != 'auto': @@ -750,9 +750,9 @@ def block_container_layout(context, box, bottom_space, skip_stack, # Top and bottom margins of this box if (box.height in ('auto', 0) and get_clearance(context, box, collapsed_margin) is None and - all(value == 0 for value in [ + all(value == 0 for value in ( box.min_height, box.border_top_width, box.padding_top, - box.border_bottom_width, box.padding_bottom])): + box.border_bottom_width, box.padding_bottom))): collapsing_through = True else: position_y += collapsed_margin diff --git a/weasyprint/layout/inline.py b/weasyprint/layout/inline.py index e818da2d..5a68bbe4 100644 --- a/weasyprint/layout/inline.py +++ b/weasyprint/layout/inline.py @@ -506,7 +506,7 @@ def split_inline_level(context, box, position_x, max_x, bottom_space, elif isinstance(box, boxes.InlineFlexBox): box.position_x = position_x box.position_y = 0 - for side in ['top', 'right', 'bottom', 'left']: + for side in ('top', 'right', 'bottom', 'left'): if getattr(box, f'margin_{side}') == 'auto': setattr(box, f'margin_{side}', 0) new_box, resume_at, _, _, _ = flex_layout( diff --git a/weasyprint/layout/page.py b/weasyprint/layout/page.py index b625bbe6..bd9bf8b8 100644 --- a/weasyprint/layout/page.py +++ b/weasyprint/layout/page.py @@ -123,7 +123,7 @@ def compute_fixed_dimension(context, box, outer, vertical, top_or_left): # Rule 2 total = box.padding_plus_border + sum( - value for value in [box.margin_a, box.margin_b, box.inner] + value for value in (box.margin_a, box.margin_b, box.inner) if value != 'auto') if total > outer: if box.margin_a == 'auto': @@ -163,7 +163,7 @@ def compute_fixed_dimension(context, box, outer, vertical, top_or_left): box.inner = (outer - box.padding_plus_border - box.margin_a - box.margin_b) # Rule 6 - if box.margin_a == 'auto' and box.margin_b == 'auto': + if box.margin_a == box.margin_b == 'auto': box.margin_a = box.margin_b = ( outer - box.padding_plus_border - box.inner) / 2 @@ -362,7 +362,7 @@ def make_margin_boxes(context, page, state): # https://drafts.csswg.org/css-page-3/#margin-box-dimensions generated_boxes = [] - for prefix, vertical, containing_block, position_x, position_y in [ + for prefix, vertical, containing_block, position_x, position_y in ( ('top', False, (max_box_width, margin_top), margin_left, 0), ('bottom', False, (max_box_width, margin_bottom), @@ -371,7 +371,7 @@ def make_margin_boxes(context, page, state): 0, margin_top), ('right', True, (margin_right, max_box_height), page_end_x, margin_top), - ]: + ): if vertical: suffixes = ['top', 'middle', 'bottom'] fixed_outer, variable_outer = containing_block @@ -399,18 +399,18 @@ def make_margin_boxes(context, page, state): variable_outer - box.margin_width()) compute_fixed_dimension( context, box, fixed_outer, not vertical, - prefix in ['top', 'left']) + prefix in ('top', 'left')) generated_boxes.append(box) # Corner boxes - for at_keyword, cb_width, cb_height, position_x, position_y in [ + for at_keyword, cb_width, cb_height, position_x, position_y in ( ('@top-left-corner', margin_left, margin_top, 0, 0), ('@top-right-corner', margin_right, margin_top, page_end_x, 0), ('@bottom-left-corner', margin_left, margin_bottom, 0, page_end_y), ('@bottom-right-corner', margin_right, margin_bottom, page_end_x, page_end_y), - ]: + ): box = make_box(at_keyword, (cb_width, cb_height)) if not box.is_generated: continue diff --git a/weasyprint/layout/percent.py b/weasyprint/layout/percent.py index f0e242a7..8577aa1c 100644 --- a/weasyprint/layout/percent.py +++ b/weasyprint/layout/percent.py @@ -93,7 +93,7 @@ def resolve_percentages(box, containing_block, main_flex_direction=None): box, 'max_height', cb_height, main_flex_direction) # Used value == computed value - for side in ['top', 'right', 'bottom', 'left']: + for side in ('top', 'right', 'bottom', 'left'): prop = f'border_{side}_width' setattr(box, prop, box.style[prop]) diff --git a/weasyprint/layout/preferred.py b/weasyprint/layout/preferred.py index 4fb355c2..f5306011 100644 --- a/weasyprint/layout/preferred.py +++ b/weasyprint/layout/preferred.py @@ -226,8 +226,7 @@ def column_group_content_width(context, box): def table_cell_min_content_width(context, box, outer): """Return the min-content width for a ``TableCellBox``.""" children_widths = [ - min_content_width(context, child, outer=True) - for child in box.children + min_content_width(context, child) for child in box.children if not child.is_absolutely_positioned()] children_min_width = margin_width( box, max(children_widths) if children_widths else 0) @@ -408,11 +407,9 @@ def table_and_columns_preferred_widths(context, box, outer=True): min_width = block_min_content_width(context, table, outer=False) max_width = block_max_content_width(context, table, outer=False) outer_min_width = adjust( - box, outer=True, width=block_min_content_width( - context, table, outer=True)) + box, outer=True, width=block_min_content_width(context, table)) outer_max_width = adjust( - box, outer=True, width=block_max_content_width( - context, table, outer=True)) + box, outer=True, width=block_max_content_width(context, table)) result = ([], [], [], [], total_horizontal_border_spacing, []) context.tables[table] = result = { False: (min_width, max_width) + result, @@ -698,7 +695,7 @@ def flex_min_content_width(context, box, outer=True): # TODO: use real values, see # https://www.w3.org/TR/css-flexbox-1/#intrinsic-sizes min_contents = [ - min_content_width(context, child, outer=True) + min_content_width(context, child) for child in box.children if child.is_flex_item] if not min_contents: return adjust(box, outer, 0) @@ -714,7 +711,7 @@ def flex_max_content_width(context, box, outer=True): # TODO: use real values, see # https://www.w3.org/TR/css-flexbox-1/#intrinsic-sizes max_contents = [ - max_content_width(context, child, outer=True) + max_content_width(context, child) for child in box.children if child.is_flex_item] if not max_contents: return adjust(box, outer, 0) diff --git a/weasyprint/layout/replaced.py b/weasyprint/layout/replaced.py index c66edc31..866f833d 100644 --- a/weasyprint/layout/replaced.py +++ b/weasyprint/layout/replaced.py @@ -99,7 +99,7 @@ def replacedbox_layout(box): if object_fit == 'fill': draw_width, draw_height = box.width, box.height else: - if object_fit == 'contain' or object_fit == 'scale-down': + if object_fit in ('contain', 'scale-down'): draw_width, draw_height = contain_constraint_image_sizing( box.width, box.height, intrinsic_ratio) elif object_fit == 'cover': @@ -140,7 +140,7 @@ def replaced_box_width(box, containing_block): # This algorithm simply follows the different points of the specification: # https://www.w3.org/TR/CSS21/visudet.html#inline-replaced-width - if box.height == 'auto' and box.width == 'auto': + if box.height == box.width == 'auto': if width is not None: # Point #1 box.width = width @@ -173,12 +173,12 @@ def replaced_box_height(box): box.style['image_resolution'], box.style['font_size']) # Test 'auto' on the computed width, not the used width - if box.height == 'auto' and box.width == 'auto': + if box.height == box.width == 'auto': box.height = height elif box.height == 'auto' and ratio: box.height = box.width / ratio - if box.height == 'auto' and box.width == 'auto' and height is not None: + if box.height == box.width == 'auto' and height is not None: box.height = height elif ratio is not None and box.height == 'auto': box.height = box.width / ratio @@ -191,14 +191,14 @@ def replaced_box_height(box): def inline_replaced_box_layout(box, containing_block): """Lay out an inline :class:`boxes.ReplacedBox` ``box``.""" - for side in ['top', 'right', 'bottom', 'left']: + for side in ('top', 'right', 'bottom', 'left'): if getattr(box, f'margin_{side}') == 'auto': setattr(box, f'margin_{side}', 0) inline_replaced_box_width_height(box, containing_block) def inline_replaced_box_width_height(box, containing_block): - if box.style['width'] == 'auto' and box.style['height'] == 'auto': + if box.style['width'] == box.style['height'] == 'auto': replaced_box_width.without_min_max(box, containing_block) replaced_box_height.without_min_max(box) min_max_auto_replaced(box) @@ -269,7 +269,7 @@ def block_replaced_box_layout(context, box, containing_block): from .float import avoid_collisions box = box.copy() - if box.style['width'] == 'auto' and box.style['height'] == 'auto': + if box.style['width'] == box.style['height'] == 'auto': computed_margins = box.margin_left, box.margin_right block_replaced_width.without_min_max( box, containing_block) diff --git a/weasyprint/svg/__init__.py b/weasyprint/svg/__init__.py index cb434bfe..5a4fb64e 100644 --- a/weasyprint/svg/__init__.py +++ b/weasyprint/svg/__init__.py @@ -1,6 +1,7 @@ """Render SVG images.""" import re +from contextlib import suppress from math import cos, hypot, pi, radians, sin, sqrt from xml.etree import ElementTree @@ -415,10 +416,8 @@ class SVG: # Draw node if visible and node.tag in TAGS: - try: + with suppress(PointError): TAGS[node.tag](self, node, font_size) - except PointError: - pass # Draw node children if display and node.tag not in DEF_TYPES: diff --git a/weasyprint/svg/bounding_box.py b/weasyprint/svg/bounding_box.py index d2457c04..de38545e 100644 --- a/weasyprint/svg/bounding_box.py +++ b/weasyprint/svg/bounding_box.py @@ -252,7 +252,7 @@ def _bounding_box_elliptical_arc(x1, y1, rx, ry, phi, large, sweep, x, y): cx = cxprime * cos(phi) - cyprime * sin(phi) + (x1 + x) / 2 cy = cxprime * sin(phi) + cyprime * cos(phi) + (y1 + y) / 2 - if phi == 0 or phi == pi: + if phi in (0, pi): minx = cx - rx tminx = atan2(0, -rx) maxx = cx + rx @@ -261,7 +261,7 @@ def _bounding_box_elliptical_arc(x1, y1, rx, ry, phi, large, sweep, x, y): tminy = atan2(-ry, 0) maxy = cy + ry tmaxy = atan2(ry, 0) - elif phi == pi / 2 or phi == 3 * pi / 2: + elif phi in (pi / 2, 3 * pi / 2): minx = cx - ry tminx = atan2(0, -ry) maxx = cx + ry diff --git a/weasyprint/svg/utils.py b/weasyprint/svg/utils.py index e21d9e0e..30ec9a92 100644 --- a/weasyprint/svg/utils.py +++ b/weasyprint/svg/utils.py @@ -1,6 +1,7 @@ """Util functions for SVG rendering.""" import re +from contextlib import suppress from math import cos, radians, sin, tan from urllib.parse import urlparse @@ -29,12 +30,10 @@ def size(string, font_size=None, percentage_reference=None): if not string: return 0 - try: + with suppress(ValueError): return float(string) - except ValueError: - # Not a float, try something else - pass + # Not a float, try something else string = normalize(string).split(' ', 1)[0] if string.endswith('%'): assert percentage_reference is not None diff --git a/weasyprint/text/ffi.py b/weasyprint/text/ffi.py index eda147ce..09f614aa 100644 --- a/weasyprint/text/ffi.py +++ b/weasyprint/text/ffi.py @@ -1,6 +1,7 @@ """Imports of dynamic libraries used for text layout.""" import os +from contextlib import suppress import cffi @@ -391,10 +392,8 @@ ffi.cdef(''' def _dlopen(ffi, *names): """Try various names for the same library, for different platforms.""" for name in names: - try: + with suppress(OSError): return ffi.dlopen(name) - except OSError: - pass # Re-raise the exception. print( '\n-----\n\n' @@ -413,10 +412,8 @@ if hasattr(os, 'add_dll_directory'): # pragma: no cover 'WEASYPRINT_DLL_DIRECTORIES', 'C:\\Program Files\\GTK3-Runtime Win64\\bin').split(';') for dll_directory in dll_directories: - try: + with suppress((OSError, FileNotFoundError)): os.add_dll_directory(dll_directory) - except (OSError, FileNotFoundError): - pass gobject = _dlopen( ffi, 'gobject-2.0-0', 'gobject-2.0', 'libgobject-2.0-0',