Apologies for my ineptness. When I run the following in IDLE on my python 3.4 install it fails.
>>> sys.stdout.fileno()
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
sys.stdout.fileno()
io.UnsupportedOperation: fileno
This, seems to give something useful though...
>>> sys.stdout.fileno
<built-in method fileno of PseudoOutputFile object at 0x030927D0>
What obvious thing am I doing wrong?
thanks.
to cut a long story short I am actually trying to do this:
import os
os.write(1, "Hello world!\n")
But got the following error, so went down the route of trying out stdout
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
os.write(1, "Hello world!\n")
TypeError: 'str' does not support the buffer interface
and so the call to print sys.stdout.fileno() would give me the number, and I thought it might just be that it shouldn't be 1.
IDLE under windows is started with pythonw.exe, a console-less GUI. As such there is no stdout handle assigned, at all.
The shell window itself needs to redirect all stdout output to the GUI window, which is the PseudoOutputFile object you see.
If you wanted to experiment with writing to the 1 filenumber, you need to start IDLE with a console attached:
py -m idlelib
from a console should be enough to give you a process with an actual sys.__stdout__ file handle, and writing with os.write(1, ...) will work.
Do remember that writing directly to a file handle requires bytes, not Unicode text. Encode your text or use a b'...' bytes literal.
Related
I was trying to debug an issue with abc.ABCMeta - in particular a subclass check that didn't work as expected and I wanted to start by simply adding a print to the __subclasscheck__ method (I know there are better ways to debug code, but pretend for the sake of this question that there's no alternative). However when starting Python afterwards Python crashes (like a segmentation fault) and I get this exception:
Fatal Python error: Py_Initialize: can't initialize sys standard streams
Traceback (most recent call last):
File "C:\...\lib\io.py", line 84, in <module>
File "C:\...\lib\abc.py", line 158, in register
File "C:\...\lib\abc.py", line 196, in __subclasscheck__
RuntimeError: lost sys.stdout
So it probebly wasn't a good idea to put the print in there. But where exactly does the exception come from? I only changed Python code, that shouldn't crash, right?
Does someone know where this exception is coming from and if/how I can avoid it but still put a print in the abc.ABCMeta.__subclasscheck__ method?
I'm using Windows 10, Python-3.5 (just in case it might be important).
This exception stems from the fact that CPython imports io, and, indirectly, abc.py during the initialization of the standard streams:
if (!(iomod = PyImport_ImportModule("io"))) {
goto error;
}
io imports the abc module and registers FileIO as a virtual subclass of RawIOBase, a couple of other classes for BufferedIOBase and others for TextIOBase. ABCMeta.register invokes __subclasscheck__ in the process.
As you understand, using print in __subclasscheck__ when sys.stdout isn't set-up is a big no-no; the initialization fails and you get back your error:
if (initstdio() < 0)
Py_FatalError(
"Py_NewInterpreter: can't initialize sys standard streams");
You can get around it by guarding it with a hasattr(sys, 'stdout'), sys has been initialized by this point while stdout hasn't (and, as such, won't exist in sys in the early initialization phase):
if hasattr(sys, 'stdout'):
print("Debug")
you should get good amount of output when firing Python up now.
using cProfile of python, I cprofiled my code, but I keep getting this error related with compile() and null character which I can't quite understand.
The error message is:
[cProfileV]: cProfile output available at http://127.0.0.1:4000
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/2.7/bin/cprofilev", line 9, in
load_entry_point('CProfileV==1.0.6', 'console_scripts', 'cprofilev')()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/cprofilev.py", line 217, in main
code = compile(fp.read(), progname, 'exec')
TypeError: compile() expected string without null bytes
Is it a problem with my client code, or the server just isn't up?
Thank you in advance.
I believe the error is caused by a mismatch between versions of cprofile and cprofilev.
Cprofilev can be run directly using:
python -m cprofilev your_script.py
This has been asked before.
Anyhow, the error means you're recieving a string which you cannot read well, because it includes nulls. That means the server is up & responding but you cannot read the respond properly. That is because it's in another format, which is very likely JSON. try using the JSON module, included in python 2.6 and beyond; you could see some examples for it, here. If you'll provide us with your code, I can help you convert your application into a JSON-friendly one. :)
We have an issue using the subprocess library in Python. We tried to pass 2 message with the function communicate. The first one work correctly, but the second one generates a IOError
error. I think that we probably use incorrectly the function of the subprocess library, but we are not able to fix it.
Can anyone help us?
Here is the code:
from subprocess import *
video=Popen("omxplayer myvideo.mp4",shell=True,stdout=PIPE,stdin=PIPE)
video.stdin.write('+')
video.stdin.flush()
result=video.stdout.read()
print "Vol +: "+result
video.communicate('p')
print "Pause"
And the error:
Traceback (most recent call last):
File "youtube.py", line 55, in <module>
proc.stdin.write('+')
IOError: [Errno 32] Broken pipe
Thank you
I am trying to use some temporary files in an little python script and running into problems while using the tempfile-module:
test-code
import tempfile
with tempfile.NamedTemporaryFile() as temp:
print temp.name
temp.write('Some data')
print temp.seek(0).read()
output
s$ python tmpFileTester.py
/var/folders/cp/8kmr4fhs4l94y8fdc6g_84lw0000gn/T/tmpcDgrzF
Traceback
(most recent call last): File "tmpFileTester.py", line 5, in
print temp.seek(0).read() AttributeError: 'NoneType' object has no attribute 'read'
--> This happens on my MacBook with MacOS 10.9 and Python 2.7.5, it also happens with "delete=False".
Any ideas? It's probably a noob-error, but I can't find the problem...
Whenever you are chaining methods in Python and you get this, you can bet that one of them returns None (even if implicitly by not returning at all). The solution is generally to split up the chain over multiple lines.
temp.seek(0)
print temp.read()
file.seek returns None.
You should separated following statement:
print temp.seek(0).read()
into two statements:
temp.seek(0)
print temp.read()
I've been experimenting with Python a little bit under Emacs and when I have a buffer containing Python code I type C-c C-c to try and evaluate it. What I get is:
>>> Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'emacs' is not defined
What could I do to resolve this? I've done a little bit of digging but sadly not been able to fix my issue. In the *scratch* buffer I get the following output:
data-directory
"c:/emacs/emacs-24.0.90/etc/"
(getenv "PYTHONPATH")
nil
Which I presume is partly used to try and pick up emacs.py (which does exist in c:/emacs/emacs-24.0.90/etc/"
Any pointers greatly appreciated.