Repository navigation
new public API - #574
new public API#574
Conversation
|
At the moment, I just pipe everything through the new @almarklein We didn't discuss the replacement for |
gc will recycle them when the array is destroyed
|
There it is :) This should pump the entire public API through
If called with |
|
Thanks! It's interesting to see the code move into the direction that we discussed! |
|
I also like the idea of a wrapper plugin to be able to keep the old plugin system for the time being. Should help make the transition more smooth. |
rfezzani
left a comment
There was a problem hiding this comment.
Here is my first round, very promising 😉! Thank you @FirefoxMetzger
|
As far as I can see, there are 4 points we need to solve for this PR before we can seriously look at merging it:
|
The Pillow plugin would make sense, I think. I wonder if we can take smaller steps. Doing the whole transition in a single PR would become such a big change that it wont be possible to oversee it. My suggestion is that in this PR the foundations are made to start the transition process. Then transform plugins one at a time in different PR's. |
|
@almarklein Absolutely. I don't intend to convert all plugins in this PR. My reasoning behind having one is that it might be good to have an example plugin (a) for reference, (b) to not "dream up" documentation on how to write plugins but to write it based on working code, and (c) to be able to write meaningful unit tests for the plugin manager. |
|
Mmm, yeah that makes sense. |
I updated the test to see if the problem really comes from the legacy reader being destroyed. (It works fine in CPython, but breaks in pypy; I don't have pypy)
|
Solved :) It's not my prettiest bugfix, but I think it should do the trick. What I'm doing now is that I pass |
|
From my side, I am happy with this PR. The codecov/patch test fails only because of legacy_pillow.py, which I only renamed (but git tracks it as a new file), so I am not concerned about it. Functional/API documentation is written; however, more documentation and usage examples still need to be done. I would do these in a separate PR though, because this one is becoming so massive that it became hard to review. I'd rather add further refactoring and documentation in a series of small PRs... @almarklein Let me know what you think, and what should be added from your side to get this PR merged. |
| arr = arr.astype(np.uint8) | ||
| mode = mode_base = "L" | ||
|
|
||
| except UnidentifiedImageError: |
There was a problem hiding this comment.
I think we can simply use the code from can_read and can_write here. As it is now, every image is opened twice.
Also, let's raise a more specific error when a plugin cannot process a request. NotImplementedError seems suited, or perhaps a custom error based on it.
There was a problem hiding this comment.
I ended up going with an exception instead of can_read/can_write, because you mentioned in a comment that plugins should raise an exception if they can't handle a request ( #574 (comment) ). It follows the pythonic mantra of "better ask for forgiveness than for permission". Personally, I like the idea from an API design perspective: A plugin just handles the request, and if that fails it screams .... loudly.
As it is now, every image is opened twice.
Maybe I misunderstood something, but in my mind the image file is handled by the stateful request object and will be kept open. We hand the same request to every plugin and, once bytes are read, they will remain in a memory buffer; this should be very fast to read.
Also, let's raise a more specific error when a plugin cannot process a request. NotImplementedError seems suited, or perhaps a custom error based on it.
I was thinking about this, too, when I was writing that code.
I first had a NotImplementedError, but I think doesn't match what is happening. It's not that the code to read/write/do-stuff isn't implemented - chances are it will never be implemented -, but rather that the request can not be handled (e.g. we are trying to open a .mp4 using pillow). Essentially, the input value passed to the constructor function is not valid, so I ended up choosing ValueError. I'm happy to raise a different error though if you think it makes more sense.
I have a personal thing against custom errors. I've never used them in a real project, and when I've encountered them they could have likely been refactored into a standard error + a good error string/message. We can introduce them, but I don't really understand how they add anything over StandardError("This is what went wrong.").
There was a problem hiding this comment.
Ah, I just realized I may have misunderstood you. Do you mean using legacy_pillow's can_read/can_write to check if pillow is compatible?
There was a problem hiding this comment.
Sorry for not being more clear. I meant using the code in the old canread/canwrite to better deterine when to raise an error here :)
Maybe I misunderstood something, but in my mind the image file is handled by the stateful request object and will be kept open.
Yes you're right. I was triggered by the use of get_file. Ideally the code that determines whether or not a file can be dealt with should only use getfirstbytes. Think e.g. of non-seekable files.
In general I am opposed to custom errors too. But in this case we really want to distinguish the plugins I-cannot-handle-this signal from something actually going wrong. E.g. with the ValueError now, it's very easy for a bug in the plugin to raise ValueError and the format manager to therefore consider that the plugin cannot deal with the request. And the user will never know.
There was a problem hiding this comment.
Regarding get_file, I can use can_read/can_write code instead. It feels a bit like we are digging in pillow's internals here, but I will see if I can refactor things.
Regarding the error, I'll change it to a custom error and we can see how things play out.
There was a problem hiding this comment.
ok, in this case I will leave the seek(0) behavior for another PR. I tried it just now and it looks like I need to dig into FreeImage and DICOM to see why their tests break if I reset the file before handing it over to LegacyPlugin. I would want to do this in a new PR that is a little more reviewable.
There was a problem hiding this comment.
I've added the _can_write logic to pillow's constructor. There is one change I need to make: the elif should be an else. Afterward coverage should go up to 100% for pillow again. Otherwise, the changes of today should conclude this thread as well.
This seems to be the last open point for this PR. If there is anything else from your side @almarklein please feel free to comment, otherwise I'd say we summarize in the main thread and then get this thing merged :)
There was a problem hiding this comment.
I forgot where we are now on the rewinding/first_bytes/auto-caching stuff, but maybe we should just cover that in subsequent prs :)
There was a problem hiding this comment.
Anything else you want to do / summarize before I hit that big green button?
There was a problem hiding this comment.
I forgot where we are now on the rewinding/first_bytes/auto-caching stuff, but maybe we should just cover that in subsequent prs :)
Yes, that's why I don't want to add more stuff here and start adding smaller chunks in dedicated PRs.
My last stand is that the best approach would be to refactor Request to have sole ownership of the raw file. It reads the file into memory on demand (maybe we can use python's mmap module here) and upon get_file() it serves a new read-only file object to the plugin which is destroyed once the plugin is done with it. In there, plugins get to do what they want and we can optimize serving cached content vs content read on-demand based on file size and and access frequency, etc.
From the top of my head, here are additional things that we need to sort out after this one is merged:
Other stuff that should happen sooner or later:
This is all stuff for new issues / new PRs, so from my side I think I am happy to get this finalized. @almarklein Should I create issues for each point mentioned above? |
Yes, that would be good! |
|
Thanks for this massive work! |
Closes: #569 (Discussion for a new public API in v3.0.0)
As a first step towards the new API, I am putting together a PR to test/demonstrate backward compatibility. The idea is to introduce both, the
imopenfactory as well as alegacyplugin. This plugin serves two functions:We could introduce the new API in v2.10.0 and default to the old API with an optional switch into the new API. Starting with v3.0.0 the new API would replace the old one. How much of the old API we want to keep around at that point is open for discussion.
For future reference, a link to a comment in this issue that I keep citing: #574 (comment)