GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-103015: Add entrypoint parameter to sqlite3.Connection.load_extension by erlend-aasland · Pull Request #103073 · python/cpython

Conversation

@erlend-aasland

@erlend-aasland erlend-aasland commented Mar 28, 2023 •
edited by bedevere-bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor
  • Add 'entrypoint' keyword arg to load_extension()
  • Add docs
  • Issue: Enhancement request: Add "entrypoint" as an option to sqlite3.load_extension() #103015

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@erlend-aasland
Add 'entrypoint' keyword arg to load_extension()
f232792
@bedevere-bot bedevere-bot mentioned this pull request Mar 28, 2023
Enhancement request: Add "entrypoint" as an option to sqlite3.load_extension() #103015
Closed
@bedevere-bot bedevere-bot added the awaiting core review label Mar 28, 2023
@erlend-aasland

erlend-aasland commented Mar 28, 2023

Copy link
Copy Markdown
Contributor Author

TODO: Currently, there is no tests for loadable SQLite extensions. We could probably add one of the example extensions SQLite provides (for example rot13).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
Add docs
094d8e2
@erlend-aasland
erlend-aasland force-pushed the sqlite-load-ext-entry branch from 5fc0772 to 094d8e2 Compare March 28, 2023 08:12
@erlend-aasland

erlend-aasland commented Mar 28, 2023

Copy link
Copy Markdown
Contributor Author

cc. @asg017

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
make regen-all
9d132cd
@asg017

asg017 commented Mar 28, 2023

Copy link
Copy Markdown

Thanks for putting this together @erlend-aasland , this looks awesome! Will try it out myself soon.

Re tests: Feel free to use this sample extension. I wrote it myself, also contributed to the Datasette project, and includes multiple entrypoints.

/*
** This file implements a SQLite extension with multiple entrypoints.
**
** The default entrypoint, sqlite3_ext_init, has a single function "a".
** The 1st alternate entrypoint, sqlite3_ext_b_init, has a single function "b".
** The 2nd alternate entrypoint, sqlite3_ext_c_init, has a single function "c".
**
** Compiling instructions: 
**     https://www.sqlite.org/loadext.html#compiling_a_loadable_extension
**
*/

#include "sqlite3ext.h"

SQLITE_EXTENSION_INIT1

// SQL function that returns back the value supplied during sqlite3_create_function()
static void func(sqlite3_context *context, int argc, sqlite3_value **argv) {
  sqlite3_result_text(context, (char *) sqlite3_user_data(context), -1, SQLITE_STATIC);
}


// The default entrypoint, since it matches the "ext.dylib"/"ext.so" name
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_ext_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
  SQLITE_EXTENSION_INIT2(pApi);
  return sqlite3_create_function(db, "a", 0, 0, "a", func, 0, 0);
}

// Alternate entrypoint #1
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_ext_b_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
  SQLITE_EXTENSION_INIT2(pApi);
  return sqlite3_create_function(db, "b", 0, 0, "b", func, 0, 0);
}

// Alternate entrypoint #2
#ifdef _WIN32
__declspec(dllexport)
#endif
int sqlite3_ext_c_init(sqlite3 *db, char **pzErrMsg, const sqlite3_api_routines *pApi) {
  SQLITE_EXTENSION_INIT2(pApi);
  return sqlite3_create_function(db, "c", 0, 0, "c", func, 0, 0);
}

Compiling SQLite extensions are always tricky, but it should need just something like this:

gcc ext.c -fPIC -shared -o ext.dylib

With possibly -I path/to/sqlite if it's not globally installed, and only .dylib for MacOS system. For windows, change the suffx to .dll, and Linux to .so. To test that extension, it should be something like:

import sqlite3
db = sqlite3.connect(":memory:");

# You can leave off the dylib/so/dll suffix, since SQLite will implicitly add it

# loading without an entrypoint will resolve to the default sqlite3_ext_init entrypoint
db.load_extension("./ext");
assert db.execute("select a()").fetchone()[0] == "a"

db.load_extension("./ext", "sqlite3_ext_b_init");
assert db.execute("select b()").fetchone()[0] == "b"

db.load_extension("./ext", "sqlite3_ext_c_init");
assert db.execute("select c()").fetchone()[0] == "c"

Let me know if you want some help adding an extension to the test suite!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Apr 5, 2023

