From 9bbcc1ea355a396680ada6e4e238e25cc37c63f5 Mon Sep 17 00:00:00 2001 From: Mike Shevchenko Date: Fri, 18 Sep 2026 13:36:38 +0300 Subject: [PATCH] [Fixed][Windows] Layers cache written with \r\r\n row endings csv.writer emits \r\n, and the stream was opened in text mode without newline='', so on Windows the \n was translated again and every row of layers.csv ended \r\r\n. Reading it back produced a blank row between each real one, so load_cached_layers() raised IndexError on r[0] and the second diff of any board failed. Adds newline='' to both open() calls, as the csv module documents, and skips blank rows on read so caches already written by earlier versions keep working instead of having to be deleted. --- CHANGELOG.md | 5 +++++ kicad-diff.py | 9 +++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1203cda..19836b6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [Unreleased] +### Fixed +* Layers cache was written with `\r\r\n` row endings on Windows, so the second diff of any + board failed with an `IndexError` in `load_cached_layers` + ## [2.6.0] - 2026-06-01 ### Added * KiCad 10 variants support for PCB diff --git a/kicad-diff.py b/kicad-diff.py index 9b09203..1f9499f 100755 --- a/kicad-diff.py +++ b/kicad-diff.py @@ -696,11 +696,15 @@ def load_cached_layers(layers_file): layer_names = {} name_to_id = {} logger.debug('Loading layers from cache '+layers_file) - with open(layers_file) as csvfile: + # newline='' as the csv docs require; also tolerates caches written by older versions, + # where the missing newline='' made every row end \r\r\n and yield a blank row here + with open(layers_file, newline='') as csvfile: reader = csv.reader(csvfile) header = next(reader) logger.debug(header) for r in reader: + if not r: + continue ilnum = int(r[0]) lname = r[1] lname_user = r[2] @@ -721,7 +725,8 @@ def save_layers_to_cache(layers_file, all_layers, kiri_mode): makedirs(dname, exist_ok=True) if kiri_mode: return - with open(layers_file, 'wt') as csvfile: + # Without newline='' the writer's \r\n becomes \r\r\n on Windows, which reads back broken + with open(layers_file, 'wt', newline='') as csvfile: writer = csv.writer(csvfile) writer.writerow(('Layer ID', 'Layer name', 'User name')) writer.writerows(all_layers)