Skip to content

Commit fd0956f

Browse files
authored
In _dump(), use Python PPM save, instead of C (#9566)
1 parent e369e84 commit fd0956f

5 files changed

Lines changed: 15 additions & 119 deletions

File tree

Tests/test_image.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,11 @@ def test_dump(self, tmp_path: Path) -> None:
270270
im = Image.new("RGB", (10, 10))
271271
im._dump(str(tmp_path / "temp_RGB.ppm"))
272272

273+
im = Image.new("RGBA", (10, 10))
274+
im._dump(str(tmp_path / "temp_RGBA.ppm"))
275+
273276
im = Image.new("HSV", (10, 10))
274-
with pytest.raises(ValueError):
277+
with pytest.raises(OSError):
275278
im._dump(str(tmp_path / "temp_HSV.ppm"))
276279

277280
def test_comparison_with_other_type(self) -> None:

setup.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def get_version() -> str:
9494
"Draw",
9595
"Effects",
9696
"EpsEncode",
97-
"File",
9897
"Fill",
9998
"Filter",
10099
"FliDecode",

src/PIL/Image.py

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -731,24 +731,17 @@ def _ensure_mutable(self) -> None:
731731
def _dump(
732732
self, file: str | None = None, format: str | None = None, **options: Any
733733
) -> str:
734-
suffix = ""
735-
if format:
736-
suffix = f".{format}"
734+
suffix = f".{format}" if format else ""
737735

738-
if not file:
739-
f, filename = tempfile.mkstemp(suffix)
740-
os.close(f)
741-
else:
736+
if file:
742737
filename = file
743738
if not filename.endswith(suffix):
744-
filename = filename + suffix
745-
746-
self.load()
747-
748-
if not format or format == "PPM":
749-
self.im.save_ppm(filename)
739+
filename += suffix
750740
else:
751-
self.save(filename, format, **options)
741+
f, filename = tempfile.mkstemp(suffix)
742+
os.close(f)
743+
744+
self.save(filename, format or "PPM", **options)
752745

753746
return filename
754747

src/_imaging.c

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
#define S16(v) ((v) < 32768 ? (v) : ((v) - 65536))
109109

110110
/* -------------------------------------------------------------------- */
111-
/* OBJECT ADMINISTRATION */
111+
/* OBJECT ADMINISTRATION */
112112
/* -------------------------------------------------------------------- */
113113

114114
typedef struct {
@@ -375,7 +375,7 @@ ImagingError_ValueError(const char *message) {
375375
}
376376

377377
/* -------------------------------------------------------------------- */
378-
/* HELPERS */
378+
/* HELPERS */
379379
/* -------------------------------------------------------------------- */
380380

381381
static int
@@ -679,7 +679,7 @@ getink(PyObject *color, Imaging im, char *ink) {
679679
}
680680

681681
/* -------------------------------------------------------------------- */
682-
/* FACTORIES */
682+
/* FACTORIES */
683683
/* -------------------------------------------------------------------- */
684684

685685
static PyObject *
@@ -3607,7 +3607,7 @@ _effect_spread(ImagingObject *self, PyObject *args) {
36073607
}
36083608

36093609
/* -------------------------------------------------------------------- */
3610-
/* UTILITIES */
3610+
/* UTILITIES */
36113611
/* -------------------------------------------------------------------- */
36123612

36133613
static PyObject *
@@ -3642,25 +3642,6 @@ _getcodecstatus(PyObject *self, PyObject *args) {
36423642
return PyUnicode_FromString(msg);
36433643
}
36443644

3645-
/* -------------------------------------------------------------------- */
3646-
/* DEBUGGING HELPERS */
3647-
/* -------------------------------------------------------------------- */
3648-
3649-
static PyObject *
3650-
_save_ppm(ImagingObject *self, PyObject *args) {
3651-
char *filename;
3652-
3653-
if (!PyArg_ParseTuple(args, "s", &filename)) {
3654-
return NULL;
3655-
}
3656-
3657-
if (!ImagingSavePPM(self->image, filename)) {
3658-
return NULL;
3659-
}
3660-
3661-
Py_RETURN_NONE;
3662-
}
3663-
36643645
/* -------------------------------------------------------------------- */
36653646

36663647
/* methods */
@@ -3744,9 +3725,6 @@ static struct PyMethodDef methods[] = {
37443725
/* Special effects */
37453726
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},
37463727

3747-
/* Misc. */
3748-
{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},
3749-
37503728
/* arrow */
37513729
{"__arrow_c_schema__", (PyCFunction)ExportArrowSchemaPyCapsule, METH_VARARGS},
37523730
{"__arrow_c_array__", (PyCFunction)ExportArrowArrayPyCapsule, METH_VARARGS},

src/libImaging/File.c

Lines changed: 0 additions & 77 deletions
This file was deleted.

0 commit comments

Comments
 (0)