Skip to content

Commit a4b59b5

Browse files
authored
BUG: Reject in-memory streams for SEGY export (#1538)
1 parent 3548253 commit a4b59b5

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/xtgeo/cube/_cube_export.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import io
56
import json
67
import math
78
import struct
@@ -26,7 +27,16 @@ def export_segy(cube: Cube, sfile: str) -> None:
2627
Args:
2728
cube (:class:`xtgeo.cube.Cube`): The instance
2829
sfile (str): File name to export to.
30+
31+
Raises:
32+
TypeError: If ``sfile`` is an in-memory stream (e.g. ``BytesIO``).
2933
"""
34+
if isinstance(sfile, (io.StringIO, io.BytesIO)):
35+
raise TypeError(
36+
"SEGY export requires a filesystem path; in-memory streams are not "
37+
"supported."
38+
)
39+
3040
logger.debug("Exporting segy format using segyio")
3141
cvalues = cube.values
3242

tests/test_cube/test_cube.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import io
12
import logging
23
import pathlib
34

@@ -231,6 +232,16 @@ def test_segyio_import_export(tmp_path, pristine, smallcube):
231232
assert input_cube.values.flatten().tolist() == read_cube.values.flatten().tolist()
232233

233234

235+
def test_segy_export_to_bytesio_raises():
236+
"""SEGY cube export to BytesIO is not supported."""
237+
cube = xtgeo.Cube(ncol=3, nrow=2, nlay=5, xinc=10, yinc=10, zinc=1)
238+
cube.values = list(range(30))
239+
240+
stream = io.BytesIO()
241+
with pytest.raises(TypeError, match="filesystem path"):
242+
cube.to_file(stream, fformat="segy")
243+
244+
234245
def test_cube_resampling(loadsfile1):
235246
"""Import a cube, then make a smaller and resample, then export the new"""
236247

0 commit comments

Comments
 (0)