1# engine/processors.py
2# Copyright (C) 2010-2026 the SQLAlchemy authors and contributors
3# <see AUTHORS file>
4# Copyright (C) 2010 Gaetan de Menten gdementen@gmail.com
5#
6# This module is part of SQLAlchemy and is released under
7# the MIT License: https://www.opensource.org/licenses/mit-license.php
8
9"""defines generic type conversion functions, as used in bind and result
10processors.
11
12They all share one common characteristic: None is passed through unchanged.
13
14"""
15
16from __future__ import annotations
17
18import typing
19
20from ._py_processors import str_to_datetime_processor_factory # noqa
21from ..util._has_cy import HAS_CYEXTENSION
22
23if typing.TYPE_CHECKING or not HAS_CYEXTENSION:
24 from ._py_processors import int_to_boolean as int_to_boolean
25 from ._py_processors import str_to_date as str_to_date
26 from ._py_processors import str_to_datetime as str_to_datetime
27 from ._py_processors import str_to_time as str_to_time
28 from ._py_processors import (
29 to_decimal_processor_factory as to_decimal_processor_factory,
30 )
31 from ._py_processors import to_float as to_float
32 from ._py_processors import to_str as to_str
33else:
34 from sqlalchemy.cyextension.processors import (
35 DecimalResultProcessor,
36 )
37 from sqlalchemy.cyextension.processors import ( # noqa: F401
38 int_to_boolean as int_to_boolean,
39 )
40 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
41 str_to_date as str_to_date,
42 )
43 from sqlalchemy.cyextension.processors import ( # noqa: F401
44 str_to_datetime as str_to_datetime,
45 )
46 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
47 str_to_time as str_to_time,
48 )
49 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
50 to_float as to_float,
51 )
52 from sqlalchemy.cyextension.processors import ( # noqa: F401,E501
53 to_str as to_str,
54 )
55
56 def to_decimal_processor_factory(target_class, scale):
57 # Note that the scale argument is not taken into account for integer
58 # values in the C implementation while it is in the Python one.
59 # For example, the Python implementation might return
60 # Decimal('5.00000') whereas the C implementation will
61 # return Decimal('5'). These are equivalent of course.
62 return DecimalResultProcessor(target_class, "%%.%df" % scale).process