Copy link
Copy Markdown
Contributor Author

Will try it out myself soon.

@asg017: did you get to try it out?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@asg017

asg017 commented Apr 5, 2023

Copy link
Copy Markdown

@erlend-aasland Just tried this out, and it works as expected!

I compiled the above ext.c extension to ext.so, compiled a version of python on this branch, and ran the following:

import sqlite3    
db = sqlite3.connect(":memory:")

db.load_extension('./ext')
assert db.execute("select a()").fetchone()[0] == "a"

db.load_extension("./ext", entrypoint="sqlite3_ext_b_init")
assert db.execute("select b()").fetchone()[0] == "b"

db.load_extension("./ext", entrypoint="sqlite3_ext_c_init")
assert db.execute("select c()").fetchone()[0] == "c"

And it worked! Thanks again for this work.

erlend-aasland reacted with heart emoji
All reactions
  • ❤️ 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@arhadthedev arhadthedev removed a link to an issue Apr 7, 2023
MANAGER CREDENTIALS #103345
Closed
@erlend-aasland

erlend-aasland commented Apr 8, 2023

Copy link
Copy Markdown
Contributor Author

@asg017, thanks! I'll revisit this PR soon; I need to tweak the sqlite3 extension module build to incorporate the SQLite extension check first. I think I'll do that as a separate PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Apr 8, 2023

Copy link
Copy Markdown
Contributor Author

(Ping me in two weeks if you did not hear anything yet.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
Pull in main
b405cc1
@asg017

asg017 commented Apr 19, 2023

Copy link
Copy Markdown

Hey @erlend-aasland , just pinging here after two weeks! Let me know if I can help in any way

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

Hey @erlend-aasland , just pinging here after two weeks! Let me know if I can help in any way

Thanks, I'll revisit this shortly!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
Pull in main
a2052d2
@erlend-aasland

erlend-aasland commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

I need to tweak the sqlite3 extension module build to incorporate the SQLite extension check first. I think I'll do that as a separate PR.

FTR, I won't let this block this PR.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
erlend-aasland marked this pull request as ready for review April 24, 2023 12:46
@erlend-aasland
erlend-aasland requested a review from berkerpeksag as a code owner April 24, 2023 12:46
@erlend-aasland
erlend-aasland requested a review from AlexWaygood April 24, 2023 12:47
@erlend-aasland

erlend-aasland commented Apr 24, 2023

Copy link
Copy Markdown
Contributor Author

@AlexWaygood, do you want to take a look at the added docs?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AlexWaygood
AlexWaygood reviewed Apr 24, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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.

A few small comments :)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/sqlite3.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/sqlite3.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Doc/library/sqlite3.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

erlend-aasland added 3 commits April 25, 2023 10:04
@erlend-aasland
Pull in main again
c22350a
@erlend-aasland
Address review
45a7a08
@erlend-aasland
Link to SQLite docs and try to improve wording
89b272a
AlexWaygood
AlexWaygood approved these changes Apr 25, 2023
View reviewed changes

@AlexWaygood AlexWaygood left a comment

Copy link
Copy Markdown
Member

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.

The docs look great!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Doc/library/sqlite3.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland

erlend-aasland commented Apr 26, 2023

Copy link
Copy Markdown
Contributor Author

Thanks for the reviews; I'll land this later today.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@erlend-aasland
erlend-aasland merged commit 222c63f into python:main Apr 26, 2023
@erlend-aasland
erlend-aasland deleted the sqlite-load-ext-entry branch April 26, 2023 19:22
@erlend-aasland erlend-aasland linked an issue Apr 26, 2023 that may be closed by this pull request
Enhancement request: Add "entrypoint" as an option to sqlite3.load_extension() #103015
Closed
itamaro pushed a commit to itamaro/cpython that referenced this pull request Apr 26, 2023
@erlend-aasland @itamaro
pythongh-103015: Add entrypoint keyword param to sqlite3.Connection.l…
ffc55c4
…oad_extension (python#103073)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AlexWaygood AlexWaygood AlexWaygood approved these changes
@berkerpeksag berkerpeksag Awaiting requested review from berkerpeksag
+1 more reviewer
@asg017 asg017 asg017 left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Enhancement request: Add "entrypoint" as an option to sqlite3.load_extension()

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@erlend-aasland @asg017 @AlexWaygood @bedevere-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.