Skip to content

Conversation

@sblondon
Copy link
Contributor

@sblondon sblondon commented Dec 9, 2017

This patch implements the with statement 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 if close() 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 of with for the tests, not the interpreter. Another one (23865) is about idempotency for close(). Both are fixed.

https://bugs.python.org/issue32234

@csabella
Copy link
Contributor

Should this be added to the documentation page (Doc/library/mailbox.rst)?

@sblondon
Copy link
Contributor Author

Thank you for you remark @csabella.
I committed few lines in mailbox.rst about it.

Copy link
Member

@warsaw warsaw left a 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!

@bedevere-bot
Copy link

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

And if you don't make the requested changes, you will be put in the comfy chair!

@warsaw
Copy link
Member

warsaw commented Dec 10, 2017

Oh, and as for your test question: I don't think you need to mock close(). If you agree with the lock suggestion, then you can write the test to check that the mailbox is locked inside the CM and unlocked outside of it.

@sblondon
Copy link
Contributor Author

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 see several solutions:

  • implement a subclass of Maildir in the tests and check the context manager with this one.
  • use mock
  • the code is obvious, so there is no need to test it.

I didn't add the unlock() call in __exit__() because it's not necessary. The documentation says that close() 'unlock it if necessary'. The current implementations respect this behaviour. I agree to add it if you think it's better (because there will be a symetry: lock at enter, unlock at exit).

@bedevere-bot
Copy link

Thanks for making the requested changes!

@warsaw: please review the changes made to this pull request.

Copy link
Member

@warsaw warsaw left a 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!

@bedevere-bot
Copy link

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@sblondon
Copy link
Contributor Author

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@warsaw: please review the changes made to this pull request.

@sblondon
Copy link
Contributor Author

sblondon commented Jan 3, 2018

@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?

@csabella csabella requested a review from warsaw March 18, 2019 10:38
@csabella
Copy link
Contributor

csabella commented Jul 3, 2019

@maxking, please take a look at this one and the comments on the bug tracker to see if this can move forward. Thanks!

Copy link
Contributor

@maxking maxking left a comment

Choose a reason for hiding this comment

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

Overall I think this should be merged and is complete. I didn't see any objects/conflicts on BPO. If you can address the comments from @csabella and one small suggestion from me, we can get this merged.

Thanks for your contribution @sblondon !

Copy link
Member

@matrixise matrixise left a 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__)

Copy link
Member

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?

Copy link
Contributor Author

@sblondon sblondon Aug 5, 2019

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:

  • Maildir doesn't use the lock. So the test is not useful there.
  • Mailbox uses 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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, thank you!

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

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.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, that makes sense.

@bedevere-bot
Copy link

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. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@matrixise matrixise self-assigned this Sep 13, 2019
@github-actions
Copy link

This PR is stale because it has been open for 30 days with no activity.

@github-actions github-actions bot added the stale Stale PR or inactive for long period of time. label Aug 13, 2022
@encukou
Copy link
Member

encukou commented Feb 10, 2026

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.

@github-actions github-actions bot removed the stale Stale PR or inactive for long period of time. label Feb 10, 2026
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.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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__)

Copy link
Member

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.

@sblondon
Copy link
Contributor Author

Thanks everyone to restart working on this PR. I'm the first author of this PR. Do you expect something from me ?
I can rebase the patch on the last commit on python:main if you want.

@encukou
Copy link
Member

encukou commented Feb 12, 2026

Congratulations, you have one of the oldest still-open PRs in CPython :)

Do you expect something from me?

No, but thanks for the offer!
I've merged the main branch already (to get CI to run). The current problem is that docs linters are more nitpicky than in 2017, but to appease them I think it's best for me to push fixes to your branch.

@encukou
Copy link
Member

encukou commented Feb 12, 2026

@warsaw @matrixise, may I dismiss your "red" reviews?

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants