Skip to content

Commit d25d7b7

Browse files
authored
[SOT] Add str.encode support (#73555)
1 parent 5f8e3eb commit d25d7b7

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

python/paddle/jit/sot/opcode_translator/executor/variable_dispatch.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,23 @@ def str_format(var: ConstantVariable, *args: ConstantVariable):
798798
return var.format(*args)
799799

800800

801+
@Dispatcher.register_decorator(str.encode)
802+
def str_encode(
803+
var: ConstantVariable,
804+
encoding: ConstantVariable = None, # type: ignore
805+
errors: ConstantVariable = None, # type: ignore
806+
):
807+
if encoding is None:
808+
encoding = ConstantVariable('utf-8', var.graph, DanglingTracker())
809+
if errors is None:
810+
errors = ConstantVariable('strict', var.graph, DanglingTracker())
811+
return ConstantVariable(
812+
var.get_py_value().encode(encoding=encoding.get_py_value()),
813+
graph=var.graph,
814+
tracker=DummyTracker([var, encoding]),
815+
)
816+
817+
801818
Dispatcher.register(
802819
str.lower,
803820
("ConstantVariable",),

0 commit comments

Comments
 (0)