File tree Expand file tree Collapse file tree 2 files changed +12
-2
lines changed Expand file tree Collapse file tree 2 files changed +12
-2
lines changed Original file line number Diff line number Diff line change 1
1
#include "pycall_internal.h"
2
+ #include <ruby/encoding.h>
2
3
3
4
#include <stdarg.h>
4
5
@@ -1469,24 +1470,26 @@ pycall_pystring_to_ruby(PyObject *pyobj)
1469
1470
{
1470
1471
char * str = NULL ;
1471
1472
Py_ssize_t len = 0 ;
1472
- int res = -1 ;
1473
+ int encindex , res = -1 ;
1473
1474
1474
1475
/* TODO: PyUnicode_Check */
1475
1476
if (pyobj -> ob_type == Py_API (PyUnicode_Type )) {
1476
1477
pyobj = Py_API (PyUnicode_AsUTF8String )(pyobj );
1477
1478
res = Py_API (PyString_AsStringAndSize )(pyobj , & str , & len );
1479
+ encindex = rb_utf8_encindex ();
1478
1480
pycall_Py_DecRef (pyobj );
1479
1481
}
1480
1482
/* TODO: PyString_Check */
1481
1483
else if (pyobj -> ob_type == Py_API (PyString_Type )) {
1482
1484
res = Py_API (PyString_AsStringAndSize )(pyobj , & str , & len );
1485
+ encindex = rb_ascii8bit_encindex ();
1483
1486
}
1484
1487
1485
1488
if (res < 0 ) {
1486
1489
return Qnil ;
1487
1490
}
1488
1491
1489
- return rb_str_new (str , len );
1492
+ return rb_enc_str_new (str , len , rb_enc_from_index ( encindex ) );
1490
1493
}
1491
1494
1492
1495
static VALUE
Original file line number Diff line number Diff line change @@ -102,6 +102,13 @@ module PyCall
102
102
it { is_expected . to be_a ( Module ) }
103
103
it { is_expected . to be_a ( PyObjectWrapper ) }
104
104
end
105
+
106
+ context 'for a unicode string' do
107
+ let ( :ruby_snowman ) { "\u{2603} " }
108
+ let ( :python_snowman ) { p Conversion . from_ruby ( ruby_snowman ) }
109
+ subject { Conversion . to_ruby ( python_snowman ) }
110
+ it { is_expected . to eq ( ruby_snowman ) }
111
+ end
105
112
end
106
113
107
114
describe '.from_ruby' do
You can’t perform that action at this time.
0 commit comments