subprocess, invoke C-program from within Python - python

I am trying to invoke a C-program, named “drule.c”, from within my Python-program “drulewrapper.py”. I am trying to use "subprocess" but cannot get it to work.
1) I compile “drule.c” on the Mac’s terminal and all works okay:
$ gcc -o drule drule c
$ ./drule D11
>P>Q>RQ
Fyi, the input -- “D11” -- are axioms in predicate logic; the output -- “>P>Q>RQ” -- is the theorem that is proven and which I then want to process further in my Python program.
2) I write a short Python program (drulewrapper.py) and compile it:
From subprocess import call
def CheckString():
call(“./drule”, “D11”)
3) But when I run CheckString() I get errors:
Traceback (most recent call last):
File "<pyshell#7>", line 1, in <module>
CheckString()
File "/Users/georgeszpiro/Dropbox/metamath/GApl/drulewrapper.py", line 3, in CheckString
call("./drule","D11")
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 267, in call
with Popen(*popenargs, **kwargs) as p:
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/subprocess.py", line 609, in __init__
raise TypeError("bufsize must be an integer")
TypeError: bufsize must be an integerT
Can anybody help?

Related

Can't create process in pwntools

I am trying to use python's pwntools. I want to start a process using
from pwn import *
s = process('./step1')
When I do this I receive the following error message:
Traceback (most recent call last):
File "", line 1, in
File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/process.py", line 267, in init
stdin, stdout, stderr, master, slave = self._handles(*handles)
File "/usr/local/lib/python2.7/dist-packages/pwnlib/tubes/process.py", line 603, in _handles
tty.setraw(master)
File "/usr/lib/python2.7/tty.py", line 28, in setraw
tcsetattr(fd, when, mode)
termios.error: (22, 'Invalid argument')
I am already in the directory that contains the file step1 and step1 is executable. Does anyone have an idea why I get this error. If it helps, I am using the Linux subsystem on Windows 10.
Check out this link. process() needs its first argument as a list of program arguments. So
$ ./step1 arg1 arg2
is equivalent to
p = process(['step1', 'arg1', 'arg2'])

Python subprocess call throws error when writing file

I'd like to use SVOX/pico2wave to write a wav-file from Python code. When I execute this line from a terminal the file is written just fine:
/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."
I've verified that pico2wave is located in /usr/bin.
This is my Python code:
from subprocess import call
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
... which throws this error:
Traceback (most recent call last):
File "app/app.py", line 63, in <module>
call('/usr/bin/pico2wave -w=/tmp/tmp_say.wav "Hello world."')
File "/usr/lib/python2.7/subprocess.py", line 168, in call
return Popen(*popenargs, **kwargs).wait()
File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1024, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
From the documentation
Providing a sequence of arguments is generally preferred, as it allows
the module to take care of any required escaping and quoting of
arguments (e.g. to permit spaces in file names). If passing a single
string, either shell must be True (see below) or else the string must
simply name the program to be executed without specifying any
arguments.
So you might try with
call(['/usr/bin/pico2wave', '-w=/tmp/tmp_say.wav', '"Hello world."'])

python subprocess.call arguments

on Linux I have the following python source file called visca.py:
from subprocess import call
def recall(preset):
call(["visca-cli", "memory_recall", str(preset)])
I open python interpreter in shell and import visca, then i type visca.recall(0) and get
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "visca.py", line 13, in recall
subprocess.call(["visca-cli", "memory_recall", str(preset)]) File "/usr/lib/python2.7/subprocess.py", line 493, in call
return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 629, in __init__
raise TypeError("bufsize must be an integer") TypeError: bufsize must be an integer
However, if I type directly in python shell
>>> from subprocess import call
>>> call(["visca-cli", "memory_recall", "0"])
10 OK - no return value
0
it works. What's the problem?
It's telling you that bufsize must be an integer. I am going to guess that whatever value you set for preset in the script is not an integer (keep in mind that 0.0 is a float, not an integer). Do a quick check for what your argument is by printing it out in the function.

Python: AssertionError when running nose tests with coverage

