@@ -56,11 +56,17 @@ def _reference_from_object(args):
56
56
return bytes (obj )
57
57
raise TypeError ("cannot convert '%s' object to bytes" % type (obj ).__name__ )
58
58
59
+ def _as_string (args ):
60
+ if not isinstance (args [0 ], bytes ):
61
+ return TypeError ()
62
+ return args [0 ].decode ()
59
63
60
64
def _as_string_and_size (args ):
65
+ if not isinstance (args [0 ], bytes ):
66
+ return TypeError ()
61
67
arg_bytes = args [0 ]
62
68
s = arg_bytes .decode ("utf-8" )
63
- return ( 0 , s , len (s ) )
69
+ return s , len (s )
64
70
65
71
66
72
def _reference_format (args ):
@@ -127,30 +133,43 @@ def compile_module(self, name):
127
133
128
134
# PyBytes_AsString
129
135
test_PyBytes_AsString = CPyExtFunction (
130
- lambda b : b [ 0 ]. decode () ,
136
+ _as_string ,
131
137
lambda : (
132
138
(b"hello" ,),
133
139
(b"world" ,),
134
140
(BytesSubclass (b"hello" ),),
141
+ (list (),),
142
+ ("hello" ,)
135
143
),
136
144
resultspec = "s" ,
137
145
argspec = "O" ,
138
146
arguments = ["PyObject* arg" ],
147
+ cmpfunc = unhandled_error_compare
139
148
)
140
149
141
150
# PyBytes_AsStringAndSize
142
- test_PyBytes_AsStringAndSize = CPyExtFunctionOutVars (
151
+ test_PyBytes_AsStringAndSize = CPyExtFunction (
143
152
_as_string_and_size ,
144
153
lambda : (
145
154
(b"hello" ,),
146
155
(b"world" ,),
147
156
(BytesSubclass (b"hello" ),),
157
+ (list (),),
158
+ ("hello" ,)
148
159
),
149
- resultspec = "isn" ,
160
+ code = """
161
+ static PyObject* wrap_PyBytes_AsStringAndSize(PyObject* arg) {
162
+ char* s;
163
+ Py_ssize_t sz;
164
+ if (PyBytes_AsStringAndSize(arg, &s, &sz) < 0)
165
+ return NULL;
166
+ return Py_BuildValue("sn", s, sz);
167
+ }
168
+ """ ,
169
+ callfunction = 'wrap_PyBytes_AsStringAndSize' ,
150
170
argspec = "O" ,
151
171
arguments = ["PyObject* arg" ],
152
- resultvars = ("char* s" , "Py_ssize_t sz" ),
153
- resulttype = "int"
172
+ cmpfunc = unhandled_error_compare
154
173
)
155
174
156
175
test_native_storage = CPyExtFunction (
0 commit comments