Skip to content
This repository was archived by the owner on Dec 20, 2025. It is now read-only.

Commit 9d95176

Browse files
committed
use ctypes as fallback
1 parent c6c82bd commit 9d95176

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

all/ptyprocess/resource.py

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,36 @@
1+
import sys
12
import subprocess
3+
import ctypes
4+
import ctypes.util
25

36

4-
RLIMIT_NOFILE = 7
7+
class StructRLimit(ctypes.Structure):
8+
_fields_ = [('rlim_cur', ctypes.c_ulong), ('rlim_max', ctypes.c_ulong)]
9+
10+
11+
if sys.platform == "darwin":
12+
RLIMIT_NOFILE = 8
13+
else:
14+
RLIMIT_NOFILE = 7
15+
16+
17+
def _getgetrlimit(resource):
18+
try:
19+
limits = StructRLimit()
20+
libc = ctypes.CDLL(ctypes.util.find_library('c'), use_errno=True)
21+
libc.getrlimit(resource, ctypes.byref(limits))
22+
return (limits.rlim_cur, limits.rlim_max)
23+
except Exception:
24+
# better than error !?
25+
return (10000, None)
526

627

728
def getrlimit(resource):
829
if resource == RLIMIT_NOFILE:
9-
soft_limit = int(subprocess.check_output(['sh', '-c', 'ulimit -Sn']))
30+
try:
31+
soft_limit = int(subprocess.check_output(
32+
['sh', '-c', 'ulimit -Sn'], stderr=subprocess.PIPE))
33+
except Exception:
34+
soft_limit = _getgetrlimit(resource)[0]
1035
hard_limit = None
1136
return (soft_limit, hard_limit)

0 commit comments

Comments
 (0)