日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python段错误原因_python – 捕获崩溃的子进程的“分段错误”...

發布時間:2024/9/15 python 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python段错误原因_python – 捕获崩溃的子进程的“分段错误”... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

shell可能會生成“Segmentation fault”消息.要找出該過程是否被SIGSEGV殺死,請檢查proc.returncode == -signal.SIGSEGV.

如果要查看消息,可以在shell中運行該命令:

#!/usr/bin/env python

from subprocess import Popen, PIPE

proc = Popen(shell_command, shell=True, stdout=PIPE, stderr=PIPE)

out, err = proc.communicate()

print out, err, proc.returncode

我已經用ctypes import *; memset(0,1,1)’中的shell_command =“python -c”測試了它,導致了段錯誤并且錯誤捕獲了消息.

如果消息直接打印到終端,那么您可以使用pexpect模塊捕獲它:

#!/usr/bin/env python

from pipes import quote

from pexpect import run # $pip install pexpect

out, returncode = run("sh -c " + quote(shell_command), withexitstatus=1)

signal = returncode - 128 # 128+n

print out, signal

或直接使用pty stdlib模塊:

#!/usr/bin/env python

import os

import pty

from select import select

from subprocess import Popen, STDOUT

# use pseudo-tty to capture output printed directly to the terminal

master_fd, slave_fd = pty.openpty()

p = Popen(shell_command, shell=True, stdin=slave_fd, stdout=slave_fd,

stderr=STDOUT, close_fds=True)

buf = []

while True:

if select([master_fd], [], [], 0.04)[0]: # has something to read

data = os.read(master_fd, 1 << 20)

if data:

buf.append(data)

else: # EOF

break

elif p.poll() is not None: # process is done

assert not select([master_fd], [], [], 0)[0] # nothing to read

break

os.close(slave_fd)

os.close(master_fd)

print "".join(buf), p.returncode-128

總結

以上是生活随笔為你收集整理的python段错误原因_python – 捕获崩溃的子进程的“分段错误”...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。