-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use ImagingCore.ptr instead of ImagingCore.id #8341
Conversation
1fddcce
to
9bd6434
Compare
PytestUnraisableExceptionWarning: Exception ignored in: <function PhotoImage.__del__> AttributeError: 'PhotoImage' object has no attribute '_PhotoImage__photo'
830a245
to
491e637
Compare
@python-pillow/pillow-team I need help here) Could anyone debug pypy3.10 on windows? I can't get what's wrong, since as I can see, the capsule object is correctly restored from the pointer and the capsule object itself is alive. |
10db853
to
bc83e33
Compare
Get it! In pypy https://github.com/pypy/pypy/blob/branches/py3.10/pypy/objspace/std/capsuleobject.py#L39 |
So is the motivation here just to simplify the code? Also, we don't necessarily need the deprecations - #4532 (comment)
|
Added description to PR.
While |
@@ -190,10 +181,10 @@ def paste(self, im: Image.Image) -> None: | |||
if image.isblock() and im.mode == self.__mode: | |||
block = image | |||
else: | |||
block = image.new_block(self.__mode, im.size) | |||
block = Image.core.new_block(self.__mode, im.size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I imagine this change was just because you didn't like image.new_block(self.__mode, im.size)
?
Rather than moving new_block
in C, wouldn't it have been better for the C method to access the size on the image object instead of being passed it? Then this call could become image.new_block(self.__mode)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I’d like to clarify that the reason for this change is not a matter of personal preference. The new_block
method doesn't pertain to an existing image but instead creates a new block based on the passed parameters. As such, it makes more sense for it to reside in the Image.core
module rather than being a method of the image object itself.
There is no intent to modify or enhance the behavior; I was simply correcting the method's improper placement.
I'm not aware of TK API, but it seems it converts any arguments to strings, at least |
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
In CPython, sure, but from your comments, not in PyPy. It seems simpler to note that CPython converts the capsule to a string, Depends on whether your thinking was already aligned with how Tk works, I guess. I've created uploadcare#148, but feel free to say you prefer the current version. |
That isn't in my comments. In both implementations, the Capsule is converted to a string. In the case of CPython, it's a string like |
Oh, I see, I very much misunderstood this comment. |
Interestingly, I guess this is what is preventing you from checking the capsule name against the magic with
|
This check is the best we can do here. It’s not as secure as PyCapsule_IsValid(), since it only verifies that the passed string is a correctly formatted string representation of the Capsule object. It’s still possible to craft a well-formatted string with the wrong pointer, which could result in a segmentation fault when calling a Tk operation. However, it’s still a significant improvement over the previous implementation, which simply obtained some number and assumed it was a pointer. |
For the sake of interest,
I looked, and this would be mirroring https://www.tcl.tk/man/tcl8.6/TclLib/CrtCommand.htm |
Co-authored-by: Andrew Murray <3112309+radarhere@users.noreply.github.com>
Co-authored-by: Hugo van Kemenade <1324225+hugovk@users.noreply.github.com>
Partially suppress #8340
The non-primary extensions in Pillow don't have direct access to Python's
_imaging
types, such asImaging_Type
andImagingObject
. However, they often still need to interact with objects passed from Python code. Currently, the raw memory pointer to anImaging
object (ImageCore.id
) is passed as an integer and then cast to a memory pointer:It's not safe and can lead to segmentation faults. Python has Capsule objects, which provide a safer way to pass pointers to C code. These are exposed through
ImageCore.ptr
.This PR deprecates the raw pointer properties
ImageCore.id
andImageCore.unsafe_ptrs
, and migrates non-primary extensions to interact with Capsule objects.