Skip to content

Remove conditionally-compiled or unreachable debug C code - #9915

Open
akx wants to merge 13 commits into
python-pillow:mainfrom
akx:ndebug
Open

akx wants to merge 13 commits into
python-pillow:mainfrom
akx:ndebug

Conversation

@akx

@akx akx commented Aug 27, 2026

Copy link
Copy Markdown
Contributor

I was looking at Mode.c and noticed 0567f06 had gotten the semantics of NDEBUG wrong; NDEBUG is true when not debugging, so these checks were running all the time in release builds. That said, those checks weren't really necessary anyway; we can see that all of the constant ModeData in that file has a non-NULL mode string. It's not worth testing at runtime.

Following that, I took a look at other #ifdef'd code to get rid of other debug-like code that was never compiled in... and on top of that, there's a fun commit that removes an exit(1); from an unlikely error branch of Quant.c. I suppose Quant.c was ported from some application code in 1998 and no one noticed all of the exit()s.

@akx
akx marked this pull request as ready for review August 27, 2026 13:28
@akx akx changed the title Remove conditionally-compiled debug C code Remove conditionally-compiled or unreachable debug C code Sep 1, 2026
@radarhere
radarhere requested a review from wiredfool September 3, 2026 07:15

@wiredfool wiredfool left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So I'm seeing that we've got

  • multiple different conditional compliation flags for c level trace debugging.
  • Personally the tiff stuff was necessary when putting that together, but it's probably outlived 90% of it's usefulness. Especially since it borks the terminal when dumping under python3, but presumably that's fixable.
  • An actual misunderstanding of the NDEBUG flag
  • One random extra free.

Instrumentation like what's in the tiff bits is useful for seeing a big picture where debuggers don't give it.

I'm not going to object, at least assuming I don't have to add stuff like this back in.

Comment thread src/libImaging/Quant.c
printf("Oops, split failed...\n");
#endif
exit(1);
ImagingQuantHeapFree(h);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like something more than just a dead code removal.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's described in the PR message and commit message.

on top of that, there's a fun commit that removes an exit(1); from an unlikely error branch of Quant.c. I suppose Quant.c was ported from some application code in 1998 and no one noticed all of the exit()s.

Comment thread src/libImaging/Dib.c

dib->palette = CreatePalette(pal);
} else if (mode == IMAGING_MODE_RGB) {
#ifdef CUBE216

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this can never be set?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I've trawled through the Git history, it has never been set. You would've needed to know about this undocumented preprocessor variable, and compiled the library with it by hand, to get a different automatic palette for DIB exports.

The obvious way is to convert your RGB image to P mode in Pillow code with a palette of your heart's desire, and then DIB it, rather than have this code (from PIL 1.1.1, Oct 20 2000, if my archaeology is correct) guess a palette.

@akx

akx commented Sep 20, 2026

Copy link
Copy Markdown
Contributor Author

Instrumentation like what's in the tiff bits is useful for seeing a big picture where debuggers don't give it.

I'm not going to object, at least assuming I don't have to add stuff like this back in.

Yep, I'd assume people working with hairy code like the TIFF or quantization stuff would add their own prints in their working copy. (I know that's what I've done with stuff like #9829 and the like.) But those probably don't belong in main :)

Thanks for taking a look!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants