Skip to content

Commit d2fb8cf

Browse files
committed
ByteArray enhancements
1 parent ebe0ba4 commit d2fb8cf

3 files changed

Lines changed: 36 additions & 14 deletions

File tree

mathics/autoload/formats/Base64/Export.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
strm = OpenWrite[filename];
1212
If[strm === $Failed, Return[$Failed]];
1313
data = B64Encode[expr];
14-
WriteString[strm, data];
14+
WriteString[strm, data];
1515
Close[strm];
1616
]
1717

@@ -21,7 +21,7 @@
2121
FunctionChannels -> {"FileNames"},
2222
Options -> {"CharacterEncoding", "ByteOrderMark"},
2323
DefaultElement -> "Plaintext",
24-
BinaryFormat -> True
24+
BinaryFormat -> False
2525
]
2626

2727

mathics/builtin/lists.py

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,21 +187,41 @@ class ByteArray(Builtin):
187187
<dd> Constructs a byte array where bytes comes from decode a b64 encoded String
188188
</dl>
189189
190-
>> A=ByteArray[{1,25,3}]
190+
>> A=ByteArray[{1, 25, 3}]
191191
= ByteArray["ARkD"]
192192
>> A[[2]]
193193
= 25
194+
>> System`Normal[A]
195+
= {1, 25, 3}
196+
>> ToString[A]
197+
= ByteArray["ARkD"]
198+
>> ByteArray["ARkD"]
199+
= ByteArray["ARkD"]
194200
>> B=ByteArray["asy"]
195-
= ByteArray["WVhONQ=="]
201+
: The first argument in Bytearray[asy] should be a B64 enconded string or a vector of integers.
202+
= $Failed
196203
"""
197204

198205
messages = {'aotd': 'Elements in `1` are inconsistent with type Byte',
199206
'lend': 'The first argument in Bytearray[`1`] should ' + \
200-
'be a B64 enconded string or a vector of integers',}
207+
'be a B64 enconded string or a vector of integers.',}
201208

202209
def apply_str(self, string, evaluation):
203210
'ByteArray[string_String]'
204-
return Expression("ByteArray", ByteArrayAtom(string.value))
211+
try:
212+
atom = ByteArrayAtom(string.value)
213+
except Exception as e:
214+
evaluation.message("ByteArray", 'lend', string)
215+
return SymbolFailed
216+
return Expression("ByteArray", atom)
217+
218+
def apply_to_str(self, baa, evaluation):
219+
'ToString[ByteArray[baa_ByteArrayAtom]]'
220+
return String('ByteArray["' + baa.__str__() + '"]')
221+
222+
def apply_normal(self, baa, evaluation):
223+
'System`Normal[ByteArray[baa_ByteArrayAtom]]'
224+
return Expression("List", *[Integer(x) for x in baa.value])
205225

206226
def apply_list(self, values, evaluation):
207227
'ByteArray[values_List]'
@@ -1067,6 +1087,7 @@ def apply(self, list, i, evaluation):
10671087
indices = i.get_sequence()
10681088
# How to deal with ByteArrays
10691089
if list.get_head_name() == "System`ByteArray":
1090+
list = list.evaluate(evaluation)
10701091
if len(indices) > 1:
10711092
print("Part::partd1: Depth of object ByteArray[<3>] " +
10721093
"is not sufficient for the given part specification.")

mathics/core/expression.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2697,33 +2697,33 @@ def __new__(cls, value):
26972697
elif type(value) is list:
26982698
self.value = bytearray(list)
26992699
elif type(value) is str:
2700-
self.value = base64.b64encode(bytearray(value, 'utf8'))
2700+
self.value = base64.b64decode(value)
27012701
else:
27022702
raise Exception("value does not belongs to a valid type")
27032703
return self
27042704

27052705
def __str__(self) -> str:
2706-
return '"' + base64.b64encode(self.value).decode('utf8') + '"'
2706+
return base64.b64encode(self.value).decode('utf8')
27072707

27082708
def boxes_to_text(self, **options) -> str:
2709-
return '"' + base64.b64encode(self.value).decode('utf8') + '"'
2709+
return '"' + self.__str__() + '"'
27102710

27112711
def boxes_to_xml(self, **options) -> str:
2712-
return encode_mathml(String(base64.b64encode(self.value).decode('utf8')))
2712+
return encode_mathml(String('"'+self.__str__()+'"'))
27132713

27142714
def boxes_to_tex(self, **options) -> str:
2715-
from mathics.builtin import builtins
2716-
return encode_tex(String(base64.b64encode(self.value).decode('utf8')))
2715+
return encode_tex(String('"'+self.__str__()+'"'))
27172716

27182717
def atom_to_boxes(self, f, evaluation):
2719-
return String('"' + self.__str__() + '"')
2718+
res = String('""' + self.__str__() + '""')
2719+
return res
27202720

27212721
def do_copy(self) -> 'ByteArray':
27222722
return ByteArrayAtom(self.value)
27232723

27242724
def default_format(self, evaluation, form) -> str:
27252725
value = self.value
2726-
return value.__str__()
2726+
return '"' + value.__str__() + '"'
27272727

27282728
def get_sort_key(self, pattern_sort=False):
27292729
if pattern_sort:
@@ -2760,6 +2760,7 @@ def user_hash(self, update):
27602760
def __getnewargs__(self):
27612761
return (self.value,)
27622762

2763+
27632764
class StringFromPython(String):
27642765
def __new__(cls, value):
27652766
self = super(StringFromPython, cls).__new__(cls, value)

0 commit comments

Comments
 (0)