Skip to content

Commit 505d782

Browse files
committed
feat: add warning about data loss/corruption with JSONStorage access_mode options
Closes: msiemens#506
1 parent d0416e9 commit 505d782

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

tinydb/storages.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import io
77
import json
88
import os
9+
import warnings
910
from abc import ABC, abstractmethod
1011
from typing import Dict, Any, Optional
1112

@@ -100,6 +101,12 @@ def __init__(self, path: str, create_dirs=False, encoding=None, access_mode='r+'
100101
self._mode = access_mode
101102
self.kwargs = kwargs
102103

104+
if access_mode not in ('r', 'rb', 'r+', 'rb+'):
105+
warnings.warn(
106+
'Using an `access_mode` other than \'r\', \'rb\', \'r+\' '
107+
'or \'rb+\' can cause data loss or corruption'
108+
)
109+
103110
# Create the file if it doesn't exist and creating is allowed by the
104111
# access mode
105112
if any([character in self._mode for character in ('+', 'w', 'a')]): # any of the writing modes

0 commit comments

Comments
 (0)