1 |
On Saturday 29 October 2005 23:22, Brian Harring wrote: |
2 |
> On Sat, Oct 29, 2005 at 07:49:49PM +0900, Jason Stubbs wrote: |
3 |
> > This patch simplifies the moving around and closing of file descriptors |
4 |
> > when setting up the pipe. The addition of fd_unused to the parameter list |
5 |
> > is in preparation of a later patch. |
6 |
> > |
7 |
> > +++ portage_exec.py 2005-10-29 19:07:21.000000000 +0900 |
8 |
> > - # this may look ugly, but basically it moves file descriptors around |
9 |
> > to ensure no - # handles that are needed are accidentally closed during |
10 |
> > the final dup2 calls. - trg_fd=[] |
11 |
> > - if type(fd_pipes)==types.DictType: |
12 |
> > - src_fd=[] |
13 |
> > - k=fd_pipes.keys() |
14 |
> > - k.sort() |
15 |
> > - for x in k: |
16 |
> > - trg_fd.append(x) |
17 |
> > - src_fd.append(fd_pipes[x]) |
18 |
> > - for x in range(0,len(trg_fd)): |
19 |
> > - if trg_fd[x] == src_fd[x]: |
20 |
> > - continue |
21 |
> > - if trg_fd[x] in src_fd[x+1:]: |
22 |
> > - new=os.dup2(trg_fd[x],max(src_fd) + 1) |
23 |
> > - os.close(trg_fd[x]) |
24 |
> > - try: |
25 |
> > - while True: |
26 |
> > - src_fd[s.index(trg_fd[x])]=new |
27 |
|
28 |
"s" doesn't exist. Whatever this code was intending to do, it's not doing it. |
29 |
|
30 |
> > - except SystemExit, e: |
31 |
> > - raise |
32 |
> > - except: |
33 |
> > - pass |
34 |
> > - for x in range(0,len(trg_fd)): |
35 |
> > - if trg_fd[x] != src_fd[x]: |
36 |
> > - os.dup2(src_fd[x], trg_fd[x]) |
37 |
> > - else: |
38 |
> > - trg_fd=[0,1,2] |
39 |
> |
40 |
> Above *does* need reworking, knew that when I wrote it- but the reason |
41 |
> it's so damn ugly is to cover against dup2 calls stomping handles that |
42 |
> are needed. Must be a collect, then re-arrange algo, not single pass |
43 |
> |
44 |
> fd_pipes = [1:2, 2:1] |
45 |
> |
46 |
> > + for x in fd_pipes: |
47 |
> > + if x != fd_pipes[x]: |
48 |
> > + os.dup2(fd_pipes[x], x) |
49 |
> > + if fd_pipes[x] not in fd_pipes and fd_pipes[x] not in fd_unused: |
50 |
> > + fd_unused.append(fd_pipes[x]) |
51 |
> > + for x in fd_unused: |
52 |
> > + os.close(x) |
53 |
> |
54 |
> Replacement code will fail with the simple stdout <-> stderr switch. |
55 |
> dup2 will set the actual fd 2 handle to fd 1, but *prior* to |
56 |
> duplicating fd 2 to a throw away handle so it remains open, end result |
57 |
> is that fd 2 is now fd 1, so the 2 -> 1 move does nothing. |
58 |
> |
59 |
> Code above will result in 1 == 1, 2 == 1, rather then 1 == 2, 2 == 1 |
60 |
> |
61 |
> Which is a regression, although existing code _probably_ won't choke |
62 |
> on it- the protection I added made it robust so that it handles the |
63 |
> corner case without tripping up any dev writing the code, rather then |
64 |
> silently tieing the fd's together. |
65 |
|
66 |
Yep, missed that case. Respun and left the fd_unused for later. |
67 |
|
68 |
-- |
69 |
Jason Stubbs |