-
-
Notifications
You must be signed in to change notification settings - Fork 34.8k
bpo-31843: sqlite3.connect() now accepts PathLike objects as database name #4299
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
8dd99cb
954615c
62a7d00
be022cf
ceb6de5
72e4cf7
4cef352
8b7f340
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,9 +172,13 @@ Module functions and constants | |
|
|
||
| .. function:: connect(database[, timeout, detect_types, isolation_level, check_same_thread, factory, cached_statements, uri]) | ||
|
|
||
| Opens a connection to the SQLite database file *database*. You can use | ||
| ``":memory:"`` to open a database connection to a database that resides in RAM | ||
| instead of on disk. | ||
| Opens a connection to the SQLite database file *database* and return a | ||
| :class:`Connection` object. | ||
|
|
||
| *database* is a :term:`path-like object` giving the pathname (absolute or | ||
| relative to the current working directory) of the database file to be opened. | ||
| You can use ``":memory:"`` to open a database connection to a database that | ||
| resides in RAM instead of on disk. | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You have to document the change. Something like:
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
| When a database is accessed by multiple connections, and one of the processes | ||
| modifies the database, the SQLite database is locked until that transaction is | ||
|
|
@@ -223,6 +227,9 @@ Module functions and constants | |
| .. versionchanged:: 3.4 | ||
| Added the *uri* parameter. | ||
|
|
||
| .. versionchanged:: 3.7 | ||
| *database* can now also be a :term:`path-like object`, not only a string. | ||
|
|
||
|
|
||
| .. function:: register_converter(typename, callable) | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -160,6 +160,15 @@ def CheckInTransactionRO(self): | |
| with self.assertRaises(AttributeError): | ||
| self.cx.in_transaction = True | ||
|
|
||
| def CheckOpenWithPathLikeObject(self): | ||
| """ Checks that we can succesfully connect to a database using an object that | ||
| is PathLike, i.e. has __fspath__(). """ | ||
| self.addCleanup(unlink, TESTFN) | ||
| import pathlib | ||
| path = pathlib.Path(TESTFN) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure that it's ok to depend on pathlib when testing sqlite. I propose to replace it with: |
||
| with sqlite.connect(path) as cx: | ||
| cx.execute('create table test(id integer)') | ||
|
|
||
| def CheckOpenUri(self): | ||
| if sqlite.sqlite_version_info < (3, 7, 7): | ||
| with self.assertRaises(sqlite.NotSupportedError): | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* | |
| "check_same_thread", "factory", "cached_statements", "uri", | ||
| NULL | ||
| }; | ||
| char* database; | ||
| PyObject* database; | ||
| int detect_types = 0; | ||
| PyObject* isolation_level; | ||
| PyObject* factory = NULL; | ||
|
|
@@ -66,7 +66,7 @@ static PyObject* module_connect(PyObject* self, PyObject* args, PyObject* | |
|
|
||
| PyObject* result; | ||
|
|
||
| if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s|diOiOip", kwlist, | ||
| if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|diOiOip", kwlist, | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand why the function checks arguments. Why not only relying on Connection constructor to check arguments? Either remove the whole code to parse arguments, or fix the reference leak: Py_DECREF(database); is needed.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Oops, it's not needed, you don't use PyUnicode_FSConverter, sorry. Your code is correct.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Phaqui pointed me on IRC that the function contains a comment explaining that we have to parse all arguments to just extract the "factory" keyword argument... In that case, the code is ok. |
||
| &database, &timeout, &detect_types, | ||
| &isolation_level, &check_same_thread, | ||
| &factory, &cached_statements, &uri)) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"return a Connection" is wrong, since it can be overridden by the factory parameter.
Either remove this addition, or rephrase it like: "Open a connection (...). By default, return a Connection object."