| 1 |
|
| 2 |
import py |
| 3 |
import sys |
| 4 |
from rpython.rtyper.lltypesystem.rffi import * |
| 5 |
from rpython.rtyper.lltypesystem.rffi import _keeper_for_type |
| 6 |
from rpython.rlib.rposix import get_saved_errno, set_saved_errno |
| 7 |
from rpython.translator.c.test.test_genc import compile as compile_c |
| 8 |
from rpython.rtyper.lltypesystem.lltype import Signed, Ptr, Char, malloc |
| 9 |
from rpython.rtyper.lltypesystem import lltype |
| 10 |
from rpython.translator import cdir |
| 11 |
from rpython.tool.udir import udir |
| 12 |
from rpython.rtyper.test.test_llinterp import interpret |
| 13 |
from rpython.annotator.annrpython import RPythonAnnotator |
| 14 |
from rpython.rtyper.rtyper import RPythonTyper |
| 15 |
from rpython.translator.backendopt.all import backend_optimizations |
| 16 |
from rpython.translator.translator import graphof |
| 17 |
from rpython.conftest import option |
| 18 |
from rpython.flowspace.model import summary |
| 19 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 20 |
from rpython.rlib.rarithmetic import r_singlefloat |
| 21 |
|
| 22 |
PYPY_HOST = '__pypy__' in sys.modules |
| 23 |
|
| 24 |
class BaseTestRffi: |
| 25 |
def test_basic(self): |
| 26 |
c_source = py.code.Source(""" |
| 27 |
int someexternalfunction(int x) |
| 28 |
{ |
| 29 |
return (x + 3); |
| 30 |
} |
| 31 |
""") |
| 32 |
|
| 33 |
eci = ExternalCompilationInfo(separate_module_sources=[c_source], |
| 34 |
pre_include_bits=("int someexternalfunction(int);",)) |
| 35 |
z = llexternal('someexternalfunction', [Signed], Signed, |
| 36 |
compilation_info=eci) |
| 37 |
|
| 38 |
def f(): |
| 39 |
return z(8) |
| 40 |
|
| 41 |
xf = self.compile(f, []) |
| 42 |
assert xf() == 8+3 |
| 43 |
|
| 44 |
def test_no_float_to_int_conversion(self): |
| 45 |
c_source = py.code.Source(""" |
| 46 |
int someexternalfunction(int x) |
| 47 |
{ |
| 48 |
return (x + 3); |
| 49 |
} |
| 50 |
""") |
| 51 |
|
| 52 |
eci = ExternalCompilationInfo(separate_module_sources=[c_source]) |
| 53 |
z = llexternal('someexternalfunction', [Signed], Signed, |
| 54 |
compilation_info=eci) |
| 55 |
|
| 56 |
def f(): |
| 57 |
return z(8.2) |
| 58 |
|
| 59 |
py.test.raises(TypeError, f) |
| 60 |
py.test.raises(TypeError, self.compile, f, []) |
| 61 |
|
| 62 |
def test_hashdefine(self): |
| 63 |
h_source = """ |
| 64 |
#define X(i) (i+3) |
| 65 |
""" |
| 66 |
|
| 67 |
h_file = udir.join("stuff.h") |
| 68 |
h_file.write(h_source) |
| 69 |
|
| 70 |
eci = ExternalCompilationInfo(includes=['stuff.h'], |
| 71 |
include_dirs=[udir]) |
| 72 |
z = llexternal('X', [Signed], Signed, compilation_info=eci) |
| 73 |
py.test.raises(AssertionError, z, 8, 9) |
| 74 |
|
| 75 |
def f(): |
| 76 |
return z(8) |
| 77 |
|
| 78 |
xf = self.compile(f, []) |
| 79 |
assert xf() == 8+3 |
| 80 |
|
| 81 |
def test_string(self): |
| 82 |
eci = ExternalCompilationInfo(includes=['string.h']) |
| 83 |
z = llexternal('strlen', [CCHARP], Signed, compilation_info=eci) |
| 84 |
|
| 85 |
def f(): |
| 86 |
s = str2charp("xxx") |
| 87 |
res = z(s) |
| 88 |
free_charp(s) |
| 89 |
return res |
| 90 |
|
| 91 |
xf = self.compile(f, [], backendopt=False) |
| 92 |
assert xf() == 3 |
| 93 |
|
| 94 |
def test_unicode(self): |
| 95 |
eci = ExternalCompilationInfo(includes=['string.h', 'wchar.h']) |
| 96 |
z = llexternal('wcslen', [CWCHARP], Signed, compilation_info=eci) |
| 97 |
|
| 98 |
def f(): |
| 99 |
s = unicode2wcharp(u"xxx\xe9") |
| 100 |
res = z(s) |
| 101 |
free_wcharp(s) |
| 102 |
return res |
| 103 |
|
| 104 |
xf = self.compile(f, [], backendopt=False) |
| 105 |
assert xf() == 4 |
| 106 |
|
| 107 |
def test_charp2str_exact_result(self): |
| 108 |
from rpython.annotator.annrpython import RPythonAnnotator |
| 109 |
from rpython.rtyper.llannotation import SomePtr |
| 110 |
a = RPythonAnnotator() |
| 111 |
s = a.build_types(charpsize2str, [SomePtr(CCHARP), int]) |
| 112 |
assert s.knowntype == str |
| 113 |
assert s.can_be_None is False |
| 114 |
assert s.no_nul is False |
| 115 |
|
| 116 |
a = RPythonAnnotator() |
| 117 |
s = a.build_types(charp2str, [SomePtr(CCHARP)]) |
| 118 |
assert s.knowntype == str |
| 119 |
assert s.can_be_None is False |
| 120 |
assert s.no_nul is True |
| 121 |
|
| 122 |
def test_string_reverse(self): |
| 123 |
c_source = py.code.Source(""" |
| 124 |
#include <string.h> |
| 125 |
#include <src/mem.h> |
| 126 |
|
| 127 |
void f(char *target, char* arg) |
| 128 |
{ |
| 129 |
strcpy(target, arg); |
| 130 |
} |
| 131 |
""") |
| 132 |
eci = ExternalCompilationInfo(separate_module_sources=[c_source], |
| 133 |
post_include_bits=['void f(char*,char*);']) |
| 134 |
z = llexternal('f', [CCHARP, CCHARP], lltype.Void, compilation_info=eci) |
| 135 |
|
| 136 |
def f(): |
| 137 |
s = str2charp("xxx") |
| 138 |
l_res = lltype.malloc(CCHARP.TO, 10, flavor='raw') |
| 139 |
z(l_res, s) |
| 140 |
res = charp2str(l_res) |
| 141 |
lltype.free(l_res, flavor='raw') |
| 142 |
free_charp(s) |
| 143 |
return len(res) |
| 144 |
|
| 145 |
xf = self.compile(f, [], backendopt=False) |
| 146 |
assert xf() == 3 |
| 147 |
|
| 148 |
def test_constcharp2str(self): |
| 149 |
c_source = py.code.Source(""" |
| 150 |
const char *z(void) |
| 151 |
{ |
| 152 |
return "hello world"; |
| 153 |
} |
| 154 |
""") |
| 155 |
eci = ExternalCompilationInfo(separate_module_sources=[c_source], |
| 156 |
post_include_bits=['const char *z(void);']) |
| 157 |
z = llexternal('z', [], CONST_CCHARP, compilation_info=eci) |
| 158 |
|
| 159 |
def f(): |
| 160 |
l_buf = lltype.malloc(CCHARP.TO, 5, flavor='raw') |
| 161 |
l_buf[0] = 'A' |
| 162 |
l_buf[1] = 'B' |
| 163 |
l_buf[2] = 'C' |
| 164 |
l_buf[3] = '\x00' |
| 165 |
l_buf[4] = 'E' |
| 166 |
l_constbuf = cast(CONST_CCHARP, l_buf) |
| 167 |
res = constcharp2str(l_constbuf) |
| 168 |
lltype.free(l_buf, flavor='raw') |
| 169 |
return len(res) |
| 170 |
|
| 171 |
assert f() == 3 |
| 172 |
xf = self.compile(f, [], backendopt=False) |
| 173 |
assert xf() == 3 |
| 174 |
|
| 175 |
def test_constcharpsize2str(self): |
| 176 |
def f(): |
| 177 |
l_buf = lltype.malloc(CCHARP.TO, 5, flavor='raw') |
| 178 |
l_buf[0] = 'A' |
| 179 |
l_buf[1] = 'B' |
| 180 |
l_buf[2] = 'C' |
| 181 |
l_buf[3] = '\x00' |
| 182 |
l_buf[4] = 'E' |
| 183 |
l_constbuf = cast(CONST_CCHARP, l_buf) |
| 184 |
res = constcharpsize2str(l_constbuf, 5) |
| 185 |
lltype.free(l_buf, flavor='raw') |
| 186 |
return res |
| 187 |
|
| 188 |
assert f() == "ABC\x00E" |
| 189 |
xf = self.compile(f, [], backendopt=False) |
| 190 |
assert xf() == "ABC\x00E" |
| 191 |
|
| 192 |
def test_stringstar(self): |
| 193 |
c_source = """ |
| 194 |
#include <string.h> |
| 195 |
|
| 196 |
int f(char *args[]) { |
| 197 |
char **p = args; |
| 198 |
int l = 0; |
| 199 |
while (*p) { |
| 200 |
l += strlen(*p); |
| 201 |
p++; |
| 202 |
} |
| 203 |
return (l); |
| 204 |
} |
| 205 |
""" |
| 206 |
eci = ExternalCompilationInfo(separate_module_sources=[c_source], |
| 207 |
pre_include_bits=('int f(char**);',)) |
| 208 |
z = llexternal('f', [CCHARPP], Signed, compilation_info=eci) |
| 209 |
|
| 210 |
def f(): |
| 211 |
l = ["xxx", "x", "xxxx"] |
| 212 |
ss = liststr2charpp(l) |
| 213 |
result = z(ss) |
| 214 |
free_charpp(ss) |
| 215 |
return result |
| 216 |
|
| 217 |
xf = self.compile(f, [], backendopt=False) |
| 218 |
assert xf() == 8 |
| 219 |
|
| 220 |
def test_struct(self): |
| 221 |
h_source = """ |
| 222 |
#ifndef _MY_SOURCE_H |
| 223 |
#define _MY_SOURCE_H |
| 224 |
struct xx { |
| 225 |
int one; |
| 226 |
char two; |
| 227 |
int three; |
| 228 |
}; |
| 229 |
int f(struct xx*); |
| 230 |
#endif |
| 231 |
""" |
| 232 |
h_file = udir.join("structxx.h") |
| 233 |
h_file.write(h_source) |
| 234 |
|
| 235 |
c_source = """ |
| 236 |
#include <structxx.h> |
| 237 |
|
| 238 |
int f(struct xx* z) |
| 239 |
{ |
| 240 |
return (z->one + z->three); |
| 241 |
} |
| 242 |
""" |
| 243 |
TP = CStructPtr('xx', ('one', INT), ('two', Char), ('three', INT)) |
| 244 |
|
| 245 |
eci = ExternalCompilationInfo( |
| 246 |
includes=['structxx.h'], |
| 247 |
include_dirs=[udir], |
| 248 |
separate_module_sources=[c_source] |
| 249 |
) |
| 250 |
z = llexternal('f', [TP], INT, compilation_info=eci) |
| 251 |
|
| 252 |
def f(): |
| 253 |
struct = lltype.malloc(TP.TO, flavor='raw') |
| 254 |
struct.c_one = cast(INT, 3) |
| 255 |
struct.c_two = '\x33' |
| 256 |
struct.c_three = cast(INT, 5) |
| 257 |
result = z(struct) |
| 258 |
lltype.free(struct, flavor='raw') |
| 259 |
return cast(SIGNED, result) |
| 260 |
|
| 261 |
fn = self.compile(f, [], backendopt=False) |
| 262 |
assert fn() == 8 |
| 263 |
|
| 264 |
def test_externvar(self): |
| 265 |
import os |
| 266 |
if os.name == 'nt': |
| 267 |
|
| 268 |
bad_fd = 0 |
| 269 |
else: |
| 270 |
bad_fd = 12312312 |
| 271 |
|
| 272 |
def f(): |
| 273 |
set_saved_errno(12) |
| 274 |
return get_saved_errno() |
| 275 |
|
| 276 |
def g(): |
| 277 |
try: |
| 278 |
os.write(bad_fd, "xxx") |
| 279 |
except OSError: |
| 280 |
pass |
| 281 |
return get_saved_errno() |
| 282 |
|
| 283 |
fn = self.compile(f, []) |
| 284 |
assert fn() == 12 |
| 285 |
gn = self.compile(g, []) |
| 286 |
import errno |
| 287 |
assert gn() == errno.EBADF |
| 288 |
|
| 289 |
|
| 290 |
def test_extra_include_dirs(self): |
| 291 |
udir.ensure("incl", dir=True) |
| 292 |
udir.join("incl", "incl.h").write("#define C 3\nint fun();") |
| 293 |
c_source = py.code.Source(""" |
| 294 |
#include <incl.h> |
| 295 |
int fun () |
| 296 |
{ |
| 297 |
return (C); |
| 298 |
} |
| 299 |
""") |
| 300 |
eci = ExternalCompilationInfo( |
| 301 |
includes=['incl.h'], |
| 302 |
include_dirs=[str(udir.join('incl'))], |
| 303 |
separate_module_sources=[c_source] |
| 304 |
) |
| 305 |
z = llexternal('fun', [], Signed, compilation_info=eci) |
| 306 |
|
| 307 |
def f(): |
| 308 |
return z() |
| 309 |
|
| 310 |
res = self.compile(f, []) |
| 311 |
assert res() == 3 |
| 312 |
|
| 313 |
def test_compile_cast(self): |
| 314 |
def f(n): |
| 315 |
return cast(SIZE_T, n) |
| 316 |
|
| 317 |
f1 = self.compile(f, [int]) |
| 318 |
res = f1(-1) |
| 319 |
assert res == r_size_t(-1) |
| 320 |
|
| 321 |
def test_opaque_type(self): |
| 322 |
h_source = py.code.Source(""" |
| 323 |
#ifndef _OPAQUE_H |
| 324 |
#define _OPAQUE_H |
| 325 |
struct stuff { |
| 326 |
char data[38]; |
| 327 |
}; |
| 328 |
char get(struct stuff *); |
| 329 |
#endif /* _OPAQUE_H */ |
| 330 |
""") |
| 331 |
|
| 332 |
c_source = py.code.Source(""" |
| 333 |
#include "opaque.h" |
| 334 |
|
| 335 |
char get(struct stuff* x) |
| 336 |
{ |
| 337 |
x->data[13] = 'a'; |
| 338 |
return x->data[13]; |
| 339 |
} |
| 340 |
""") |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
h_file = udir.join("opaque.h") |
| 345 |
h_file.write(h_source) |
| 346 |
|
| 347 |
from rpython.rtyper.tool import rffi_platform |
| 348 |
eci = ExternalCompilationInfo( |
| 349 |
includes=['opaque.h'], |
| 350 |
include_dirs=[str(udir)], |
| 351 |
separate_module_sources=[c_source] |
| 352 |
) |
| 353 |
STUFFP = COpaquePtr('struct stuff', compilation_info=eci) |
| 354 |
|
| 355 |
ll_get = llexternal('get', [STUFFP], CHAR, compilation_info=eci) |
| 356 |
|
| 357 |
def f(): |
| 358 |
ll_stuff = lltype.malloc(STUFFP.TO, flavor='raw') |
| 359 |
result = ll_get(ll_stuff) |
| 360 |
lltype.free(ll_stuff, flavor='raw') |
| 361 |
return result |
| 362 |
|
| 363 |
f1 = self.compile(f, []) |
| 364 |
assert f1() == 'a' |
| 365 |
|
| 366 |
def test_opaque_typedef(self): |
| 367 |
code = """ |
| 368 |
#include <stddef.h> |
| 369 |
struct stuff; |
| 370 |
typedef struct stuff *stuff_ptr; |
| 371 |
static int get(stuff_ptr ptr) { return (ptr != NULL); } |
| 372 |
""" |
| 373 |
|
| 374 |
eci = ExternalCompilationInfo( |
| 375 |
post_include_bits = [code] |
| 376 |
) |
| 377 |
|
| 378 |
STUFFP = COpaquePtr(typedef='stuff_ptr', compilation_info=eci) |
| 379 |
ll_get = llexternal('get', [STUFFP], lltype.Signed, |
| 380 |
compilation_info=eci) |
| 381 |
|
| 382 |
def f(): |
| 383 |
return ll_get(lltype.nullptr(STUFFP.TO)) |
| 384 |
|
| 385 |
f1 = self.compile(f, []) |
| 386 |
assert f1() == 0 |
| 387 |
|
| 388 |
def return_char(self, signed): |
| 389 |
ctype_pref = ["un", ""][signed] |
| 390 |
rffi_type = [UCHAR, SIGNEDCHAR][signed] |
| 391 |
h_source = py.code.Source(""" |
| 392 |
%ssigned char returnchar(void) |
| 393 |
{ |
| 394 |
return 42; |
| 395 |
} |
| 396 |
""" % (ctype_pref, )) |
| 397 |
h_file = udir.join("opaque2%s.h" % (ctype_pref, )) |
| 398 |
h_file.write(h_source) |
| 399 |
|
| 400 |
from rpython.rtyper.tool import rffi_platform |
| 401 |
eci = ExternalCompilationInfo( |
| 402 |
includes=[h_file.basename], |
| 403 |
include_dirs=[str(udir)] |
| 404 |
) |
| 405 |
ll_returnchar = llexternal('returnchar', [], rffi_type, compilation_info=eci) |
| 406 |
|
| 407 |
def f(): |
| 408 |
result = ll_returnchar() |
| 409 |
return result |
| 410 |
|
| 411 |
f1 = self.compile(f, []) |
| 412 |
assert f1() == chr(42) |
| 413 |
|
| 414 |
def test_generate_return_char_tests(self): |
| 415 |
yield self.return_char, False |
| 416 |
yield self.return_char, True |
| 417 |
|
| 418 |
def test_prebuilt_constant(self): |
| 419 |
py.test.skip("Think how to do it sane") |
| 420 |
h_source = py.code.Source(""" |
| 421 |
int x = 3; |
| 422 |
char** z = NULL; |
| 423 |
#endif |
| 424 |
""") |
| 425 |
h_include = udir.join('constants.h') |
| 426 |
h_include.write(h_source) |
| 427 |
|
| 428 |
eci = ExternalCompilationInfo(includes=['stdio.h', |
| 429 |
str(h_include.basename)], |
| 430 |
include_dirs=[str(udir)]) |
| 431 |
|
| 432 |
get_x, set_x = CExternVariable(lltype.Signed, 'x', eci) |
| 433 |
get_z, set_z = CExternVariable(CCHARPP, 'z', eci) |
| 434 |
|
| 435 |
def f(): |
| 436 |
one = get_x() |
| 437 |
set_x(13) |
| 438 |
return one + get_x() |
| 439 |
|
| 440 |
def g(): |
| 441 |
l = liststr2charpp(["a", "b", "c"]) |
| 442 |
try: |
| 443 |
set_z(l) |
| 444 |
return charp2str(get_z()[2]) |
| 445 |
finally: |
| 446 |
free_charpp(l) |
| 447 |
|
| 448 |
fn = self.compile(f, []) |
| 449 |
assert fn() == 16 |
| 450 |
gn = self.compile(g, []) |
| 451 |
assert gn() == "c" |
| 452 |
|
| 453 |
def eating_callback(self): |
| 454 |
h_source = py.code.Source(""" |
| 455 |
#ifndef _CALLBACK_H |
| 456 |
#define _CALLBACK_H |
| 457 |
RPY_EXTERN Signed eating_callback(Signed arg, Signed(*call)(Signed)); |
| 458 |
#endif /* _CALLBACK_H */ |
| 459 |
""") |
| 460 |
|
| 461 |
h_include = udir.join('callback.h') |
| 462 |
h_include.write(h_source) |
| 463 |
|
| 464 |
c_source = py.code.Source(""" |
| 465 |
#include "src/precommondefs.h" |
| 466 |
|
| 467 |
RPY_EXTERN Signed eating_callback(Signed arg, Signed(*call)(Signed)) |
| 468 |
{ |
| 469 |
Signed res = call(arg); |
| 470 |
if (res == -1) |
| 471 |
return -1; |
| 472 |
return res; |
| 473 |
} |
| 474 |
""") |
| 475 |
|
| 476 |
eci = ExternalCompilationInfo(includes=['callback.h'], |
| 477 |
include_dirs=[str(udir), cdir], |
| 478 |
separate_module_sources=[c_source]) |
| 479 |
|
| 480 |
args = [SIGNED, CCallback([SIGNED], SIGNED)] |
| 481 |
eating_callback = llexternal('eating_callback', args, SIGNED, |
| 482 |
compilation_info=eci) |
| 483 |
|
| 484 |
return eating_callback |
| 485 |
|
| 486 |
def test_c_callback(self): |
| 487 |
eating_callback = self.eating_callback() |
| 488 |
def g(i): |
| 489 |
return i + 3 |
| 490 |
|
| 491 |
def f(): |
| 492 |
return eating_callback(3, g) |
| 493 |
|
| 494 |
fn = self.compile(f, []) |
| 495 |
assert fn() == 6 |
| 496 |
assert eating_callback._ptr._obj._callbacks.callbacks == {g: True} |
| 497 |
|
| 498 |
def test_double_callback(self): |
| 499 |
eating_callback = self.eating_callback() |
| 500 |
|
| 501 |
def one(i): |
| 502 |
return i |
| 503 |
|
| 504 |
def two(i): |
| 505 |
return i + 2 |
| 506 |
|
| 507 |
def f(i): |
| 508 |
if i > 3: |
| 509 |
return eating_callback(i, one) |
| 510 |
else: |
| 511 |
return eating_callback(i, two) |
| 512 |
|
| 513 |
fn = self.compile(f, [int]) |
| 514 |
assert fn(4) == 4 |
| 515 |
assert fn(1) == 3 |
| 516 |
assert eating_callback._ptr._obj._callbacks.callbacks == {one: True, |
| 517 |
two: True} |
| 518 |
|
| 519 |
def test_exception_callback(self): |
| 520 |
eating_callback = self.eating_callback() |
| 521 |
|
| 522 |
def raising(i): |
| 523 |
if i > 3: |
| 524 |
raise ValueError |
| 525 |
else: |
| 526 |
return 3 |
| 527 |
raising._errorcode_ = -1 |
| 528 |
|
| 529 |
def f(i): |
| 530 |
return eating_callback(i, raising) |
| 531 |
|
| 532 |
fn = self.compile(f, [int]) |
| 533 |
assert fn(13) == -1 |
| 534 |
|
| 535 |
def test_callback_already_llptr(self): |
| 536 |
eating_callback = self.eating_callback() |
| 537 |
def g(i): |
| 538 |
return i + 3 |
| 539 |
G = lltype.Ptr(lltype.FuncType([lltype.Signed], lltype.Signed)) |
| 540 |
|
| 541 |
def f(): |
| 542 |
return eating_callback(3, llhelper(G, g)) |
| 543 |
|
| 544 |
fn = self.compile(f, []) |
| 545 |
assert fn() == 6 |
| 546 |
|
| 547 |
def test_pass_opaque_pointer_via_callback(self): |
| 548 |
eating_callback = self.eating_callback() |
| 549 |
TP = lltype.Ptr(lltype.GcStruct('X', ('x', lltype.Signed))) |
| 550 |
struct = lltype.malloc(TP.TO) |
| 551 |
struct.x = 8 |
| 552 |
|
| 553 |
def g(i): |
| 554 |
return get_keepalive_object(i, TP).x |
| 555 |
|
| 556 |
pos = register_keepalive(struct) |
| 557 |
assert _keeper_for_type(TP).stuff_to_keepalive[pos] is struct |
| 558 |
del struct |
| 559 |
res = eating_callback(pos, g) |
| 560 |
unregister_keepalive(pos, TP) |
| 561 |
assert res == 8 |
| 562 |
|
| 563 |
def test_nonmoving(self): |
| 564 |
d = 'non-moving data stuff' |
| 565 |
def f(): |
| 566 |
with scoped_alloc_buffer(len(d)) as s: |
| 567 |
for i in range(len(d)): |
| 568 |
s.raw[i] = d[i] |
| 569 |
return s.str(len(d)-1) |
| 570 |
assert f() == d[:-1] |
| 571 |
fn = self.compile(f, [], gcpolicy='ref') |
| 572 |
assert fn() == d[:-1] |
| 573 |
|
| 574 |
def test_nonmoving_unicode(self): |
| 575 |
d = u'non-moving data' |
| 576 |
def f(): |
| 577 |
with scoped_alloc_unicodebuffer(len(d)) as s: |
| 578 |
for i in range(len(d)): |
| 579 |
s.raw[i] = d[i] |
| 580 |
return s.str(len(d)-1).encode('ascii') |
| 581 |
assert f() == d[:-1] |
| 582 |
fn = self.compile(f, [], gcpolicy='ref') |
| 583 |
assert fn() == d[:-1] |
| 584 |
|
| 585 |
def test_nonmovingbuffer(self): |
| 586 |
d = 'some cool data that should not move' |
| 587 |
def f(): |
| 588 |
buf, llobj, flag = get_nonmovingbuffer_ll(d) |
| 589 |
try: |
| 590 |
counter = 0 |
| 591 |
for i in range(len(d)): |
| 592 |
if buf[i] == d[i]: |
| 593 |
counter += 1 |
| 594 |
return counter |
| 595 |
finally: |
| 596 |
free_nonmovingbuffer_ll(buf, llobj, flag) |
| 597 |
assert f() == len(d) |
| 598 |
fn = self.compile(f, [], gcpolicy='ref') |
| 599 |
assert fn() == len(d) |
| 600 |
|
| 601 |
def test_nonmovingbuffer_semispace(self): |
| 602 |
d = 'cool data' |
| 603 |
def f(): |
| 604 |
counter = 0 |
| 605 |
for n in range(32): |
| 606 |
buf, llobj, flag = get_nonmovingbuffer_ll(d) |
| 607 |
try: |
| 608 |
for i in range(len(d)): |
| 609 |
if buf[i] == d[i]: |
| 610 |
counter += 1 |
| 611 |
finally: |
| 612 |
free_nonmovingbuffer_ll(buf, llobj, flag) |
| 613 |
return counter |
| 614 |
fn = self.compile(f, [], gcpolicy='semispace') |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
res = fn(expected_extra_mallocs=range(30)) |
| 620 |
assert res == 32 * len(d) |
| 621 |
|
| 622 |
def test_nonmovingbuffer_incminimark(self): |
| 623 |
d = 'cool data' |
| 624 |
def f(): |
| 625 |
counter = 0 |
| 626 |
for n in range(32): |
| 627 |
buf, llobj, flag = get_nonmovingbuffer_ll(d) |
| 628 |
try: |
| 629 |
for i in range(len(d)): |
| 630 |
if buf[i] == d[i]: |
| 631 |
counter += 1 |
| 632 |
finally: |
| 633 |
free_nonmovingbuffer_ll(buf, llobj, flag) |
| 634 |
return counter |
| 635 |
fn = self.compile(f, [], gcpolicy='incminimark') |
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
res = fn(expected_extra_mallocs=range(30)) |
| 641 |
assert res == 32 * len(d) |
| 642 |
|
| 643 |
def test_wcharp_to_utf8(self): |
| 644 |
wchar = lltype.malloc(CWCHARP.TO, 3, flavor='raw') |
| 645 |
wchar[0] = u'\u1234' |
| 646 |
wchar[1] = u'\x80' |
| 647 |
wchar[2] = u'a' |
| 648 |
assert wcharpsize2utf8(wchar, 3).decode("utf8") == u'\u1234\x80a' |
| 649 |
lltype.free(wchar, flavor='raw') |
| 650 |
|
| 651 |
class TestRffiInternals: |
| 652 |
def test_struct_create(self): |
| 653 |
X = CStruct('xx', ('one', INT)) |
| 654 |
def f(): |
| 655 |
p = make(X, c_one=cast(INT, 3)) |
| 656 |
res = p.c_one |
| 657 |
lltype.free(p, flavor='raw') |
| 658 |
return cast(SIGNED, res) |
| 659 |
assert f() == 3 |
| 660 |
assert interpret(f, []) == 3 |
| 661 |
|
| 662 |
def test_structcopy(self): |
| 663 |
X2 = lltype.Struct('X2', ('x', SIGNED)) |
| 664 |
X1 = lltype.Struct('X1', ('a', SIGNED), ('x2', X2), ('p', lltype.Ptr(X2))) |
| 665 |
def f(): |
| 666 |
p2 = make(X2, x=123) |
| 667 |
p1 = make(X1, a=5, p=p2) |
| 668 |
p1.x2.x = 456 |
| 669 |
p1bis = make(X1) |
| 670 |
p2bis = make(X2) |
| 671 |
structcopy(p1bis, p1) |
| 672 |
assert p1bis.a == 5 |
| 673 |
assert p1bis.x2.x == 456 |
| 674 |
assert p1bis.p == p2 |
| 675 |
structcopy(p2bis, p2) |
| 676 |
res = p2bis.x |
| 677 |
lltype.free(p2bis, flavor='raw') |
| 678 |
lltype.free(p1bis, flavor='raw') |
| 679 |
lltype.free(p2, flavor='raw') |
| 680 |
lltype.free(p1, flavor='raw') |
| 681 |
return res |
| 682 |
assert f() == 123 |
| 683 |
res = interpret(f, []) |
| 684 |
assert res == 123 |
| 685 |
|
| 686 |
def test_make_annotation(self): |
| 687 |
X = CStruct('xx', ('one', INT)) |
| 688 |
def f(): |
| 689 |
p = make(X) |
| 690 |
try: |
| 691 |
q = make(X) |
| 692 |
lltype.free(q, flavor='raw') |
| 693 |
finally: |
| 694 |
lltype.free(p, flavor='raw') |
| 695 |
return 3 |
| 696 |
assert interpret(f, []) == 3 |
| 697 |
|
| 698 |
def test_implicit_cast(self): |
| 699 |
z = llexternal('z', [USHORT, ULONG, USHORT, DOUBLE], USHORT, |
| 700 |
sandboxsafe=True) |
| 701 |
|
| 702 |
def f(x, y, xx, yy): |
| 703 |
return z(x, y, xx, yy) |
| 704 |
|
| 705 |
a = RPythonAnnotator() |
| 706 |
r = a.build_types(f, [int, int, int, int]) |
| 707 |
rtyper = RPythonTyper(a) |
| 708 |
rtyper.specialize() |
| 709 |
a.translator.rtyper = rtyper |
| 710 |
backend_optimizations(a.translator) |
| 711 |
if option.view: |
| 712 |
a.translator.view() |
| 713 |
graph = graphof(a.translator, f) |
| 714 |
s = summary(graph) |
| 715 |
|
| 716 |
expected = {'force_cast': 3, 'cast_int_to_float': 1, 'direct_call': 1} |
| 717 |
for k, v in expected.items(): |
| 718 |
assert s[k] == v |
| 719 |
|
| 720 |
def test_stringpolicy1(self): |
| 721 |
eci = ExternalCompilationInfo(includes=['string.h']) |
| 722 |
strlen = llexternal('strlen', [CCHARP], SIZE_T, compilation_info=eci) |
| 723 |
def f(): |
| 724 |
return cast(SIGNED, strlen("Xxx")) |
| 725 |
assert interpret(f, [], backendopt=True) == 3 |
| 726 |
|
| 727 |
def test_stringpolicy3(self): |
| 728 |
eci = ExternalCompilationInfo(includes=['string.h']) |
| 729 |
strlen = llexternal('strlen', [CCHARP], INT, compilation_info=eci) |
| 730 |
def f(): |
| 731 |
ll_str = str2charp("Xxx") |
| 732 |
res = strlen(ll_str) |
| 733 |
lltype.free(ll_str, flavor='raw') |
| 734 |
return res |
| 735 |
|
| 736 |
assert interpret(f, [], backendopt=True) == 3 |
| 737 |
|
| 738 |
def test_stringpolicy_mixed(self): |
| 739 |
eci = ExternalCompilationInfo(includes=['string.h']) |
| 740 |
strlen = llexternal('strlen', [CCHARP], SIZE_T, |
| 741 |
compilation_info=eci) |
| 742 |
def f(): |
| 743 |
res1 = strlen("abcd") |
| 744 |
ll_str = str2charp("Xxx") |
| 745 |
res2 = strlen(ll_str) |
| 746 |
lltype.free(ll_str, flavor='raw') |
| 747 |
return cast(SIGNED, res1*10 + res2) |
| 748 |
|
| 749 |
assert interpret(f, [], backendopt=True) == 43 |
| 750 |
|
| 751 |
def test_str2chararray(self): |
| 752 |
eci = ExternalCompilationInfo(includes=['string.h']) |
| 753 |
strlen = llexternal('strlen', [CCHARP], SIZE_T, |
| 754 |
compilation_info=eci) |
| 755 |
def f(): |
| 756 |
raw = str2charp("XxxZy") |
| 757 |
n = str2chararray("abcdef", raw, 4) |
| 758 |
assert raw[0] == 'a' |
| 759 |
assert raw[1] == 'b' |
| 760 |
assert raw[2] == 'c' |
| 761 |
assert raw[3] == 'd' |
| 762 |
assert raw[4] == 'y' |
| 763 |
lltype.free(raw, flavor='raw') |
| 764 |
return n |
| 765 |
|
| 766 |
assert interpret(f, []) == 4 |
| 767 |
|
| 768 |
def test_external_callable(self): |
| 769 |
""" Try to call some llexternal function with llinterp |
| 770 |
""" |
| 771 |
z = llexternal('z', [Signed], Signed, _callable=lambda x:x+1) |
| 772 |
|
| 773 |
def f(): |
| 774 |
return z(2) |
| 775 |
|
| 776 |
res = interpret(f, []) |
| 777 |
assert res == 3 |
| 778 |
|
| 779 |
def test_size_t_sign(self): |
| 780 |
assert r_size_t(-1) > 0 |
| 781 |
|
| 782 |
def test_cast(self): |
| 783 |
res = cast(SIZE_T, -1) |
| 784 |
assert type(res) is r_size_t |
| 785 |
assert res == r_size_t(-1) |
| 786 |
|
| 787 |
res = cast(lltype.Signed, 42.5) |
| 788 |
assert res == 42 |
| 789 |
|
| 790 |
res = cast(lltype.SingleFloat, 12.3) |
| 791 |
assert res == r_singlefloat(12.3) |
| 792 |
res = cast(lltype.SingleFloat, res) |
| 793 |
assert res == r_singlefloat(12.3) |
| 794 |
|
| 795 |
res = cast(lltype.Float, r_singlefloat(12.)) |
| 796 |
assert res == 12. |
| 797 |
|
| 798 |
def test_rffi_sizeof(self): |
| 799 |
try: |
| 800 |
import ctypes |
| 801 |
except ImportError: |
| 802 |
py.test.skip("Cannot test without ctypes") |
| 803 |
cache = { |
| 804 |
lltype.Signed: ctypes.c_long, |
| 805 |
lltype.Unsigned: ctypes.c_ulong, |
| 806 |
lltype.UniChar: ctypes.c_wchar, |
| 807 |
lltype.Char: ctypes.c_ubyte, |
| 808 |
} |
| 809 |
if sys.platform == 'win32' and sys.maxint > 2**32: |
| 810 |
cache[lltype.Signed] = ctypes.c_longlong |
| 811 |
cache[lltype.Unsigned] = ctypes.c_ulonglong |
| 812 |
cache2 = { |
| 813 |
DOUBLE: ctypes.c_double, |
| 814 |
FLOAT: ctypes.c_float, |
| 815 |
SIGNEDCHAR: ctypes.c_byte, |
| 816 |
UCHAR: ctypes.c_ubyte, |
| 817 |
SHORT: ctypes.c_short, |
| 818 |
USHORT: ctypes.c_ushort, |
| 819 |
INT: ctypes.c_int, |
| 820 |
UINT: ctypes.c_uint, |
| 821 |
LONG: ctypes.c_long, |
| 822 |
ULONG: ctypes.c_ulong, |
| 823 |
LONGLONG: ctypes.c_longlong, |
| 824 |
ULONGLONG: ctypes.c_ulonglong, |
| 825 |
SIZE_T: ctypes.c_size_t, |
| 826 |
} |
| 827 |
|
| 828 |
for ll, ctp in cache.items(): |
| 829 |
assert sizeof(ll) == ctypes.sizeof(ctp) |
| 830 |
assert sizeof(lltype.Typedef(ll, 'test')) == sizeof(ll) |
| 831 |
for ll, ctp in cache2.items(): |
| 832 |
assert sizeof(ll) == ctypes.sizeof(ctp) |
| 833 |
assert sizeof(lltype.Typedef(ll, 'test')) == sizeof(ll) |
| 834 |
assert not size_and_sign(lltype.Signed)[1] |
| 835 |
assert size_and_sign(lltype.Char) == (1, True) |
| 836 |
assert size_and_sign(lltype.UniChar)[1] |
| 837 |
assert size_and_sign(UINT)[1] |
| 838 |
assert not size_and_sign(INT)[1] |
| 839 |
|
| 840 |
def test_rffi_offsetof(self): |
| 841 |
import struct |
| 842 |
from rpython.rtyper.tool import rffi_platform |
| 843 |
S = rffi_platform.getstruct("struct S", |
| 844 |
""" |
| 845 |
struct S { |
| 846 |
short a; |
| 847 |
int b, c; |
| 848 |
}; """, |
| 849 |
[("a", INT), |
| 850 |
("b", INT), |
| 851 |
("c", INT)]) |
| 852 |
assert sizeof(S) == struct.calcsize("hii") |
| 853 |
assert offsetof(S, "c_a") == 0 |
| 854 |
assert offsetof(S, "c_b") == struct.calcsize("hi") - struct.calcsize("i") |
| 855 |
assert offsetof(S, "c_c") == struct.calcsize("hii") - struct.calcsize("i") |
| 856 |
|
| 857 |
ARRAY_OF_CHAR = lltype.Array(CHAR, hints={'nolength': True}) |
| 858 |
|
| 859 |
def test_ptradd(): |
| 860 |
data = "hello, world!" |
| 861 |
a = lltype.malloc(ARRAY_OF_CHAR, len(data), flavor='raw') |
| 862 |
for i in xrange(len(data)): |
| 863 |
a[i] = data[i] |
| 864 |
a2 = ptradd(a, 2) |
| 865 |
assert lltype.typeOf(a2) == lltype.typeOf(a) == lltype.Ptr(ARRAY_OF_CHAR) |
| 866 |
for i in xrange(len(data) - 2): |
| 867 |
assert a2[i] == a[i + 2] |
| 868 |
lltype.free(a, flavor='raw') |
| 869 |
|
| 870 |
def test_ptradd_interpret(): |
| 871 |
interpret(test_ptradd, []) |
| 872 |
|
| 873 |
def test_voidptr(): |
| 874 |
assert repr(VOIDP) == "<* Array of void {'nolength': True, 'render_as_void': True} >" |
| 875 |
|
| 876 |
class TestCRffi(BaseTestRffi): |
| 877 |
def compile(self, func, args, **kwds): |
| 878 |
return compile_c(func, args, **kwds) |
| 879 |
|
| 880 |
def test_generate_return_char_tests(self): |
| 881 |
py.test.skip("GenC does not handle char return values correctly") |
| 882 |
|
| 883 |
def test__get_raw_address_buf_from_string(self): |
| 884 |
from rpython.rlib import rgc |
| 885 |
from rpython.rtyper.lltypesystem import rffi |
| 886 |
|
| 887 |
def check_content(strings, rawptrs): |
| 888 |
for i in range(len(strings)): |
| 889 |
p = rawptrs[i] |
| 890 |
expected = strings[i] + '\x00' |
| 891 |
for j in range(len(expected)): |
| 892 |
assert p[j] == expected[j] |
| 893 |
|
| 894 |
def f(n): |
| 895 |
strings = ["foo%d" % i for i in range(n)] |
| 896 |
rawptrs = [rffi._get_raw_address_buf_from_string(s) |
| 897 |
for s in strings] |
| 898 |
check_content(strings, rawptrs) |
| 899 |
rgc.collect(); rgc.collect(); rgc.collect() |
| 900 |
check_content(strings, rawptrs) |
| 901 |
for i in range(len(strings)): |
| 902 |
|
| 903 |
p1 = rffi._get_raw_address_buf_from_string(strings[i]) |
| 904 |
assert rawptrs[i] == p1 |
| 905 |
del strings |
| 906 |
rgc.collect(); rgc.collect(); rgc.collect() |
| 907 |
return 42 |
| 908 |
|
| 909 |
rffi._StrFinalizerQueue.print_debugging = True |
| 910 |
try: |
| 911 |
xf = self.compile(f, [int], gcpolicy="incminimark", |
| 912 |
return_stderr=True) |
| 913 |
finally: |
| 914 |
rffi._StrFinalizerQueue.print_debugging = False |
| 915 |
|
| 916 |
os.environ['PYPYLOG'] = ':-' |
| 917 |
try: |
| 918 |
error = xf(10000) |
| 919 |
finally: |
| 920 |
del os.environ['PYPYLOG'] |
| 921 |
|
| 922 |
import re |
| 923 |
r = re.compile(r"freeing str [[] [0-9a-fxA-FX]+ []]") |
| 924 |
matches = r.findall(error) |
| 925 |
assert len(matches) == 10000 |
| 926 |
assert len(set(matches)) == 10000 |
| 927 |
|
| 928 |
|
| 929 |
def test_enforced_args(): |
| 930 |
from rpython.annotator.model import s_None |
| 931 |
from rpython.rtyper.annlowlevel import MixLevelHelperAnnotator |
| 932 |
from rpython.translator.interactive import Translation |
| 933 |
def f1(): |
| 934 |
str2charp("hello") |
| 935 |
def f2(): |
| 936 |
str2charp("world") |
| 937 |
t = Translation(f1, []) |
| 938 |
t.rtype() |
| 939 |
mixann = MixLevelHelperAnnotator(t.context.rtyper) |
| 940 |
mixann.getgraph(f2, [], s_None) |
| 941 |
mixann.finish() |
| 942 |
|
| 943 |
@py.test.mark.skipif(PYPY_HOST, reason="ll2ctypes cannot do this cast on PyPy2") |
| 944 |
def test_force_cast_unichar(): |
| 945 |
x = cast(lltype.UniChar, -1) |
| 946 |
assert isinstance(x, unicode) |
| 947 |
if sys.maxunicode == 65535: |
| 948 |
assert cast(LONG, x) == 65535 |
| 949 |
else: |
| 950 |
assert cast(LONG, cast(INT, x)) == -1 |
| 951 |
|
| 952 |
def test_c_memcpy(): |
| 953 |
p1 = str2charp("hello") |
| 954 |
p2 = str2charp("WORLD") |
| 955 |
c_memcpy(cast(VOIDP, p2), cast(CONST_VOIDP, p1), 3) |
| 956 |
assert charp2str(p1) == "hello" |
| 957 |
assert charp2str(p2) == "helLD" |
| 958 |
free_charp(p1) |
| 959 |
free_charp(p2) |
| 960 |
|
| 961 |
def test_sign_when_casting_uint_to_larger_int(): |
| 962 |
from rpython.rtyper.lltypesystem import rffi |
| 963 |
from rpython.rlib.rarithmetic import r_uint32, r_uint64 |
| 964 |
|
| 965 |
value = 0xAAAABBBB |
| 966 |
assert cast(lltype.SignedLongLong, r_uint32(value)) == value |
| 967 |
if hasattr(rffi, '__INT128_T'): |
| 968 |
value = 0xAAAABBBBCCCCDDDD |
| 969 |
assert cast(rffi.__INT128_T, r_uint64(value)) == value |
| 970 |
|
| 971 |
def test_scoped_view_charp(): |
| 972 |
s = 'bar' |
| 973 |
with scoped_view_charp(s) as buf: |
| 974 |
assert buf[0] == 'b' |
| 975 |
assert buf[1] == 'a' |
| 976 |
assert buf[2] == 'r' |
| 977 |
assert buf[3] == '\x00' |
| 978 |
|
| 979 |
def test_scoped_nonmoving_unicodebuffer(): |
| 980 |
s = u'bar' |
| 981 |
with scoped_nonmoving_unicodebuffer(s) as buf: |
| 982 |
assert buf[0] == u'b' |
| 983 |
assert buf[1] == u'a' |
| 984 |
assert buf[2] == u'r' |
| 985 |
with py.test.raises(IndexError): |
| 986 |
buf[3] |
| 987 |
|
| 988 |
def test_wcharp2utf8n(): |
| 989 |
w = 'hello\x00\x00\x00\x00' |
| 990 |
u, i = wcharp2utf8n(w, len(w)) |
| 991 |
assert i == len('hello') |