Skip to content

Commit 941ab93

Browse files
committed
reconcile orjson and msgspec usage across python versions
1 parent 71a30a1 commit 941ab93

File tree

3 files changed

+28
-21
lines changed

3 files changed

+28
-21
lines changed

biothings/utils/serializer.py

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
import datetime
22
import sys
3+
# import sysconfig
34
from collections import OrderedDict, UserDict, UserList, UserString
45
from typing import Any, Union
56
from urllib.parse import parse_qs, unquote_plus, urlencode, urlparse, urlunparse
67

78
import yaml
89

10+
def _is_free_threaded_build() -> bool:
11+
"""
12+
Pseudo method for now until we drop support for 3.9 or lower
13+
"""
14+
if sys.version_info < (3, 14):
15+
return False
16+
17+
return True
18+
19+
# py_gil_disabled = sysconfig.get_config_var("Py_GIL_DISABLED")
20+
# if py_gil_disabled is not None:
21+
# return bool(py_gil_disabled)
22+
#
23+
# # Fallback for environments where Py_GIL_DISABLED isn't exposed.
24+
# if getattr(sys, "abiflags", "").endswith("t"):
25+
# return True
26+
#
27+
# cache_tag = getattr(getattr(sys, "implementation", None), "cache_tag", "")
28+
# return bool(cache_tag and cache_tag.endswith("t"))
29+
30+
931
# Determine if we should use msgspec (free-threaded build)
1032
# or orjson (standard build)
11-
_USE_MSGSPEC = (
12-
sys.version_info >= (3, 14)
13-
and hasattr(sys, "_is_gil_enabled")
14-
and not sys._is_gil_enabled()
15-
)
33+
_USE_MSGSPEC = _is_free_threaded_build()
1634

1735
# Only import orjson if not using msgspec (orjson re-enables GIL)
1836
if not _USE_MSGSPEC:

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ dependencies = [
7070
"jmespath>=0.7.1,<2.0.0", # support jmespath query parameter
7171
"PyYAML>=5.1",
7272
'orjson>=3.10.16; python_version < "3.14.0"', # a faster json lib supports inf/nan and datetime, v3.10.16 is the first version requires Python 3.9+
73-
'orjson==3.11.4; python_version >= "3.14.0"', # orjson 3.11.5 cannot be built on Python 3.14t for now
73+
# 'orjson==3.11.4; python_version >= "3.14.0"', # orjson 3.11.5 cannot be built on Python 3.14t for now
7474
'msgspec>=0.20; python_version >= "3.14.0"', # msgspec as backup json lib for Python 3.14+ when free-threaded
7575
'zstandard>=0.21.0; python_version<"3.14"', # we need zst library before 3.14
7676
]

tests/web/test_serializer.py

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,12 @@
1+
import sys
2+
13
from biothings.web.handlers import serializer
24

35

46
def test_unified_api_dispatch():
57
"""Test that unified API uses correct implementation based on Python version and free-threading"""
6-
import sys
7-
8-
# Check which implementation should be active
9-
python_version = sys.version_info
10-
# Use sys.flags.nogil for Python 3.13+ to detect free-threading
11-
is_free_threaded = (
12-
python_version >= (3, 14) and
13-
hasattr(sys.flags, 'nogil') and
14-
sys.flags.nogil
15-
)
16-
use_msgspec = (
17-
python_version >= (3, 14) and
18-
is_free_threaded
19-
)
8+
9+
use_msgspec = sys.version_info >= (3, 14)
2010

2111
# Verify unified API functions exist
2212
assert hasattr(serializer, 'load_json')
@@ -117,4 +107,3 @@ def test_yaml_01():
117107
def test_url_01():
118108
url = serializer.URL("http://www.mygene.info/v1/gene/1017?fields=symbol&format=html")
119109
assert url.remove() == "http://www.mygene.info/v1/gene/1017?fields=symbol"
120-

0 commit comments

Comments
 (0)