{"record":{"id":"8dcc3751b38c9722","repo":"donnemartin/interactive-coding-challenges","slug":"invalid-arg-empty-screen-or-width","errorCode":null,"errorMessage":"Invalid arg: Empty screen or width","messagePattern":"Invalid arg: Empty screen or width","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"bit_manipulation/draw_line/draw_line_solution.ipynb","lineNumber":163,"sourceCode":"   \"cell_type\": \"markdown\",\n   \"metadata\": {},\n   \"source\": [\n    \"## Code\"\n   ]\n  },\n  {\n   \"cell_type\": \"code\",\n   \"execution_count\": 1,\n   \"metadata\": {},\n   \"outputs\": [],\n   \"source\": [\n    \"class BitsScreen(object):\\n\",\n    \"\\n\",\n    \"    def draw_line(self, screen, width, x1, x2):\\n\",\n    \"        if None in (screen, width, x1, x2):\\n\",\n    \"            raise TypeError('Invalid argument: None')\\n\",\n    \"        if not screen or not width:\\n\",\n    \"            raise ValueError('Invalid arg: Empty screen or width')\\n\",\n    \"        MAX_BIT_VALUE = len(screen) * 8\\n\",\n    \"        if x1 < 0 or x2 < 0 or x1 >= MAX_BIT_VALUE or x2 >= MAX_BIT_VALUE:\\n\",\n    \"            raise ValueError('Invalid arg: x1 or x2 out of bounds')\\n\",\n    \"        start_bit = x1 % 8\\n\",\n    \"        end_bit = x2 % 8\\n\",\n    \"        first_full_byte = x1 // 8\\n\",\n    \"        if start_bit != 0:\\n\",\n    \"            first_full_byte += 1\\n\",\n    \"        last_full_byte = x2 // 8\\n\",\n    \"        if end_bit != (8 - 1):\\n\",\n    \"            last_full_byte -= 1\\n\",\n    \"        for byte in range(first_full_byte, last_full_byte + 1):\\n\",\n    \"            screen[byte] = int('11111111', base=2)\\n\",\n    \"        start_byte = x1 // 8\\n\",\n    \"        end_byte = x2 // 8\\n\",\n    \"        if start_byte == end_byte:\\n\",\n    \"            left_mask = (1 << (8 - start_bit)) - 1\\n\",\n    \"            right_mask = ~((1 << (8 - end_bit - 1)) - 1)\\n\",","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/donnemartin/interactive-coding-challenges/blob/358f2cc60426d5c4c3d7d580910eec9a7b393fa9/bit_manipulation/draw_line/draw_line_solution.ipynb#L145-L181","documentation":"BitsScreen.draw_line raises ValueError('Invalid arg: Empty screen or width') when the screen buffer is empty/falsy or width is 0. Drawing a line requires at least one byte of screen and a positive width, so truthiness is checked before computing MAX_BIT_VALUE.","triggerScenarios":"draw_line(bytearray(), 8, 0, 0) or draw_line(screen, 0, 0, 7) — an empty bytearray or zero width; also passing a screen variable that was never allocated.","commonSituations":"Allocating the screen buffer conditionally and skipping allocation on some path; computing width from a division/config value that evaluates to 0.","solutions":["Allocate a non-empty screen: screen = bytearray(height_bytes)","Ensure width is a positive multiple related to screen size before calling","Add an early guard that skips drawing when there is nothing to draw"],"exampleFix":"# before\nscreen = bytearray(0)\nbits.draw_line(screen, 16, 0, 15)\n\n# after\nscreen = bytearray(2)  # 16 bits\nbits.draw_line(screen, 16, 0, 15)","handlingStrategy":"validation","validationCode":"if not screen or not width:\n    raise ValueError('screen buffer and width must be non-empty/positive')\nbits_screen.draw_line(screen, width, x1, x2)","typeGuard":"def valid_screen(buf, w) -> bool:\n    return bool(buf) and isinstance(w, int) and w > 0","tryCatchPattern":"try:\n    bits_screen.draw_line(screen, width, x1, x2)\nexcept ValueError as e:\n    if 'Empty screen' in str(e):\n        screen = bytearray(max(1, width // 8))\n        bits_screen.draw_line(screen, width, x1, x2)\n    else:\n        raise","preventionTips":["Allocate buffers eagerly, not conditionally","Treat zero-width as a skip condition early in render loops","Derive buffer size from width so they stay consistent"],"tags":["python","value-error","empty-buffer","graphics","argument-validation"],"backgroundTag":"empty-argument-value","analyzedSha":"358f2cc60426d5c4c3d7d580910eec9a7b393fa9","analyzedAt":"2026-08-28T10:16:54.480Z","schemaVersion":2},"datasetVersion":"2026-08-28T11:17:15.048Z"}