@@ -11,6 +11,7 @@ When calling Python functions or evaluating expressions, Erlang values are autom
1111| ` integer() ` | ` int ` | Arbitrary precision supported |
1212| ` float() ` | ` float ` | IEEE 754 double precision |
1313| ` binary() ` | ` str ` | UTF-8 encoded |
14+ | ` {bytes, binary()} ` | ` bytes ` | Explicit bytes (no UTF-8 decode) |
1415| ` atom() ` | ` str ` | Converted to string (except special atoms) |
1516| ` true ` | ` True ` | Boolean |
1617| ` false ` | ` False ` | Boolean |
@@ -56,6 +57,38 @@ py:call(mymod, func, [{1, 2, 3}]). %% Python receives: (1, 2, 3)
5657py :call (mymod , func , [#{a => 1 , b => 2 }]). % % Python receives: {"a": 1, "b": 2}
5758```
5859
60+ ### Explicit Bytes Conversion
61+
62+ By default, Erlang binaries are converted to Python ` str ` using UTF-8 decoding.
63+ To explicitly send raw bytes without string conversion, use the ` {bytes, Binary} ` tuple:
64+
65+ ``` erlang
66+ % % Default: binary -> str
67+ py :call (mymod , func , [<<" hello" >>]). % % Python sees: "hello" (str)
68+
69+ % % Explicit: {bytes, binary} -> bytes
70+ py :call (mymod , func , [{bytes , <<" hello" >>}]). % % Python sees: b"hello" (bytes)
71+
72+ % % Useful for binary protocols, images, compressed data
73+ py :call (image_processor , load , [{bytes , ImageData }]).
74+ ```
75+
76+ This is useful when you need to ensure binary data is treated as raw bytes in Python,
77+ for example when working with binary protocols, image data, or compressed content.
78+
79+ Note that on the return path, both Python ` str ` and ` bytes ` become Erlang ` binary() ` :
80+
81+ ``` erlang
82+ % % Python str -> Erlang binary
83+ {ok , <<" hello" >>} = py :eval (<<" 'hello'" >>).
84+
85+ % % Python bytes -> Erlang binary
86+ {ok , <<" hello" >>} = py :eval (<<" b'hello'" >>).
87+
88+ % % Non-UTF8 bytes also work
89+ {ok , <<255 , 254 >>} = py :eval (<<" b'\\ xff\\ xfe'" >>).
90+ ```
91+
5992## Python to Erlang
6093
6194Return values from Python are converted back to Erlang:
0 commit comments