本文整理汇总了Python中os.closerange方法的典型用法代码示例。如果您正苦于以下问题:Python os.closerange方法的具体用法?Python os.closerange怎么用?Python os.closerange使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类os
的用法示例。
在下文中一共展示了os.closerange方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _close_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _close_fds(self, but):
if hasattr(os, 'closerange'):
os.closerange(3, but)
os.closerange(but + 1, MAXFD)
else:
for i in xrange(3, MAXFD):
if i == but:
continue
try:
os.close(i)
except:
pass
# Used as a bandaid workaround for https://bugs.python.org/issue27448
# to prevent multiple simultaneous subprocess launches from interfering
# with one another and leaving gc disabled.
示例2: test_closerange
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def test_closerange(self):
first = os.open(test_support.TESTFN, os.O_CREAT|os.O_RDWR)
# We must allocate two consecutive file descriptors, otherwise
# it will mess up other file descriptors (perhaps even the three
# standard ones).
second = os.dup(first)
try:
retries = 0
while second != first + 1:
os.close(first)
retries += 1
if retries > 10:
# XXX test skipped
self.skipTest("couldn't allocate two consecutive fds")
first, second = second, os.dup(second)
finally:
os.close(second)
# close a fd that is open, and one that isn't
os.closerange(first, first + 2)
self.assertRaises(OSError, os.write, first, "a")
示例3: test_open
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def test_open(self):
test_filename = "tmp.open.test"
fd1 = os.open(test_filename + "1", flags)
# make sure fd+1 and fd+2 are closed
os.closerange(fd1 + 1, fd1 + 2)
# open should return the lowest-numbered file descriptor not currently open
# for the process
fd2 = os.open(test_filename + "2", flags)
fd3 = os.open(test_filename + "3", flags)
os.close(fd2)
self.assertRaisesMessage(OSError, "[Errno 9] Bad file descriptor", os.lseek, fd2, os.SEEK_SET, 0)
fd4 = os.open(test_filename + "4", flags)
self.assertEqual(fd4, fd2)
os.close(fd1)
os.close(fd3)
os.close(fd4)
for i in range(1, 5):
os.unlink(test_filename + str(i))
示例4: test_closerange
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def test_closerange(self):
first = os.open(support.TESTFN, os.O_CREAT|os.O_RDWR)
# We must allocate two consecutive file descriptors, otherwise
# it will mess up other file descriptors (perhaps even the three
# standard ones).
second = os.dup(first)
try:
retries = 0
while second != first + 1:
os.close(first)
retries += 1
if retries > 10:
# XXX test skipped
self.skipTest("couldn't allocate two consecutive fds")
first, second = second, os.dup(second)
finally:
os.close(second)
# close a fd that is open, and one that isn't
os.closerange(first, first + 2)
self.assertRaises(OSError, os.write, first, b"a")
示例5: _close_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _close_fds(self, fds_to_keep):
start_fd = 3
for fd in sorted(fds_to_keep):
if fd >= start_fd:
os.closerange(start_fd, fd)
start_fd = fd + 1
if start_fd <= MAXFD:
os.closerange(start_fd, MAXFD)
示例6: closerange
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def closerange(fd_low, fd_high):
# Iterate through and close all file descriptors.
for fd in range(fd_low, fd_high):
try:
os.close(fd)
except OSError: # ERROR, fd wasn't open to begin with (ignored)
pass
示例7: exit
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def exit():
"""
Causes python to exit without garbage-collecting any objects, and thus avoids
calling object destructor methods. This is a sledgehammer workaround for
a variety of bugs in PyQt and Pyside that cause crashes on exit.
This function does the following in an attempt to 'safely' terminate
the process:
* Invoke atexit callbacks
* Close all open file handles
* os._exit()
Note: there is some potential for causing damage with this function if you
are using objects that _require_ their destructors to be called (for example,
to properly terminate log files, disconnect from devices, etc). Situations
like this are probably quite rare, but use at your own risk.
"""
## first disable our own cleanup function; won't be needing it.
setConfigOptions(exitCleanup=False)
## invoke atexit callbacks
atexit._run_exitfuncs()
## close file handles
if sys.platform == 'darwin':
for fd in range(3, 4096):
if fd not in [7]: # trying to close 7 produces an illegal instruction on the Mac.
os.close(fd)
else:
os.closerange(3, 4096) ## just guessing on the maximum descriptor count..
os._exit(0)
## Convenience functions for command-line use
示例8: _run_child
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _run_child(self, cmd):
if isinstance(cmd, basestring):
cmd = ['/bin/sh', '-c', cmd]
os.closerange(3, MAXFD)
try:
os.execvp(cmd[0], cmd)
finally:
os._exit(1)
示例9: _close_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _close_fds(self, but):
if hasattr(os, 'closerange'):
os.closerange(3, but)
os.closerange(but + 1, MAXFD)
else:
for i in xrange(3, MAXFD):
if i == but:
continue
try:
os.close(i)
except:
pass
示例10: _run_as_another_namespace
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _run_as_another_namespace(
pid,
namespaces,
function,
_args=(),
_kwargs={}
):
# os.closerange(1, 1000)
context = ProcessContext(pid, namespaces)
context.attach()
try:
return run_as_another_process(function, _args, _kwargs)
finally:
context.detach()
示例11: _close_fds
# 需要导入模块: import os [as 别名]
# 或者: from os import closerange [as 别名]
def _close_fds(self, keep):
# `keep` is a set of fds, so we
# use os.closerange from 3 to min(keep)
# and then from max(keep + 1) to MAXFD and
# loop through filling in the gaps.
# Under new python versions, we need to explicitly set
# passed fds to be inheritable or they will go away on exec
if hasattr(os, 'set_inheritable'):
set_inheritable = os.set_inheritable
else:
set_inheritable = lambda i, v: True
if hasattr(os, 'closerange'):
keep = sorted(keep)
min_keep = min(keep)
max_keep = max(keep)
os.closerange(3, min_keep)
os.closerange(max_keep + 1, MAXFD)
for i in xrange(min_keep, max_keep):
if i in keep:
set_inheritable(i, True)
continue
try:
os.close(i)
except:
pass
else:
for i in xrange(3, MAXFD):
if i in keep:
set_inheritable(i, True)
continue
try:
os.close(i)
except:
pass