林果皞
林果皞

Reputation: 7813

the purpose of interpreter interactive mode keep file opening

If the code is run as script:

$ cat open_sleep.py 
import time
open("/tmp/test")
time.sleep(1000)

$ python open_sleep.py 

OR I do this without interactive mode:

$ python -c 'import time;open("/tmp/test");time.sleep(1000)' 

There is no file keep opening:

$ ls -la /proc/`pgrep python`/fd
total 0
dr-x------. 2 ack0hole ack0hole  0 Aug 30 14:19 .
dr-xr-xr-x. 8 ack0hole ack0hole  0 Aug 30 14:19 ..
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:19 0 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:19 1 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:19 2 -> /dev/pts/2
$ 

Unless I assign a variable return by open()

$ cat open_sleep.py 
import time
o = open("/tmp/test")
time.sleep(1000)

$ python open_sleep.py 

OR

$ python -c 'import time;o=open("/tmp/test");time.sleep(1000)' 

Then the file will keep opening::

$ ls -la /proc/`pgrep python`/fd
total 0
dr-x------. 2 ack0hole ack0hole  0 Aug 30 14:21 .
dr-xr-xr-x. 8 ack0hole ack0hole  0 Aug 30 14:21 ..
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:21 0 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:21 1 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:21 2 -> /dev/pts/2
lr-x------. 1 ack0hole ack0hole 64 Aug 30 14:21 3 -> /tmp/test
$ 

But the interactive mode is not the case, even i'm not assign variable to open():

>>> import time;open("/tmp/test");time.sleep(1000)
<open file '/tmp/test', mode 'r' at 0xb7400128>

I still can see the file keep opening:

$ ls -la /proc/`pgrep python`/fd
total 0
dr-x------. 2 ack0hole ack0hole  0 Aug 30 14:16 .
dr-xr-xr-x. 8 ack0hole ack0hole  0 Aug 30 14:16 ..
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:16 0 -> /dev/pts/4
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:16 1 -> /dev/pts/4
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:16 2 -> /dev/pts/4
lr-x------. 1 ack0hole ack0hole 64 Aug 30 14:17 3 -> /tmp/test
$ 

If the indentation fail:

>>>     import time;open("/tmp/test");time.sleep(1000)
  File "<stdin>", line 1
    import time;open("/tmp/test");time.sleep(1000)
    ^
IndentationError: unexpected indent
>>> 

the socket is keep opening without filename:

$ ls -la /proc/`pgrep python`/fd
total 0
dr-x------. 2 ack0hole ack0hole  0 Aug 30 14:38 .
dr-xr-xr-x. 8 ack0hole ack0hole  0 Aug 30 14:38 ..
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:38 0 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:38 1 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:38 2 -> /dev/pts/2
lrwx------. 1 ack0hole ack0hole 64 Aug 30 14:38 3 -> socket:[411151]

I have two questions:

  1. What is the purpose of interpreter interactive mode keep file opening, even though open(file) did not assign return value? If the purpose is debugging purpose, any example of this debugging?

  2. Why interpreter interactive mode open file in the first place even there are indentation error exist?

Upvotes: -1

Views: 109

Answers (1)

shx2
shx2

Reputation: 64338

The reason you don't see open files in the examples where you don't see them, is that right after the file is opened, the reference-count of the file object drops to 0 because the result is not assigned to a variable, so the file is closed immediately.

The reason this does not happen in interactive mode, is that a reference to the file object is kept in the _ variable while the second sleep function runs, thus the file remains open.

See here for a discussion about the _ special variable.

As to question 2, it is not suppose to happen. You must have made a mistake in you check. Nothing runs if your code raises IndentationError.

Upvotes: 2

Related Questions