Penguin
Annotated edit history of PythonNotes version 7, including all changes. View license author blame.
Rev Author # Line
2 AristotlePagaltzis 1 !!! Timezones and [Python] < 2.3
3 JohnMcPherson 2
1 MattBrown 3
2 AristotlePagaltzis 4 [Python] 2.3 has made some impressive leaps forward in its support of timezones and the classes that it makes available to handle them. If you are doing anything that uses timezones even remotely you will save yourself a __lot__ of hassle by not using a version of [Python] older than 2.3! If for some reason you're stuck with [Python] < 2.3 (like you run a pathetically old distro like [Woody]) then watch out for the following.
1 MattBrown 5
2 AristotlePagaltzis 6 !! strptime
1 MattBrown 7
2 AristotlePagaltzis 8 In [Python] < 2.3 strptime can cause problems if your format string does not specify a timezone as there is no data for [Python] to set the DST flag in the returned tuple from, so it is left undefined and is filled with a platform specific value. This causes problems if you then try and convert the parsed date into a timestamp and then an ascii time. Usually you end up with a time that is 1 hour out from what it should be! To fix this problem, you can do two things:
1 MattBrown 9
2 AristotlePagaltzis 10 * Modify your format string so that it reads a timezone
11
12 * Set the DST field to -1 (unknown) explicitly, which causes other time related functions to handle the date correctly using the current timezone of the machine.
13 <verbatim>
14 ptime = time.strptime("2004-10-06 10:00:00", "%Y-%m-%d %H:%M:%S")[:8] + (-1,)
15 </verbatim>
16 This is what [Python] 2.3 does, implicitly.
4 JohnMcPherson 17
18 !!! threads
19
20 !! subprocess module
7 JohnMcPherson 21 If you use the ''subprocess'' module to spawn a child in one of your threads and you are using locks, there is a possibility of a deadlock. See the discussion in the [python newsgroup|http://groups.google.com/group/comp.lang.python/browse_thread/thread/5fae8a453c95ae89/42f5c9f9215dbb1e#42f5c9f9215dbb1e]. Basically, python internally uses locks during imports, and you can fix it in this instance by moving the ''"from errno import ENOENT, ENOTDIR"'' line from the ''_execvpe'' function in the os.py module up to the top, so that it doesn't happen after the fork(). This affects python 2.4 at least, not sure if it's fixed in 2.5.
5 JohnMcPherson 22
23 The ''subprocess'' module is also buggy if you have multiple threads using subprocess to create child processes. Basically, it isn't thread-safe when checking if a child has finished, and you occasionally get "<tt>~[Errno 10] No child processes</tt>" exceptions. See [mailing list thread|http://www.mail-archive.com/python-bugs-list@python.org/msg12685.html].
4 JohnMcPherson 24
25 !! python-gtk
26 If you want to do ''thread.start_new_thread()'' from within an app using pygtk, you need to call ''gtk.gdk.threads_init()'' first, otherwise the gtk event loop will screw you up.
6 JohnMcPherson 27
28 !!! Miscallaneous
29
30 !! os.getlogin()
31 The ''getlogin()'' function is supposed to return the username of the owner of the process. Unfortunately this works by looking in utmp(5),
32 and most terminal emulators in linux don't bother creating a new record for each window. This means the OS returns "ENOENT", and python
33 happily prints out <tt>OSError: ~[Errno 2] No such file or directory</tt>. On Linux/POSIX systems, you are probably better off doing:
34 <verbatim>
35 import os
36 import pwd
37 username = pwd.getpwuid(os.geteuid())[0]
38 </verbatim>
39
1 MattBrown 40
41 ----
42 CategoryProgramming, CategoryNotes