Showing posts with label programming language. Show all posts
Showing posts with label programming language. Show all posts

Thursday, November 13, 2008

Different outputs in Tcl - Stdout vs Return Value

When both set a {hi} and puts "hi" echo a "hi" in an interactive tcl shell, what is the difference between them?

Clarification on "output"
1. standard output
puts command will output to stdout by default.
% puts "hello"
hello
2. return values
set hi {hello} has a return value hello and will echo back to user
% set hi {hello}
hello

To make things clear, look at this example.
% set ret [puts "hello"]
hello
% puts $ret
(blank)

So that the return value from puts is blank.

Compound statements
Let move on to compound statements.
% set a "hello"; set b "hello2"
hello2
This gives a single line of output from `set b`, and a and b are both set.
% puts $a
hello
% puts $b
hello2

Here's two lines of output from stdout.
% puts "hello"; puts "hello2"
hello
hello2

Importance
Why is this important? What if we would like to store a calculated value into variable a.
% set a [expr 3+3]
6
% puts $a
6
This is what we expected. However, if we wrote another version of expr that output the result into stdout.
% set a [set b [expr 3+3]; puts $b]
6
% puts $a
(blank)
We get nothing.

Generally speaking, it is not a good idea to print to screen from many functions. Because this would make the testing difficult, if not impossible. Usually, assert will compare the return result to expected result, while the output to standard output are ignored. Despite all that, we can still try to test function which output to stdout. The idea is to trap the stdout. This assert for stdout would look like this pseudo code:
procedure assertStdout (expected, command) {
  create out for this instance
  execute command > out
  flush out
  return expected == out
}
One trick is that if multiple assertStdout procedures are running, 'out's must be distinct in order to keep thread-safe.

The echo back of tcl might be of use, but it could confuse people with the output from within procedures. However, it is mandatory to distinguish them for a programmer. Other scripting languages like shell does not have such echo back, but that doesn't mean it is OK to output from many functions for them. Especially, for compicated software that requires a thorough test, standard out or output to file everywhere would definitely bring down the testing.

Thursday, April 17, 2008

os.system() and its return value

When os.system('exit 1') returns 256, you might wonder what is going on with python? Why isn't it simply 1 (one)? Let's take a look at this command.

A system() command might be frequently used when you were interacting with the system or other scripts.

Try help(os.system) will only show:
system(command) -> exit_status

Execute the command (a string) in a subshell.

And it talks nothing about the exit status.

This function appears in the tutorial as an example and in library reference:

system(command)
Execute the command (a string) in a subshell. This is implemented by calling the Standard C function system(), and has the same limitations. Changes to posix.environ, sys.stdin, etc. are not reflected in the environment of the executed command.

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

On Windows, the return value is that returned by the system shell after running command, given by the Windows environment variable COMSPEC: on command.com systems (Windows 95, 98 and ME) this is always 0; on cmd.exe systems (Windows NT, 2000 and XP) this is the exit status of the command run; on systems using a non-native shell, consult your shell documentation.

Availability: Macintosh, Unix, Windows.

wait(command)

Wait for completion of a child process, and return a tuple containing its pid and exit status indication: a 16-bit number, whose low byte is the signal number that killed the process, and whose high byte is the exit status (if the signal number is zero); the high bit of the low byte is set if a core file was produced. Availability: Macintosh, Unix.
So this explains why os.system('exit 1') returns 256 instead of 1. Actually, the signal number killed the process is zero. To get the exit status, just shift the value 8-bit to the right.

Here is a link talking about the return code of os.system(). http://mail.python.org/pipermail/python-list/2001-March/073147.html