473,394 Members | 1,852 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Nice "bug" to loose a contest

Hello,

Yesterday, I was at a programming competition. We programmed on Linux
liveCD's and Python was one of the allowed languages (among C and
Java). I cared just about the algorithmic approach so I used Python.
One of the main rules is, that the code reads its standard input and
dumps the result on the standard output. Here happened my bigger
programming mistake ever - I used the following approach :

====
import sys
print sys.stdout.readlines() # TYPO ! stdin != stdout
====

So the WEIRD issue is, that it worked and, executing the following
code I get :

====
azi@magicb0x ~ $ python la.py
test
test #2
['test \n', 'test #2\n']
azi@magicb0x ~ $
====

Ok, as the algorithm worked perfectly, and all the test cases we were
given were positive, I've continued with the next problem, copy/
pasting the above idiom... When they tested the code, they used file
redirection like :

==
python la.py < /somefile
==

And, my code resulted in no output/input (=0 points), which can be
proved here too :

====
azi@magicb0x ~ $ python la.py < /etc/passwd
===

Some friend of mine told me that's the Unix way, (stdout can act like
stdin) so I tried some C code :

===
1 #include <stdio.h>
2
3 int main() {
4 char buf[120];
5 while (fgets(buf, sizeof(buf), stdout) != NULL) {
6 puts(buf);
7 }
8 return 0;
9}
===

The code returns with no delay, so I'm really wondering where is that
nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
anything. I'd spot the mistake before submitting the problem solutions
if it was written in C :)

Thanks!

Apr 8 '07 #1
5 1198
This code works like the python one,
I dont use buffered stdio f... functions,
but the more 'system call' read and write

int main()
{
char buf[120];
int len;

while (len=read(1, buf, sizeof(buf))) {
write(1, buf, len);
}
return 0;
}

I dont understand what is appening,
but you have to know their is a link between
stdin and stdout in 'terminal mode', when you make a read on stdin,
stdout is automatically flushed, this link disappears when onr is not
liked to a terminal.
This is to facilitate user input, to avoid to put a flush after each
write,
in fact before any user input.

Hope someone else will give the trick

On 8 avr, 12:52, "stdazi" <std...@gmail.comwrote:
Hello,

Yesterday, I was at a programming competition. We programmed on Linux
liveCD's and Python was one of the allowed languages (among C and
Java). I cared just about the algorithmic approach so I used Python.
One of the main rules is, that the code reads its standard input and
dumps the result on the standard output. Here happened my bigger
programming mistake ever - I used the following approach :

====
import sys
print sys.stdout.readlines() # TYPO ! stdin != stdout
====

So the WEIRD issue is, that it worked and, executing the following
code I get :

====
azi@magicb0x ~ $ python la.py
test
test #2
['test \n', 'test #2\n']
azi@magicb0x ~ $
====

Ok, as the algorithm worked perfectly, and all the test cases we were
given were positive, I've continued with the next problem, copy/
pasting the above idiom... When they tested the code, they used file
redirection like :

==
python la.py < /somefile
==

And, my code resulted in no output/input (=0 points), which can be
proved here too :

====
azi@magicb0x ~ $ python la.py < /etc/passwd

===

Some friend of mine told me that's the Unix way, (stdout can act like
stdin) so I tried some C code :

===
1 #include <stdio.h>
2
3 int main() {
4 char buf[120];
5 while (fgets(buf, sizeof(buf), stdout) != NULL) {
6 puts(buf);
7 }
8 return 0;
9}
===

The code returns with no delay, so I'm really wondering where is that
nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
anything. I'd spot the mistake before submitting the problem solutions
if it was written in C :)

Thanks!

Apr 8 '07 #2
On Apr 8, 1:40 pm, "aspineux" <aspin...@gmail.comwrote:
This code works like the python one,
I dont use buffered stdio f... functions,
but the more 'system call' read and write

int main()
{
char buf[120];
int len;

while (len=read(1, buf, sizeof(buf))) {
write(1, buf, len);
}
return 0;

}
Yeah, I've noticed that too, altough I'm clueless on how stdio handles
that differently. Now I'm wondering, what's the behaviour of the
Python snippet that reads from stdout in Windows.. Can someone on
Windows try it and report please?

Apr 9 '07 #3
En Mon, 09 Apr 2007 09:19:16 -0300, <az********@gmail.comescribió:
Yeah, I've noticed that too, altough I'm clueless on how stdio handles
that differently. Now I'm wondering, what's the behaviour of the
Python snippet that reads from stdout in Windows.. Can someone on
Windows try it and report please?
It does the right thing:

pyprint sys.stdout.readlines()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor

--
Gabriel Genellina

