GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — `NotImplementedError` should be thrown on feeding inappripriate objects into the `with` statement · Issue #94724 · python/cpython

NotImplementedError should be thrown on feeding inappripriate objects into the with statement #94724

New issue
Copy link
New issue
Copy link
Closed as not planned
Closed as not planned
NotImplementedError should be thrown on feeding inappripriate objects into the with statement#94724
Copy link
Labels
type-featureA feature request or enhancementA feature request or enhancement

Description

@arhadthedev
arhadthedev
opened on Jul 10, 2022
Issue body actions

Feature or enhancement

When an object like socket.socket is used in with statement, NotImplementedError should be risen.

Pitch

The standard library has some classes that implement thin low-level wrappers but look and feel like manageable resources. For example, socket.socket has the same semantics as a file stream so a library user can conclude that an ability to use a file in with means that every openable I/O stream may be used in such a way.

However, it leads to hard to track bugs. A default behavior of __exit__ is to do nothing so we get untraceable resource leaks or stream deadlocks. For example, a server thread needs to close all of its sockets if an exception occurs (otherwise there will be a lonely server socket with a hanged client and a leaked owner thread).

According to documentation, the issue is solved by wrapping such an object into contextlib.closing but it comes in a hard postfactum way.

Anectotally, I was backlashed by this mismatch of a mental model trying to hunt down unstable tests in gh-92475 for some fruitless weeks.

Activity

  1. arhadthedev
    added
    type-featureA feature request or enhancementA feature request or enhancement
    on Jul 10, 2022
  2. stevendaprano commented on Jul 10, 2022

    @stevendaprano
    stevendaprano
    on Jul 10, 2022
    Member
    More actions

    Socket objects intentionally support the context manager protocol (with statements), and exiting the with block closes the socket.

    Generally passing an inappropriate object to with raises AttributeError.

    Can you provide a minimal reproducible example of the failure you are experiencing?

  3. arhadthedev
    closed this as not plannedon Jul 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.