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

Blocks.py BlockBox has no attribute index #1191

Closed
JohannesMunk opened this issue Aug 14, 2020 · 5 comments
Closed

Blocks.py BlockBox has no attribute index #1191

JohannesMunk opened this issue Aug 14, 2020 · 5 comments
Labels
crash Problems preventing documents from being rendered
Milestone

Comments

@JohannesMunk
Copy link

Dear WeasyPrint-Team,

one more crash reduced enough to report:

File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 839
in find_earlier_page_break resume_at = (new_child.index, resume_at)
AttributeError: 'BlockBox' object has no attribute 'index'
Traceback (most recent call last):
  File "I:\yRuntimes-x64-msvc2017\Python\lib\runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "I:\yRuntimes-x64-msvc2017\Python\lib\runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "I:\xClients\WeasyPrint\weasyprint\__main__.py", line 248, in <module>
    main()
  File "I:\xClients\WeasyPrint\weasyprint\__main__.py", line 239, in main
    main_doc = getattr(html, 'render')(**kwargsRender)
  File "I:\xClients\WeasyPrint\weasyprint\__init__.py", line 174, in render
    optimize_images, font_config, counter_style, image_cache)
  File "I:\xClients\WeasyPrint\weasyprint\document.py", line 401, in _render
    [Page(page_box, enable_hinting) for page_box in page_boxes],
  File "I:\xClients\WeasyPrint\weasyprint\document.py", line 401, in <listcomp>
    [Page(page_box, enable_hinting) for page_box in page_boxes],
  File "I:\xClients\WeasyPrint\weasyprint\layout\__init__.py", line 123, in layout_document
    pages = list(make_all_pages(context, root_box, html, pages))
  File "I:\xClients\WeasyPrint\weasyprint\layout\pages.py", line 800, in make_all_pages
    page, resume_at = remake_page(i, context, root_box, html)
  File "I:\xClients\WeasyPrint\weasyprint\layout\pages.py", line 739, in remake_page
    page_number, page_state)
  File "I:\xClients\WeasyPrint\weasyprint\layout\pages.py", line 550, in make_page
    positioned_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 60, in block_level_layout
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 74, in block_level_layout_switch
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 127, in block_box_layout
    absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 508, in block_container_layout
    absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 60, in block_level_layout
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 74, in block_level_layout_switch
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 127, in block_box_layout
    absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 508, in block_container_layout
    absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 60, in block_level_layout
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 74, in block_level_layout_switch
    page_is_empty, absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 127, in block_box_layout
    absolute_boxes, fixed_boxes, adjoining_margins)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 553, in block_container_layout
    new_children, absolute_boxes, fixed_boxes)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 825, in find_earlier_page_break
    child.children, absolute_boxes, fixed_boxes)
  File "I:\xClients\WeasyPrint\weasyprint\layout\blocks.py", line 839, in find_earlier_page_break
    resume_at = (new_child.index, resume_at)
AttributeError: 'BlockBox' object has no attribute 'index'
<html>
    <head>
        <style>
            .colCount2{
                column-count:2;
            }
            .nobreak{
                break-after:avoid;
            }
            .box{
                border:1px solid black;
                width:350px;
                height:350px;
            }
        </style>
    </head>
    <body>
        <section>
            <p>A</p>
        </section>
        <section>
            <div class="colCount2">
                <div class="nobreak">
                    <div class="box"></div>
                </div>
                <div class="nobreak">
                    <div class="box"></div>
                    <p>a</p>
                    <p>a</p>
                </div>
            </div>
            <div class="colCount2">
                <div class="nobreak">
                    <div class="box"></div>
                    <p>a</p>
                </div>
            </div>
        </section>
    </body>
</html>

Please let me know, if I can do anything different in preparing/reducing these examples to make your life easier.

Thanks again for your efforts!

I wish you a good weekend,

Johannes

@liZe
Copy link
Member

liZe commented Aug 14, 2020

Hello!

Isn’t this issue the same as #1168?

@Tontyna
Copy link
Contributor

Tontyna commented Aug 15, 2020

Reason to crash is the same as in #1168.

When WeasyPrint paginates a multi-column block, columns_layout wraps the columns into an anonymous BlockBox.

When trying to find an opportunity to break a multi-column box, find_earlier_page_break recurses the given children and their grandchildren.
Sooner or later it will bump into such an anonymous column BlockBox which (of course) doesn't have no index in its non-laid-out parent.

Thus: AttributeError.

The solution is skipping if child.is_column like we already do for child.is_header or child.is_footer.
After all we cannot break a multi-column block at an arbitrary position in an arbitrary column... that's the job of columns_layout, i.e. the multi-column block itself.

@liZe Are there more anonymous BlockBoxes without index in their non-laid-out parent lurking? Should we continue if !hasattr(child, 'index')?

@liZe liZe added the crash Problems preventing documents from being rendered label Sep 8, 2020
@mag-chang
Copy link

Hello, great developers of WeasyPrint.

Thank you for developing a great library.

I had the same problem yesterday.

If the text is set to a two-column layout, the index attribute will appear to be lost depending on the page break position.

As @Tontyna mentioned earlier, if you try to avoid the index attribute if it doesn't exist, the PDF will output without any problems.

I'll patch the code here with if hasattr(new_child, 'index') and run it for a while.

Have a nice weekend!

@liZe
Copy link
Member

liZe commented Dec 22, 2020

The solution is skipping if child.is_column like we already do for child.is_header or child.is_footer.
After all we cannot break a multi-column block at an arbitrary position in an arbitrary column... that's the job of columns_layout, i.e. the multi-column block itself.

❤️

@liZe Are there more anonymous BlockBoxes without index in their non-laid-out parent lurking? Should we continue if !hasattr(child, 'index')?

I wonder why columns, table elements, flex items are currently marked as in normal flow.

@liZe liZe closed this as completed in c35a629 Dec 22, 2020
@liZe
Copy link
Member

liZe commented Dec 22, 2020

I'll patch the code here with if hasattr(new_child, 'index') and run it for a while.

Have a nice weekend!

@mag-chang Hi!

If you’re still interested in this issue, could you please try c35a629 and see if it fixes your problem?

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Problems preventing documents from being rendered
Projects
None yet
Development

No branches or pull requests

4 participants