Commit 85e816c6 authored by Luke Macken's avatar Luke Macken

Pull our subprocess code out into a new pyrasite.utils module

parent 3543596c
......@@ -28,7 +28,10 @@ Authors:
David Malcolm <dmalcolm@redhat.com>
"""
import os, subprocess, warnings
import os
import warnings
from utils import run
class CodeInjector(object):
......@@ -53,17 +56,5 @@ class CodeInjector(object):
'PyRun_SimpleString("execfile(\\"%s\\")")' % self.filename,
'PyGILState_Release($1)',
]
self._run('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
run('%sgdb -p %d -batch %s' % (self.gdb_prefix, self.pid,
' '.join(["-eval-command='call %s'" % cmd for cmd in gdb_cmds])))
def _run(self, cmd):
if self.verbose:
print(cmd)
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
if self.verbose:
print(out)
if err:
print(err)
......@@ -15,7 +15,7 @@
#
# Copyright (C) 2011 Red Hat, Inc.
import subprocess
from utils import run
class ObjectInspector(object):
"""Inspects objects in a running Python program"""
......@@ -24,13 +24,10 @@ class ObjectInspector(object):
self.pid = pid
def inspect(self, address):
cmd = [
cmd = ' '.join([
'gdb --quiet -p %s -batch' % self.pid,
'-eval-command="print (PyObject *)%s"' % address,
]
p = subprocess.Popen(' '.join(cmd), shell=True, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
for line in out.split('\n'):
])
for line in run(cmd).split('\n'):
if line.startswith('$1 = '):
return line[5:]
# This file is part of pyrasite.
#
# pyrasite is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pyrasite is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pyrasite. If not, see <http://www.gnu.org/licenses/>.
#
# Copyright (C) 2011 Red Hat, Inc.
import subprocess, warnings
def run(cmd):
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
out, err = p.communicate()
if err:
warnings.warn(err)
return out.strip()
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment