Skip to content

Commit

Permalink
Add a default reader conditional for the current platform (#744)
Browse files Browse the repository at this point in the history
Fixes #692
  • Loading branch information
chrisrink10 authored Dec 28, 2023
1 parent 0cd8ab7 commit 65e4b1c
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Added rudimentary support for `clojure.stacktrace` with `print-cause-trace` (part of #721)
* Added support for `bytes` literals using a `#b` prefix (#732)
* Added support for Python 3.12 (#734)
* Added a default reader conditional for the current platform (`windows`, `darwin`, `linux`, etc.) (#692)

### Changed
* Basilisp now supports PyTest 7.0+ (#660)
Expand Down
8 changes: 8 additions & 0 deletions docs/reader.rst
Original file line number Diff line number Diff line change
Expand Up @@ -486,3 +486,11 @@ Basilisp takes advantage of this in :lpy:ns:`basilisp.io`.
(throw e))))
:lpy38+ (.unlink (as-path f) ** :missing-ok (if silently true false)))
silently))
Platform Reader Features
^^^^^^^^^^^^^^^^^^^^^^^^

Basilisp includes a specialized reader feature based on the current platform (Linux, MacOS, Windows, etc.).
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.
The reader conditional name is always a keyword containing the lowercase version of the platform name as reported by ``platform.system()``.
For example, if ``platform.system()`` returns the Python string ``"Windows"``, the platform specific reader conditional would be ``:windows``.
10 changes: 4 additions & 6 deletions src/basilisp/io.lpy
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,10 @@
(as-path [f]
(if (contains? #{"file" ""} (.-scheme f))
(let [path (.-path f)]
(if (and (= sys/platform "win32") (os.path/isabs path))
;; On MS-Windows, extracting an absolute path from the URL
;; incorrectly adds a leading `/', .e.g. /C:\xyz.
(pathlib/Path (subs path 1))

(pathlib/Path path)))
;; On MS-Windows, extracting an absolute path from the URL
;; incorrectly adds a leading `/', .e.g. /C:\xyz.
(pathlib/Path #?(:windows (if (os.path/isabs path) (subs path 1) path)
:default path)))
(throw
(ex-info "Cannot coerce non-File URL to pathlib.Path"
{:file f})))))
Expand Down
3 changes: 3 additions & 0 deletions src/basilisp/lang/runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import logging
import math
import numbers
import platform
import re
import sys
import threading
Expand Down Expand Up @@ -175,9 +176,11 @@ def _supported_python_versions_features() -> Iterable[kw.Keyword]:

READER_COND_BASILISP_FEATURE_KW = kw.keyword("lpy")
READER_COND_DEFAULT_FEATURE_KW = kw.keyword("default")
READER_COND_PLATFORM = kw.keyword(platform.system().lower())
READER_COND_DEFAULT_FEATURE_SET = lset.s(
READER_COND_BASILISP_FEATURE_KW,
READER_COND_DEFAULT_FEATURE_KW,
READER_COND_PLATFORM,
*_supported_python_versions_features(),
)

Expand Down
6 changes: 6 additions & 0 deletions tests/basilisp/runtime_test.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import platform
import sys
from decimal import Decimal
from fractions import Fraction
Expand Down Expand Up @@ -40,6 +41,7 @@ def test_is_supported_python_version():
"lpy310-",
"lpy311-",
"lpy312-",
platform.system().lower(),
],
)
),
Expand All @@ -56,6 +58,7 @@ def test_is_supported_python_version():
"lpy310-",
"lpy311-",
"lpy312-",
platform.system().lower(),
],
)
),
Expand All @@ -72,6 +75,7 @@ def test_is_supported_python_version():
"lpy310-",
"lpy39+",
"lpy38+",
platform.system().lower(),
],
)
),
Expand All @@ -88,6 +92,7 @@ def test_is_supported_python_version():
"lpy310+",
"lpy39+",
"lpy38+",
platform.system().lower(),
],
)
),
Expand All @@ -104,6 +109,7 @@ def test_is_supported_python_version():
"lpy311+",
"lpy39+",
"lpy38+",
platform.system().lower(),
],
)
),
Expand Down

0 comments on commit 65e4b1c

Please sign in to comment.