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).
Descrição
A função
extract_table_dataempacktools/sps/formats/pdf/pipeline/xml.py(linhas ~564-618) lançaUnboundLocalErrorquando o elementotable-wrapnão contém um elemento<table>filho (por exemplo, quando otable-wraptraz apenas<graphic>/alternativas).Causa
As variáveis
header_spanserow_spanssó são atribuídas dentro do blocoif table is not None:, mas são utilizadas noreturnfinal independentemente desse bloco ter sido executado:Como reproduzir
Resulta em 11 falhas, todas com:
Falhas afetadas:
TestExtractTableData::test_extract_table_data_completeTestExtractTableData::test_extract_table_data_empty_tableTestExtractTableData::test_extract_table_data_multiple_header_rowsTestExtractTableData::test_extract_table_data_no_label_no_titleTestExtractTableData::test_extract_table_data_no_tableTestExtractBodyData::test_extract_body_data_basicTestExtractBodyData::test_extract_body_data_with_nested_sectionsTestExtractBodyData::test_extract_body_data_with_table_referencesTestExtractBodyData::test_extract_body_data_with_tablesTestExtractSupplementaryData::test_app_group_with_tableTestExtractSupplementaryData::test_mixed_content_app_groupSugestão de correção
Inicializar
header_spans = []erow_spans = []junto comheaders/rows, antes doif table is not None:.Contexto
Identificado durante revisão/execução da suíte
tests/sps/formats(conversores SciELO → PDF).