You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the bug
I quite think this is a bug, though I am surprised no one stumbled upon it before.
What I observe is, if I fake a file that's in a directory that does exist on the real filesystem, then sqlite3 can open it. No problem, or almost, because the database file is then really created on the real filesystem (unexpected).
But if I fake a file in a directory that does not exist on the real filesystem, then sqlite3 cannot open it (raises: sqlite3.OperationalError: unable to open database file).
Is this a bug, or maybe a compatibility problem with sqlite3?
I have used pathlib.Path and os.makedirs(), I thought they're both supported in pyfakefs. I have replaced the call to os.makedirs() by Path.mkdir(), I had to add the parents=True in order for this second option to work, but the results are the same.
How To Reproduce
Here's the complete test file (note: the only difference between the passing and the failing tests is the directory's name: in the passing test, /home/nico/tests/db/ is an existing directory on my real filesystem; whereas in the failing test, /home/nico/tests/db2/ does not exist):
import os
import sys
import sqlite3
from pathlib import Path
class DBCM:
"""
Simple Context Manager for sqlite3 databases.
"""
def __init__(self, path):
self.path = path
self.conn = None
self.cursor = None
def __enter__(self):
self.conn = sqlite3.connect(str(self.path))
self.cursor = self.conn.cursor()
return self.cursor
def __exit__(self, exc_class, exc, traceback):
self.conn.close()
def test_passing(fs):
testdb_path = Path('/home/nico/tests/db/blank.sqlite3')
assert not testdb_path.exists()
os.makedirs(testdb_path.parent, mode=0o770)
# testdb_path.parent.mkdir(mode=0o770, parents=True)
testdb_path.touch(mode=0o777)
assert testdb_path.exists()
sys.stderr.write(f'type(testdb_path)={type(testdb_path)}\n')
with DBCM(testdb_path) as cursor:
print(f"alright, cursor is {cursor}")
def test_failing(fs):
testdb_path = Path('/home/nico/tests/db2/blank.sqlite3')
assert not testdb_path.exists()
os.makedirs(testdb_path.parent, mode=0o770)
# testdb_path.parent.mkdir(mode=0o770, parents=True)
testdb_path.touch(mode=0o777)
assert testdb_path.exists()
sys.stderr.write(f'type(testdb_path)={type(testdb_path)}\n')
with DBCM(testdb_path) as cursor:
print(f"alright, cursor is {cursor}")
Thanks for the report! Without looking into that I suspect that this is a case of a pyfakefs limitation, given that sqlite3 is written in C and the Python package is just a wrapper around it - though that is only a guess.
Unfortunately, as I suspected, sqlite3 will not work nicely with pyfakefs. Opening files is done using the C interface of squite3 itself (e.g. sqlite3_open and related functions), and there is no way to patch this in Python code.
I will probably add sqlite3 to the list of modules not working with pyfakefs.
I've also just opened a new category "limitations" in the discussions, where I will move this and related issues.
Describe the bug
I quite think this is a bug, though I am surprised no one stumbled upon it before.
What I observe is, if I fake a file that's in a directory that does exist on the real filesystem, then
sqlite3
can open it. No problem, or almost, because the database file is then really created on the real filesystem (unexpected).But if I fake a file in a directory that does not exist on the real filesystem, then
sqlite3
cannot open it (raises:sqlite3.OperationalError: unable to open database file
).Is this a bug, or maybe a compatibility problem with
sqlite3
?I have used
pathlib.Path
andos.makedirs()
, I thought they're both supported inpyfakefs
. I have replaced the call toos.makedirs()
byPath.mkdir()
, I had to add theparents=True
in order for this second option to work, but the results are the same.How To Reproduce
Here's the complete test file (note: the only difference between the passing and the failing tests is the directory's name: in the passing test,
/home/nico/tests/db/
is an existing directory on my real filesystem; whereas in the failing test,/home/nico/tests/db2/
does not exist):Here's the complete output of the tests:
Your environment
The text was updated successfully, but these errors were encountered: