[PATCH 10 of 10 logilab-database] [py3] Assume convert_timedelta() receives bytes

Olivier Giorgis olivier.giorgis at logilab.fr
Mon Jun 4 12:20:43 CEST 2018


On 04/06/2018 12:00, Denis Laxalde wrote:
> # HG changeset patch
> # User Denis Laxalde <denis.laxalde at logilab.fr>
> # Date 1528105414 -7200
> #      Mon Jun 04 11:43:34 2018 +0200
> # Node ID 3963f5b61b90c1e0c714dcbd25a7bb9a04eb9505
> # Parent  8de0ea17b6695dff9e5cb9d340a26ab208a33a3c
> # Available At https://hg.logilab.org/review/logilab/database
> #              hg pull https://hg.logilab.org/review/logilab/database -r 3963f5b61b90
> # EXP-Topic py3
> [py3] Assume convert_timedelta() receives bytes
>
> In a downstream project (cubicweb-rememberme), we get into this
> convert_timedelta() function with a bytes value on Python 3. So all
> string operations (.split()) will fail if their argument is not bytes as
> well. So use bytes literal in every place needed in this function.
>
> diff --git a/logilab/database/sqlite.py b/logilab/database/sqlite.py
> --- a/logilab/database/sqlite.py
> +++ b/logilab/database/sqlite.py
> @@ -132,15 +132,15 @@ class _Sqlite3Adapter(db.DBAPIAdapter):
>                   return "%d %s" % (data.days, frac)
>               sqlite.register_adapter(timedelta, adapt_timedelta)
>               def convert_timedelta(data):
> -                parts = data.split(" ")
> +                parts = data.split(b" ")
>                   if len(parts) == 2:
>                       daypart, timepart = parts
>                       days = int(daypart)
>                   else:
>                       days = 0
>                       timepart = parts[-1]
> -                timepart_full = timepart.split(".")
> -                hours, minutes, seconds = map(int, timepart_full[0].split(":"))
> +                timepart_full = timepart.split(b".")
> +                hours, minutes, seconds = map(int, timepart_full[0].split(b":"))
>                   if len(timepart_full) == 2:
>                       microseconds = int(float("0." + timepart_full[1]) * 1000000)
>                   else:

series LGTM

-- 
Olivier GIORGIS
Logilab         http://www.logilab.fr


More information about the cubicweb-devel mailing list