Skip to content

Commit e51a26d

Browse files
authored
Correct conversion from Python bytes & bytearrays (#1516)
A small misfeature/Bug noticed by bdlucas1 in implementing NumericArray. Before the recent work on ByteArray, this was less wrong, but still a little wrong.
1 parent 277a455 commit e51a26d

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

mathics/core/convert/python.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@
77

88
import numpy
99

10-
from mathics.core.atoms import Complex, Integer, NumericArray, Rational, Real, String
10+
from mathics.core.atoms import (
11+
ByteArray,
12+
Complex,
13+
Integer,
14+
NumericArray,
15+
Rational,
16+
Real,
17+
String,
18+
)
1119
from mathics.core.number import get_type
1220
from mathics.core.symbols import (
1321
BaseElement,
@@ -16,7 +24,7 @@
1624
SymbolNull,
1725
SymbolTrue,
1826
)
19-
from mathics.core.systemsymbols import SymbolByteArray, SymbolRule
27+
from mathics.core.systemsymbols import SymbolRule
2028

2129

2230
def from_bool(arg: bool) -> BooleanType:
@@ -112,9 +120,7 @@ def from_python(arg: Any) -> BaseElement:
112120
elif isinstance(arg, list) or isinstance(arg, tuple):
113121
return to_mathics_list(*arg, elements_conversion_fn=from_python)
114122
elif isinstance(arg, bytearray) or isinstance(arg, bytes):
115-
from mathics.builtin.binary.bytearray import ByteArray
116-
117-
return Expression(SymbolByteArray, ByteArray(arg))
123+
return ByteArray(arg)
118124
elif isinstance(arg, numpy.ndarray):
119125
return NumericArray(arg)
120126
else:
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from mathics.core.atoms import ByteArray
2+
from mathics.core.convert.python import from_python
3+
4+
5+
def test_from_python():
6+
assert isinstance(
7+
from_python(b"abc"), ByteArray
8+
), "Python bytes() converts to a Mathics3 ByteArray"
9+
assert isinstance(
10+
from_python(bytearray([1, 2, 3])), ByteArray
11+
), "Python bytearray converts to a Mathics3 ByteArray"
12+
# Many other tests are imagined to come...

0 commit comments

Comments
 (0)