Skip to content

Commit

Permalink
Return error when attempting to serialize an empty path (#279)
Browse files Browse the repository at this point in the history
* Test for empty path warning

* Return ErrorCode::EmptyPath when path has too few points
  • Loading branch information
jatoben authored Nov 25, 2024
1 parent 1b36890 commit 21bbdad
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/gdstk/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ enum struct ErrorCode {
NoError = 0,
// Warnings
BooleanError,
EmptyPath,
IntersectionNotFound,
MissingReference,
UnsupportedRecord,
Expand Down
3 changes: 3 additions & 0 deletions python/gdstk_module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ static int return_error(ErrorCode error_code) {
case ErrorCode::NoError:
return 0;
// Warnings
case ErrorCode::EmptyPath:
if (PyErr_WarnEx(PyExc_RuntimeWarning, "Empty path.", 1) != 0) return -1;
return 0;
case ErrorCode::MissingReference:
if (PyErr_WarnEx(PyExc_RuntimeWarning, "Missing reference.", 1) != 0) return -1;
return 0;
Expand Down
6 changes: 3 additions & 3 deletions src/flexpath.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ void FlexPath::remove_overlapping_points() {

ErrorCode FlexPath::to_polygons(bool filter, Tag tag, Array<Polygon*>& result) {
remove_overlapping_points();
if (spine.point_array.count < 2) return ErrorCode::NoError;
if (spine.point_array.count < 2) return ErrorCode::EmptyPath;

const Array<Vec2> spine_points = spine.point_array;
uint64_t curve_size_guess = spine_points.count * 2 + 4;
Expand Down Expand Up @@ -774,7 +774,7 @@ ErrorCode FlexPath::to_gds(FILE* out, double scaling) {

remove_overlapping_points();

if (spine.point_array.count < 2) return error_code;
if (spine.point_array.count < 2) return ErrorCode::EmptyPath;

uint16_t buffer_end[] = {4, 0x1100};
big_endian_swap16(buffer_end, COUNT(buffer_end));
Expand Down Expand Up @@ -918,7 +918,7 @@ ErrorCode FlexPath::to_oas(OasisStream& out, OasisState& state) {

remove_overlapping_points();

if (spine.point_array.count < 2) return error_code;
if (spine.point_array.count < 2) return ErrorCode::EmptyPath;

bool has_repetition = repetition.get_count() > 1;

Expand Down
32 changes: 32 additions & 0 deletions tests/library_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,35 @@ def test_write_min_length_path(
source.cells[0].paths[0].spine(),
rw.cells[0].paths[0].spine(),
)


@pytest.mark.parametrize(
"simple_path", (True, False),
)
@pytest.mark.parametrize(
"write_f",
(gdstk.Library.write_oas, gdstk.Library.write_gds),
)
def test_empty_path_warning(
simple_path: bool,
write_f: Callable[[gdstk.Library, Union[str, pathlib.Path]], None],
tmp_path: pathlib.Path
):
lib = gdstk.Library("test")
tol = lib.precision / lib.unit

# Simple paths are written to GDS & OASIS with PATH records; non-simple
# paths are serialized as polygons.
path = gdstk.FlexPath(
((0, 0), (tol / 2, 0)),
width=0.01,
tolerance=tol,
simple_path=simple_path
)

cell = gdstk.Cell("top")
cell.add(path)
lib.add(cell)

with pytest.warns(RuntimeWarning, match="Empty path"):
write_f(lib, tmp_path / "out")

0 comments on commit 21bbdad

Please sign in to comment.