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

  • Home
  • SEARCH
  • 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 8926525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:59:29+00:00 2026-06-15T07:59:29+00:00

The following Python 3 code exhibits some strange behavior (to me, at least) when

  • 0

The following Python 3 code exhibits some strange behavior (to me, at least) when I run it through strace:

import os
import sys

if len(sys.argv) != 2:
    print('Usage: ecpy <filename>')
sys.exit(1)
try:
    print('my PID: %d' % os.getpid())
    with open(sys.argv[1], 'w') as fp:
        try:
            fp.write('Hello Stack Overflow!')
        except IOError as e:
            print('### before close')
            print(str(e))
            sys.stdout.flush()
except IOError as e:
    print('### after close')
    print(str(e))
    sys.stdout.flush()

print('### after exception block')
sys.stdout.flush()

Since I/O is buffered, if you run this code with /dev/full, it doesn’t fail until fp closes at the end of the with block. That’s no surprise. In Python 2.7.3rc2 (on my system), the code runs the exception handler after actually closing the file descriptor corresponding to fp:

write(3, "Hello Stack Overflow!", 21)   = -1 ENOSPC (No space left on device)
close(3)                                = 0
munmap(0x7f9de3f78000, 4096)            = 0
write(1, "### after close\n", 16)       = 16
write(1, "[Errno 28] No space left on devi"..., 35) = 35
write(1, "### after exception block\n", 26) = 26

However, in Python 3.2.3 (on my system), the file descriptor is still open after the exception block runs:

write(3, "Hello Stack Overflow!", 21)   = -1 ENOSPC (No space left on device)
write(1, "### after close\n", 16)       = 16
write(1, "[Errno 28] No space left on devi"..., 35) = 35
write(1, "### after exception block\n", 26) = 26
...
write(3, "Hello Stack Overflow!", 21)   = -1 ENOSPC (No space left on device)
write(3, "Hello Stack Overflow!", 21)   = -1 ENOSPC (No space left on device)
close(3)                                = 0

The interpreter tries to write to the file a few more times and fails silently. When does Python actually call close()? What is invoking it? This behavior seems to leak a file descriptor.

  • 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-06-15T07:59:30+00:00Added an answer on June 15, 2026 at 7:59 am

    I took the liberty of submitting an issue to bugs.python.org, let’s wait and see if it pans out.

    http://bugs.python.org/issue16597

    EDIT: Looks like it’s a bug, good catch!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in the following python code: narg=len(sys.argv) print @length arg= , narg if narg ==
I have the following python code. import sys ins = open( sys.argv[1], "r" )
What is the following Python code in PHP? import sys li = range(1,777); def
Consider the following Python code: import os print os.getcwd() I use os.getcwd() to get
I have the following python code: def split_arg(argv): buildDescriptor = argv[1] buildfile, target =
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
I have the following python code... import Tkinter root = Tkinter.Tk() root.tk.eval('puts {printed by
I have the following python code: import logging import logging.config logging.config.fileConfig('a.conf') logging.debug('aaaa') with the
I need a ruby equivalent for the following python code import array my_array =
I am using the following Python code to compute the User+Sys time. t =

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.