2017-06-16 77 views
4

當我運行單元測試時,我在下面的代碼中的「logo」圖像上得到了Python 3未關閉的緩衝區錯誤。如何正確關閉徽標圖像緩衝區?請注意,Image班來自reportlab.platypusPython3:Reportlab Image - ResourceWarning:unclosed file <_io.BufferedReader name = ...>

我試過logo.close()with Image(logo_path) as logo:,它們都不起作用。

>>python -m unittest tests.test_sample_pdf 

>>/tests/test_sample_pdf.py:51: ResourceWarning: unclosed file <_io.BufferedReader name='/Users/my_prj/statics/my-logo.gif'> 
     get_pdf() 

源代碼

import unittest 
import os 
from io import BytesIO 
from os.path import abspath, dirname 
from reportlab.lib.colors import HexColor 
from reportlab.lib.enums import TA_RIGHT 
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle 
from reportlab.lib.units import inch, cm, mm 
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, BaseDocTemplate, Paragraph, Image, Spacer 


COL_SORT = [{"headerName": "name", 
      "field": "name", 
      "width": 1000,}] 

def get_pdf(): 
    # setup PDF template 
    buffer = BytesIO() 
    side_margin = 12 
    col_widths = [row['width'] for row in COL_SORT] 
    page_width = sum(col_widths) + side_margin * 3 
    pdf = SimpleDocTemplate(buffer, pagesize=(page_width, 8.5 * inch), rightMargin=side_margin, leftMargin=side_margin, 
          topMargin=side_margin, bottomMargin=side_margin) 
    elements = [] 

    # logo 
    parent_dir = dirname(dirname(abspath(__file__))) 
    logo_path = os.path.join(parent_dir, 'statics', 'my-logo.gif') 
    logo = Image(logo_path) 
    logo.hAlign = 'LEFT' 

    heading_style = ParagraphStyle(name='heading', fontSize=16, leading=20, spaceAfter=0, 
            textColor=HexColor('#ffffff'), backColor=HexColor('#465a81')) 
    heading_right_style = ParagraphStyle(name='heading', fontSize=16, leading=20, spaceAfter=0, 
             textColor=HexColor('#ffffff'), backColor=HexColor('#465a81'), 
             alignment=TA_RIGHT) 
    logo_tbl = Table([[logo]], colWidths=sum(col_widths)) 
    logo_tbl.hAlign = 'LEFT' 
    logo_tbl.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, -1), HexColor('#B90002'))])) 
    elements.append(logo_tbl) 

    # build PDF 
    pdf.build(elements) 
    pdf_string = buffer.getvalue() 
    buffer.close() 

class TestPDF(unittest.TestCase): 
    def test_pdf(self): 
     get_pdf() 
+0

'德爾logo'不一定關閉文件。 – ForceBru

+0

顯示完整追蹤 – stovfl

+0

我已經顯示完整的Traceback和完整的源代碼。 – user1187968

回答

5

似乎reportlab預計打開和關閉圖像文件。使用with open(logo_path, 'rb') as image_fd:

此變通辦法解決了警告。我已經添加了提到的with並縮進了以下幾行。

def get_pdf(): 
    # setup PDF template 
    buffer = BytesIO() 
    side_margin = 12 
    col_widths = [row['width'] for row in COL_SORT] 
    page_width = sum(col_widths) + side_margin * 3 
    pdf = SimpleDocTemplate(buffer, pagesize=(page_width, 8.5 * inch), rightMargin=side_margin, leftMargin=side_margin, 
          topMargin=side_margin, bottomMargin=side_margin) 
    elements = [] 

    # logo 
    parent_dir = dirname(dirname(abspath(__file__))) 
    logo_path = os.path.join(parent_dir, 'statics', 'nci-logo.gif') 
    with open(logo_path, 'rb') as image_fd:   # edited this line 
     logo = Image(image_fd)       # ... and this line 
     logo.hAlign = 'LEFT' 

     heading_style = ParagraphStyle(name='heading', fontSize=16, leading=20, spaceAfter=0, 
            textColor=HexColor('#ffffff'), backColor=HexColor('#465a81')) 
     heading_right_style = ParagraphStyle(name='heading', fontSize=16, leading=20, spaceAfter=0, 
             textColor=HexColor('#ffffff'), backColor=HexColor('#465a81'), 
             alignment=TA_RIGHT) 
     logo_tbl = Table([[logo]], colWidths=sum(col_widths)) 
     logo_tbl.hAlign = 'LEFT' 
     logo_tbl.setStyle(TableStyle([('BACKGROUND', (0, 0), (-1, -1), HexColor('#B90002'))])) 
     elements.append(logo_tbl) 

     # build PDF 
     pdf.build(elements) 
     pdf_string = buffer.getvalue() 
     buffer.close() 

輸出:

$ python -m unittest tests.test_sample_pdf 
. 
---------------------------------------------------------------------- 
Ran 1 test in 0.042s 

OK 

我已經把完整的示例中Github

相關問題