object.enable() anti-pattern

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri May 10 05:47:37 EDT 2013


On Fri, 10 May 2013 01:50:09 -0400, Roy Smith wrote:

> In article <518c7f05$0$29997$c3e8da3$5496439d at news.astraweb.com>,
>  Steven D'Aprano <steve+comp.lang.python at pearwood.info> wrote:
> 
>> there is no way to create a C file descriptor in a closed state. Such a
>> thing does not exist. If you have a file descriptor, the file is open.
>> Once you close it, the file descriptor is no longer valid.
> 
> Of course there is.
> 
> int fd = 37;
> 
> I've just created a file descriptor.  There is not enough information
> given to know if it corresponds to an open file or not.

No, you haven't created a file descriptor. You've made up a number which 
C will allow you to use as an index into the file descriptor table, 
because C is a high-level assembler with very little in the way of type 
safety, and what little there is you can normally bypass. What you 
haven't done is create the record in the file descriptor table. You can't 
expect that read(fd) or write(fd) will work, although both should fail 
safe rather than segfault if 37 happens to not be an actual file 
descriptor.

What you've done is the moral equivalent of choosing an integer at 
random, coercing it to a pointer, then dereferencing it to peek or poke 
at some memory address. (Although fortunately much safer.)

It's a nice hack, but not one that takes away from what I'm saying.


-- 
Steven



More information about the Python-list mailing list