1import sys 2# 3# This is only a demo based on the GMP library. 4# There is a rather more complete (but perhaps outdated) version available at: 5# http://bazaar.launchpad.net/~tolot-solar-empire/+junk/gmpy_cffi/files 6# 7 8try: 9 from _gmp_cffi import ffi, lib 10except ImportError: 11 print 'run gmp_build first, then make sure the shared object is on sys.path' 12 sys.exit(1) 13 14# ffi "knows" about the declared variables and functions from the 15# cdef parts of the module created from gmp_build 16# lib "knows" how to call the functions from the set_source parts 17# of the module. 18 19# ____________________________________________________________ 20 21a = ffi.new("mpz_t") 22b = ffi.new("mpz_t") 23 24if len(sys.argv) < 3: 25 print 'call as %s bigint1, bigint2' % sys.argv[0] 26 sys.exit(2) 27 28lib.mpz_init_set_str(a, sys.argv[1], 10) # Assume decimal integers 29lib.mpz_init_set_str(b, sys.argv[2], 10) # Assume decimal integers 30lib.mpz_add(a, a, b) # a=a+b 31 32s = lib.mpz_get_str(ffi.NULL, 10, a) 33print ffi.string(s) 34