Skip to content

Bug: extract_table_data lança UnboundLocalError quando table-wrap não contém <table> #1213

@Rossi-Luciano

Description

@Rossi-Luciano

Descrição

A função extract_table_data em packtools/sps/formats/pdf/pipeline/xml.py (linhas ~564-618) lança UnboundLocalError quando o elemento table-wrap não contém um elemento <table> filho (por exemplo, quando o table-wrap traz apenas <graphic>/alternativas).

Causa

As variáveis header_spans e row_spans só são atribuídas dentro do bloco if table is not None:, mas são utilizadas no return final independentemente desse bloco ter sido executado:

def extract_table_data(table_wrap):
    ...
    headers = []
    rows = []
    table = table_wrap.find('.//table')
    layout = determine_table_layout(table_wrap)

    if table is not None:
        thead = table.find('.//thead')
        if thead is not None:
            headers = _extract_table_rows_with_merged_cells(thead, 'th')
            header_spans = _extract_table_spans(thead, 'th')
        else:
            header_spans = []

        tbody = table.find('.//tbody')
        if tbody is not None:
            rows = _extract_table_rows_with_merged_cells(tbody, 'td')
            row_spans = _extract_table_spans(tbody, 'td')
        else:
            row_spans = []

    column_widths = _calculate_column_widths(headers, rows)

    return {
        ...
        'header_spans': header_spans,  # UnboundLocalError se table is None
        'row_spans': row_spans,
    }

Como reproduzir

.venv/bin/python -m pytest tests/sps/formats/pdf/pipeline/test_xml.py -q

Resulta em 11 falhas, todas com:

packtools/sps/formats/pdf/pipeline/xml.py:616: UnboundLocalError: cannot access local variable 'header_spans' where it is not associated with a value

Falhas afetadas:

  • TestExtractTableData::test_extract_table_data_complete
  • TestExtractTableData::test_extract_table_data_empty_table
  • TestExtractTableData::test_extract_table_data_multiple_header_rows
  • TestExtractTableData::test_extract_table_data_no_label_no_title
  • TestExtractTableData::test_extract_table_data_no_table
  • TestExtractBodyData::test_extract_body_data_basic
  • TestExtractBodyData::test_extract_body_data_with_nested_sections
  • TestExtractBodyData::test_extract_body_data_with_table_references
  • TestExtractBodyData::test_extract_body_data_with_tables
  • TestExtractSupplementaryData::test_app_group_with_table
  • TestExtractSupplementaryData::test_mixed_content_app_group

Sugestão de correção

Inicializar header_spans = [] e row_spans = [] junto com headers/rows, antes do if table is not None:.

Contexto

Identificado durante revisão/execução da suíte tests/sps/formats (conversores SciELO → PDF).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions