MCPcopy Index your code
hub / github.com/RustPython/RustPython / fd_count

Function fd_count

Lib/test/support/os_helper.py:654–718  ·  view source on GitHub ↗

Count the number of open file descriptors.

()

Source from the content-addressed store, hash-verified

652
653
654def fd_count():
655 """Count the number of open file descriptors.
656 """
657 if sys.platform.startswith(('linux', 'android', 'freebsd', 'emscripten')):
658 fd_path = "/proc/self/fd"
659 elif support.is_apple:
660 fd_path = "/dev/fd"
661 else:
662 fd_path = None
663
664 if fd_path is not None:
665 try:
666 names = os.listdir(fd_path)
667 # Subtract one because listdir() internally opens a file
668 # descriptor to list the content of the directory.
669 return len(names) - 1
670 except FileNotFoundError:
671 pass
672
673 MAXFD = 256
674 if hasattr(os, 'sysconf'):
675 try:
676 MAXFD = os.sysconf("SC_OPEN_MAX")
677 except OSError:
678 pass
679
680 old_modes = None
681 if sys.platform == 'win32':
682 # bpo-25306, bpo-31009: Call CrtSetReportMode() to not kill the process
683 # on invalid file descriptor if Python is compiled in debug mode
684 try:
685 import msvcrt
686 msvcrt.CrtSetReportMode
687 except (AttributeError, ImportError):
688 # no msvcrt or a release build
689 pass
690 else:
691 old_modes = {}
692 for report_type in (msvcrt.CRT_WARN,
693 msvcrt.CRT_ERROR,
694 msvcrt.CRT_ASSERT):
695 old_modes[report_type] = msvcrt.CrtSetReportMode(report_type,
696 0)
697
698 try:
699 count = 0
700 for fd in range(MAXFD):
701 try:
702 # Prefer dup() over fstat(). fstat() can require input/output
703 # whereas dup() doesn't.
704 fd2 = os.dup(fd)
705 except OSError as e:
706 if e.errno != errno.EBADF:
707 raise
708 else:
709 os.close(fd2)
710 count += 1
711 finally:

Callers 2

test_many_opensMethod · 0.90
runtest_refleakFunction · 0.85

Calls 6

lenFunction · 0.85
hasattrFunction · 0.85
listdirMethod · 0.80
startswithMethod · 0.45
dupMethod · 0.45
closeMethod · 0.45

Tested by 1

test_many_opensMethod · 0.72