diff --git a/mathics/builtin/optiondoc.py b/mathics/builtin/optiondoc.py
index 58c24e716f..8a80aa174b 100644
--- a/mathics/builtin/optiondoc.py
+++ b/mathics/builtin/optiondoc.py
@@ -55,6 +55,18 @@ class Bottom(Builtin):
"""
+class CharacterEncodingOption(Builtin):
+ """
+
+ - 'CharacterEncoding'
+
- is an option for input and output functions which specifies what raw character encoding should be used.
+
+
+ >> ToString["\[CirclePlus]", CharacterEncoding -> "UTF8"]
+ = ...
+ """
+
+
class Filling(Builtin):
"""
diff --git a/mathics/builtin/strings.py b/mathics/builtin/strings.py
index eddfb740f8..a84b2de126 100644
--- a/mathics/builtin/strings.py
+++ b/mathics/builtin/strings.py
@@ -1661,14 +1661,23 @@ class ToString(Builtin):
"TotalWidth": "Infinity",
}
+ messages = {"encoding": "Encoding not implemented for ToString."}
+
+
def apply_default(self, value, evaluation, options):
"ToString[value_, OptionsPattern[ToString]]"
return self.apply_form(value, Symbol("System`OutputForm"), evaluation, options)
def apply_form(self, value, form, evaluation, options):
"ToString[value_, form_, OptionsPattern[ToString]]"
- encoding = options["System`CharacterEncoding"]
- text = value.format(evaluation, form.get_name(), encoding=encoding)
+ if form.has_form("Rule", 2) and form.leaves[0] == Symbol("System`CharacterEncoding"):
+ return evaluation.message("ToString", "encoding")
+ encoding = form.leaves[1]
+ # form_name='System`OutputForm'
+ else:
+ encoding = options["System`CharacterEncoding"]
+ form_name = form.get_name()
+ text = value.format(evaluation, form_name, encoding=encoding)
text = text.boxes_to_text(evaluation=evaluation)
return String(text)