avoiding file corruption

Diez B. Roggisch deets at nospam.web.de
Sun Aug 27 10:22:12 EDT 2006


Amir Michail schrieb:
> Diez B. Roggisch wrote:
>> Amir Michail schrieb:
>>> Paolo Pantaleo wrote:
>>>> 27 Aug 2006 00:44:33 -0700, Amir  Michail <amichail at gmail.com>:
>>>>> Hi,
>>>>>
>>>>> Trying to open a file for writing that is already open for writing
>>>>> should result in an exception.
>>>>>
>>>>> It's all too easy to accidentally open a shelve for writing twice and
>>>>> this can lead to hard to track down database corruption errors.
>>>>>
>>>>> Amir
>>>>>
>>>>> --
>>>>> http://mail.python.org/mailman/listinfo/python-list
>>>>>
>>>> Even if it could be strange, the OS usually allow you to open a file
>>>> twice, that's up to the programmer to ensure the consistency of the
>>>> operations.
>>>>
>>>> PAolo
>>>>
>>> But if this is usually a serious bug, shouldn't an exception be raised?
>> executing "rm -rf /" via subprocess is usually also a bad idea. So? No
>> language can prevent you from doing such mistake. And there is no way to
>> know if a file is opened twice - it might that you open the same file
>> twice via e.g. a network share. No way to know that it is the same file.
>>
>> Diez
> 
> The scenario I have in mind is something like this:
> 
> def f():
>   db=shelve.open('test.db', 'c')
>   # do some stuff with db
>   g()
>   db.close()
> 
> def g():
>   db=shelve.open('test.db', 'c')
>   # do some stuff with db
>   db.close()
> 
> I think it would be easy for python to check for this problem in
> scenarios like this.

You are requesting a general solution for a very particular problem. As 
I pointed out, that solution is unlikely to work reliably - if not 
infeasible at all.

If you really have problems as the above, use a custom wrapper for 
shelve that prevents _you_ from making that mistake.

Diez



More information about the Python-list mailing list