Skip to content

Commit 7a693c7

Browse files
authored
Merge pull request #1177 from mathics/filenames-implement
ByteArray implementation enhancements
2 parents 60f8faf + fa49878 commit 7a693c7

3 files changed

Lines changed: 44 additions & 13 deletions

File tree

mathics/autoload/formats/Base64/Export.m

Lines changed: 1 addition & 1 deletion
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

mathics/builtin/lists.py

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,15 @@ def find_matching_indices_with_levelspec(expr, pattern, evaluation, levelspec=1,
178178
return found
179179

180180

181+
class Normal(Builtin):
182+
"""
183+
<dl>
184+
<dt>'Normal[expr_]'
185+
<dd> Brings especial expressions to a normal expression from
186+
different especial forms.
187+
</dl>
188+
"""
189+
181190
class ByteArray(Builtin):
182191
"""
183192
<dl>
@@ -187,21 +196,41 @@ class ByteArray(Builtin):
187196
<dd> Constructs a byte array where bytes comes from decode a b64 encoded String
188197
</dl>
189198
190-
>> A=ByteArray[{1,25,3}]
199+
>> A=ByteArray[{1, 25, 3}]
191200
= ByteArray["ARkD"]
192201
>> A[[2]]
193202
= 25
203+
>> Normal[A]
204+
= {1, 25, 3}
205+
>> ToString[A]
206+
= ByteArray["ARkD"]
207+
>> ByteArray["ARkD"]
208+
= ByteArray["ARkD"]
194209
>> B=ByteArray["asy"]
195-
= ByteArray["WVhONQ=="]
210+
: The first argument in Bytearray[asy] should be a B64 enconded string or a vector of integers.
211+
= $Failed
196212
"""
197213

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

202218
def apply_str(self, string, evaluation):
203219
'ByteArray[string_String]'
204-
return Expression("ByteArray", ByteArrayAtom(string.value))
220+
try:
221+
atom = ByteArrayAtom(string.value)
222+
except Exception as e:
223+
evaluation.message("ByteArray", 'lend', string)
224+
return SymbolFailed
225+
return Expression("ByteArray", atom)
226+
227+
def apply_to_str(self, baa, evaluation):
228+
'ToString[ByteArray[baa_ByteArrayAtom]]'
229+
return String('ByteArray["' + baa.__str__() + '"]')
230+
231+
def apply_normal(self, baa, evaluation):
232+
'System`Normal[ByteArray[baa_ByteArrayAtom]]'
233+
return Expression("List", *[Integer(x) for x in baa.value])
205234

206235
def apply_list(self, values, evaluation):
207236
'ByteArray[values_List]'
@@ -1067,6 +1096,7 @@ def apply(self, list, i, evaluation):
10671096
indices = i.get_sequence()
10681097
# How to deal with ByteArrays
10691098
if list.get_head_name() == "System`ByteArray":
1099+
list = list.evaluate(evaluation)
10701100
if len(indices) > 1:
10711101
print("Part::partd1: Depth of object ByteArray[<3>] " +
10721102
"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)