sqlparse-turbo 0.1.0__cp39-abi3-win_amd64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- sqlparse_turbo/__init__.py +435 -0
- sqlparse_turbo/__main__.py +20 -0
- sqlparse_turbo/_native.pyd +0 -0
- sqlparse_turbo/_unicode_drift.py +62 -0
- sqlparse_turbo-0.1.0.dist-info/METADATA +349 -0
- sqlparse_turbo-0.1.0.dist-info/RECORD +10 -0
- sqlparse_turbo-0.1.0.dist-info/WHEEL +4 -0
- sqlparse_turbo-0.1.0.dist-info/entry_points.txt +2 -0
- sqlparse_turbo-0.1.0.dist-info/licenses/LICENSE +26 -0
- sqlparse_turbo-0.1.0.dist-info/sboms/sqlparse-front.cyclonedx.json +1166 -0
|
@@ -0,0 +1,435 @@
|
|
|
1
|
+
"""sqlparse's format() and split(), run in Rust, with sqlparse itself for everything the Rust was not proven on.
|
|
2
|
+
|
|
3
|
+
import sqlparse_turbo
|
|
4
|
+
sqlparse_turbo.format(sql, reindent=True, keyword_case='upper') # same signature and result as sqlparse.format
|
|
5
|
+
sqlparse_turbo.split(sql)
|
|
6
|
+
|
|
7
|
+
sqlparse_turbo.split_many(sqls) # split() of each, over all CPU threads
|
|
8
|
+
|
|
9
|
+
sqlparse_turbo.install() # sqlparse.format and sqlparse.split now go through here, for every caller
|
|
10
|
+
# that looks them up on the module (sqlparse.format(...)); a name bound earlier
|
|
11
|
+
# with `from sqlparse import format` keeps the original. sqlparse's default
|
|
12
|
+
# lexer gets the Rust tokenizer too, so sqlparse.parse() and everything else
|
|
13
|
+
# that lexes runs it.
|
|
14
|
+
|
|
15
|
+
The Rust is a port of sqlparse 0.6.1 checked call by call against it (see the project's README). A call goes to
|
|
16
|
+
the original sqlparse, unchanged, unless all of this holds:
|
|
17
|
+
|
|
18
|
+
- the installed sqlparse is 0.6.1;
|
|
19
|
+
- `sql` is a `str` (not bytes, not a stream, not a subclass); `encoding` is then not used, as in sqlparse,
|
|
20
|
+
which only decodes bytes with it;
|
|
21
|
+
- sqlparse's default lexer is as sqlparse sets it up (no add_keywords, set_SQL_REGEX, clear), and nothing in
|
|
22
|
+
sqlparse's modules or classes was replaced, added or removed since this package was imported (a patched
|
|
23
|
+
grouping function, filter or method goes to sqlparse, which uses it);
|
|
24
|
+
- every format() option has exactly the type the port takes: bool options are `bool` (not 0/1 or None),
|
|
25
|
+
`indent_width`/`wrap_after` are `int` (not bool) and `truncate_strings`/`right_margin` `int` or None, within
|
|
26
|
+
64 bits (`indent_width` at most 4096, see _INDENT_WIDTH_MAX), `keyword_case`/`identifier_case`/`output_format`
|
|
27
|
+
`str` or None, `truncate_char` `str`; other keywords are dropped, as sqlparse never reads them (`indent_char`,
|
|
28
|
+
which it does read, it always overwrites from `indent_tabs`); split's `strip_semicolon` is a `bool`;
|
|
29
|
+
- the text can be encoded as UTF-8 (a str with lone surrogates cannot);
|
|
30
|
+
- the text holds no code point this Python's Unicode version treats differently from the Unicode 14 the Rust
|
|
31
|
+
was generated from (CPython 3.11): see _unicode_drift.py. ASCII text always qualifies; on a Unicode version
|
|
32
|
+
not listed there, only ASCII text does.
|
|
33
|
+
|
|
34
|
+
Errors are sqlparse's own: an invalid option value raises `sqlparse.exceptions.SQLParseError` with sqlparse's
|
|
35
|
+
message, and so does a statement nested past Python's recursion limit, at the same depth as in sqlparse and,
|
|
36
|
+
as there, raised from a RecursionError.
|
|
37
|
+
"""
|
|
38
|
+
|
|
39
|
+
import unicodedata as _unicodedata
|
|
40
|
+
|
|
41
|
+
import sqlparse
|
|
42
|
+
import sqlparse.engine.filter_stack
|
|
43
|
+
import sqlparse.engine.grouping
|
|
44
|
+
import sqlparse.engine.statement_splitter
|
|
45
|
+
import sqlparse.filters.aligned_indent
|
|
46
|
+
import sqlparse.filters.others
|
|
47
|
+
import sqlparse.filters.output
|
|
48
|
+
import sqlparse.filters.reindent
|
|
49
|
+
import sqlparse.filters.right_margin
|
|
50
|
+
import sqlparse.filters.tokens
|
|
51
|
+
import sqlparse.formatter
|
|
52
|
+
import sqlparse.utils
|
|
53
|
+
from sqlparse import lexer as _lexer
|
|
54
|
+
from sqlparse import sql as _sql
|
|
55
|
+
from sqlparse.exceptions import SQLParseError as _SQLParseError
|
|
56
|
+
from sqlparse import tokens as _tokens
|
|
57
|
+
|
|
58
|
+
from . import _native
|
|
59
|
+
from ._unicode_drift import DRIFT as _DRIFT
|
|
60
|
+
from ._unicode_drift import PORTED_UNICODE as _PORTED_UNICODE
|
|
61
|
+
|
|
62
|
+
__all__ = ['format', 'split', 'split_many', 'parse', 'install', 'uninstall', 'PORTED_VERSION']
|
|
63
|
+
|
|
64
|
+
PORTED_VERSION = '0.6.1'
|
|
65
|
+
|
|
66
|
+
_original_format = sqlparse.format
|
|
67
|
+
_original_split = sqlparse.split
|
|
68
|
+
_original_parse = sqlparse.parse
|
|
69
|
+
_original_get_tokens = _lexer.Lexer.get_tokens
|
|
70
|
+
_ported = sqlparse.__version__ == PORTED_VERSION
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _drift_check():
|
|
75
|
+
"""str -> bool: whether the Rust treats every character of the text as this Python does. The drift list goes
|
|
76
|
+
to the Rust once (a bit per code point); a text is then checked in one pass over it."""
|
|
77
|
+
version = _unicodedata.unidata_version
|
|
78
|
+
if version == _PORTED_UNICODE:
|
|
79
|
+
return lambda text: True
|
|
80
|
+
# a Unicode version the list does not know: no non-ASCII text runs in Rust
|
|
81
|
+
_native.set_drift(list(_DRIFT.get(version, ((0x80, 0x10FFFF),))))
|
|
82
|
+
drifted = _native.drifted
|
|
83
|
+
|
|
84
|
+
def ported(text):
|
|
85
|
+
if text.isascii():
|
|
86
|
+
return True
|
|
87
|
+
try:
|
|
88
|
+
return not drifted(text)
|
|
89
|
+
except UnicodeEncodeError: # lone surrogates: sqlparse's
|
|
90
|
+
return False
|
|
91
|
+
|
|
92
|
+
return ported
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
_text_ported = _drift_check()
|
|
96
|
+
|
|
97
|
+
_BOOL, _INT, _OPT_INT, _STR, _OPT_STR = range(5)
|
|
98
|
+
_OPTIONS = {
|
|
99
|
+
'keyword_case': _OPT_STR,
|
|
100
|
+
'identifier_case': _OPT_STR,
|
|
101
|
+
'output_format': _OPT_STR,
|
|
102
|
+
'truncate_char': _STR,
|
|
103
|
+
'strip_comments': _BOOL,
|
|
104
|
+
'use_space_around_operators': _BOOL,
|
|
105
|
+
'strip_whitespace': _BOOL,
|
|
106
|
+
'reindent': _BOOL,
|
|
107
|
+
'reindent_aligned': _BOOL,
|
|
108
|
+
'indent_columns': _BOOL,
|
|
109
|
+
'indent_after_first': _BOOL,
|
|
110
|
+
'indent_tabs': _BOOL,
|
|
111
|
+
'comma_first': _BOOL,
|
|
112
|
+
'compact': _BOOL,
|
|
113
|
+
'indent_width': _INT,
|
|
114
|
+
'wrap_after': _INT,
|
|
115
|
+
'truncate_strings': _OPT_INT,
|
|
116
|
+
'right_margin': _OPT_INT,
|
|
117
|
+
}
|
|
118
|
+
_I64_MIN = -(2**63)
|
|
119
|
+
_I64_MAX = 2**63 - 1
|
|
120
|
+
# Every new line gets indent_width x nesting characters, so a huge width turns a small statement into gigabytes.
|
|
121
|
+
# Out of memory, sqlparse raises MemoryError while Rust stops the process (a failed allocation cannot be
|
|
122
|
+
# caught), so widths past any real use stay with sqlparse.
|
|
123
|
+
_INDENT_WIDTH_MAX = 4096
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _ported_options(options):
|
|
127
|
+
"""The options to give the Rust, or None when a call has to go to sqlparse. Keywords sqlparse does not read
|
|
128
|
+
are left out."""
|
|
129
|
+
unknown = False
|
|
130
|
+
for name, value in options.items():
|
|
131
|
+
kind = _OPTIONS.get(name)
|
|
132
|
+
if kind is None:
|
|
133
|
+
unknown = True
|
|
134
|
+
continue
|
|
135
|
+
t = type(value)
|
|
136
|
+
if kind is _BOOL:
|
|
137
|
+
if t is not bool:
|
|
138
|
+
return None
|
|
139
|
+
elif kind is _OPT_STR:
|
|
140
|
+
if value is not None and t is not str:
|
|
141
|
+
return None
|
|
142
|
+
elif kind is _STR:
|
|
143
|
+
if t is not str:
|
|
144
|
+
return None
|
|
145
|
+
elif kind is _INT or kind is _OPT_INT:
|
|
146
|
+
if kind is _OPT_INT and value is None:
|
|
147
|
+
continue
|
|
148
|
+
if t is not int or not _I64_MIN <= value <= _I64_MAX:
|
|
149
|
+
return None
|
|
150
|
+
if name == 'indent_width' and value > _INDENT_WIDTH_MAX:
|
|
151
|
+
return None
|
|
152
|
+
if unknown:
|
|
153
|
+
return {name: value for name, value in options.items() if name in _OPTIONS}
|
|
154
|
+
return options
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# The default lexer as sqlparse sets it up, taken from a fresh Lexer on first use: the keyword dicts themselves
|
|
158
|
+
# (held, so compared by identity), their sizes, and SQL_REGEX by content. A rules list seen to match is
|
|
159
|
+
# remembered, so the common call compares one reference.
|
|
160
|
+
_default_keywords = None
|
|
161
|
+
_default_sizes = None
|
|
162
|
+
_default_rx = None
|
|
163
|
+
_rx_seen = None
|
|
164
|
+
|
|
165
|
+
|
|
166
|
+
def _regex_content(rx_list):
|
|
167
|
+
return [(m.__self__.pattern, m.__self__.flags, tt) for m, tt in rx_list]
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def _lexer_ported():
|
|
171
|
+
global _default_keywords, _default_sizes, _default_rx, _rx_seen
|
|
172
|
+
if _default_keywords is None:
|
|
173
|
+
ref = _lexer.Lexer()
|
|
174
|
+
ref.default_initialization()
|
|
175
|
+
_default_keywords = list(ref._keywords)
|
|
176
|
+
_default_sizes = [len(d) for d in _default_keywords]
|
|
177
|
+
_default_rx = _regex_content(ref._SQL_REGEX)
|
|
178
|
+
lx = _lexer.Lexer.get_default_instance()
|
|
179
|
+
kws = lx._keywords
|
|
180
|
+
if len(kws) != len(_default_keywords):
|
|
181
|
+
return False
|
|
182
|
+
for d, default, size in zip(kws, _default_keywords, _default_sizes):
|
|
183
|
+
if d is not default or len(d) != size:
|
|
184
|
+
return False
|
|
185
|
+
rx = lx._SQL_REGEX
|
|
186
|
+
if rx is _rx_seen:
|
|
187
|
+
return True
|
|
188
|
+
if _regex_content(rx) != _default_rx:
|
|
189
|
+
return False
|
|
190
|
+
_rx_seen = rx
|
|
191
|
+
return True
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
_RECURSION = 'Maximum recursion depth exceeded'
|
|
195
|
+
|
|
196
|
+
|
|
197
|
+
def _raise_recursion():
|
|
198
|
+
"""sqlparse's FilterStack turns a RecursionError into this, `from` it: the same chain here."""
|
|
199
|
+
try:
|
|
200
|
+
raise RecursionError('maximum recursion depth exceeded')
|
|
201
|
+
except RecursionError as err:
|
|
202
|
+
raise _SQLParseError(_RECURSION) from err
|
|
203
|
+
|
|
204
|
+
|
|
205
|
+
def format(sql, encoding=None, **options):
|
|
206
|
+
"""sqlparse.format(): *sql* formatted according to *options* (see sqlparse's documentation)."""
|
|
207
|
+
ported = _ported_options(options) if _ported and type(sql) is str else None
|
|
208
|
+
if ported is not None and _text_ported(sql) and _sqlparse_untouched():
|
|
209
|
+
too_deep = False
|
|
210
|
+
try:
|
|
211
|
+
# Called from this frame: the Rust counts it in place of sqlparse.format's for the recursion limit.
|
|
212
|
+
return _native.format(sql, **ported)
|
|
213
|
+
except UnicodeEncodeError:
|
|
214
|
+
pass
|
|
215
|
+
except _SQLParseError as err:
|
|
216
|
+
if err.args != (_RECURSION,):
|
|
217
|
+
raise
|
|
218
|
+
too_deep = True
|
|
219
|
+
if too_deep:
|
|
220
|
+
_raise_recursion()
|
|
221
|
+
return _original_format(sql, encoding, **options)
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
def split(sql, encoding=None, strip_semicolon=False):
|
|
225
|
+
"""sqlparse.split(): the statements in *sql*, each stripped."""
|
|
226
|
+
if (
|
|
227
|
+
_ported
|
|
228
|
+
and type(sql) is str
|
|
229
|
+
and type(strip_semicolon) is bool
|
|
230
|
+
and _text_ported(sql)
|
|
231
|
+
and _sqlparse_untouched()
|
|
232
|
+
):
|
|
233
|
+
try:
|
|
234
|
+
return _native.split(sql, strip_semicolon)
|
|
235
|
+
except UnicodeEncodeError:
|
|
236
|
+
pass
|
|
237
|
+
return _original_split(sql, encoding, strip_semicolon)
|
|
238
|
+
|
|
239
|
+
|
|
240
|
+
def split_many(sqls, encoding=None, strip_semicolon=False):
|
|
241
|
+
"""[split(sql, encoding, strip_semicolon) for sql in sqls], the texts the Rust takes split over all CPU
|
|
242
|
+
threads at once (without Python's lock); the others go to sqlparse one by one."""
|
|
243
|
+
sqls = list(sqls)
|
|
244
|
+
out = [None] * len(sqls)
|
|
245
|
+
if _ported and type(strip_semicolon) is bool and _sqlparse_untouched():
|
|
246
|
+
fast = [i for i, sql in enumerate(sqls) if type(sql) is str and _text_ported(sql)]
|
|
247
|
+
if fast:
|
|
248
|
+
try:
|
|
249
|
+
for i, statements in zip(fast, _native.split_many([sqls[i] for i in fast], strip_semicolon)):
|
|
250
|
+
out[i] = statements
|
|
251
|
+
except UnicodeEncodeError:
|
|
252
|
+
pass
|
|
253
|
+
for i, statements in enumerate(out):
|
|
254
|
+
if statements is None:
|
|
255
|
+
out[i] = _original_split(sqls[i], encoding, strip_semicolon)
|
|
256
|
+
return out
|
|
257
|
+
|
|
258
|
+
|
|
259
|
+
def _token_type(name):
|
|
260
|
+
t = _tokens.Token
|
|
261
|
+
for part in name.split('.')[1:]:
|
|
262
|
+
t = getattr(t, part)
|
|
263
|
+
return t
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
_native.set_token_types({name: _token_type(name) for name in _native.token_type_names()})
|
|
267
|
+
_native.set_sql_objects(
|
|
268
|
+
{name: cls for name, cls in vars(_sql).items() if isinstance(cls, type) and issubclass(cls, _sql.Token)},
|
|
269
|
+
object.__new__,
|
|
270
|
+
)
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def parse(sql, encoding=None):
|
|
274
|
+
"""sqlparse.parse(): the statements in *sql*, grouped, as sqlparse's own objects (a tuple of
|
|
275
|
+
`sqlparse.sql.Statement`). Grouped in Rust and built node for node as sqlparse leaves them; in sqlparse
|
|
276
|
+
under the same conditions as format()."""
|
|
277
|
+
if _ported and type(sql) is str and _text_ported(sql) and _sqlparse_untouched():
|
|
278
|
+
too_deep = False
|
|
279
|
+
try:
|
|
280
|
+
# Called from this frame: the Rust counts it in place of sqlparse.parse's for the recursion limit.
|
|
281
|
+
return _native.parse(sql)
|
|
282
|
+
except UnicodeEncodeError:
|
|
283
|
+
pass
|
|
284
|
+
except _SQLParseError as err:
|
|
285
|
+
if err.args != (_RECURSION,):
|
|
286
|
+
raise
|
|
287
|
+
too_deep = True
|
|
288
|
+
if too_deep:
|
|
289
|
+
_raise_recursion()
|
|
290
|
+
return _original_parse(sql, encoding)
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
def _get_tokens(self, text, encoding=None):
|
|
294
|
+
"""sqlparse's Lexer.get_tokens, with install(): sqlparse's default lexer, as sqlparse sets it up, on a `str`
|
|
295
|
+
the Rust takes, tokenizes in Rust; anything else (another Lexer, a changed one, bytes, a stream) goes to
|
|
296
|
+
sqlparse's own. Same (token type, value) pairs, same token type objects."""
|
|
297
|
+
if (self is _lexer.Lexer._default_instance and type(text) is str and _text_ported(text)
|
|
298
|
+
and _sqlparse_untouched()):
|
|
299
|
+
try:
|
|
300
|
+
tokens = _native.lex(text)
|
|
301
|
+
except UnicodeEncodeError:
|
|
302
|
+
tokens = None
|
|
303
|
+
if tokens is not None:
|
|
304
|
+
yield from tokens
|
|
305
|
+
return
|
|
306
|
+
yield from _original_get_tokens(self, text, encoding)
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
# Everything of sqlparse the Rust reproduces: its modules' names and its classes' attributes, as imported. Where
|
|
310
|
+
# install() puts the package's own function, that one is allowed as well.
|
|
311
|
+
_WATCHED_MODULES = [
|
|
312
|
+
sqlparse, _sql, _tokens, sqlparse.keywords, _lexer, sqlparse.utils, sqlparse.formatter, sqlparse.engine,
|
|
313
|
+
sqlparse.engine.grouping, sqlparse.engine.filter_stack, sqlparse.engine.statement_splitter, sqlparse.filters,
|
|
314
|
+
sqlparse.filters.aligned_indent, sqlparse.filters.others, sqlparse.filters.output, sqlparse.filters.reindent,
|
|
315
|
+
sqlparse.filters.right_margin, sqlparse.filters.tokens,
|
|
316
|
+
]
|
|
317
|
+
_INSTALLED = {
|
|
318
|
+
(sqlparse, 'format'): format, (sqlparse, 'split'): split, (sqlparse, 'parse'): parse,
|
|
319
|
+
(_lexer.Lexer, 'get_tokens'): _get_tokens,
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def _watch():
|
|
324
|
+
import gc
|
|
325
|
+
|
|
326
|
+
owners = list(_WATCHED_MODULES)
|
|
327
|
+
for module in _WATCHED_MODULES:
|
|
328
|
+
for value in list(vars(module).values()):
|
|
329
|
+
if isinstance(value, type) and value.__module__.startswith('sqlparse') and value not in owners:
|
|
330
|
+
owners.append(value)
|
|
331
|
+
watch = []
|
|
332
|
+
for owner in owners:
|
|
333
|
+
namespace = vars(owner)
|
|
334
|
+
if type(namespace) is not dict: # a class's mappingproxy: the dict behind it
|
|
335
|
+
namespace = next(r for r in gc.get_referents(namespace) if type(r) is dict)
|
|
336
|
+
entries = []
|
|
337
|
+
for name, value in namespace.items():
|
|
338
|
+
if (owner, name) == (_lexer.Lexer, '_default_instance'): # set on first use
|
|
339
|
+
allowed = None
|
|
340
|
+
elif (owner, name) in _INSTALLED:
|
|
341
|
+
allowed = [value, _INSTALLED[owner, name]]
|
|
342
|
+
else:
|
|
343
|
+
allowed = [value]
|
|
344
|
+
entries.append((name, allowed, type(value) in (int, float, str, bytes, bool, tuple, frozenset)))
|
|
345
|
+
watch.append((namespace, isinstance(owner, type), entries))
|
|
346
|
+
_native.set_watch(watch)
|
|
347
|
+
return [namespace for namespace, _, _ in watch]
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
_watched_dicts = _watch()
|
|
351
|
+
|
|
352
|
+
# Comparing every watched name costs a few microseconds a call. CPython 3.12+ can tell us instead when one of
|
|
353
|
+
# these dicts changes (a dict watcher): the comparison then runs once after a change, not on every call. On
|
|
354
|
+
# older Pythons, or when no watcher can be had, it runs on every call.
|
|
355
|
+
_changed = [True] # a change not yet compared; True until the first comparison
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def _add_watchers(dicts):
|
|
359
|
+
import sys
|
|
360
|
+
|
|
361
|
+
if sys.version_info < (3, 12) or sys.implementation.name != 'cpython':
|
|
362
|
+
return False
|
|
363
|
+
try:
|
|
364
|
+
import atexit
|
|
365
|
+
import ctypes
|
|
366
|
+
|
|
367
|
+
api = ctypes.pythonapi
|
|
368
|
+
callback_type = ctypes.CFUNCTYPE(ctypes.c_int, ctypes.c_int, ctypes.c_void_p, ctypes.c_void_p,
|
|
369
|
+
ctypes.c_void_p)
|
|
370
|
+
changed = _changed
|
|
371
|
+
|
|
372
|
+
def on_change(event, d, key, value):
|
|
373
|
+
changed[0] = True
|
|
374
|
+
return 0
|
|
375
|
+
|
|
376
|
+
callback = callback_type(on_change)
|
|
377
|
+
api.PyDict_AddWatcher.argtypes = [callback_type]
|
|
378
|
+
api.PyDict_AddWatcher.restype = ctypes.c_int
|
|
379
|
+
api.PyDict_Watch.argtypes = [ctypes.c_int, ctypes.py_object]
|
|
380
|
+
api.PyDict_Watch.restype = ctypes.c_int
|
|
381
|
+
api.PyDict_ClearWatcher.argtypes = [ctypes.c_int]
|
|
382
|
+
api.PyDict_ClearWatcher.restype = ctypes.c_int
|
|
383
|
+
watcher = api.PyDict_AddWatcher(callback)
|
|
384
|
+
if watcher < 0: # all of the interpreter's watcher slots are taken
|
|
385
|
+
return False
|
|
386
|
+
for d in dicts:
|
|
387
|
+
if api.PyDict_Watch(watcher, d) < 0:
|
|
388
|
+
api.PyDict_ClearWatcher(watcher)
|
|
389
|
+
return False
|
|
390
|
+
except (AttributeError, OSError, ValueError, TypeError, ctypes.ArgumentError):
|
|
391
|
+
return False
|
|
392
|
+
|
|
393
|
+
def stop():
|
|
394
|
+
# before the interpreter tears modules down, which would call back into Python mid-shutdown
|
|
395
|
+
api.PyDict_ClearWatcher(watcher)
|
|
396
|
+
_changed[0] = True
|
|
397
|
+
|
|
398
|
+
atexit.register(stop)
|
|
399
|
+
_add_watchers.callback = callback # the C function pointer lives as long as this object
|
|
400
|
+
return True
|
|
401
|
+
|
|
402
|
+
|
|
403
|
+
_watching = _add_watchers(_watched_dicts)
|
|
404
|
+
|
|
405
|
+
|
|
406
|
+
def _sqlparse_untouched():
|
|
407
|
+
"""sqlparse as the Rust reproduces it: nothing replaced, and its default lexer as sqlparse sets it up."""
|
|
408
|
+
if not _watching:
|
|
409
|
+
return _native.untouched() and _lexer_ported()
|
|
410
|
+
if _changed[0]:
|
|
411
|
+
_changed[0] = False # set again by a change that lands from here on
|
|
412
|
+
if not _native.untouched():
|
|
413
|
+
_changed[0] = True # still different: compared again next call, in case it is put back
|
|
414
|
+
return False
|
|
415
|
+
return _lexer_ported()
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
def install():
|
|
419
|
+
"""Make sqlparse.format and sqlparse.split these, and give sqlparse's default lexer the Rust tokenizer. Does
|
|
420
|
+
nothing (returns False) when the installed sqlparse is not the version that was ported."""
|
|
421
|
+
if not _ported:
|
|
422
|
+
return False
|
|
423
|
+
sqlparse.format = format
|
|
424
|
+
sqlparse.split = split
|
|
425
|
+
sqlparse.parse = parse
|
|
426
|
+
_lexer.Lexer.get_tokens = _get_tokens
|
|
427
|
+
return True
|
|
428
|
+
|
|
429
|
+
|
|
430
|
+
def uninstall():
|
|
431
|
+
"""Put sqlparse's own format, split and lexer back."""
|
|
432
|
+
sqlparse.format = _original_format
|
|
433
|
+
sqlparse.split = _original_split
|
|
434
|
+
sqlparse.parse = _original_parse
|
|
435
|
+
_lexer.Lexer.get_tokens = _original_get_tokens
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"""`sqlformat-turbo` / `python -m sqlparse_turbo`: sqlparse's own `sqlformat` command, with format() in Rust.
|
|
2
|
+
|
|
3
|
+
Same arguments, output and exit codes as `sqlformat` (sqlparse.cli), which looks sqlparse.format up on the module
|
|
4
|
+
when it runs, so install() is all it takes.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
import sys
|
|
8
|
+
|
|
9
|
+
from sqlparse import cli
|
|
10
|
+
|
|
11
|
+
from . import install
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def main(args=None):
|
|
15
|
+
install()
|
|
16
|
+
return cli.main(args)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
if __name__ == '__main__':
|
|
20
|
+
sys.exit(main())
|
|
Binary file
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"""Generated by tools/unicode_drift.py: do not edit.
|
|
2
|
+
|
|
3
|
+
The Rust's Unicode behaviour is CPython 3.11.9's (Unicode 14.0.0). For each other Unicode
|
|
4
|
+
version a CPython ships, the code points that behave differently there (assigned since, or with other
|
|
5
|
+
properties): text holding one of them goes to sqlparse.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
PORTED_UNICODE = '14.0.0'
|
|
9
|
+
|
|
10
|
+
DRIFT = {
|
|
11
|
+
# CPython 3.10.21: 839 code points in 79 ranges
|
|
12
|
+
'13.0.0': (
|
|
13
|
+
(0x61D, 0x61D), (0x870, 0x88E), (0x890, 0x891), (0x898, 0x89F), (0x8B5, 0x8B5), (0x8C8, 0x8D2),
|
|
14
|
+
(0xC3C, 0xC3C), (0xC5D, 0xC5D), (0xCDD, 0xCDD), (0x170D, 0x170D), (0x1715, 0x1715), (0x171F, 0x171F),
|
|
15
|
+
(0x1734, 0x1734), (0x180F, 0x180F), (0x1AC1, 0x1ACE), (0x1B4C, 0x1B4C), (0x1B7D, 0x1B7E), (0x1DFA, 0x1DFA),
|
|
16
|
+
(0x20C0, 0x20C0), (0x2C2F, 0x2C2F), (0x2C5F, 0x2C5F), (0x2E53, 0x2E5D), (0x9FFD, 0x9FFF), (0xA7C0, 0xA7C1),
|
|
17
|
+
(0xA7D0, 0xA7D1), (0xA7D3, 0xA7D3), (0xA7D5, 0xA7D9), (0xA7F2, 0xA7F4), (0xFBC2, 0xFBC2), (0xFD40, 0xFD4F),
|
|
18
|
+
(0xFDCF, 0xFDCF), (0xFDFE, 0xFDFF), (0x10570, 0x1057A), (0x1057C, 0x1058A), (0x1058C, 0x10592), (0x10594, 0x10595),
|
|
19
|
+
(0x10597, 0x105A1), (0x105A3, 0x105B1), (0x105B3, 0x105B9), (0x105BB, 0x105BC), (0x10780, 0x10785), (0x10787, 0x107B0),
|
|
20
|
+
(0x107B2, 0x107BA), (0x10F70, 0x10F89), (0x11070, 0x11075), (0x110C2, 0x110C2), (0x116B9, 0x116B9), (0x11740, 0x11746),
|
|
21
|
+
(0x11AB0, 0x11ABF), (0x12F90, 0x12FF2), (0x16A70, 0x16ABE), (0x16AC0, 0x16AC9), (0x1AFF0, 0x1AFF3), (0x1AFF5, 0x1AFFB),
|
|
22
|
+
(0x1AFFD, 0x1AFFE), (0x1B11F, 0x1B122), (0x1CF00, 0x1CF2D), (0x1CF30, 0x1CF46), (0x1CF50, 0x1CFC3), (0x1D1E9, 0x1D1EA),
|
|
23
|
+
(0x1DF00, 0x1DF1E), (0x1E290, 0x1E2AE), (0x1E7E0, 0x1E7E6), (0x1E7E8, 0x1E7EB), (0x1E7ED, 0x1E7EE), (0x1E7F0, 0x1E7FE),
|
|
24
|
+
(0x1F6DD, 0x1F6DF), (0x1F7F0, 0x1F7F0), (0x1F979, 0x1F979), (0x1F9CC, 0x1F9CC), (0x1FA7B, 0x1FA7C), (0x1FAA9, 0x1FAAC),
|
|
25
|
+
(0x1FAB7, 0x1FABA), (0x1FAC3, 0x1FAC5), (0x1FAD7, 0x1FAD9), (0x1FAE0, 0x1FAE7), (0x1FAF0, 0x1FAF6), (0x2A6DE, 0x2A6DF),
|
|
26
|
+
(0x2B735, 0x2B738),
|
|
27
|
+
),
|
|
28
|
+
# CPython 3.12.14: 4489 code points in 32 ranges
|
|
29
|
+
'15.0.0': (
|
|
30
|
+
(0xCF3, 0xCF3), (0xECE, 0xECE), (0x10EFD, 0x10EFF), (0x1123F, 0x11241), (0x11B00, 0x11B09), (0x11F00, 0x11F10),
|
|
31
|
+
(0x11F12, 0x11F3A), (0x11F3E, 0x11F59), (0x1342F, 0x1342F), (0x13439, 0x13455), (0x1B132, 0x1B132), (0x1B155, 0x1B155),
|
|
32
|
+
(0x1D2C0, 0x1D2D3), (0x1DF25, 0x1DF2A), (0x1E030, 0x1E06D), (0x1E08F, 0x1E08F), (0x1E4D0, 0x1E4F9), (0x1F6DC, 0x1F6DC),
|
|
33
|
+
(0x1F774, 0x1F776), (0x1F77B, 0x1F77F), (0x1F7D9, 0x1F7D9), (0x1FA75, 0x1FA77), (0x1FA87, 0x1FA88), (0x1FAAD, 0x1FAAF),
|
|
34
|
+
(0x1FABB, 0x1FABD), (0x1FABF, 0x1FABF), (0x1FACE, 0x1FACF), (0x1FADA, 0x1FADB), (0x1FAE8, 0x1FAE8), (0x1FAF7, 0x1FAF8),
|
|
35
|
+
(0x2B739, 0x2B739), (0x31350, 0x323AF),
|
|
36
|
+
),
|
|
37
|
+
# CPython 3.13.15: 5116 code points in 35 ranges
|
|
38
|
+
'15.1.0': (
|
|
39
|
+
(0xCF3, 0xCF3), (0xECE, 0xECE), (0x2FFC, 0x2FFF), (0x31EF, 0x31EF), (0x10EFD, 0x10EFF), (0x1123F, 0x11241),
|
|
40
|
+
(0x11B00, 0x11B09), (0x11F00, 0x11F10), (0x11F12, 0x11F3A), (0x11F3E, 0x11F59), (0x1342F, 0x1342F), (0x13439, 0x13455),
|
|
41
|
+
(0x1B132, 0x1B132), (0x1B155, 0x1B155), (0x1D2C0, 0x1D2D3), (0x1DF25, 0x1DF2A), (0x1E030, 0x1E06D), (0x1E08F, 0x1E08F),
|
|
42
|
+
(0x1E4D0, 0x1E4F9), (0x1F6DC, 0x1F6DC), (0x1F774, 0x1F776), (0x1F77B, 0x1F77F), (0x1F7D9, 0x1F7D9), (0x1FA75, 0x1FA77),
|
|
43
|
+
(0x1FA87, 0x1FA88), (0x1FAAD, 0x1FAAF), (0x1FABB, 0x1FABD), (0x1FABF, 0x1FABF), (0x1FACE, 0x1FACF), (0x1FADA, 0x1FADB),
|
|
44
|
+
(0x1FAE8, 0x1FAE8), (0x1FAF7, 0x1FAF8), (0x2B739, 0x2B739), (0x2EBF0, 0x2EE5D), (0x31350, 0x323AF),
|
|
45
|
+
),
|
|
46
|
+
# CPython 3.14.7: 10304 code points in 78 ranges
|
|
47
|
+
'16.0.0': (
|
|
48
|
+
(0x19B, 0x19B), (0x264, 0x264), (0x897, 0x897), (0xCF3, 0xCF3), (0xECE, 0xECE), (0x1B4E, 0x1B4F),
|
|
49
|
+
(0x1B7F, 0x1B7F), (0x1C89, 0x1C8A), (0x2427, 0x2429), (0x2FFC, 0x2FFF), (0x31E4, 0x31E5), (0x31EF, 0x31EF),
|
|
50
|
+
(0xA7CB, 0xA7CD), (0xA7DA, 0xA7DC), (0x105C0, 0x105F3), (0x10D40, 0x10D65), (0x10D69, 0x10D85), (0x10D8E, 0x10D8F),
|
|
51
|
+
(0x10EC2, 0x10EC4), (0x10EFC, 0x10EFF), (0x1123F, 0x11241), (0x11380, 0x11389), (0x1138B, 0x1138B), (0x1138E, 0x1138E),
|
|
52
|
+
(0x11390, 0x113B5), (0x113B7, 0x113C0), (0x113C2, 0x113C2), (0x113C5, 0x113C5), (0x113C7, 0x113CA), (0x113CC, 0x113D5),
|
|
53
|
+
(0x113D7, 0x113D8), (0x113E1, 0x113E2), (0x116D0, 0x116E3), (0x1171E, 0x1171E), (0x11B00, 0x11B09), (0x11BC0, 0x11BE1),
|
|
54
|
+
(0x11BF0, 0x11BF9), (0x11F00, 0x11F10), (0x11F12, 0x11F3A), (0x11F3E, 0x11F5A), (0x1342F, 0x1342F), (0x13439, 0x13455),
|
|
55
|
+
(0x13460, 0x143FA), (0x16100, 0x16139), (0x16D40, 0x16D79), (0x18CFF, 0x18CFF), (0x1B132, 0x1B132), (0x1B155, 0x1B155),
|
|
56
|
+
(0x1CC00, 0x1CCF9), (0x1CD00, 0x1CEB3), (0x1D2C0, 0x1D2D3), (0x1DF25, 0x1DF2A), (0x1E030, 0x1E06D), (0x1E08F, 0x1E08F),
|
|
57
|
+
(0x1E4D0, 0x1E4F9), (0x1E5D0, 0x1E5FA), (0x1E5FF, 0x1E5FF), (0x1F6DC, 0x1F6DC), (0x1F774, 0x1F776), (0x1F77B, 0x1F77F),
|
|
58
|
+
(0x1F7D9, 0x1F7D9), (0x1F8B2, 0x1F8BB), (0x1F8C0, 0x1F8C1), (0x1FA75, 0x1FA77), (0x1FA87, 0x1FA89), (0x1FA8F, 0x1FA8F),
|
|
59
|
+
(0x1FAAD, 0x1FAAF), (0x1FABB, 0x1FABF), (0x1FAC6, 0x1FAC6), (0x1FACE, 0x1FACF), (0x1FADA, 0x1FADC), (0x1FADF, 0x1FADF),
|
|
60
|
+
(0x1FAE8, 0x1FAE9), (0x1FAF7, 0x1FAF8), (0x1FBCB, 0x1FBEF), (0x2B739, 0x2B739), (0x2EBF0, 0x2EE5D), (0x31350, 0x323AF),
|
|
61
|
+
),
|
|
62
|
+
}
|