GitLab will be upgraded on 30 Jan 2023 from 2.00 pm (AEDT) to 3.00 pm (AEDT). During the update, GitLab and Mattermost services will not be available. If you have any concerns with this, please talk to us at N110 (b) CSIT building.

callback.py 1.8 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#!/usr/bin/env python3
# Copyright 2017 The Australian National University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

def get_callback(libcb_path):
    from rpython.rtyper.lltypesystem import rffi
    import py
    import sys

    libcb = py.path.local(libcb_path)
    libext = '.dylib' if sys.platform == 'darwin' else '.so'
    libname = libcb.basename[3:libcb.basename.index(libext)]

    eci = rffi.ExternalCompilationInfo(libraries=[libname], library_dirs=[libcb.dirname])
    def external(name, args, result, compilation_info=eci, **kwds):
        return rffi.llexternal(name, args, result,
                               compilation_info=compilation_info,
                               _nowrapper=True, **kwds)

    c_cb_init = external("cb_init", [rffi.CCHARP], rffi.VOIDP)
    c_cb_begin = external("cb_begin", [rffi.VOIDP], rffi.lltype.Void)
    c_cb_end = external("cb_end", [rffi.VOIDP], rffi.lltype.Void)
    c_cb_report = external("cb_report", [rffi.VOIDP], rffi.lltype.Void)

    class Callback:
        def __init__(self, param_s):
            with rffi.scoped_str2charp(param_s) as buf:
                self.cb = c_cb_init(buf)

        def begin(self):
            c_cb_begin(self.cb)

        def end(self):
            c_cb_end(self.cb)

        def report(self):
            c_cb_report(self.cb)

    return Callback