Please ignore secret bonuses. Secret tests do NOT award bonus. Max hw grade is 30+2 bonus efficiency

Do you need help?

File non chiusi nei test dell'HW 8 di recupero

Lovecrafts_Cat (600 points)
2 7 12
in Programmare in Python by (600 points)
edited by

Salve, vorrei far notare che nel file testlib.py vengono aperti dei file di testo senza fare uso del with statement, e mancando di close() dopo il loro utilizzo.

def check_img_file(self, a,b):
        f = open(a, "rb")
        g = open(b, "rb")
        #if sha1(f.read()).hexdigest() !=  sha1(g.read()).hexdigest():
        if f.read() !=  g.read():
            img_a = self.__image_load(a)
            img_b = self.__image_load(b)
            wa, ha = len(img_a[0]),len(img_a)
            wb, hb = len(img_b[0]),len(img_b)
            self.assertEqual(wa, wb, f"Images have different widths ({wa} != {wb})")
            self.assertEqual(ha, hb, f"Images have different heights ({ha} != {hb})")
            for y in range(ha):
                for x in range(wa):
                    ca, cb = img_a[y][x], img_b[y][x]
                    msg = 'Images differ, starting at coordinates {},{} (colors: {} != {})'.format(x, y, ca, cb)
                    self.assertEqual(ca, cb, msg)

Questo produce dei ResourceWarning minori riguardo a delle risorse di I/O mal gestite.


Apportando le seguenti modifiche minori, è possibile fixare questi warning:

def check_img_file(self, a,b):
        with open(a, "rb") as f_a:
            f = f_a.read()
        with open(b, "rb") as f_b:
            g = f_b.read()
        #if sha1(f.read()).hexdigest() !=  sha1(g.read()).hexdigest():
        if f != g:
            img_a = self.__image_load(a)
            img_b = self.__image_load(b)
            wa, ha = len(img_a[0]),len(img_a)
            wb, hb = len(img_b[0]),len(img_b)
            self.assertEqual(wa, wb, f"Images have different widths ({wa} != {wb})")
            self.assertEqual(ha, hb, f"Images have different heights ({ha} != {hb})")
            for y in range(ha):
                for x in range(wa):
                    ca, cb = img_a[y][x], img_b[y][x]
                    msg = 'Images differ, starting at coordinates {},{} (colors: {} != {})'.format(x, y, ca, cb)
                    self.assertEqual(ca, cb, msg)

Facendo dei test in locale, sembrerebbe che questa soluzione non causi errori con i test proposti dall'esercizio.
Spero che questo possa essere d'aiuto (e spero ancor di più di non aver commesso errori in questo report).

1 Answer

Best answer
andrea.sterbini (208020 points)
756 1270 2377
by (208k points)
selected by

GRAZIE! heart                                 .