Skip to content

Commit 65e4b1c

Browse files
authored
Add a default reader conditional for the current platform (#744)
Fixes #692
1 parent 0cd8ab7 commit 65e4b1c

File tree

5 files changed

+22
-6
lines changed

5 files changed

+22
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
* Added rudimentary support for `clojure.stacktrace` with `print-cause-trace` (part of #721)
1010
* Added support for `bytes` literals using a `#b` prefix (#732)
1111
* Added support for Python 3.12 (#734)
12+
* Added a default reader conditional for the current platform (`windows`, `darwin`, `linux`, etc.) (#692)
1213

1314
### Changed
1415
* Basilisp now supports PyTest 7.0+ (#660)

docs/reader.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,3 +486,11 @@ Basilisp takes advantage of this in :lpy:ns:`basilisp.io`.
486486
(throw e))))
487487
:lpy38+ (.unlink (as-path f) ** :missing-ok (if silently true false)))
488488
silently))
489+
490+
Platform Reader Features
491+
^^^^^^^^^^^^^^^^^^^^^^^^
492+
493+
Basilisp includes a specialized reader feature based on the current platform (Linux, MacOS, Windows, etc.).
494+
There exist cases where it may be required to use different APIs based on which platform is currently in use, so having a reader conditional to detect the current platform can simplify the development process across multiple platforms.
495+
The reader conditional name is always a keyword containing the lowercase version of the platform name as reported by ``platform.system()``.
496+
For example, if ``platform.system()`` returns the Python string ``"Windows"``, the platform specific reader conditional would be ``:windows``.

src/basilisp/io.lpy

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,10 @@
3333
(as-path [f]
3434
(if (contains? #{"file" ""} (.-scheme f))
3535
(let [path (.-path f)]
36-
(if (and (= sys/platform "win32") (os.path/isabs path))
37-
;; On MS-Windows, extracting an absolute path from the URL
38-
;; incorrectly adds a leading `/', .e.g. /C:\xyz.
39-
(pathlib/Path (subs path 1))
40-
41-
(pathlib/Path path)))
36+
;; On MS-Windows, extracting an absolute path from the URL
37+
;; incorrectly adds a leading `/', .e.g. /C:\xyz.
38+
(pathlib/Path #?(:windows (if (os.path/isabs path) (subs path 1) path)
39+
:default path)))
4240
(throw
4341
(ex-info "Cannot coerce non-File URL to pathlib.Path"
4442
{:file f})))))

src/basilisp/lang/runtime.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import logging
88
import math
99
import numbers
10+
import platform
1011
import re
1112
import sys
1213
import threading
@@ -175,9 +176,11 @@ def _supported_python_versions_features() -> Iterable[kw.Keyword]:
175176

176177
READER_COND_BASILISP_FEATURE_KW = kw.keyword("lpy")
177178
READER_COND_DEFAULT_FEATURE_KW = kw.keyword("default")
179+
READER_COND_PLATFORM = kw.keyword(platform.system().lower())
178180
READER_COND_DEFAULT_FEATURE_SET = lset.s(
179181
READER_COND_BASILISP_FEATURE_KW,
180182
READER_COND_DEFAULT_FEATURE_KW,
183+
READER_COND_PLATFORM,
181184
*_supported_python_versions_features(),
182185
)
183186

tests/basilisp/runtime_test.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import platform
12
import sys
23
from decimal import Decimal
34
from fractions import Fraction
@@ -40,6 +41,7 @@ def test_is_supported_python_version():
4041
"lpy310-",
4142
"lpy311-",
4243
"lpy312-",
44+
platform.system().lower(),
4345
],
4446
)
4547
),
@@ -56,6 +58,7 @@ def test_is_supported_python_version():
5658
"lpy310-",
5759
"lpy311-",
5860
"lpy312-",
61+
platform.system().lower(),
5962
],
6063
)
6164
),
@@ -72,6 +75,7 @@ def test_is_supported_python_version():
7275
"lpy310-",
7376
"lpy39+",
7477
"lpy38+",
78+
platform.system().lower(),
7579
],
7680
)
7781
),
@@ -88,6 +92,7 @@ def test_is_supported_python_version():
8892
"lpy310+",
8993
"lpy39+",
9094
"lpy38+",
95+
platform.system().lower(),
9196
],
9297
)
9398
),
@@ -104,6 +109,7 @@ def test_is_supported_python_version():
104109
"lpy311+",
105110
"lpy39+",
106111
"lpy38+",
112+
platform.system().lower(),
107113
],
108114
)
109115
),

0 commit comments

Comments
 (0)