Apr 9 '07 #4
On Apr 9, 2:47 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Mon, 09 Apr 2007 09:19:16 -0300, <azi.std...@gmail.comescribió:
Yeah, I've noticed that too, altough I'm clueless on how stdio handles
that differently. Now I'm wondering, what's the behaviour of the
Python snippet that reads from stdout in Windows.. Can someone on
Windows try it and report please?

It does the right thing:

pyprint sys.stdout.readlines()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor

--
Gabriel Genellina
I'm wondering if this could be treated as a bug of some kind? I always
thought that the purpose of "higher" interpreted languages is to hide
the implementation details as much as possible, offering a
unique,equal and transparent environment on all systems (as much as
this is possible).

Apr 9 '07 #5
On 8 avr, 13:40, "aspineux" <aspin...@gmail.comwrote:
This code works like the python one,
I dont use buffered stdio f... functions,
but the more 'system call' read and write

int main()
{
char buf[120];
int len;

while (len=read(1, buf, sizeof(buf))) {
write(1, buf, len);
}
return 0;

}

I dont understand what is appening,
But I have an idea !

It is possible to open a file in RW mode, nothing exceptional with
that.
And when connected in a terminal, you are connected through a file
called a PTY !
This "file" is open in RW !

File descriptors 0, 1 and 2 are certainly a "view" of this PTY and
then in fact the same thing !


but you have to know their is a link between
stdin and stdout in 'terminal mode', when you make a read on stdin,
stdout is automatically flushed, this link disappears when onr is not
liked to a terminal.
This is to facilitate user input, to avoid to put a flush after each
write,
in fact before any user input.

Hope someone else will give the trick

On 8 avr, 12:52, "stdazi" <std...@gmail.comwrote:
Hello,
Yesterday, I was at a programming competition. We programmed on Linux
liveCD's and Python was one of the allowed languages (among C and
Java). I cared just about the algorithmic approach so I used Python.
One of the main rules is, that the code reads its standard input and
dumps the result on the standard output. Here happened my bigger
programming mistake ever - I used the following approach :
====
import sys
print sys.stdout.readlines() # TYPO ! stdin != stdout
====
So the WEIRD issue is, that it worked and, executing the following
code I get :
====
azi@magicb0x ~ $ python la.py
test
test #2
['test \n', 'test #2\n']
azi@magicb0x ~ $
====
Ok, as the algorithm worked perfectly, and all the test cases we were
given were positive, I've continued with the next problem, copy/
pasting the above idiom... When they tested the code, they used file
redirection like :
==
python la.py < /somefile
==
And, my code resulted in no output/input (=0 points), which can be
proved here too :
====
azi@magicb0x ~ $ python la.py < /etc/passwd
===
Some friend of mine told me that's the Unix way, (stdout can act like
stdin) so I tried some C code :
===
1 #include <stdio.h>
2
3 int main() {
4 char buf[120];
5 while (fgets(buf, sizeof(buf), stdout) != NULL) {
6 puts(buf);
7 }
8 return 0;
9}
===
The code returns with no delay, so I'm really wondering where is that
nice sys.{stdin,stdout} feature inplemented as pydoc doesn't mention
anything. I'd spot the mistake before submitting the problem solutions
if it was written in C :)
Thanks!

Apr 12 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: mhanrahan | last post by:
Hi, I am experiencing a "bug" (maybe I am missing a setting or something) with a Windows MDI application which I am building in c# 2.0. I am doing the following: Create an MDI parent add a...
1
by: Thomas Barnet-Lamb | last post by:
I was wondering if anyone could give me some help with the following. Consider the code snippet: struct qqq{typedef qqq* pointer;}; template<class al> struct foo : public al { template...
0
by: Dave L | last post by:
I just upgraded from VS .NET 2002 to 2003. Everything built okay, but strange bugs started appearing. Apparently there is a bug in the managed C++ compiler in regards to handling of static...
2
by: Philipp Schumann | last post by:
Dear all, I'm in the process of designing a distributed application on the basis of ASP.NET, which does not contain of scripts, but of assemblies containing classes that implement IHttpHandler...
9
by: Codex Twin | last post by:
I am re-sending this in the hope that it might illicit a response. I have a corporate client who forces their workstations to get the proxy server details using an automatic proxy discovery script....
26
by: Patient Guy | last post by:
The code below shows the familiar way of restricting a function to be a method of a constructed object: function aConstructor(arg) { if (typeof(arg) == "undefined") return (null);...
8
by: gw7rib | last post by:
I've been bitten twice now by the same bug, and so I thought I would draw it to people's attention to try to save others the problems I've had. The bug arises when you copy code from a destructor...
13
by: Jen | last post by:
One user of my application is experiencing an exception "input string not in correct format". But it makes no sense where it is occurring. It is occurring when a string from a textbox ("172") is...
4
by: Sin Jeong-hun | last post by:
I don't get the message so it's hard to debug that, but some of my clients report that they get "The underlying connection was closed unexpectedly" exception. According to this site (http://...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.