Source

cffi / cffi / verifier.py

import os
from . import ffiplatform

class Verifier(object):

    def __init__(self, ffi):
        self.ffi = ffi

    def write(self, what):
        print >> self.f, '  ' + what

    def write_printf(self, what, *args):
        self.has_printf = True
        if not args:
            print >> self.f, '  printf("%s\\n");' % (what,)
        else:
            print >> self.f, '  printf("%s\\n", %s);' % (
                what, ', '.join(args))

    def verify(self, preamble, **kwargs):
        tst_file_base = ffiplatform._get_test_file_base()
        self.has_printf = False
        with open(tst_file_base + '.c', 'w') as f:
            f.write("""\
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <unistd.h>

#define __sameconstant__(x, y) \\
  { int result[1-2*((x)-(y))*((x)-(y))]; }

#define __sametype__(ppresult, typename) \\
  { ppresult = (typename**)0; }

""")
            f.write(preamble + "\n\n")
            f.write('int main() {\n')
            self.f = f
            for name, tp in self.ffi._parser._declarations.iteritems():
                kind, realname = name.split(' ', 1)
                method = getattr(tp, 'verifier_declare_' + kind)
                method(self, realname)
            del self.f
            f.write('  return 0;\n')
            f.write('}\n')
        err = os.system('gcc -Werror -S %s.c -o %s.s' %
                        (tst_file_base, tst_file_base))
        if err:
            raise ffiplatform.VerificationError(
                '%s.c: see compilation warnings and errors above' %
                (tst_file_base,))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.