15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

The entire purpose of the updArgs() function is to "update" the exception arguments. What we mean<br />

here is that the original exception is going to provide us a set of arguments. We want to take these<br />

arguments and make them part of our new exception, perhaps embellishing or adding a third argument<br />

(which is not added if nothing is givenNone is a default argument, which we will study in the next<br />

chapter). Our goal is to provide the more informative details to the user so that if and when errors<br />

occur, the problems can be tracked down as quickly as possible.<br />

Lines 2353<br />

The fileArgs() function is used only by myopen() (see below). In particular, we are seeking error EACCES,<br />

which represents "permission denied." We pass all other IOError exceptions along without modification<br />

(lines 54 - 55). If you are curious about ENXIO, EACCES, and other system error numbers, you can hunt<br />

them down by starting at file /usr/include/sys/errno.h on a Unix system, or C:\Msdev\include\Errno.h if<br />

you are using Visual C++ on Windows.<br />

In line 27, we are also checking to make sure that the machine we are using supports the os.access()<br />

function, which helps you check what kind of file permissions you have for any particular file. We do not<br />

proceed unless we receive both a permission error as well as the ability to check what kind of<br />

permissions we have. If all checks out, we set up a dictionary to help us build a string indicating the<br />

permissions we have on our file.<br />

The Unix file system uses explicit file permissions for the user, group (more than one user can belong to<br />

a group), and other (any user other than the owner or someone in the same group as the owner) in<br />

read, write, and execute (`r', `w', `x') order. Windows supports some of these permissions.<br />

Now it is time to build the permission string. If the file has a permission, its corresponding letter shows<br />

up in the string, otherwise a dash ( - ) appears. For example, a string of "rw-" means that you have read<br />

and write access to it. If the string reads "r-x", you have only read and execute access; "---" means no<br />

permission at all.<br />

After the permission string has been constructed, we create a temporary argument list. We then alter<br />

the error string to contain the permission string, something that standard IOError exception does not<br />

provide. "Permission denied" sometimes seems silly if the system does not tell you what permissions<br />

you have to correct the problem. The reason, of course, is security. When intruders do not have<br />

permission to access something, the last thing you want them to see is what the file permissions are,<br />

hence the dilemma. However, our example here is merely an exercise, so we allow for the temporary<br />

"breach of security." The point is to verify whether or not the os.chmod()functions call affected file<br />

permissions the way they are supposed to.<br />

The final thing we do is to add the filename to our argument list and return the set of arguments as a<br />

tuple.<br />

Lines 5565<br />

Our new myconnect() function simply wraps the standard socket method connect()to provide an IOError -<br />

type exception if the network connection fails. Unlike the general socket.error exception, we also<br />

provide the hostname and port number as an added value to the programmer.<br />

For those new to network programming, a hostname and port number pair are analogous to an area<br />

code and telephone number when you are trying to contact someone. In this case, we are trying to<br />

contact a program running on the remote host, presumably a server of some sort; therefore, we require

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!