I'm fairly green with python testing, so this might be something I'm doing wrong..
When I run my tests, the test runners works fine and coverage too.. but between the two I get an assertion error:
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
To make thing more difficult, I'm trying to test a command line utility. Which means I had to tell coverage to cover subprocess calls.
I think I got this part working since coverage is now reporting a % of cover for the script that is being run. But since I got coverage working I can't get rid of the AssertionError.
Some help understanding what's wrong would be really appreciated. All my code is available on github:
repo
setup.py
run_tests
subprocess call
Quick run:
cd /tmp/ && git clone git://github.com/h3/django-duke-client.git
cd django-duke-client && chmod a+x run_tests && ./run_tests
Thanks
Update
I've run the test on a different computer and I got the same AssertionError .. Plus a new TypeError. Again the tests runs correctly and coverage also seems to work properly even with those errors..
...
Ran 9 tests in 1.324s
OK
Traceback (most recent call last):
File "/usr/local/bin/coverage", line 9, in <module>
load_entry_point('coverage==3.5.1', 'console_scripts', 'coverage')()
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 657, in main
status = CoverageScript().command_line(argv)
File "/usr/local/lib/python2.7/dist-packages/coverage/cmdline.py", line 526, in command_line
self.coverage.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/control.py", line 389, in stop
self.collector.stop()
File "/usr/local/lib/python2.7/dist-packages/coverage/collector.py", line 262, in stop
assert self._collectors[-1] is self
AssertionError
Error in atexit._run_exitfuncs:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Error in sys.exitfunc:
Traceback (most recent call last):
File "/usr/lib/python2.7/atexit.py", line 24, in _run_exitfuncs
func(*targs, **kargs)
File "/usr/lib/python2.7/multiprocessing/util.py", line 284, in _exit_function
info('process shutting down')
TypeError: 'NoneType' object is not callable
Name Stmts Miss Branch BrPart Cover Missing
------------------------------------------------------------------------------
dukeclient/__init__ 53 53 2 0 4% 1-93
dukeclient/commands/__init__ 41 33 6 2 26% 1-9, 12, 14-15, 17, 24-28, 34-43, 46-63
...
Regarding the NoneType is not callable error, please find below some elements that may help you.
In your module plugintest.py from nose-1.1.2-py2.7.egg/nose/plugins/, line 174, one can read the following line :
from multiprocessing import Manager
That leads the multiprocessing.util package to be imported, and with it an exit function to be registered :
atexit.register(_exit_function)
The problem seems to be that multiprocessing.util which is loaded in plugintest is then unloaded before the _exit_function gets called, and it's function definitions by the way.
Thus, if you import it in your setup.py file :
from multiprocessing import util
The error disappear.
By the way, I had to comment some parts in the tests that were failing or change some lines of code :
the -m command does not seems to be valid ;
I had to rename duke_conf.yml to duke_conf.yml ;
the tests that checks if README.rst and LICENSE files exists are failing (don't had the time to check why) ;
Hope it helps,

python subprocess call OSX

I am trying to access the wifi interface through python:
In bash I can use the following
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport -I
-s can also be passed.
I have tried using the following in python:
from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport', '-I'])
something is definitely not correct - as I get as a reply:
Traceback (most recent call last):
File "ip3.py", line 5, in <module>
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport', '-I'])
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 467, in call
return Popen(*popenargs, **kwargs).wait()
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 741, in __init__
restore_signals, start_new_session)
File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/subprocess.py", line 1356, in _execute_child
raise child_exception_type(errno_num, err_msg)
OSError: [Errno 2] No such file or directory: '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/sbin/airport'
Any ideas would be welcome... I just want to begin by printing this to screen, saving as an array etc...
I dont have a high enough rating to answer my own question yet, so Ill say it here!
so I was being stupid!
from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '-I'])
Works fine. Just needed to remove /usr/sbin/airport
call take first argument as command and subsequent arguments to that command.
In your case
command is,
/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport
and command's two arguments are,
/usr/sbin/airport
-I
So, you need to call it as,
from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport' '/usr/sbin/airport', '-I'])
Try like this
from subprocess import call
call(['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', '/usr/sbin/airport', '-I'])
Otherwise it thinks /usr/sbin/airport is part of the first path.

Categories