Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7697589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:03:09+00:00 2026-05-31T22:03:09+00:00

I understand why contextlib.nested is deprecated . But if I write a program for

  • 0

I understand why contextlib.nested is deprecated.

But if I write a program for an old python version w/o the multiple form of with (i. e., < 2.7), I have (nearly) no other choice.

In order to avoid the following construct to fail:

with nested(open("f1"), open("f2")) as (f1, f2):

(f1 wouldn’t be closed if opening f2 fails, because the contextmanager is not entered)

I could imagine writing a context manager which moves initialization into its __enter__:

@contextmanager
def late_init(f, *a, **k):
    r = f(*a, **k)
    with r as c: yield c

Am I right thinking that

with nested(late_init(open, "f1"), late_init(open, "f2")) as (f1, f2):

will suffice here to make it “clean”?


The use case given is just an example. Imagine you have a list of files whose length is not prematurely known. Then neither the 2.7 composed with is usable nor the pre-2.7 nested way with several indented with statements.


I probably have to be more verbose about that.

The said worharound solves the prolem at first glance: calling the function is executed in a secure place so that failure can be detected and dealt with appropriately.

My question is: Does it cure the flaw, or do I get other problems?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T22:03:11+00:00Added an answer on May 31, 2026 at 10:03 pm

    I now saw that my solution (late_init()) would probably cure the first quirk:

    Firstly, as the context managers are all constructed before the
    function is invoked, the __new__() and __init__() methods of the inner
    context managers are not actually covered by the scope of the outer
    context managers. That means, for example, that using nested() to open
    two files is a programming error as the first file will not be closed
    promptly if an exception is thrown when opening the second file.

    But the second one:

    Secondly, if the __enter__() method of one of the inner context
    managers raises an exception that is caught and suppressed by the
    __exit__() method of one of the outer context managers, this construct will raise RuntimeError rather than skipping the body of the with
    statement.

    (which is probably caused by the skipped yield vars from nested()) is not covered by that.

    So either

    1. the use of “exception eaters” in combination with nested() should be avoided,
    2. the given nested() should be replaced with a better one or
    3. the use of variable length exception manager lists should be avoided and old Python versions as well.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand the principles but i have a hard time seeing where the practical
I understand these are necessary...of course to write proper code, but is there a
I understand what read and write do when used as a properties but I'm
I understand the pros of using namespaces within the actual application layers but when
I understand the value of the three-part service/host/client model offered by WCF. But is
I understand how to use it, but the syntax of it bothers me. What
I understand the reason i should use retain , but why should I ever
I understand that there is a *.className selector since there can be multiple html
I understand the normal ASC and DESC ORDER BY clause. However, I have a
I understand that the CLR needs to do marshaling in some cases, but let's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.