PageRenderTime 53ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/pypy/module/_rawffi/alt/test/test_funcptr.py

https://bitbucket.org/pypy/pypy/
Python | 645 lines | 640 code | 4 blank | 1 comment | 0 complexity | d6fceed5fae31e8c6a5ec123078ff61a MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. from rpython.rtyper.lltypesystem import rffi
  2. from rpython.translator import cdir
  3. from rpython.rlib.clibffi import get_libc_name
  4. from rpython.rlib.libffi import types
  5. from rpython.rlib.libffi import CDLL
  6. from rpython.rlib.test.test_clibffi import get_libm_name
  7. import sys, py
  8. class BaseAppTestFFI(object):
  9. spaceconfig = dict(usemodules=('_rawffi',))
  10. @classmethod
  11. def prepare_c_example(cls):
  12. from rpython.tool.udir import udir
  13. from rpython.translator.tool.cbuild import ExternalCompilationInfo
  14. from rpython.translator.platform import platform
  15. c_file = udir.ensure("test__ffi", dir=1).join("foolib.c")
  16. # automatically collect the C source from the docstrings of the tests
  17. snippets = ["""
  18. #include "src/precommondefs.h"
  19. #define DLLEXPORT RPY_EXPORTED
  20. """]
  21. for name in dir(cls):
  22. if name.startswith('test_'):
  23. meth = getattr(cls, name)
  24. # the heuristic to determine it it's really C code could be
  25. # improved: so far we just check that there is a '{' :-)
  26. if meth.__doc__ is not None and '{' in meth.__doc__:
  27. snippets.append(meth.__doc__)
  28. #
  29. c_file.write(py.code.Source('\n'.join(snippets)))
  30. eci = ExternalCompilationInfo(include_dirs=[cdir])
  31. return str(platform.compile([c_file], eci, 'x', standalone=False))
  32. def setup_class(cls):
  33. space = cls.space
  34. cls.w_iswin32 = space.wrap(sys.platform == 'win32')
  35. cls.w_libfoo_name = space.wrap(cls.prepare_c_example())
  36. cls.w_libc_name = space.wrap(get_libc_name())
  37. libm_name = get_libm_name(sys.platform)
  38. cls.w_libm_name = space.wrap(libm_name)
  39. libm = CDLL(libm_name)
  40. pow = libm.getpointer('pow', [], types.void)
  41. pow_addr = rffi.cast(rffi.LONG, pow.funcsym)
  42. cls._libm = libm # otherwise it gets unloaded - argh!
  43. cls.w_pow_addr = space.wrap(pow_addr)
  44. class AppTestFFI(BaseAppTestFFI):
  45. def setup_class(cls):
  46. BaseAppTestFFI.setup_class.im_func(cls)
  47. space = cls.space
  48. # these are needed for test_single_float_args
  49. from ctypes import c_float
  50. f_12_34 = c_float(12.34).value
  51. f_56_78 = c_float(56.78).value
  52. f_result = c_float(f_12_34 + f_56_78).value
  53. cls.w_f_12_34_plus_56_78 = space.wrap(f_result)
  54. def test_libload(self):
  55. import _rawffi.alt
  56. _rawffi.alt.CDLL(self.libc_name)
  57. def test_libload_fail(self):
  58. import _rawffi.alt
  59. raises(OSError, _rawffi.alt.CDLL, "xxxxx_this_name_does_not_exist_xxxxx")
  60. def test_libload_None(self):
  61. if self.iswin32:
  62. skip("unix specific")
  63. from _rawffi.alt import CDLL, types
  64. # this should return *all* loaded libs, dlopen(NULL)
  65. dll = CDLL(None)
  66. # libm should be loaded
  67. res = dll.getfunc('sqrt', [types.double], types.double)(1.0)
  68. assert res == 1.0
  69. def test_callfunc(self):
  70. from _rawffi.alt import CDLL, types
  71. libm = CDLL(self.libm_name)
  72. pow = libm.getfunc('pow', [types.double, types.double], types.double)
  73. assert pow(2, 3) == 8
  74. def test_getaddr(self):
  75. from _rawffi.alt import CDLL, types
  76. libm = CDLL(self.libm_name)
  77. pow = libm.getfunc('pow', [types.double, types.double], types.double)
  78. assert pow.getaddr() == self.pow_addr
  79. def test_getaddressindll(self):
  80. import sys
  81. from _rawffi.alt import CDLL
  82. libm = CDLL(self.libm_name)
  83. pow_addr = libm.getaddressindll('pow')
  84. fff = sys.maxint*2-1
  85. if sys.platform == 'win32' or sys.platform == 'darwin':
  86. fff = sys.maxint*2+1
  87. assert pow_addr == self.pow_addr & fff
  88. def test_func_fromaddr(self):
  89. from _rawffi.alt import CDLL, types, FuncPtr
  90. libm = CDLL(self.libm_name)
  91. pow_addr = libm.getaddressindll('pow')
  92. pow = FuncPtr.fromaddr(pow_addr, 'pow', [types.double, types.double],
  93. types.double)
  94. assert pow(2, 3) == 8
  95. def test_int_args(self):
  96. """
  97. DLLEXPORT int sum_xy(int x, int y)
  98. {
  99. return x+y;
  100. }
  101. """
  102. import sys
  103. from _rawffi.alt import CDLL, types
  104. libfoo = CDLL(self.libfoo_name)
  105. sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
  106. assert sum_xy(30, 12) == 42
  107. assert sum_xy(sys.maxint*2, 0) == -2
  108. def test_void_result(self):
  109. """
  110. int dummy = 0;
  111. DLLEXPORT void set_dummy(int val) { dummy = val; }
  112. DLLEXPORT int get_dummy() { return dummy; }
  113. """
  114. from _rawffi.alt import CDLL, types
  115. libfoo = CDLL(self.libfoo_name)
  116. set_dummy = libfoo.getfunc('set_dummy', [types.sint], types.void)
  117. get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
  118. assert get_dummy() == 0
  119. assert set_dummy(42) is None
  120. assert get_dummy() == 42
  121. set_dummy(0)
  122. def test_pointer_args(self):
  123. """
  124. extern int dummy; // defined in test_void_result
  125. DLLEXPORT int* get_dummy_ptr() { return &dummy; }
  126. DLLEXPORT void set_val_to_ptr(int* ptr, int val) { *ptr = val; }
  127. """
  128. from _rawffi.alt import CDLL, types
  129. libfoo = CDLL(self.libfoo_name)
  130. get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
  131. get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p)
  132. set_val_to_ptr = libfoo.getfunc('set_val_to_ptr',
  133. [types.void_p, types.sint],
  134. types.void)
  135. assert get_dummy() == 0
  136. ptr = get_dummy_ptr()
  137. set_val_to_ptr(ptr, 123)
  138. assert get_dummy() == 123
  139. set_val_to_ptr(ptr, 0)
  140. def test_convert_pointer_args(self):
  141. """
  142. extern int dummy; // defined in test_void_result
  143. DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args
  144. DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto
  145. """
  146. from _rawffi.alt import CDLL, types
  147. class MyPointerWrapper(object):
  148. def __init__(self, value):
  149. self.value = value
  150. def _as_ffi_pointer_(self, ffitype):
  151. assert ffitype is types.void_p
  152. return self.value
  153. libfoo = CDLL(self.libfoo_name)
  154. get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
  155. get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], types.void_p)
  156. set_val_to_ptr = libfoo.getfunc('set_val_to_ptr',
  157. [types.void_p, types.sint],
  158. types.void)
  159. assert get_dummy() == 0
  160. ptr = get_dummy_ptr()
  161. assert type(ptr) in (int, long)
  162. ptr2 = MyPointerWrapper(ptr)
  163. set_val_to_ptr(ptr2, 123)
  164. assert get_dummy() == 123
  165. set_val_to_ptr(ptr2, 0)
  166. #
  167. class OldStyle:
  168. pass
  169. raises(TypeError, "set_val_to_ptr(OldStyle(), 0)")
  170. def test_convert_strings_to_char_p(self):
  171. """
  172. DLLEXPORT
  173. long mystrlen(char* s)
  174. {
  175. long len = 0;
  176. while(*s++)
  177. len++;
  178. return len;
  179. }
  180. """
  181. from _rawffi.alt import CDLL, types
  182. import _rawffi
  183. libfoo = CDLL(self.libfoo_name)
  184. mystrlen = libfoo.getfunc('mystrlen', [types.char_p], types.slong)
  185. #
  186. # first, try automatic conversion from a string
  187. assert mystrlen('foobar') == 6
  188. # then, try to pass an explicit pointer
  189. CharArray = _rawffi.Array('c')
  190. mystr = CharArray(7, 'foobar')
  191. assert mystrlen(mystr.buffer) == 6
  192. mystr.free()
  193. mystrlen.free_temp_buffers()
  194. def test_convert_unicode_to_unichar_p(self):
  195. """
  196. #include <wchar.h>
  197. DLLEXPORT
  198. long mystrlen_u(wchar_t* s)
  199. {
  200. long len = 0;
  201. while(*s++)
  202. len++;
  203. return len;
  204. }
  205. """
  206. from _rawffi.alt import CDLL, types
  207. import _rawffi
  208. libfoo = CDLL(self.libfoo_name)
  209. mystrlen = libfoo.getfunc('mystrlen_u', [types.unichar_p], types.slong)
  210. #
  211. # first, try automatic conversion from strings and unicode
  212. assert mystrlen('foobar') == 6
  213. assert mystrlen(u'foobar') == 6
  214. assert mystrlen(u'ab\u2070') == 3
  215. # then, try to pass an explicit pointer
  216. UniCharArray = _rawffi.Array('u')
  217. mystr = UniCharArray(7, u'foobar')
  218. assert mystrlen(mystr.buffer) == 6
  219. mystr.free()
  220. mystrlen.free_temp_buffers()
  221. def test_keepalive_temp_buffer(self):
  222. """
  223. DLLEXPORT
  224. char* do_nothing(char* s)
  225. {
  226. return s;
  227. }
  228. """
  229. from _rawffi.alt import CDLL, types
  230. import _rawffi
  231. libfoo = CDLL(self.libfoo_name)
  232. do_nothing = libfoo.getfunc('do_nothing', [types.char_p], types.char_p)
  233. CharArray = _rawffi.Array('c')
  234. #
  235. ptr = do_nothing('foobar')
  236. array = CharArray.fromaddress(ptr, 7)
  237. assert list(array) == list('foobar\00')
  238. do_nothing.free_temp_buffers()
  239. def test_typed_pointer_args(self):
  240. """
  241. extern int dummy; // defined in test_void_result
  242. DLLEXPORT int* get_dummy_ptr(); // defined in test_pointer_args
  243. DLLEXPORT void set_val_to_ptr(int* ptr, int val); // ditto
  244. """
  245. from _rawffi.alt import CDLL, types
  246. libfoo = CDLL(self.libfoo_name)
  247. intptr = types.Pointer(types.sint)
  248. get_dummy = libfoo.getfunc('get_dummy', [], types.sint)
  249. get_dummy_ptr = libfoo.getfunc('get_dummy_ptr', [], intptr)
  250. set_val_to_ptr = libfoo.getfunc('set_val_to_ptr', [intptr, types.sint], types.void)
  251. assert get_dummy() == 0
  252. ptr = get_dummy_ptr()
  253. set_val_to_ptr(ptr, 123)
  254. assert get_dummy() == 123
  255. set_val_to_ptr(ptr, 0)
  256. def test_huge_pointer_args(self):
  257. """
  258. #include <stdlib.h>
  259. DLLEXPORT long is_null_ptr(void* ptr) { return ptr == NULL; }
  260. """
  261. import sys
  262. from _rawffi.alt import CDLL, types
  263. libfoo = CDLL(self.libfoo_name)
  264. is_null_ptr = libfoo.getfunc('is_null_ptr', [types.void_p], types.ulong)
  265. assert not is_null_ptr(sys.maxint+1)
  266. def test_unsigned_long_args(self):
  267. """
  268. DLLEXPORT unsigned long sum_xy_ul(unsigned long x, unsigned long y)
  269. {
  270. return x+y;
  271. }
  272. """
  273. import sys
  274. from _rawffi.alt import CDLL, types
  275. libfoo = CDLL(self.libfoo_name)
  276. sum_xy = libfoo.getfunc('sum_xy_ul', [types.ulong, types.ulong],
  277. types.ulong)
  278. assert sum_xy(sys.maxint, 12) == sys.maxint+12
  279. assert sum_xy(sys.maxint+1, 12) == sys.maxint+13
  280. #
  281. res = sum_xy(sys.maxint*2+3, 0)
  282. assert res == 1
  283. def test_unsigned_short_args(self):
  284. """
  285. DLLEXPORT unsigned short sum_xy_us(unsigned short x, unsigned short y)
  286. {
  287. return x+y;
  288. }
  289. """
  290. from _rawffi.alt import CDLL, types
  291. libfoo = CDLL(self.libfoo_name)
  292. sum_xy = libfoo.getfunc('sum_xy_us', [types.ushort, types.ushort],
  293. types.ushort)
  294. assert sum_xy(32000, 8000) == 40000
  295. assert sum_xy(60000, 30000) == 90000 % 65536
  296. def test_unsigned_byte_args(self):
  297. """
  298. DLLEXPORT unsigned char sum_xy_ub(unsigned char x, unsigned char y)
  299. {
  300. return x+y;
  301. }
  302. """
  303. from _rawffi.alt import CDLL, types
  304. libfoo = CDLL(self.libfoo_name)
  305. sum_xy = libfoo.getfunc('sum_xy_us', [types.ubyte, types.ubyte],
  306. types.ubyte)
  307. assert sum_xy(100, 40) == 140
  308. assert sum_xy(200, 60) == 260 % 256
  309. def test_unsigned_int_args(self):
  310. r"""
  311. DLLEXPORT unsigned int sum_xy_ui(unsigned int x, unsigned int y)
  312. {
  313. return x+y;
  314. }
  315. """
  316. import sys
  317. from _rawffi.alt import CDLL, types
  318. maxint32 = 2147483647
  319. libfoo = CDLL(self.libfoo_name)
  320. sum_xy = libfoo.getfunc('sum_xy_ui', [types.uint, types.uint],
  321. types.uint)
  322. assert sum_xy(maxint32, 1) == maxint32+1
  323. assert sum_xy(maxint32, maxint32+2) == 0
  324. def test_signed_byte_args(self):
  325. """
  326. DLLEXPORT signed char sum_xy_sb(signed char x, signed char y)
  327. {
  328. return x+y;
  329. }
  330. """
  331. from _rawffi.alt import CDLL, types
  332. libfoo = CDLL(self.libfoo_name)
  333. sum_xy = libfoo.getfunc('sum_xy_sb', [types.sbyte, types.sbyte],
  334. types.sbyte)
  335. assert sum_xy(10, 20) == 30
  336. assert sum_xy(100, 28) == -128
  337. def test_char_args(self):
  338. """
  339. DLLEXPORT char my_toupper(char x)
  340. {
  341. return x - ('a'-'A');
  342. }
  343. """
  344. from _rawffi.alt import CDLL, types
  345. libfoo = CDLL(self.libfoo_name)
  346. my_toupper = libfoo.getfunc('my_toupper', [types.char],
  347. types.char)
  348. assert my_toupper('c') == 'C'
  349. def test_unichar_args(self):
  350. """
  351. #include <stddef.h>
  352. DLLEXPORT wchar_t sum_xy_wc(wchar_t x, wchar_t y)
  353. {
  354. return x + y;
  355. }
  356. """
  357. from _rawffi.alt import CDLL, types
  358. libfoo = CDLL(self.libfoo_name)
  359. sum_xy = libfoo.getfunc('sum_xy_wc', [types.unichar, types.unichar],
  360. types.unichar)
  361. res = sum_xy(unichr(1000), unichr(2000))
  362. assert type(res) is unicode
  363. assert ord(res) == 3000
  364. def test_single_float_args(self):
  365. """
  366. DLLEXPORT float sum_xy_float(float x, float y)
  367. {
  368. return x+y;
  369. }
  370. """
  371. from _rawffi.alt import CDLL, types
  372. libfoo = CDLL(self.libfoo_name)
  373. sum_xy = libfoo.getfunc('sum_xy_float', [types.float, types.float],
  374. types.float)
  375. res = sum_xy(12.34, 56.78)
  376. assert res == self.f_12_34_plus_56_78
  377. def test_slonglong_args(self):
  378. """
  379. DLLEXPORT long long sum_xy_longlong(long long x, long long y)
  380. {
  381. return x+y;
  382. }
  383. """
  384. from _rawffi.alt import CDLL, types
  385. maxint32 = 2147483647 # we cannot really go above maxint on 64 bits
  386. # (and we would not test anything, as there long
  387. # is the same as long long)
  388. libfoo = CDLL(self.libfoo_name)
  389. sum_xy = libfoo.getfunc('sum_xy_longlong', [types.slonglong, types.slonglong],
  390. types.slonglong)
  391. x = maxint32+1
  392. y = maxint32+2
  393. res = sum_xy(x, y)
  394. expected = maxint32*2 + 3
  395. assert res == expected
  396. def test_ulonglong_args(self):
  397. """
  398. DLLEXPORT unsigned long long sum_xy_ulonglong(unsigned long long x,
  399. unsigned long long y)
  400. {
  401. return x+y;
  402. }
  403. """
  404. from _rawffi.alt import CDLL, types
  405. maxint64 = 9223372036854775807 # maxint64+1 does not fit into a
  406. # longlong, but it does into a
  407. # ulonglong
  408. libfoo = CDLL(self.libfoo_name)
  409. sum_xy = libfoo.getfunc('sum_xy_ulonglong', [types.ulonglong, types.ulonglong],
  410. types.ulonglong)
  411. x = maxint64+1
  412. y = 2
  413. res = sum_xy(x, y)
  414. expected = maxint64 + 3
  415. assert res == expected
  416. #
  417. res = sum_xy(maxint64*2+3, 0)
  418. assert res == 1
  419. def test_byval_argument(self):
  420. """
  421. struct Point {
  422. long x;
  423. long y;
  424. };
  425. DLLEXPORT long sum_point(struct Point p) {
  426. return p.x + p.y;
  427. }
  428. """
  429. from _rawffi.alt import CDLL, types, _StructDescr, Field
  430. Point = _StructDescr('Point', [
  431. Field('x', types.slong),
  432. Field('y', types.slong),
  433. ])
  434. libfoo = CDLL(self.libfoo_name)
  435. sum_point = libfoo.getfunc('sum_point', [Point.ffitype], types.slong)
  436. #
  437. p = Point.allocate()
  438. p.setfield('x', 30)
  439. p.setfield('y', 12)
  440. res = sum_point(p)
  441. assert res == 42
  442. def test_byval_result(self):
  443. """
  444. DLLEXPORT struct Point make_point(long x, long y) {
  445. struct Point p;
  446. p.x = x;
  447. p.y = y;
  448. return p;
  449. }
  450. """
  451. from _rawffi.alt import CDLL, types, _StructDescr, Field
  452. Point = _StructDescr('Point', [
  453. Field('x', types.slong),
  454. Field('y', types.slong),
  455. ])
  456. libfoo = CDLL(self.libfoo_name)
  457. make_point = libfoo.getfunc('make_point', [types.slong, types.slong],
  458. Point.ffitype)
  459. #
  460. p = make_point(12, 34)
  461. assert p.getfield('x') == 12
  462. assert p.getfield('y') == 34
  463. # XXX: long ago the plan was to kill _rawffi structures in favor of
  464. # _rawffi.alt structures. The plan never went anywhere, so we're
  465. # stuck with both.
  466. def test_byval_argument__rawffi(self):
  467. """
  468. // defined above
  469. struct Point;
  470. DLLEXPORT long sum_point(struct Point p);
  471. """
  472. import _rawffi
  473. from _rawffi.alt import CDLL, types
  474. POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')])
  475. ffi_point = POINT.get_ffi_type()
  476. libfoo = CDLL(self.libfoo_name)
  477. sum_point = libfoo.getfunc('sum_point', [ffi_point], types.slong)
  478. #
  479. p = POINT()
  480. p.x = 30
  481. p.y = 12
  482. res = sum_point(p)
  483. assert res == 42
  484. p.free()
  485. def test_byval_result__rawffi(self):
  486. """
  487. // defined above
  488. DLLEXPORT struct Point make_point(long x, long y);
  489. """
  490. import _rawffi
  491. from _rawffi.alt import CDLL, types
  492. POINT = _rawffi.Structure([('x', 'l'), ('y', 'l')])
  493. ffi_point = POINT.get_ffi_type()
  494. libfoo = CDLL(self.libfoo_name)
  495. make_point = libfoo.getfunc('make_point', [types.slong, types.slong], ffi_point)
  496. #
  497. p = make_point(12, 34)
  498. assert p.x == 12
  499. assert p.y == 34
  500. p.free()
  501. def test_TypeError_numargs(self):
  502. from _rawffi.alt import CDLL, types
  503. libfoo = CDLL(self.libfoo_name)
  504. sum_xy = libfoo.getfunc('sum_xy', [types.sint, types.sint], types.sint)
  505. raises(TypeError, "sum_xy(1, 2, 3)")
  506. raises(TypeError, "sum_xy(1)")
  507. def test_TypeError_voidarg(self):
  508. from _rawffi.alt import CDLL, types
  509. libfoo = CDLL(self.libfoo_name)
  510. raises(TypeError, "libfoo.getfunc('sum_xy', [types.void], types.sint)")
  511. def test_OSError_loading(self):
  512. from _rawffi.alt import CDLL, types
  513. raises(OSError, "CDLL('I do not exist')")
  514. def test_AttributeError_missing_function(self):
  515. from _rawffi.alt import CDLL, types
  516. libfoo = CDLL(self.libfoo_name)
  517. raises(AttributeError, "libfoo.getfunc('I_do_not_exist', [], types.void)")
  518. if self.iswin32:
  519. skip("unix specific")
  520. libnone = CDLL(None)
  521. raises(AttributeError, "libnone.getfunc('I_do_not_exist', [], types.void)")
  522. def test_calling_convention1(self):
  523. if not self.iswin32:
  524. skip("windows specific")
  525. from _rawffi.alt import WinDLL, types
  526. libm = WinDLL(self.libm_name)
  527. pow = libm.getfunc('pow', [types.double, types.double], types.double)
  528. try:
  529. pow(2, 3)
  530. except ValueError as e:
  531. assert e.message.startswith('Procedure called with')
  532. else:
  533. assert 0, 'test must assert, wrong calling convention'
  534. def test_calling_convention2(self):
  535. if not self.iswin32:
  536. skip("windows specific")
  537. from _rawffi.alt import WinDLL, types
  538. kernel = WinDLL('Kernel32.dll')
  539. sleep = kernel.getfunc('Sleep', [types.uint], types.void)
  540. sleep(10)
  541. def test_calling_convention3(self):
  542. if not self.iswin32:
  543. skip("windows specific")
  544. from _rawffi.alt import CDLL, types
  545. wrong_kernel = CDLL('Kernel32.dll')
  546. wrong_sleep = wrong_kernel.getfunc('Sleep', [types.uint], types.void)
  547. try:
  548. wrong_sleep(10)
  549. except ValueError as e:
  550. assert e.message.startswith('Procedure called with')
  551. else:
  552. assert 0, 'test must assert, wrong calling convention'
  553. def test_func_fromaddr2(self):
  554. if not self.iswin32:
  555. skip("windows specific")
  556. from _rawffi.alt import CDLL, types, FuncPtr
  557. from _rawffi import FUNCFLAG_STDCALL
  558. libm = CDLL(self.libm_name)
  559. pow_addr = libm.getaddressindll('pow')
  560. wrong_pow = FuncPtr.fromaddr(pow_addr, 'pow',
  561. [types.double, types.double], types.double, FUNCFLAG_STDCALL)
  562. try:
  563. wrong_pow(2, 3) == 8
  564. except ValueError as e:
  565. assert e.message.startswith('Procedure called with')
  566. else:
  567. assert 0, 'test must assert, wrong calling convention'
  568. def test_func_fromaddr3(self):
  569. if not self.iswin32:
  570. skip("windows specific")
  571. from _rawffi.alt import WinDLL, types, FuncPtr
  572. from _rawffi import FUNCFLAG_STDCALL
  573. kernel = WinDLL('Kernel32.dll')
  574. sleep_addr = kernel.getaddressindll('Sleep')
  575. sleep = FuncPtr.fromaddr(sleep_addr, 'sleep', [types.uint],
  576. types.void, FUNCFLAG_STDCALL)
  577. sleep(10)
  578. def test_by_ordinal(self):
  579. """
  580. int DLLEXPORT AAA_first_ordinal_function()
  581. {
  582. return 42;
  583. }
  584. """
  585. if not self.iswin32:
  586. skip("windows specific")
  587. from _rawffi.alt import CDLL, types
  588. libfoo = CDLL(self.libfoo_name)
  589. f_name = libfoo.getfunc('AAA_first_ordinal_function', [], types.sint)
  590. f_ordinal = libfoo.getfunc(1, [], types.sint)
  591. assert f_name.getaddr() == f_ordinal.getaddr()