-
-
Notifications
You must be signed in to change notification settings - Fork 34.1k
bpo-32234: Context manager available for mailbox instances #4770
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
base: main
Are you sure you want to change the base?
Conversation
|
Should this be added to the documentation page (Doc/library/mailbox.rst)? |
|
Thank you for you remark @csabella. |
warsaw
left a comment
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 claim that locking the underlying mailbox is the only reasonable semantic for entering the CM.
Also, please use blurb to add a NEWS entry. Other than that, I think this is a good addition. Thanks!
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase And if you don't make the requested changes, you will be put in the comfy chair! |
|
Oh, and as for your test question: I don't think you need to mock |
|
I have made the requested changes; please review again I don't understand how to check easily because there are no boolean (or equivalent) to know if the lock has been acquired.
I didn't add the |
|
Thanks for making the requested changes! @warsaw: please review the changes made to this pull request. |
warsaw
left a comment
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.
This looks great. I have just a minor comment about the documentation. Please fix that and then I think we're good to go. Thanks for your contribution!
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
|
I have made the requested changes; please review again |
|
Thanks for making the requested changes! @warsaw: please review the changes made to this pull request. |
|
@warsaw Sorry to recall you about this issue. Should I do something more? Or you didn't have the time to review it and I just have to wait? |
|
@maxking, please take a look at this one and the comments on the bug tracker to see if this can move forward. Thanks! |
maxking
left a comment
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.
Misc/NEWS.d/next/Library/2017-12-15-09-32-57.bpo-32234.XaOkhR.rst
Outdated
Show resolved
Hide resolved
matrixise
left a comment
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.
Thank you for your contribution, but could you add a test for the lock with the with statement?
| # Mailboxes are usable as a context manager | ||
| with self._factory(self._path) as box: | ||
| self.assertIsInstance(box, self._box.__class__) | ||
|
|
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.
Could you add a test for the lock?
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.
@matrixise I'm unsure how to do it right. For example:
Maildirdoesn't use the lock. So the test is not useful there.Mailboxuses it and stores the state in an internal boolean variable (self._lock). So the test would check the implementation.
It would be nice to have a locked attribute for Mailbox but it will not work for each case and is probably too invasive for this patch.
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.
good question, I don't remember your code, but I am going to assign myself your PR.
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.
Ok, thank you!
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.
This can be tested in _TestSingleFile, which tests the classes that have _locked and _file attributes.
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.
It looks like this test is redundant now with the new test.
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.
The other test is only for the subclasses that have _locked & _file. This one tests that all Mailboxes are usable as a context manager.
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.
Ah, that makes sense.
|
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
…ondon/cpython into mailbox_with_context_manager
…lbox_with_context_manager
|
This PR is stale because it has been open for 30 days with no activity. |
|
This PR is stalled but looks ready to go, let's push it over the finish line now? I updated, and took the liberty of touching up the tests, as @matrixse assigned himself for that. |
Doc/library/mailbox.rst
Outdated
| The :class:`Mailbox` class supports the :keyword:`with` statement. When used | ||
| like this, the Mailbox acquires a lock when the :keyword:`with` statement | ||
| enters and releases it and :meth:`close` when the :keyword:`with` statement | ||
| exits. |
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.
| exits. | |
| The :class:`Mailbox` class supports the :keyword:`with` statement. When used | |
| as a context manager, :class:`!Mailbox` calls :meth:`lock` when the context is entered, | |
| returns the mailbox object as the context object, and at context end calls :meth:`close`, | |
| thereby releasing the lock. |
| # Mailboxes are usable as a context manager | ||
| with self._factory(self._path) as box: | ||
| self.assertIsInstance(box, self._box.__class__) | ||
|
|
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.
It looks like this test is redundant now with the new test.
Misc/NEWS.d/next/Library/2017-12-15-09-32-57.bpo-32234.XaOkhR.rst
Outdated
Show resolved
Hide resolved
Co-authored-by: R. David Murray <rdmurray@bitdance.com>
|
Thanks everyone to restart working on this PR. I'm the first author of this PR. Do you expect something from me ? |
|
Congratulations, you have one of the oldest still-open PRs in CPython :)
No, but thanks for the offer! |
|
@warsaw @matrixise, may I dismiss your "red" reviews? |
This patch implements the
withstatement for all mailbox formats.The test is limited to syntax and instance checking because there is no way to check if
close()has been called. If you prefer, I can write a specific subclass of Mailbox or make a mock to check ifclose()has been called.However, as I'm not sure there is a consensus about what it the requested behaviour at
__exit__(), I consider it more as a first step.I tried to find previous discussions in the bug tracker, without success. Bug 7944 is about some
close()not called and suggests the use ofwithfor the tests, not the interpreter. Another one (23865) is about idempotency forclose(). Both are fixed.https://bugs.python.org/issue32234