Skip to content

Commit 629273a

Browse files
committed
Minor optimization to the code
* remove the c version of distill params since it's actually slower than the python one * add a function to langhelpers to check if the cextensions are active * minor cleanup to the OrderedSet implementation Change-Id: Iec3d0c3f0f42cdf51f802aaca342ba37b8783b85
1 parent 227fd31 commit 629273a

File tree

10 files changed

+83
-349
lines changed

10 files changed

+83
-349
lines changed

.github/workflows/create-wheels.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ jobs:
8787
# for python 2.7 visual studio 9 is missing
8888
if: matrix.os != 'windows-latest' || matrix.python-version != '2.7'
8989
run: |
90-
python -c 'from sqlalchemy import cprocessors, cresultproxy, cutils'
90+
python -c 'from sqlalchemy.util import has_compiled_ext; assert has_compiled_ext()'
9191
9292
- name: Test created wheel
9393
# the mock reconnect test seems to fail on the ci in windows
@@ -222,7 +222,7 @@ jobs:
222222
then
223223
pip install greenlet "importlib-metadata;python_version<'3.8'"
224224
pip install -f dist --no-index sqlalchemy
225-
python -c 'from sqlalchemy import cprocessors, cresultproxy, cutils'
225+
python -c 'from sqlalchemy.util import has_compiled_ext; assert has_compiled_ext()'
226226
pip install pytest pytest-xdist ${{ matrix.extra-requires }}
227227
pytest -n2 -q test --nomemory --notimingintensive
228228
else
@@ -362,7 +362,7 @@ jobs:
362362
python --version &&
363363
pip install greenlet \"importlib-metadata;python_version<'3.8'\" &&
364364
pip install -f dist --no-index sqlalchemy &&
365-
python -c 'from sqlalchemy import cprocessors, cresultproxy, cutils' &&
365+
python -c 'from sqlalchemy.util import has_compiled_ext; assert has_compiled_ext()' &&
366366
pip install pytest pytest-xdist ${{ matrix.extra-requires }} &&
367367
pytest -n2 -q test --nomemory --notimingintensive"
368368

lib/sqlalchemy/cextension/utils.c

-249
This file was deleted.

lib/sqlalchemy/engine/util.py

+45-59
Original file line numberDiff line numberDiff line change
@@ -34,63 +34,55 @@ def decorated(fn, self, connection):
3434
_no_kw = util.immutabledict()
3535

3636

37-
def py_fallback():
38-
# TODO: pass the Connection in so that there can be a standard
39-
# method for warning on parameter format
40-
def _distill_params(connection, multiparams, params): # noqa
41-
r"""Given arguments from the calling form \*multiparams, \**params,
42-
return a list of bind parameter structures, usually a list of
43-
dictionaries.
44-
45-
In the case of 'raw' execution which accepts positional parameters,
46-
it may be a list of tuples or lists.
47-
48-
"""
49-
50-
# C version will fail if this assertion is not true.
51-
# assert isinstance(multiparams, tuple)
52-
53-
if not multiparams:
54-
if params:
55-
connection._warn_for_legacy_exec_format()
56-
return [params]
37+
def _distill_params(connection, multiparams, params):
38+
r"""Given arguments from the calling form \*multiparams, \**params,
39+
return a list of bind parameter structures, usually a list of
40+
dictionaries.
41+
42+
In the case of 'raw' execution which accepts positional parameters,
43+
it may be a list of tuples or lists.
44+
45+
"""
46+
47+
if not multiparams:
48+
if params:
49+
connection._warn_for_legacy_exec_format()
50+
return [params]
51+
else:
52+
return []
53+
elif len(multiparams) == 1:
54+
zero = multiparams[0]
55+
if isinstance(zero, (list, tuple)):
56+
if (
57+
not zero
58+
or hasattr(zero[0], "__iter__")
59+
and not hasattr(zero[0], "strip")
60+
):
61+
# execute(stmt, [{}, {}, {}, ...])
62+
# execute(stmt, [(), (), (), ...])
63+
return zero
5764
else:
58-
return []
59-
elif len(multiparams) == 1:
60-
zero = multiparams[0]
61-
if isinstance(zero, (list, tuple)):
62-
if (
63-
not zero
64-
or hasattr(zero[0], "__iter__")
65-
and not hasattr(zero[0], "strip")
66-
):
67-
# execute(stmt, [{}, {}, {}, ...])
68-
# execute(stmt, [(), (), (), ...])
69-
return zero
70-
else:
71-
# this is used by exec_driver_sql only, so a deprecation
72-
# warning would already be coming from passing a plain
73-
# textual statement with positional parameters to
74-
# execute().
75-
# execute(stmt, ("value", "value"))
76-
return [zero]
77-
elif hasattr(zero, "keys"):
78-
# execute(stmt, {"key":"value"})
65+
# this is used by exec_driver_sql only, so a deprecation
66+
# warning would already be coming from passing a plain
67+
# textual statement with positional parameters to
68+
# execute().
69+
# execute(stmt, ("value", "value"))
7970
return [zero]
80-
else:
81-
connection._warn_for_legacy_exec_format()
82-
# execute(stmt, "value")
83-
return [[zero]]
71+
elif hasattr(zero, "keys"):
72+
# execute(stmt, {"key":"value"})
73+
return [zero]
8474
else:
8575
connection._warn_for_legacy_exec_format()
86-
if hasattr(multiparams[0], "__iter__") and not hasattr(
87-
multiparams[0], "strip"
88-
):
89-
return multiparams
90-
else:
91-
return [multiparams]
92-
93-
return locals()
76+
# execute(stmt, "value")
77+
return [[zero]]
78+
else:
79+
connection._warn_for_legacy_exec_format()
80+
if hasattr(multiparams[0], "__iter__") and not hasattr(
81+
multiparams[0], "strip"
82+
):
83+
return multiparams
84+
else:
85+
return [multiparams]
9486

9587

9688
def _distill_cursor_params(connection, multiparams, params):
@@ -161,9 +153,3 @@ def _distill_params_20(params):
161153
return (params,), _no_kw
162154
else:
163155
raise exc.ArgumentError("mapping or sequence expected for parameters")
164-
165-
166-
try:
167-
from sqlalchemy.cutils import _distill_params # noqa
168-
except ImportError:
169-
globals().update(py_fallback())

0 commit comments

Comments
 (0)