Skip to content

[3.11] gh-91922: Fix sqlite connection on nonstardard locales and paths (GH-92926) #93006

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

Merged
merged 1 commit into from
May 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions Lib/test/test_sqlite3/test_dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,19 @@
# 3. This notice may not be removed or altered from any source distribution.

import contextlib
import os
import sqlite3 as sqlite
import subprocess
import sys
import threading
import unittest
import urllib.parse

from test.support import (
SHORT_TIMEOUT,
bigmemtest,
check_disallow_instantiation,
threading_helper,
)
from test.support import SHORT_TIMEOUT, bigmemtest, check_disallow_instantiation
from test.support import threading_helper
from _testcapi import INT_MAX, ULLONG_MAX
from os import SEEK_SET, SEEK_CUR, SEEK_END
from test.support.os_helper import TESTFN, unlink, temp_dir
from test.support.os_helper import TESTFN, TESTFN_UNDECODABLE, unlink, temp_dir, FakePath


# Helper for tests using TESTFN
Expand Down Expand Up @@ -663,11 +661,19 @@ class OpenTests(unittest.TestCase):
def test_open_with_path_like_object(self):
""" Checks that we can successfully connect to a database using an object that
is PathLike, i.e. has __fspath__(). """
class Path:
def __fspath__(self):
return TESTFN
path = Path()
path = FakePath(TESTFN)
with managed_connect(path) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)

@unittest.skipIf(sys.platform == "win32", "skipped on Windows")
@unittest.skipIf(sys.platform == "darwin", "skipped on macOS")
@unittest.skipUnless(TESTFN_UNDECODABLE, "only works if there are undecodable paths")
def test_open_with_undecodable_path(self):
self.addCleanup(unlink, TESTFN_UNDECODABLE)
path = TESTFN_UNDECODABLE
with managed_connect(path) as cx:
self.assertTrue(os.path.exists(path))
cx.execute(self._sql)

def test_open_uri(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix function :func:`sqlite.connect` and the :class:`sqlite.Connection`
constructor on non-UTF-8 locales. Also, they now support bytes paths
non-decodable with the current FS encoding.
17 changes: 6 additions & 11 deletions Modules/_sqlite/clinic/connection.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 14 additions & 42 deletions Modules/_sqlite/connection.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,32 +92,6 @@ isolation_level_converter(PyObject *str_or_none, const char **result)
return 1;
}

static int
clinic_fsconverter(PyObject *pathlike, const char **result)
{
PyObject *bytes = NULL;
Py_ssize_t len;
char *str;

if (!PyUnicode_FSConverter(pathlike, &bytes)) {
goto error;
}
if (PyBytes_AsStringAndSize(bytes, &str, &len) < 0) {
goto error;
}
if ((*result = (const char *)PyMem_Malloc(len+1)) == NULL) {
goto error;
}

memcpy((void *)(*result), str, len+1);
Py_DECREF(bytes);
return 1;

error:
Py_XDECREF(bytes);
return 0;
}

#define clinic_state() (pysqlite_get_state_by_type(Py_TYPE(self)))
#include "clinic/connection.c.h"
#undef clinic_state
Expand Down Expand Up @@ -159,25 +133,17 @@ new_statement_cache(pysqlite_Connection *self, pysqlite_state *state,
}

/*[python input]
class FSConverter_converter(CConverter):
type = "const char *"
converter = "clinic_fsconverter"
def converter_init(self):
self.c_default = "NULL"
def cleanup(self):
return f"PyMem_Free((void *){self.name});\n"

class IsolationLevel_converter(CConverter):
type = "const char *"
converter = "isolation_level_converter"

[python start generated code]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=be142323885672ab]*/
/*[python end generated code: output=da39a3ee5e6b4b0d input=cbcfe85b253061c2]*/

/*[clinic input]
_sqlite3.Connection.__init__ as pysqlite_connection_init

database: FSConverter
database: object
timeout: double = 5.0
detect_types: int = 0
isolation_level: IsolationLevel = ""
Expand All @@ -188,14 +154,19 @@ _sqlite3.Connection.__init__ as pysqlite_connection_init
[clinic start generated code]*/

static int
pysqlite_connection_init_impl(pysqlite_Connection *self,
const char *database, double timeout,
int detect_types, const char *isolation_level,
pysqlite_connection_init_impl(pysqlite_Connection *self, PyObject *database,
double timeout, int detect_types,
const char *isolation_level,
int check_same_thread, PyObject *factory,
int cache_size, int uri)
/*[clinic end generated code: output=7d640ae1d83abfd4 input=342173993434ba1e]*/
/*[clinic end generated code: output=839eb2fee4293bda input=b8ce63dc6f70a383]*/
{
if (PySys_Audit("sqlite3.connect", "s", database) < 0) {
if (PySys_Audit("sqlite3.connect", "O", database) < 0) {
return -1;
}

PyObject *bytes;
if (!PyUnicode_FSConverter(database, &bytes)) {
return -1;
}

Expand All @@ -210,14 +181,15 @@ pysqlite_connection_init_impl(pysqlite_Connection *self,
sqlite3 *db;
int rc;
Py_BEGIN_ALLOW_THREADS
rc = sqlite3_open_v2(database, &db,
rc = sqlite3_open_v2(PyBytes_AS_STRING(bytes), &db,
SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
(uri ? SQLITE_OPEN_URI : 0), NULL);
if (rc == SQLITE_OK) {
(void)sqlite3_busy_timeout(db, (int)(timeout*1000));
}
Py_END_ALLOW_THREADS

Py_DECREF(bytes);
if (db == NULL && rc == SQLITE_NOMEM) {
PyErr_NoMemory();
return -1;
Expand Down