Example #1
0
    def __init__(self, geom_input, srs=None):
        "Initializes Geometry on either WKT or an OGR pointer as input."

        str_instance = isinstance(geom_input, basestring)

        # If HEX, unpack input to to a binary buffer.
        if str_instance and hex_regex.match(geom_input):
            geom_input = buffer(a2b_hex(geom_input.upper()))
            str_instance = False

        # Constructing the geometry,
        if str_instance:
            # Checking if unicode
            if isinstance(geom_input, unicode):
                # Encoding to ASCII, WKT or HEX doesn't need any more.
                geom_input = geom_input.encode('ascii')

            wkt_m = wkt_regex.match(geom_input)
            json_m = json_regex.match(geom_input)
            if wkt_m:
                if wkt_m.group('type').upper() == 'LINEARRING':
                    # OGR_G_CreateFromWkt doesn't work with LINEARRING WKT.
                    #  See http://trac.osgeo.org/gdal/ticket/1992.
                    g = capi.create_geom(OGRGeomType(wkt_m.group('type')).num)
                    capi.import_wkt(g, byref(c_char_p(geom_input)))
                else:
                    g = capi.from_wkt(byref(c_char_p(geom_input)), None, byref(c_void_p()))
            elif json_m:
                if GEOJSON:
                    g = capi.from_json(geom_input)
                else:
                    raise NotImplementedError('GeoJSON input only supported on GDAL 1.5+.')
            else:
                # Seeing if the input is a valid short-hand string
                # (e.g., 'Point', 'POLYGON').
                ogr_t = OGRGeomType(geom_input)
                g = capi.create_geom(OGRGeomType(geom_input).num)
        elif isinstance(geom_input, buffer):
            # WKB was passed in
            g = capi.from_wkb(str(geom_input), None, byref(c_void_p()), len(geom_input))
        elif isinstance(geom_input, OGRGeomType):
            # OGRGeomType was passed in, an empty geometry will be created.
            g = capi.create_geom(geom_input.num)
        elif isinstance(geom_input, self.ptr_type):
            # OGR pointer (c_void_p) was the input.
            g = geom_input
        else:
            raise OGRException('Invalid input type for OGR Geometry construction: %s' % type(geom_input))

        # Now checking the Geometry pointer before finishing initialization
        # by setting the pointer for the object.
        if not g:
            raise OGRException('Cannot create OGR Geometry from input: %s' % str(geom_input))
        self.ptr = g

        # Assigning the SpatialReference object to the geometry, if valid.
        if bool(srs): self.srs = srs

        # Setting the class depending upon the OGR Geometry Type
        self.__class__ = GEO_CLASSES[self.geom_type.num]
Example #2
0
 def __init__(self, type=None, label=""):
     self._w = ctypes.c_void_p()
     if type is not None:
         _check_result(gp.gp_widget_new(int(type), str(label), byref(self._w)))
         _check_result(gp.gp_widget_ref(self._w))
     else:
         self._w = ctypes.c_void_p()
Example #3
0
    def on_display(self):
        """
        Rendering callback.
        """
        self._camera.render()

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)

        self._scale += 0.1
        pipeline = Pipeline(rotation=[0, self._scale, 0],
                            translation=[0, 0, 6],
                            projection=self._projection)
        pipeline.set_camera(self._camera)

        self._effect.set_wvp(pipeline.get_wvp())
        self._effect.set_directional_light(
            self._dir_light_color, self._dir_light_ambient_intensity)

        position, tex_coord = 0, 1
        glEnableVertexAttribArray(position)
        glEnableVertexAttribArray(tex_coord)

        glBindBuffer(GL_ARRAY_BUFFER, self._vbo)
        glVertexAttribPointer(position, 3, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(0))
        glVertexAttribPointer(tex_coord, 2, GL_FLOAT, GL_FALSE, 20, ctypes.c_void_p(12))
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self._ibo)

        self._texture.bind(GL_TEXTURE0)
        glDrawElements(GL_TRIANGLES, 18, GL_UNSIGNED_INT, ctypes.c_void_p(0))
        glDisableVertexAttribArray(position)
        glDisableVertexAttribArray(tex_coord)
        glutSwapBuffers()
Example #4
0
def runloop(depth=None, rgb=None):
    """Sets up the kinect and maintains a runloop

    This is where most of the action happens.  You can get the dev pointer from the callback
    and let this function do all of the setup for you.  You may want to use threads to perform
    computation externally as the callbacks should really just be used for copying data.

    Args:
        depth: A function that takes (dev, depth, timestamp), corresponding to C function.
            If None (default), then you won't get a callback for depth.
        rgb: A function that takes (dev, rgb, timestamp), corresponding to C function.
            If None (default), then you won't get a callback for rgb.
    """
    depth = depth_cb(depth if depth else lambda *x: None)
    rgb = rgb_cb(rgb if rgb else lambda *x: None)
    ctx = ctypes.c_void_p()
    if init(ctypes.byref(ctx), 0) < 0:
        print('Error: Cant open')
    dev = ctypes.c_void_p()
    if open_device(ctx, ctypes.byref(dev), 0) < 0:
        print('Error: Cant open')
    set_depth_format(dev, 0)
    set_depth_callback(dev, depth)
    start_depth(dev)
    set_rgb_format(dev, FORMAT_RGB)
    set_rgb_callback(dev, rgb)
    start_rgb(dev)
    while process_events(ctx) >= 0:
        pass
Example #5
0
def create_cairo_font_face_for_file(filename, faceindex=0, loadoptions=0):
    global _initialized
    global _freetype_so
    global _cairo_so
    global _ft_lib
    global _surface

    CAIRO_STATUS_SUCCESS = 0
    FT_Err_Ok = 0
    if not _initialized:
        # find shared objects
        _freetype_so = ctypes.CDLL("libfreetype.so.6")
        _cairo_so = ctypes.CDLL("libcairo.so.2")
        # initialize freetype
        _ft_lib = ctypes.c_void_p()
#        if FT_Err_Ok != _freetype_so.FT_Init_FreeType(ctypes.byref(_ft_lib)):
#          raise "Error initialising FreeType library."
        _surface = cairo.ImageSurface(cairo.FORMAT_A8, 0, 0)
        _initialized = True
    # create freetype face
    ft_face = ctypes.c_void_p()
    cairo_ctx = cairo.Context(_surface)
    cairo_t = PycairoContext.from_address(id(cairo_ctx)).ctx
    _cairo_so.cairo_ft_font_face_create_for_ft_face.restype = ctypes.c_void_p
    if FT_Err_Ok != _freetype_so.FT_New_Face(_ft_lib, filename, faceindex, ctypes.byref(ft_face)):
        raise "Error creating FreeType font face for " + filename
    # create cairo font face for freetype face
    cr_face = _cairo_so.cairo_ft_font_face_create_for_ft_face(ft_face, loadoptions)
    if CAIRO_STATUS_SUCCESS != _cairo_so.cairo_font_face_status(cr_face):
        raise "Error creating cairo font face for " + filename
    _cairo_so.cairo_set_font_face(cairo_t, cr_face)
    if CAIRO_STATUS_SUCCESS != _cairo_so.cairo_status(cairo_t):
        raise "Error creating cairo font face for " + filename
    face = cairo_ctx.get_font_face()
    return face
Example #6
0
def init():
    global vao, vertexBufferObject, indexBufferObject
    InitializeProgram()
    InitializeVertexBuffer()
    
    n = 1
    vao = arrays.GLuintArray.zeros((n,))
    glBindVertexArray( vao )
    
    colorDataOffset = sizeOfFloat * 3 * numberOfVertices
    glBindBuffer(GL_ARRAY_BUFFER, vertexBufferObject)
    glEnableVertexAttribArray(0)
    glEnableVertexAttribArray(1)
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, c_void_p(0))
    glVertexAttribPointer(1, 4, GL_FLOAT, GL_FALSE, 0, c_void_p(colorDataOffset))
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferObject)

    glBindVertexArray(0)

    glEnable(GL_CULL_FACE)
    glCullFace(GL_BACK)
    glFrontFace(GL_CW)
    
    glEnable(GL_DEPTH_TEST)
    glDepthMask(GL_TRUE)
    glDepthFunc(GL_LEQUAL)
    glDepthRange(0.0, 1.0)
Example #7
0
    def _create_bitmap(self, width, height):
        self._data = (ctypes.c_byte * (4 * width * height))()
        self._bitmap = ctypes.c_void_p()
        self._format = PixelFormat32bppARGB 
        gdiplus.GdipCreateBitmapFromScan0(width, height, width * 4,
            self._format, self._data, ctypes.byref(self._bitmap))

        self._graphics = ctypes.c_void_p()
        gdiplus.GdipGetImageGraphicsContext(self._bitmap,
            ctypes.byref(self._graphics))
        gdiplus.GdipSetPageUnit(self._graphics, UnitPixel)

        self._dc = user32.GetDC(0)
        gdi32.SelectObject(self._dc, self.font.hfont)

        gdiplus.GdipSetTextRenderingHint(self._graphics,
            TextRenderingHintAntiAliasGridFit)


        self._brush = ctypes.c_void_p()
        gdiplus.GdipCreateSolidFill(0xffffffff, ctypes.byref(self._brush))

        
        self._matrix = ctypes.c_void_p()
        gdiplus.GdipCreateMatrix(ctypes.byref(self._matrix))

        self._flags = (DriverStringOptionsCmapLookup |
                       DriverStringOptionsRealizedAdvance)

        self._rect = Rect(0, 0, width, height)

        self._bitmap_height = height
Example #8
0
def load_module_image(context, image):
    """
    image must be a pointer
    """
    logsz = os.environ.get('NUMBAPRO_CUDA_LOG_SIZE', 1024)

    jitinfo = (c_char * logsz)()
    jiterrors = (c_char * logsz)()

    options = {
        enums.CU_JIT_INFO_LOG_BUFFER: addressof(jitinfo),
        enums.CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES: c_void_p(logsz),
        enums.CU_JIT_ERROR_LOG_BUFFER: addressof(jiterrors),
        enums.CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES: c_void_p(logsz),
        enums.CU_JIT_LOG_VERBOSE: c_void_p(VERBOSE_JIT_LOG),
    }

    option_keys = (drvapi.cu_jit_option * len(options))(*options.keys())
    option_vals = (c_void_p * len(options))(*options.values())

    handle = drvapi.cu_module()
    try:
        driver.cuModuleLoadDataEx(byref(handle), image, len(options),
                                  option_keys, option_vals)
    except CudaAPIError as e:
        msg = "cuModuleLoadDataEx error:\n%s" % jiterrors.value.decode("utf8")
        raise CudaAPIError(e.code, msg)

    info_log = jitinfo.value

    return Module(weakref.proxy(context), handle, info_log,
                  _module_finalizer(context, handle))
Example #9
0
    def _parse_script_result(self, ptr):
        restype = ctypes.c_int()
        _call("wdGetScriptResultType", ptr, ctypes.byref(restype))
        restype = restype.value

        # See webdriver.cpp
        # FIXME: I'm *guessing* 6 is a string, see wdExecuteScript in
        # webdriver.cpp
        if restype in (1, 6): # String
            voidp = ctypes.c_void_p()
            _call("wdGetStringScriptResult", ptr, ctypes.byref(voidp))
            return self._extract_string(voidp)
        elif restype == 2: # Integer
            n = ctypes.c_int()
            _call("wdGetNumberScriptResult", ptr, ctypes.byref(n))
            return n.value
        elif restype == 3: # Boolean
            n = ctypes.c_int()
            _call("wdGetBooleanScriptResult", ptr, ctypes.byref(n))
            return bool(n)
        elif restype == 4: # WebElement
            element = ctypes.c_void_p()
            _call("wdGetElementScriptResult" ,ptr, ctypes.byref(element))
            return WebElement(ptr, self)
        elif restype == 5: # FIXME: None?
            return None
        elif restype == 7: # Double
            n = ctypes.c_double()
            _call("wdGetDoubleScriptResult", ptr, ctypes.byref(n))
            return n.value
        else:
            raise ValueError("Unknown result type - %d" % restype)
Example #10
0
 def _cleanup(self):
     if self.qmh:
         self.lib.quvi_parse_close(byref(self.qmh))
         self.qmh = c_void_p()
     if self.qh:
         self.lib.quvi_close(byref(self.qh))
         self.qh = c_void_p()
Example #11
0
    def __init__(self):
        logsz = int(os.environ.get('NUMBAPRO_CUDA_LOG_SIZE', 1024))
        linkerinfo = (c_char * logsz)()
        linkererrors = (c_char * logsz)()

        options = {
            enums.CU_JIT_INFO_LOG_BUFFER: addressof(linkerinfo),
            enums.CU_JIT_INFO_LOG_BUFFER_SIZE_BYTES: c_void_p(logsz),
            enums.CU_JIT_ERROR_LOG_BUFFER: addressof(linkererrors),
            enums.CU_JIT_ERROR_LOG_BUFFER_SIZE_BYTES: c_void_p(logsz),
            enums.CU_JIT_LOG_VERBOSE: c_void_p(1),
        }

        raw_keys = list(options.keys()) + [enums.CU_JIT_TARGET_FROM_CUCONTEXT]
        raw_values = list(options.values())
        del options

        option_keys = (drvapi.cu_jit_option * len(raw_keys))(*raw_keys)
        option_vals = (c_void_p * len(raw_values))(*raw_values)

        self.handle = handle = drvapi.cu_link_state()
        driver.cuLinkCreate(len(raw_keys), option_keys, option_vals,
                            byref(self.handle))

        self.finalizer = lambda: driver.cuLinkDestroy(handle)

        self.linker_info_buf = linkerinfo
        self.linker_errors_buf = linkererrors

        self._keep_alive = [linkerinfo, linkererrors, option_keys, option_vals]
def _read_metadata(bitmap):
    metadata = {}
    models = [(name[5:], number) for name, number in
        METADATA_MODELS.__dict__.items() if name.startswith('FIMD_')]

    tag = ctypes.c_void_p()
    for model_name, number in models:
        mdhandle = _FI.FreeImage_FindFirstMetadata(number, bitmap,
                                                   ctypes.byref(tag))
        mdhandle = ctypes.c_void_p(mdhandle)
        if mdhandle:
            more = True
            while more:
                tag_name = asstr(_FI.FreeImage_GetTagKey(tag))
                tag_type = _FI.FreeImage_GetTagType(tag)
                byte_size = _FI.FreeImage_GetTagLength(tag)
                char_ptr = ctypes.c_char * byte_size
                tag_str = char_ptr.from_address(_FI.FreeImage_GetTagValue(tag))
                if tag_type == METADATA_DATATYPE.FIDT_ASCII:
                    tag_val = asstr(tag_str.value)
                else:
                    tag_val = numpy.fromstring(tag_str,
                            dtype=METADATA_DATATYPE.dtypes[tag_type])
                    if len(tag_val) == 1:
                        tag_val = tag_val[0]
                metadata[(model_name, tag_name)] = tag_val
                more = _FI.FreeImage_FindNextMetadata(mdhandle, ctypes.byref(tag))
            _FI.FreeImage_FindCloseMetadata(mdhandle)
    return metadata
Example #13
0
def createTopAndFrontMaps(raw, num, top_flip, front_flip, top_paras, front_paras, c_lib_path):
	[TOP_X_MIN, TOP_X_MAX, TOP_Y_MIN, 
	TOP_Y_MAX, TOP_Z_MIN, TOP_Z_MAX, 
	TOP_X_DIVISION, TOP_Y_DIVISION, TOP_Z_DIVISION, 
	Xn, Yn, Zn] = top_paras

	[FRONT_PHI_MIN, FRONT_PHI_MAX, 
	FRONT_THETA_MIN, FRONT_THETA_MAX, 
	FRONT_PHI_DIVISION, FRONT_THETA_DIVISION, 
	Rn, Cn, Fn] = front_paras

	# create a handle to LidarPreprocess.c
	SharedLib = ctypes.cdll.LoadLibrary(c_lib_path)

	# call the C function to create top view maps
	# The np array indata will be edited by createTopViewMaps to populate it with the 8 top view maps 
	SharedLib.createTopAndFrontMaps(ctypes.c_void_p(raw.ctypes.data),
								ctypes.c_int(num),
								ctypes.c_void_p(top_flip.ctypes.data), 
								ctypes.c_void_p(front_flip.ctypes.data),
								ctypes.c_float(TOP_X_MIN), ctypes.c_float(TOP_X_MAX), 
								ctypes.c_float(TOP_Y_MIN), ctypes.c_float(TOP_Y_MAX), 
								ctypes.c_float(TOP_Z_MIN), ctypes.c_float(TOP_Z_MAX), 
								ctypes.c_float(TOP_X_DIVISION), ctypes.c_float(TOP_Y_DIVISION), ctypes.c_float(TOP_Z_DIVISION), 
								ctypes.c_int(Xn), ctypes.c_int(Yn), ctypes.c_int(Zn),
								ctypes.c_float(FRONT_PHI_MIN), ctypes.c_float(FRONT_PHI_MAX), 
								ctypes.c_float(FRONT_THETA_MIN), ctypes.c_float(FRONT_THETA_MAX), 
								ctypes.c_float(FRONT_PHI_DIVISION), ctypes.c_float(FRONT_THETA_DIVISION),
								ctypes.c_float(Cn), ctypes.c_float(Rn), ctypes.c_float(Fn)
								)	
Example #14
0
def recognize(handle):
    global g_libtesseract
    assert(g_libtesseract)

    return g_libtesseract.TessBaseAPIRecognize(
        ctypes.c_void_p(handle), ctypes.c_void_p(None)
    )
def _process_multipage(filename, flags, process_func):
    filename = asbytes(filename)
    ftype = _FI.FreeImage_GetFileType(filename, 0)
    if ftype == -1:
        raise ValueError('Cannot determine type of file %s' % filename)
    create_new = False
    read_only = True
    keep_cache_in_memory = True
    multibitmap = _FI.FreeImage_OpenMultiBitmap(ftype, filename, create_new,
                                                read_only, keep_cache_in_memory,
                                                flags)
    multibitmap = ctypes.c_void_p(multibitmap)
    if not multibitmap:
        raise ValueError('Could not open %s as multi-page image.' % filename)
    try:
        pages = _FI.FreeImage_GetPageCount(multibitmap)
        out = []
        for i in range(pages):
            bitmap = _FI.FreeImage_LockPage(multibitmap, i)
            bitmap = ctypes.c_void_p(bitmap)
            if not bitmap:
                raise ValueError('Could not open %s as a multi-page image.'
                                  % filename)
            try:
                out.append(process_func(bitmap))
            finally:
                _FI.FreeImage_UnlockPage(multibitmap, bitmap, False)
        return out
    finally:
        _FI.FreeImage_CloseMultiBitmap(multibitmap, 0)
Example #16
0
def call_pipe(app_data):
    rows = app_data['R']
    cols = app_data['C']

    app_args = app_data['app_args']
    patch_size = int(app_args.patch_size)
    search_area = int(app_args.search_area)

    img_data = app_data['img_data']
    INL = img_data['INL']
    INR = img_data['INR']
    OUT = img_data['OUT']

    # lib function name
    func_name = 'pipeline_'+app_data['app']
    pipe_func = app_data[func_name]

    # lib function args
    pipe_args = []
    pipe_args += [ctypes.c_int(cols)]
    pipe_args += [ctypes.c_int(rows)]
    pipe_args += [ctypes.c_void_p(INL.ctypes.data)]
    pipe_args += [ctypes.c_void_p(INR.ctypes.data)]
    pipe_args += [ctypes.c_void_p(OUT.ctypes.data)]

    # call lib function
    pipe_func(*pipe_args)
    
    return
Example #17
0
    def __init__(
        self,
        callbacks,
        cache_location="",
        settings_location="",
        app_key=None,
        user_agent=None,
        compress_playlists=False,
        dont_save_metadata_for_playlists=False,
        initially_unload_playlists=False,
        device_id=None,
        proxy=None,
        proxy_username=None,
        proxy_password=None,
        ca_certs_filename=None,
        tracefile=None,
    ):
        # Low level interface
        self.__session_interface = _session.SessionInterface()

        # Callback managers
        self._user_callbacks = spotify.CallbackQueueManager()
        self._metadata_callbacks = spotify.CallbackQueueManager()

        # prepare callbacks
        self.__callback_manager = spotify.CallbackManager()
        self.__callbacks = ProxySessionCallbacks(self, callbacks, self.__callback_manager)

        # app key conversion
        appkey_c = (ctypes.c_byte * len(app_key))(*app_key)

        args = [
            self.api_version,
            cache_location,
            settings_location,
            appkey_c,
            ctypes.sizeof(appkey_c),
            user_agent,
            ctypes.pointer(self.__callbacks.get_callback_struct()),
            ctypes.c_void_p(),
            compress_playlists,
            dont_save_metadata_for_playlists,
            initially_unload_playlists,
            device_id,
            proxy,
            proxy_username,
            proxy_password,
            tracefile,
        ]

        # Linux builds have an extra member just before tracefile
        if is_linux():
            args.insert(-1, ca_certs_filename)

        # initialize app config
        config = _session.config(*args)

        self.__session_struct = ctypes.c_void_p()
        err = self.__session_interface.create(ctypes.byref(config), ctypes.byref(self.__session_struct))
        spotify.handle_sp_error(err)
Example #18
0
def calc_norm(U_, app_data):
    N = app_data['N']

    grid_data = app_data['grid_data']
    F_ = grid_data['F_']
    U_EXACT_ = grid_data['U_EXACT_']

    # lib function name
    norm = app_data['pipeline_norm']

    resid = np.zeros((1), np.float64)
    err   = np.zeros((1), np.float64)

    # lib function args
    norm_args = []
    norm_args += [ctypes.c_int(N)]
    norm_args += [ctypes.c_void_p(F_.ctypes.data)]
    norm_args += [ctypes.c_void_p(U_EXACT_.ctypes.data)]
    norm_args += [ctypes.c_void_p(U_.ctypes.data)]
    norm_args += [ctypes.c_void_p(err.ctypes.data)]
    norm_args += [ctypes.c_void_p(resid.ctypes.data)]

    # call lib function
    norm(*norm_args)

    # save the old norm values
    app_data['old_residual'] = app_data['resid']
    app_data['old_err'] = app_data['err']

    # register the norm values in the data dictionary
    app_data['resid'] = resid[0]
    app_data['err'] = err[0]

    return
Example #19
0
def backward(heads, head_grads=None, retain_graph=False, train_mode=True): #pylint: disable=redefined-outer-name
    """Compute the gradients of heads w.r.t previously marked variables.

    Parameters
    ----------
    heads: NDArray or list of NDArray
        Output NDArray(s)
    head_grads: NDArray or list of NDArray or None
        Gradients with respect to heads.
    train_mode: bool, optional
        Whether to do backward for training or predicting.
    """
    head_handles, hgrad_handles = _parse_head(heads, head_grads)

    check_call(_LIB.MXAutogradBackwardEx(
        len(head_handles),
        head_handles,
        hgrad_handles,
        0,
        ctypes.c_void_p(0),
        ctypes.c_int(retain_graph),
        ctypes.c_int(0),
        ctypes.c_int(train_mode),
        ctypes.c_void_p(0),
        ctypes.c_void_p(0)))
Example #20
0
    def generateAttachmentTexture(self, depth, stencil):
        # what enum to use?
        attachment_type = GL_RGB
        if not depth and not stencil:
            #attachment_type = GL_RGB
            pass
        elif depth and not stencil:
            attachment_type = GL_DEPTH_COMPONENT
        elif not depth and stencil:
            attachment_type = GL_STENCIL_INDEX

        # generate texture ID and load texture data
        textureId = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D, textureId)
        if not depth and not stencil:
            glTexImage2D(GL_TEXTURE_2D, 0, attachment_type, self.width(), self.height(), 0,
                         attachment_type, GL_UNSIGNED_BYTE, ctypes.c_void_p(0))
        else:
            # Using both a stencil and depth test, needs special format arguments
            glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8, self.width(), self.height(), 0,
                         GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, ctypes.c_void_p(0))
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glBindTexture(GL_TEXTURE_2D, 0)
        return textureId
Example #21
0
    def write(self, audio_data, length):
        # Pass audio_data=None to write silence
        if length == 0:
            return 0

        self.lock()

        p1 = ctypes.c_void_p()
        l1 = lib.DWORD()
        p2 = ctypes.c_void_p()
        l2 = lib.DWORD()
        self._buffer.Lock(self._write_cursor_ring, length,
                          ctypes.byref(p1), l1, ctypes.byref(p2), l2, 0)
        assert length == l1.value + l2.value

        if audio_data:
            ctypes.memmove(p1, audio_data.data, l1.value)
            audio_data.consume(l1.value, self.source_group.audio_format)
            if l2.value:
                ctypes.memmove(p2, audio_data.data, l2.value)
                audio_data.consume(l2.value, self.source_group.audio_format)
        else:
            ctypes.memset(p1, 0, l1.value)
            if l2.value:
                ctypes.memset(p2, 0, l2.value)
        self._buffer.Unlock(p1, l1, p2, l2)

        self._write_cursor += length
        self._write_cursor_ring += length
        self._write_cursor_ring %= self._buffer_size
        self.unlock()
Example #22
0
def _handler_keymaps(context, handlers):
    if not handlers.first:
        return []

    wm = context.window_manager
    keymaps = []
    handler_ptr = ct.cast(ct.c_void_p(handlers.first),
                          ct.POINTER(structures.wmEventHandler))
    while handler_ptr:
        handler = handler_ptr.contents
        if ord(handler.flag) & structures.WM_HANDLER_DO_FREE:
            pass
        else:
            if handler.keymap:
                if handler.keymap:
                    km = ct.cast(ct.c_void_p(handler.keymap),
                                 ct.POINTER(wmKeyMap)).contents
                    name = km.idname.decode()
                    keymap = wm.keyconfigs.user.keymaps.get(name)
                    if keymap:
                        keymaps.append(keymap.active())
                    else:
                        # ミス
                        raise ValueError()
        handler_ptr = handler.next
    return keymaps
Example #23
0
def glVertexAttribPointer(indx, size, type, normalized, stride, offset):
    if offset is None:
        offset = ctypes.c_void_p(0)
    elif isinstance(offset, (int, ctypes.c_int)):
        offset = ctypes.c_void_p(int(offset))
    return GL.glVertexAttribPointer(indx, size, type, normalized, stride,
                                    offset)
Example #24
0
 def createPulse(self,shot):
     """Create pulse.
     @param shot: Shot number to create
     @type shot: int
     @rtype: None
     """
     _treeshr=_mimport('_treeshr',1)
     import ctypes as _C
     from numpy import array
     try:
         Tree.lock()
         try:
             subtrees=self.getNodeWild('***','subtree')
             included=subtrees.nid_number.compress(subtrees.include_in_pulse)
             included=included.toList()
             included.insert(0,0)
             included=array(included)
             status = _treeshr.TreeCreatePulseFile(self.ctx,shot,len(included),_C.c_void_p(included.ctypes.data))
         except:
             status = _treeshr.TreeCreatePulseFile(self.ctx,shot,0,_C.c_void_p(0))
     finally:
         pass
         Tree.unlock()
     if not (status & 1):
         raise _treeshr.TreeException("Error creating pulse: %s" % (_mimport('_mdsshr',1).MdsGetMsg(status),))
def datetime_data(dtype):
    """Return (unit, numerator, denominator, events) from a datetime dtype
    """
    try:
        import ctypes
    except ImportError:
        raise RuntimeError, "Cannot access date-time internals without ctypes installed"

    if dtype.kind not in ['m','M']:
        raise ValueError, "Not a date-time dtype"
    
    obj = dtype.metadata[METADATA_DTSTR]
    class DATETIMEMETA(ctypes.Structure):
        _fields_ = [('base', ctypes.c_int),
                    ('num', ctypes.c_int),
                    ('den', ctypes.c_int),
                    ('events', ctypes.c_int)]

    func = ctypes.pythonapi.PyCObject_AsVoidPtr
    func.argtypes = [ctypes.py_object]
    func.restype = ctypes.c_void_p

    result = func(ctypes.py_object(obj))
    result = ctypes.cast(ctypes.c_void_p(result), ctypes.POINTER(DATETIMEMETA))

    struct = result[0]
    base = struct.base

    # FIXME: This needs to be kept consistent with enum in ndarrayobject.h
    from numpy.core.multiarray import DATETIMEUNITS
    obj = ctypes.py_object(DATETIMEUNITS)
    result = func(obj)
    _unitnum2name = ctypes.cast(ctypes.c_void_p(result), ctypes.POINTER(ctypes.c_char_p))

    return (_unitnum2name[base], struct.num, struct.den, struct.events)
Example #26
0
    def draw(self, modelview_matrix, color):
        self.program.bind()
        self.vertex_buffer.bind()

        # Size (in bytes) of GLfloat (32-bit floating point)
        sizeof_float = 4

        # Offset between two vertices in vertex buffer
        stride = (3 + 3) * sizeof_float

        # Offset (in a single vertex) of normal vector
        offset = 3 * sizeof_float

        vtxcoord_loc = self.program.attrib('vtxcoord')
        glEnableVertexAttribArray(vtxcoord_loc)
        glVertexAttribPointer(vtxcoord_loc, 3, GL_FLOAT, GL_FALSE, stride, c_void_p(0))

        normal_loc = self.program.attrib('normal')
        glEnableVertexAttribArray(normal_loc)
        glVertexAttribPointer(normal_loc, 3, GL_FLOAT, GL_FALSE, stride, c_void_p(offset))

        glUniformMatrix4fv(self.program.uniform('projection'), 1, GL_TRUE,
                to_glfloat(projection_matrix))
        glUniformMatrix4fv(self.program.uniform('modelview'), 1, GL_TRUE,
                to_glfloat(modelview_matrix))
        glUniform4f(self.program.uniform('color'), *color)

        glDrawArrays(GL_TRIANGLE_STRIP, 0, self.count)

        glDisableVertexAttribArray(normal_loc)
        glDisableVertexAttribArray(vtxcoord_loc)

        self.vertex_buffer.unbind()
        self.program.unbind()
Example #27
0
    def render(self):
        glBindBuffer(GL_ARRAY_BUFFER, self._vboid_vertices)
        if not self._vbo_initialized:
            # upload vbo contents to the graphics card
            self._vbo_initialized = True
            glBufferData(GL_ARRAY_BUFFER, sizeof(self._vbo_vertices), self._vbo_vertices, GL_STATIC_DRAW)
            #glBufferData(GL_ARRAY_BUFFER, sizeof(self._vbo_vertices), pointer(self._vbo_vertices), GL_STATIC_DRAW)
            #glBufferData(GL_ARRAY_BUFFER, sizeof(data), 0, GL_DYNAMIC_DRAW)
            #glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(data), data)

        v_offset = c_void_p(0)
        c_offset = c_void_p(12)
        n_offset = c_void_p(16)

        glVertexPointer(3, GL_FLOAT, sizeof(CubeVertex), v_offset);
        glColorPointer(4, GL_UNSIGNED_BYTE, sizeof(CubeVertex), c_offset);
        glNormalPointer(GL_FLOAT, sizeof(CubeVertex), n_offset);
        glEnableClientState(GL_VERTEX_ARRAY);
        glEnableClientState(GL_COLOR_ARRAY);
        glEnableClientState(GL_NORMAL_ARRAY);
        glDrawArrays(GL_TRIANGLES, 0, self._vbo_vertices_num);
        glDisableClientState(GL_NORMAL_ARRAY);
        glDisableClientState(GL_COLOR_ARRAY);
        glDisableClientState(GL_VERTEX_ARRAY);
        glBindBuffer(GL_ARRAY_BUFFER, 0)
Example #28
0
def vertexAttribPointer(indx, size, type, normalized, stride, offset):
    # --- gl es
    if offset is None:
        offset = ctypes.c_void_p(0)
    elif isinstance(offset, ctypes.c_void_p):
        pass
    elif isinstance(offset, (int, ctypes.c_int)):
        offset = ctypes.c_void_p(int(offset))
    else:
        if not offset.flags['C_CONTIGUOUS']:
            offset = offset.copy('C')
        offset_ = offset
        offset = offset.ctypes.data
        # We need to ensure that the data exists at draw time :(
        # PyOpenGL does this too
        key = '_vert_attr_'+str(indx)
        setattr(glVertexAttribPointer, key, offset_)
    ptr = offset
    ()
    # --- pyopengl
    if offset is None:
        offset = ctypes.c_void_p(0)
    elif isinstance(offset, (int, ctypes.c_int)):
        offset = ctypes.c_void_p(int(offset))
    ()
Example #29
0
    def __init__(self, name, size, bold=False, italic=False, dpi=None):
        if not name:
            name = self._default_name
        super(GDIPlusFont, self).__init__(name, size, bold, italic, dpi)

        family = ctypes.c_void_p()
        name = ctypes.c_wchar_p(name)

        # Look in private collection first:
        if self._private_fonts:
            gdiplus.GdipCreateFontFamilyFromName(name, self._private_fonts, ctypes.byref(family))

        # Then in system collection:
        if not family:
            gdiplus.GdipCreateFontFamilyFromName(name, None, ctypes.byref(family))

        # Nothing found, use default font.
        if not family:
            name = self._default_name
            gdiplus.GdipCreateFontFamilyFromName(ctypes.c_wchar_p(name), None, ctypes.byref(family))

        if dpi is None:
            unit = UnitPoint
        else:
            unit = UnitPixel
            size = (size * dpi) / 72

        style = 0
        if bold:
            style |= FontStyleBold
        if italic:
            style |= FontStyleItalic
        self.italic = italic  # XXX needed for HACK HACK HACK
        self._gdipfont = ctypes.c_void_p()
        gdiplus.GdipCreateFont(family, ctypes.c_float(size), style, unit, ctypes.byref(self._gdipfont))
Example #30
0
    def _fincore(fileobj):
        st_size = os.fstat(fileobj.fileno()).st_size

        if not st_size:
            return CacheStats(0, 0, ())

        pa = mmap(0, st_size, PROT_NONE, MAP_SHARED, fileobj.fileno(), 0)

        if pa == MAP_FAILED:
            raise ctypes_os_error("mmap('%s')" % path)

        # round the filesize to the nearest page size
        # note the use of integer division here
        total_pages = (st_size+PAGESIZE-1)//PAGESIZE

        try:
            vec = (ctypes.c_uint8*total_pages)()
            ret = mincore(ctypes.c_void_p(pa), ctypes.c_size_t(st_size), vec)
            if ret != 0:
                raise ctypes_os_error("mincore")
        finally:
            ret = munmap(ctypes.c_void_p(pa), ctypes.c_size_t(st_size))
            if ret != 0:
                raise ctypes_os_error("munmap")

        cached_count = 0
        cached_count = sum(1 for page in vec if page & 1)
        pages = ()
        if enumerate_pages:
            pages = tuple(offset
                          for offset, page in enumerate(vec)
                          if page & 1)
        return CacheStats(total_pages, cached_count, pages)
Example #31
0
 def __del__(self):
     if self._initialized is True:
         self.finalize()
         self._initialized = False
         self._libhandle = ctypes.c_void_p(0)
     self._lib.unload()
Example #32
0
    ]


pa_name = ctypes.util.find_library("portaudio")
pa = ctypes.CDLL(pa_name)

pa.Pa_GetDeviceInfo.restype = ctypes.POINTER(PaDeviceInfo)
pa.Pa_GetHostApiInfo.restype = ctypes.POINTER(PaHostApiInfo)
pa.Pa_GetVersionText.restype = ctypes.c_char_p

inputParameters = PaStreamParameters(
    device=0,
    channelCount=2,
    sampleFormat=2,
    suggestedLatency=0,  # format 2 is paInt32
    hostApiSpecificStreamInfo=ctypes.c_void_p())

outputParameters = PaStreamParameters(
    device=0,
    channelCount=2,
    sampleFormat=2,
    suggestedLatency=0,  # format 2 is paInt32
    hostApiSpecificStreamInfo=ctypes.c_void_p())

print 'Open', pa.Pa_Initialize()
try:
    print 'Version', pa.Pa_GetVersion()
    print 'Version Text', pa.Pa_GetVersionText()
    count = pa.Pa_GetDeviceCount()
    print 'NumDev', count
    for i in range(count):
 def void_ptr( self ):
     return ctypes.c_void_p( id(self) )
Example #34
0
    def __init__(self, geom_input, srs=None):
        "Initializes Geometry on either WKT or an OGR pointer as input."

        str_instance = isinstance(geom_input, six.string_types)

        # If HEX, unpack input to a binary buffer.
        if str_instance and hex_regex.match(geom_input):
            geom_input = six.memoryview(a2b_hex(geom_input.upper().encode()))
            str_instance = False

        # Constructing the geometry,
        if str_instance:
            wkt_m = wkt_regex.match(geom_input)
            json_m = json_regex.match(geom_input)
            if wkt_m:
                if wkt_m.group('srid'):
                    # If there's EWKT, set the SRS w/value of the SRID.
                    srs = int(wkt_m.group('srid'))
                if wkt_m.group('type').upper() == 'LINEARRING':
                    # OGR_G_CreateFromWkt doesn't work with LINEARRING WKT.
                    #  See http://trac.osgeo.org/gdal/ticket/1992.
                    g = capi.create_geom(OGRGeomType(wkt_m.group('type')).num)
                    capi.import_wkt(
                        g, byref(c_char_p(wkt_m.group('wkt').encode())))
                else:
                    g = capi.from_wkt(
                        byref(c_char_p(wkt_m.group('wkt').encode())), None,
                        byref(c_void_p()))
            elif json_m:
                g = capi.from_json(geom_input.encode())
            else:
                # Seeing if the input is a valid short-hand string
                # (e.g., 'Point', 'POLYGON').
                OGRGeomType(geom_input)
                g = capi.create_geom(OGRGeomType(geom_input).num)
        elif isinstance(geom_input, six.memoryview):
            # WKB was passed in
            g = capi.from_wkb(bytes(geom_input), None, byref(c_void_p()),
                              len(geom_input))
        elif isinstance(geom_input, OGRGeomType):
            # OGRGeomType was passed in, an empty geometry will be created.
            g = capi.create_geom(geom_input.num)
        elif isinstance(geom_input, self.ptr_type):
            # OGR pointer (c_void_p) was the input.
            g = geom_input
        else:
            raise GDALException(
                'Invalid input type for OGR Geometry construction: %s' %
                type(geom_input))

        # Now checking the Geometry pointer before finishing initialization
        # by setting the pointer for the object.
        if not g:
            raise GDALException('Cannot create OGR Geometry from input: %s' %
                                str(geom_input))
        self.ptr = g

        # Assigning the SpatialReference object to the geometry, if valid.
        if srs:
            self.srs = srs

        # Setting the class depending upon the OGR Geometry Type
        self.__class__ = GEO_CLASSES[self.geom_type.num]
Example #35
0
def _check_result (val, func, args):
    if val == 0:
        raise ValueError
    else:
        return ctypes.c_void_p(val)
Example #36
0
def enable_ansi_colors_win10():
    import ctypes

    # Function factory for errcheck callbacks that raise WinError on failure.
    def raise_if(error_result):
        def check(result, _func, args):
            if result == error_result:
                raise ctypes.WinError(ctypes.get_last_error())
            return args

        return check

    # Windows API types.
    from ctypes.wintypes import BOOL, DWORD, HANDLE, LPCWSTR, LPVOID
    LPDWORD = ctypes.POINTER(DWORD)

    # Generic constants.
    NULL = ctypes.c_void_p(0).value
    INVALID_HANDLE_VALUE = ctypes.c_void_p(-1).value
    ERROR_INVALID_PARAMETER = 87

    # CreateFile flags.
    # yapf: disable
    GENERIC_READ  = 0x80000000
    GENERIC_WRITE = 0x40000000
    FILE_SHARE_READ  = 0x01
    FILE_SHARE_WRITE = 0x02
    OPEN_EXISTING = 3
    # yapf: enable

    # Get/SetConsoleMode flags.
    ENABLE_VIRTUAL_TERMINAL_PROCESSING = 0x04

    kernel32 = ctypes.WinDLL('kernel32', use_last_error=True)

    # HANDLE CreateFileW(...)
    CreateFileW = kernel32.CreateFileW
    CreateFileW.restype = HANDLE
    CreateFileW.errcheck = raise_if(INVALID_HANDLE_VALUE)
    # yapf: disable
    CreateFileW.argtypes = (LPCWSTR,  # lpFileName
                            DWORD,    # dwDesiredAccess
                            DWORD,    # dwShareMode
                            LPVOID,   # lpSecurityAttributes
                            DWORD,    # dwCreationDisposition
                            DWORD,    # dwFlagsAndAttributes
                            HANDLE)   # hTemplateFile
    # yapf: enable

    # BOOL CloseHandle(HANDLE hObject)
    CloseHandle = kernel32.CloseHandle
    CloseHandle.restype = BOOL
    CloseHandle.errcheck = raise_if(False)
    CloseHandle.argtypes = (HANDLE, )

    # BOOL GetConsoleMode(HANDLE hConsoleHandle, LPDWORD lpMode)
    GetConsoleMode = kernel32.GetConsoleMode
    GetConsoleMode.restype = BOOL
    GetConsoleMode.errcheck = raise_if(False)
    GetConsoleMode.argtypes = (HANDLE, LPDWORD)

    # BOOL SetConsoleMode(HANDLE hConsoleHandle, DWORD dwMode)
    SetConsoleMode = kernel32.SetConsoleMode
    SetConsoleMode.restype = BOOL
    SetConsoleMode.errcheck = raise_if(False)
    SetConsoleMode.argtypes = (HANDLE, DWORD)

    # Open the console output device.
    conout = CreateFileW("CONOUT$", GENERIC_READ | GENERIC_WRITE,
                         FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
                         OPEN_EXISTING, 0, 0)

    # Get the current mode.
    mode = DWORD()
    GetConsoleMode(conout, ctypes.byref(mode))

    # Try to set the flag that controls ANSI escape code support.
    try:
        SetConsoleMode(conout, mode.value | ENABLE_VIRTUAL_TERMINAL_PROCESSING)
    except WindowsError as e:  # pylint:disable=undefined-variable
        if e.winerror == ERROR_INVALID_PARAMETER:
            return False  # Not supported, likely an older version of Windows.
        raise
    finally:
        CloseHandle(conout)

    return True
Example #37
0
    def _construct_args(self, kwargs) -> Tuple[Tuple[Any], Tuple[Any]]:
        """ Main function that controls argument construction for calling
            the C prototype of the SDFG.

            Organizes arguments first by `sdfg.arglist`, then data descriptors
            by alphabetical order, then symbols by alphabetical order.
        """
        # Return value initialization (for values that have not been given)
        self._initialize_return_values(kwargs)
        if self._return_arrays is not None:
            if len(self._retarray_shapes) == 1:
                kwargs[self._retarray_shapes[0][0]] = self._return_arrays
            else:
                for desc, arr in zip(self._retarray_shapes,
                                     self._return_arrays):
                    kwargs[desc[0]] = arr

        # Argument construction
        sig = self._sig
        typedict = self._typedict
        if len(kwargs) > 0:
            # Construct mapping from arguments to signature
            arglist = []
            argtypes = []
            argnames = []
            for a in sig:
                try:
                    arglist.append(kwargs[a])
                    argtypes.append(typedict[a])
                    argnames.append(a)
                except KeyError:
                    raise KeyError("Missing program argument \"{}\"".format(a))
        else:
            arglist = []
            argtypes = []
            argnames = []
            sig = []
        # Type checking
        for a, arg, atype in zip(argnames, arglist, argtypes):
            if not dtypes.is_array(arg) and isinstance(atype, dt.Array):
                if isinstance(arg, list):
                    print('WARNING: Casting list argument "%s" to ndarray' % a)
                elif arg is None:
                    # None values are passed as null pointers
                    pass
                else:
                    raise TypeError(
                        'Passing an object (type %s) to an array in argument "%s"'
                        % (type(arg).__name__, a))
            elif dtypes.is_array(arg) and not isinstance(atype, dt.Array):
                # GPU scalars are pointers, so this is fine
                if atype.storage != dtypes.StorageType.GPU_Global:
                    raise TypeError(
                        'Passing an array to a scalar (type %s) in argument "%s"'
                        % (atype.dtype.ctype, a))
            elif not isinstance(atype, dt.Array) and not isinstance(
                    atype.dtype, dtypes.callback) and not isinstance(
                        arg,
                        (atype.dtype.type,
                         sp.Basic)) and not (isinstance(arg, symbolic.symbol)
                                             and arg.dtype == atype.dtype):
                if isinstance(arg, int) and atype.dtype.type == np.int64:
                    pass
                elif isinstance(arg, float) and atype.dtype.type == np.float64:
                    pass
                elif (isinstance(arg, int) and atype.dtype.type == np.int32
                      and abs(arg) <= (1 << 31) - 1):
                    pass
                elif (isinstance(arg, int) and atype.dtype.type == np.uint32
                      and arg >= 0 and arg <= (1 << 32) - 1):
                    pass
                else:
                    print(
                        'WARNING: Casting scalar argument "%s" from %s to %s' %
                        (a, type(arg).__name__, atype.dtype.type))
            elif (isinstance(atype, dt.Array) and isinstance(arg, np.ndarray)
                  and atype.dtype.as_numpy_dtype() != arg.dtype):
                # Make exception for vector types
                if (isinstance(atype.dtype, dtypes.vector)
                        and atype.dtype.vtype.as_numpy_dtype() == arg.dtype):
                    pass
                else:
                    print(
                        'WARNING: Passing %s array argument "%s" to a %s array'
                        % (arg.dtype, a, atype.dtype.type.__name__))
            elif (isinstance(atype, dt.Array) and isinstance(arg, np.ndarray)
                  and arg.base is not None and not '__return' in a
                  and not Config.get_bool('compiler', 'allow_view_arguments')):
                raise TypeError(
                    'Passing a numpy view (e.g., sub-array or "A.T") to DaCe '
                    'programs is not allowed in order to retain analyzability. '
                    'Please make a copy with "numpy.copy(...)". If you know what '
                    'you are doing, you can override this error in the '
                    'configuration by setting compiler.allow_view_arguments '
                    'to True.')

        # Explicit casting
        for index, (arg, argtype) in enumerate(zip(arglist, argtypes)):
            # Call a wrapper function to make NumPy arrays from pointers.
            if isinstance(argtype.dtype, dtypes.callback):
                arglist[index] = argtype.dtype.get_trampoline(arg, kwargs)
            # List to array
            elif isinstance(arg, list) and isinstance(argtype, dt.Array):
                arglist[index] = np.array(arg, dtype=argtype.dtype.type)
            # Null pointer
            elif arg is None and isinstance(argtype, dt.Array):
                arglist[index] = ctypes.c_void_p(0)

        # Retain only the element datatype for upcoming checks and casts
        arg_ctypes = [t.dtype.as_ctypes() for t in argtypes]

        sdfg = self._sdfg

        # Obtain SDFG constants
        constants = sdfg.constants

        # Remove symbolic constants from arguments
        callparams = tuple(
            (arg, actype, atype)
            for arg, actype, atype in zip(arglist, arg_ctypes, argtypes)
            if not symbolic.issymbolic(arg) or (
                hasattr(arg, 'name') and arg.name not in constants))

        # Replace symbols with their values
        callparams = tuple(
            (actype(arg.get()), actype,
             atype) if isinstance(arg, symbolic.symbol) else (arg, actype,
                                                              atype)
            for arg, actype, atype in callparams)

        # Replace arrays with their base host/device pointers
        newargs = tuple(
            (ctypes.c_void_p(_array_interface_ptr(arg, atype)), actype,
             atype) if dtypes.is_array(arg) else (arg, actype, atype)
            for arg, actype, atype in callparams)

        initargs = tuple(atup for atup in callparams
                         if not dtypes.is_array(atup[0]))

        newargs = tuple(
            actype(arg) if (not isinstance(arg, ctypes._SimpleCData)) else arg
            for arg, actype, atype in newargs)

        initargs = tuple(
            actype(arg) if (not isinstance(arg, ctypes._SimpleCData)) else arg
            for arg, actype, atype in initargs)

        self._lastargs = newargs, initargs
        return self._lastargs
Example #38
0
 def _usleep(microseconds, obj=None):
     delay = ctypes.c_longlong(int(-microseconds * 10))
     _kernel32.SetWaitableTimer(obj, ctypes.byref(delay), 0,
                                ctypes.c_void_p(), ctypes.c_void_p(),
                                False)
     _kernel32.WaitForSingleObject(obj, 0xffffffff)
    def upload(self, program):
        """ Actual upload of data to GPU memory  """

        # If there is not data, there is no point in uploading
        if self._data is None:
            if self._show_warning_notset:
                logger.warn("Value for attribute '%s' is not set." % self.name)
                self._show_warning_notset = False
            return

        # Check active status (mandatory)
        if self._loc is None:
            raise VariableError("Attribute is not active")

        # Note: It has been seen (on my (AK) machine) that attributes
        # are *not* local to the program object; the splitscreen example
        # is broken if we use the early exits here.

        # todo: instead of setting dirty to true, remove early exits
        # (leaving for now for testing on other machines)
        self._dirty = True

        # Generic vertex attribute (all vertices receive the same value)
        if self._generic:

            # Tell OpenGL to use the constant value
            gl.glDisableVertexAttribArray(self._loc)

            # Early exit
            if not self._dirty:
                return

            # Apply
            self._afunction(self._loc, *self._data)

        # Client side array
        elif isinstance(self._data, ClientVertexBuffer):

            # Tell OpenGL to use the array and not the glVertexAttrib* value
            gl.glEnableVertexAttribArray(self._loc)

            # Disable any VBO
            gl.glBindBuffer(gl.GL_ARRAY_BUFFER, 0)

            # Early exit (pointer to CPU-data is still known by Program)
            if not self._dirty:
                return

            # Get numpy array from its container
            data = self._data.data

            # Check attribute format against data format
            size, gtype, _ = gl_typeinfo[self._gtype]
            # if self._gtype != self._data._gtype:
            #    raise ValueError("Data not compatible with attribute type")
            offset = 0
            stride = self._data.stride

            # Apply (first disable any previous VertexBuffer)
            gl.glVertexAttribPointer(self._loc, size, gtype, False, stride,
                                     data)

        # Regular vertex buffer or vertex buffer view
        else:

            data = self._data
            # todo: check offset = -1?

            # Tell OpenGL to use the array and not the glVertexAttrib* value
            gl.glEnableVertexAttribArray(self._loc)

            # Enable the VBO
            program.activate_object(data)

            # Early exit
            if not self._dirty:
                return

            # Check attribute format against data format
            size, gtype, _ = gl_typeinfo[self._gtype]
            # if self._gtype != self._data._gtype:
            #    raise ValueError("Data not compatible with attribute type")
            offset = self._data.offset
            stride = self._data.stride

            #size, gtype, dtype = gl_typeinfo[self._gtype]
            # offset, stride = data._offset, data._stride  # view_params not on
            # VertexBuffer

            # Make offset a pointer, or it will be interpreted as a small array
            offset = ctypes.c_void_p(offset)

            # Apply
            gl.glVertexAttribPointer(self._loc, size, gtype, False, stride,
                                     offset)

        # Mark as uploaded
        self._dirty = False
        logger.debug('upload attribute %s to %s' % (self.name, self._loc))
Example #40
0
def checkSignature(file, bundle=None):
    SECURITY_FRAMEWORK = '/System/Library/Frameworks/Security.framework/Versions/Current/Security'
    kSecCSDefaultFlags = 0x0
    kSecCSDoNotValidateResources = 0x4
    kSecCSCheckAllArchitectures = 0x1
    kSecCSCheckNestedCode = 0x8
    kSecCSStrictValidate = 0x16
    kSecCSStrictValidate_kSecCSCheckAllArchitectures = 0x17
    kSecCSStrictValidate_kSecCSCheckAllArchitectures_kSecCSCheckNestedCode = 0x1f
    errSecSuccess = 0x0
    SecCSSignatureOK = errSecSuccess
    errSecCSUnsigned = -67062
    kPOSIXErrorEACCES = 100013
    kSecCSSigningInformation = 0x2
    kSecCodeInfoCertificates = 'certificates'

    #return dictionary
    signingInfo = {}
    sigCheckFlags = kSecCSStrictValidate_kSecCSCheckAllArchitectures_kSecCSCheckNestedCode
    securityFramework = ctypes.cdll.LoadLibrary(SECURITY_FRAMEWORK)
    objcRuntime = ctypes.cdll.LoadLibrary(ctypes.util.find_library('objc'))
    objcRuntime.objc_getClass.restype = ctypes.c_void_p
    objcRuntime.sel_registerName.restype = ctypes.c_void_p
    status = not errSecSuccess
    signedStatus = None
    isApple = False
    authorities = []

    file = Foundation.NSString.stringWithString_(file)
    file = file.stringByAddingPercentEscapesUsingEncoding_(
        Foundation.NSUTF8StringEncoding).encode('utf-8')
    path = Foundation.NSURL.URLWithString_(
        Foundation.NSString.stringWithUTF8String_(file))
    staticCode = ctypes.c_void_p(0)
    result = securityFramework.SecStaticCodeCreateWithPath(
        ctypes.c_void_p(objc.pyobjc_id(path)), kSecCSDefaultFlags,
        ctypes.byref(staticCode))
    signedStatus = securityFramework.SecStaticCodeCheckValidityWithErrors(
        staticCode, sigCheckFlags, None, None)
    if errSecSuccess == signedStatus:
        requirementReference = "anchor apple"
        NSString = objcRuntime.objc_getClass('NSString')
        objcRuntime.objc_msgSend.restype = ctypes.c_void_p
        objcRuntime.objc_msgSend.argtypes = [
            ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p
        ]
        requirementsString = objcRuntime.objc_msgSend(
            NSString, objcRuntime.sel_registerName('stringWithUTF8String:'),
            requirementReference)
        requirement = ctypes.c_void_p(0)
        if errSecSuccess == securityFramework.SecRequirementCreateWithString(
                ctypes.c_void_p(requirementsString), kSecCSDefaultFlags,
                ctypes.byref(requirement)):
            if errSecSuccess == securityFramework.SecStaticCodeCheckValidity(
                    staticCode, sigCheckFlags, requirement):
                isApple = True

        information = ctypes.c_void_p(0)
        result = securityFramework.SecCodeCopySigningInformation(
            staticCode, kSecCSSigningInformation, ctypes.byref(information))
        objcRuntime.objc_msgSend.restype = ctypes.c_void_p
        objcRuntime.objc_msgSend.argtypes = [
            ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p
        ]
        key = objcRuntime.objc_msgSend(
            NSString, objcRuntime.sel_registerName('stringWithUTF8String:'),
            kSecCodeInfoCertificates)
        objcRuntime.objc_msgSend.restype = ctypes.c_void_p
        objcRuntime.objc_msgSend.argtypes = [
            ctypes.c_void_p, ctypes.c_void_p, ctypes.c_void_p
        ]
        certChain = objcRuntime.objc_msgSend(
            information, objcRuntime.sel_registerName('objectForKey:'), key)
        objcRuntime.objc_msgSend.restype = ctypes.c_uint
        objcRuntime.objc_msgSend.argtypes = [ctypes.c_void_p, ctypes.c_void_p]
        count = objcRuntime.objc_msgSend(certChain,
                                         objcRuntime.sel_registerName('count'))
        certName = ctypes.c_char_p(0)
        for index in range(count):
            objcRuntime.objc_msgSend.restype = ctypes.c_void_p
            objcRuntime.objc_msgSend.argtypes = [
                ctypes.c_void_p, ctypes.c_void_p, ctypes.c_uint
            ]
            cert = objcRuntime.objc_msgSend(
                certChain, objcRuntime.sel_registerName('objectAtIndex:'),
                index)
            result = securityFramework.SecCertificateCopyCommonName(
                ctypes.c_void_p(cert), ctypes.byref(certName))
            if errSecSuccess != result:
                continue
            objcRuntime.objc_msgSend.restype = ctypes.c_char_p
            objcRuntime.objc_msgSend.argtypes = [
                ctypes.c_void_p, ctypes.c_void_p
            ]
            authorities.append(
                objcRuntime.objc_msgSend(
                    certName, objcRuntime.sel_registerName('UTF8String')))

    status = errSecSuccess
    if signedStatus == 0:
        signingInfo['status'] = "signed"
    else:
        signingInfo['status'] = "unsigned"
    signingInfo['apple_binary'] = isApple
    signingInfo['Authority'] = authorities
    return (signingInfo)
Example #41
0
def main():
    os.system('nvidia-smi')

    libnames = ('libcuda.so', 'libcuda.dylib', 'cuda.dll')
    for libname in libnames:
        try:
            cuda = ctypes.CDLL(libname)
        except OSError:
            continue
        else:
            break
    else:
        raise OSError("could not load any of: " + ' '.join(libnames))

    nGpus = ctypes.c_int()
    name = b' ' * 100
    cc_major = ctypes.c_int()
    cc_minor = ctypes.c_int()
    cores = ctypes.c_int()
    threads_per_core = ctypes.c_int()
    clockrate = ctypes.c_int()
    bandwidth = ctypes.c_int()
    freeMem = ctypes.c_size_t()
    totalMem = ctypes.c_size_t()

    result = ctypes.c_int()
    device = ctypes.c_int()
    context = ctypes.c_void_p()
    error_str = ctypes.c_char_p()

    result = cuda.cuInit(0)

    if result != CUDA_SUCCESS:
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        print("cuInit failed with error code %d: %s" %
              (result, error_str.value.decode()))
        return 1
    result = cuda.cuDeviceGetCount(ctypes.byref(nGpus))

    if result != CUDA_SUCCESS:
        cuda.cuGetErrorString(result, ctypes.byref(error_str))
        print("cuDeviceGetCount failed with error code %d: %s" %
              (result, error_str.value.decode()))
        return 1
    print(" %d carte(s) détectée(s)." % nGpus.value)

    for i in range(nGpus.value):
        result = cuda.cuDeviceGet(ctypes.byref(device), i)

        if result != CUDA_SUCCESS:
            cuda.cuGetErrorString(result, ctypes.byref(error_str))
            print("cuDeviceGet failed with error code %d: %s" %
                  (result, error_str.value.decode()))
            return 1
        print("\n Carte n° : %d" % i)

        if cuda.cuDeviceGetName(ctypes.c_char_p(name), len(name),
                                device) == CUDA_SUCCESS:
            print("  Nom : %s" % (name.split(b'\0', 1)[0].decode()))

        if cuda.cuDeviceComputeCapability(ctypes.byref(cc_major),
                                          ctypes.byref(cc_minor),
                                          device) == CUDA_SUCCESS:
            print("  Capacité de calcul : %d.%d" %
                  (cc_major.value, cc_minor.value))

        if cuda.cuDeviceGetAttribute(ctypes.byref(cores),
                                     CU_DEVICE_ATTRIBUTE_MULTIPROCESSOR_COUNT,
                                     device) == CUDA_SUCCESS:
            print("  Multiprocesseurs : %d" % cores.value)
            print("  Cores CUDA : %d" %
                  (cores.value *
                   ConvertSMVer2Cores(cc_major.value, cc_minor.value)))

            if cuda.cuDeviceGetAttribute(
                    ctypes.byref(threads_per_core),
                    CU_DEVICE_ATTRIBUTE_MAX_THREADS_PER_MULTIPROCESSOR,
                    device) == CUDA_SUCCESS:
                print("  Unités de calcul : %d" % (threads_per_core.value))

        if cuda.cuDeviceGetAttribute(ctypes.byref(clockrate),
                                     CU_DEVICE_ATTRIBUTE_CLOCK_RATE,
                                     device) == CUDA_SUCCESS:
            print("  Fréquence du GPU : %g MHz" % (clockrate.value / 1000.))

        if cuda.cuDeviceGetAttribute(
                ctypes.byref(bandwidth),
                CU_DEVICE_ATTRIBUTE_GLOBAL_MEMORY_BUS_WIDTH,
                device) == CUDA_SUCCESS:
            print("  Bande passante : %g GB/s " % ((bandwidth.value)))
        result = cuda.cuCtxCreate(ctypes.byref(context), 0, device)

        if result != CUDA_SUCCESS:
            cuda.cuGetErrorString(result, ctypes.byref(error_str))
            print("cuCtxCreate failed with error code %d: %s" %
                  (result, error_str.value.decode()))
        else:
            result = cuda.cuMemGetInfo(ctypes.byref(freeMem),
                                       ctypes.byref(totalMem))
            if result == CUDA_SUCCESS:
                print("  Mémoire Totale : %ld MiB" %
                      (totalMem.value / 1024**2))
                print("  Mémoire libre : %ld MiB" % (freeMem.value / 1024**2))
            else:
                cuda.cuGetErrorString(result, ctypes.byref(error_str))
                print("cuMemGetInfo failed with error code %d: %s" %
                      (result, error_str.value.decode()))
            cuda.cuCtxDetach(context)
    return 0
Example #42
0
def createObject(shaderProgram, vertices, indices):

    # Create a new VAO (Vertex Array Object)
    VAO = glGenVertexArrays(1)
    VBO = glGenBuffers(1)
    EBO = glGenBuffers(1)

    # Bind the Vertex Array Object first
    glBindVertexArray(VAO);

    # Bind the Vertex Buffer
    glBindBuffer(GL_ARRAY_BUFFER, VBO)
    glBufferData(GL_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(vertices), vertices, GL_STATIC_DRAW);

    # Bind the Entity Buffer
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO)
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, ArrayDatatype.arrayByteCount(indices), indices, GL_STATIC_DRAW)

    # Configure vertex attributes

    # - Position attribute
    attrPositionIndex = glGetAttribLocation(shaderProgram, 'attrPosition')
    if (attrPositionIndex != -1):
        glVertexAttribPointer(0, NB_POSITION_AXES, GL_FLOAT, GL_FALSE, 8 * ctypes.sizeof(ctypes.c_float), ctypes.c_void_p(0));
        glEnableVertexAttribArray(0);

    # - Color attribute
    attrColorIndex = glGetAttribLocation(shaderProgram, 'attrColor')
    if (attrColorIndex != -1):
        glVertexAttribPointer(1, NB_COLOR_AXES, GL_FLOAT, GL_FALSE, 8 * ctypes.sizeof(ctypes.c_float), ctypes.c_void_p(3* ctypes.sizeof(ctypes.c_float)));
        glEnableVertexAttribArray(1);

    # Unbind the VAO
    glBindVertexArray(0)
    
    # Unbind the VBO
    glBindBuffer(GL_ARRAY_BUFFER, 0)
    if (attrPositionIndex != -1):
        glDisableVertexAttribArray(attrPositionIndex)
    if (attrColorIndex != -1):
        glDisableVertexAttribArray(attrColorIndex)

    return VAO
    def __init__(self, w, h):
        self.__caption = 'OpenGL Window'
        self.__vp_size = [w, h]

        pygame.display.gl_set_attribute(pygame.GL_DEPTH_SIZE, 24)  
        self.__screen = pygame.display.set_mode(self.__vp_size, pygame.DOUBLEBUF| pygame.OPENGL)
        
        self.__program = compileProgram( 
            compileShader(glsl_vert, GL_VERTEX_SHADER),
            compileShader(glsl_frag, GL_FRAGMENT_SHADER),
        )
        self.___attrib = { a : glGetAttribLocation (self.__program, a) for a in ['a_pos', 'a_nv', 'a_uv'] }
        print(self.___attrib)
        self.___uniform = { u : glGetUniformLocation (self.__program, u) for u in ['u_model', 'u_view', 'u_proj'] }
        print(self.___uniform)

        v = [[-1,-1,1], [1,-1,1], [1,1,1], [-1,1,1], [-1,-1,-1], [1,-1,-1], [1,1,-1], [-1,1,-1]]
        n = [[0,0,1], [1,0,0], [0,0,-1], [-1,0,0], [0,1,0], [0,-1,0]]
        e = [[0,1,2,3], [1,5,6,2], [5,4,7,6], [4,0,3,7], [3,2,6,7], [1,0,4,5]]
        t = [[0, 0], [1, 0], [1, 1], [0, 1]]
        index_array = [si*4+[0, 1, 2, 0, 2, 3][vi] for si in range(6) for vi in range(6)]
        attr_array = []
        for si in range(len(e)):
            for i, vi in enumerate(e[si]):
                attr_array += [*v[vi], *n[si], *t[i], si]

        self.__no_vert = len(attr_array) // 10
        self.__no_indices = len(index_array)
        vertex_attributes = (ctypes.c_float * len(attr_array))(*attr_array)
        indices = (ctypes.c_uint32 * self.__no_indices)(*index_array)

        self.__vao = glGenVertexArrays(1)
        self.__vbo, self.__ibo = glGenBuffers(2)

        glBindVertexArray(self.__vao)
        glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, self.__ibo)
        glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices, GL_STATIC_DRAW)
        glBindBuffer(GL_ARRAY_BUFFER, self.__vbo)
        glBufferData(GL_ARRAY_BUFFER, vertex_attributes, GL_STATIC_DRAW)

        float_size = ctypes.sizeof(ctypes.c_float)   
        glVertexAttribPointer(self.___attrib['a_pos'], 3, GL_FLOAT, False, 9*float_size, None)
        glVertexAttribPointer(self.___attrib['a_nv'], 3, GL_FLOAT, False, 9*float_size, ctypes.c_void_p(3*float_size))
        glVertexAttribPointer(self.___attrib['a_uv'], 3, GL_FLOAT, False, 9*float_size, ctypes.c_void_p(6*float_size))
        glEnableVertexAttribArray(self.___attrib['a_pos'])
        glEnableVertexAttribArray(self.___attrib['a_nv'])
        glEnableVertexAttribArray(self.___attrib['a_uv'])

        glEnable(GL_DEPTH_TEST)
        glUseProgram(self.__program)

        glActiveTexture(GL_TEXTURE0)
        sizeX, sizeY = image_size
        self.tex_obj = glGenTextures(1)
        glBindTexture(GL_TEXTURE_2D_ARRAY, self.tex_obj)
        glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, GL_RGBA, sizeX, sizeY, 6, 0, GL_RGBA, GL_UNSIGNED_BYTE, None)
        for i in range(6):
            glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, 0, 0, i, sizeX, sizeY, 1, GL_RGBA, GL_UNSIGNED_BYTE, image_planes[i])
        glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MAG_FILTER, GL_LINEAR)
        glTexParameterf(GL_TEXTURE_2D_ARRAY, GL_TEXTURE_MIN_FILTER, GL_LINEAR)
Example #44
0
    def eval(self, jsEnv, js):

        chakraCoreLibrary = None

        # check current working directory.
        for _libraryFile in [
                'libChakraCore.so', 'libChakraCore.dylib', 'ChakraCore.dll'
        ]:
            if os.path.isfile(os.path.join(os.getcwd(), _libraryFile)):
                chakraCoreLibrary = os.path.join(os.getcwd(), _libraryFile)
                continue

        if not chakraCoreLibrary:
            chakraCoreLibrary = ctypes.util.find_library('ChakraCore')

        if not chakraCoreLibrary:
            sys.tracebacklimit = 0
            raise RuntimeError(
                'ChakraCore library not found in current path or any of your system library paths, '
                'please download from https://www.github.com/VeNoMouS/cloudscraper/tree/ChakraCore/, '
                'or https://github.com/Microsoft/ChakraCore/')

        try:
            chakraCore = CDLL(chakraCoreLibrary)
        except OSError:
            sys.tracebacklimit = 0
            raise RuntimeError(
                'There was an error loading the ChakraCore library {}'.format(
                    chakraCoreLibrary))

        if sys.platform != 'win32':
            chakraCore.DllMain(0, 1, 0)
            chakraCore.DllMain(0, 2, 0)

        script = create_string_buffer('{}{}'.format(jsEnv,
                                                    js).encode('utf-16'))

        runtime = c_void_p()
        chakraCore.JsCreateRuntime(0, 0, byref(runtime))

        context = c_void_p()
        chakraCore.JsCreateContext(runtime, byref(context))
        chakraCore.JsSetCurrentContext(context)

        fname = c_void_p()
        chakraCore.JsCreateString('iuam-challenge.js',
                                  len('iuam-challenge.js'), byref(fname))

        scriptSource = c_void_p()
        chakraCore.JsCreateExternalArrayBuffer(script, len(script), 0, 0,
                                               byref(scriptSource))

        jsResult = c_void_p()
        chakraCore.JsRun(scriptSource, 0, fname, 0x02, byref(jsResult))

        resultJSString = c_void_p()
        chakraCore.JsConvertValueToString(jsResult, byref(resultJSString))

        stringLength = c_size_t()
        chakraCore.JsCopyString(resultJSString, 0, 0, byref(stringLength))

        resultSTR = create_string_buffer(stringLength.value + 1)
        chakraCore.JsCopyString(resultJSString, byref(resultSTR),
                                stringLength.value + 1, 0)

        chakraCore.JsDisposeRuntime(runtime)

        return resultSTR.value
Example #45
0
 def _lowLevelClearDataBuffers(self, channel):
     m = self.lib.ps4000SetDataBuffers(c_int16(self.handle),
                                       c_enum(channel), c_void_p(),
                                       c_void_p(), c_uint32(0))
     self.checkResult(m)
    def initializeGL(self):
        # setup some OpenGL options
        glEnable(GL_DEPTH_TEST)
        glEnable(GL_BLEND)
        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA)

        vertexShader, fragmentShader = self.loadShaders()
        self.__shaderProgram = shaders.compileProgram(vertexShader, fragmentShader)

        vertices = np.array([
            -0.5, -0.5, -0.5,  0.0, 0.0,
             0.5, -0.5, -0.5,  1.0, 0.0,
             0.5,  0.5, -0.5,  1.0, 1.0,
             0.5,  0.5, -0.5,  1.0, 1.0,
            -0.5,  0.5, -0.5,  0.0, 1.0,
            -0.5, -0.5, -0.5,  0.0, 0.0,

            -0.5, -0.5,  0.5,  0.0, 0.0,
             0.5, -0.5,  0.5,  1.0, 0.0,
             0.5,  0.5,  0.5,  1.0, 1.0,
             0.5,  0.5,  0.5,  1.0, 1.0,
            -0.5,  0.5,  0.5,  0.0, 1.0,
            -0.5, -0.5,  0.5,  0.0, 0.0,

            -0.5,  0.5,  0.5,  1.0, 0.0,
            -0.5,  0.5, -0.5,  1.0, 1.0,
            -0.5, -0.5, -0.5,  0.0, 1.0,
            -0.5, -0.5, -0.5,  0.0, 1.0,
            -0.5, -0.5,  0.5,  0.0, 0.0,
            -0.5,  0.5,  0.5,  1.0, 0.0,

             0.5,  0.5,  0.5,  1.0, 0.0,
             0.5,  0.5, -0.5,  1.0, 1.0,
             0.5, -0.5, -0.5,  0.0, 1.0,
             0.5, -0.5, -0.5,  0.0, 1.0,
             0.5, -0.5,  0.5,  0.0, 0.0,
             0.5,  0.5,  0.5,  1.0, 0.0,

            -0.5, -0.5, -0.5,  0.0, 1.0,
             0.5, -0.5, -0.5,  1.0, 1.0,
             0.5, -0.5,  0.5,  1.0, 0.0,
             0.5, -0.5,  0.5,  1.0, 0.0,
            -0.5, -0.5,  0.5,  0.0, 0.0,
            -0.5, -0.5, -0.5,  0.0, 1.0,

            -0.5,  0.5, -0.5,  0.0, 1.0,
             0.5,  0.5, -0.5,  1.0, 1.0,
             0.5,  0.5,  0.5,  1.0, 0.0,
             0.5,  0.5,  0.5,  1.0, 0.0,
            -0.5,  0.5,  0.5,  0.0, 0.0,
            -0.5,  0.5, -0.5,  0.0, 1.0
            ], np.float32)

        self.planeVertices = np.array([
            # Positions     Texture Coords (note we set these higher than 1 that together with GL_REPEAT as texture wrapping mode will cause the floor texture to repeat)
            5.0, -0.5, 5.0, 2.0, 0.0,
            -5.0, -0.5, 5.0, 0.0, 0.0,
            -5.0, -0.5, -5.0, 0.0, 2.0,

            5.0, -0.5, 5.0, 2.0, 0.0,
            -5.0, -0.5, -5.0, 0.0, 2.0,
            5.0, -0.5, -5.0, 2.0, 2.0
        ], np.float32)

        self.transparentVertices = np.array([
            # Position     # Texture Coords (swapped y coordinates because texture is flipped upside down)
            0.0, 0.5, 0.0, 0.0, 0.0,
            0.0, -0.5, 0.0, 0.0, 1.0,
            1.0, -0.5, 0.0, 1.0, 1.0,
            0.0, 0.5, 0.0, 0.0, 0.0,
            1.0, -0.5, 0.0, 1.0, 1.0,
            1.0, 0.5, 0.0, 1.0, 0.0
        ], np.float32)

        # setup cube VAO
        self.cubeVAO = glGenVertexArrays(1)
        vbo = glGenBuffers(1)

        glBindVertexArray(self.cubeVAO)

        glBindBuffer(GL_ARRAY_BUFFER, vbo)
        glBufferData(GL_ARRAY_BUFFER, vertices.nbytes, vertices, GL_STATIC_DRAW)

        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * vertices.itemsize, None)
        glEnableVertexAttribArray(0)

        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * vertices.itemsize, ctypes.c_void_p(3 * vertices.itemsize))
        glEnableVertexAttribArray(1)

        # setup plane VAO
        self.planeVAO = glGenVertexArrays(1)
        planeVBO = glGenBuffers(1)

        glBindVertexArray(self.planeVAO)
        glBindBuffer(GL_ARRAY_BUFFER, planeVBO)
        glBufferData(GL_ARRAY_BUFFER, self.planeVertices.nbytes, self.planeVertices, GL_STATIC_DRAW)
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * self.planeVertices.itemsize, None)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * self.planeVertices.itemsize, ctypes.c_void_p(3 * self.planeVertices.itemsize))
        glBindBuffer(GL_ARRAY_BUFFER, 0)

        glBindVertexArray(0)

        # setup transparent plane VAO
        self.transparentVAO = glGenVertexArrays(1)
        transparentVBO = glGenBuffers(1)
        glBindVertexArray(self.transparentVAO)
        glBindBuffer(GL_ARRAY_BUFFER, transparentVBO)
        glBufferData(GL_ARRAY_BUFFER, self.transparentVertices.nbytes, self.transparentVertices, GL_STATIC_DRAW)
        glEnableVertexAttribArray(0)
        glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5 * self.transparentVertices.itemsize, None)
        glEnableVertexAttribArray(1)
        glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5 * self.transparentVertices.itemsize, ctypes.c_void_p(3 * self.transparentVertices.itemsize))
        glBindVertexArray(0)

        # load and create a texture
        texturePath = os.path.join(abPath, '..', '..', 'resources', 'textures', 'marble.jpg')
        self.cubeTexture = loadTexture(texturePath)
        texture2Path = os.path.join(abPath, '..', '..', 'resources', 'textures', 'metal.png')
        self.floorTexture = loadTexture(texture2Path)
        texture3Path = os.path.join(abPath, '..', '..', 'resources', 'textures', 'window.png')
        self.transparentTexture = loadTexture(texture3Path)

        self.vegetation = ((-1.5, 0.0, -0.48),
                           (1.5, 0.0, 0.51),
                           (0.0, 0.0, 0.7),
                           (-0.3, 0.0, -2.3),
                           (0.5, 0.0, -0.6))
Example #47
0
def safe_read_chunked_memory_region_as_one(base, size):
    mbi = D.MEMORY_BASIC_INFORMATION()
    VirtualQueryEx = C.windll.kernel32.VirtualQueryEx
    VirtualProtectEx = C.windll.kernel32.VirtualProtectEx
    GRANULARITY = 0x1000

    h_process = wintypes.HANDLE(api.Plugingetvalue(api.VAL_HPROCESS))
    try:
        rv = bytearray(size)
    except MemoryError:
        return

    guarded = list()
    gpoints = dict()
    protect = 0

    queried = VirtualQueryEx(h_process, C.c_void_p(base), C.byref(mbi),
                             C.sizeof(mbi))
    if queried:
        protect = mbi.Protect
    else:
        print >> sys.stderr, 'safe_read_chunked_memory_region_as_one: VirtualQueryEx() failed'
    if queried and mbi.Protect & D.PAGE_GUARD:
        g = {'ea': base, 'size': GRANULARITY, 'p': mbi.Protect}
        gpoints[base] = 0
        ea = base
        while True:
            ea -= GRANULARITY
            if VirtualQueryEx(h_process, C.c_void_p(ea), C.byref(mbi), C.sizeof(mbi)) and\
                    (mbi.Protect & D.PAGE_GUARD) != 0 and g['p'] == mbi.Protect:
                g['ea'] -= GRANULARITY
                g['size'] += GRANULARITY
            else:
                break

        guarded.append(g)

    for i in range(base + GRANULARITY, base + size, GRANULARITY):
        p_addr = C.c_void_p(i)
        if VirtualQueryEx(h_process, p_addr, C.byref(mbi), C.sizeof(mbi)) and\
                        mbi.Protect & D.PAGE_GUARD:
            prevaddr = i - GRANULARITY
            if prevaddr in gpoints and guarded[
                    gpoints[prevaddr]]['p'] == mbi.Protect:
                idx = gpoints[prevaddr]
            else:
                guarded.append({'ea': i, 'size': 0, 'p': mbi.Protect})
                idx = len(guarded) - 1
            guarded[idx]['size'] += GRANULARITY
            gpoints[i] = idx

    ea = base + size - GRANULARITY
    if ea in gpoints:
        while True:
            ea += GRANULARITY
            if VirtualQueryEx(h_process, C.c_void_p(ea), C.byref(mbi), C.sizeof(mbi)) and\
                        mbi.Protect & D.PAGE_GUARD:
                guarded[-1]['size'] += GRANULARITY
            else:
                break

    # turn off page guard before read
    dummy = C.c_long()
    for g in guarded:
        for off in range(0, g['size'], GRANULARITY):
            g['ok'] = VirtualProtectEx(h_process, C.c_void_p(g['ea'] + off),
                                       GRANULARITY,
                                       C.c_long(g['p'] & ~D.PAGE_GUARD),
                                       C.byref(dummy))

    for i in range(base, base + size, GRANULARITY):
        p_addr = C.c_void_p(i)
        if VirtualQueryEx(h_process, p_addr, C.byref(mbi), C.sizeof(mbi)):
            if mbi.Protect & D.PAGE_GUARD:
                # TODO
                pass
            mem = unsafe_read_process_memory(i, GRANULARITY)
            if mem is None:
                continue
            mem = mem[1]
            if mem:
                off = i - base
                rv[off:off + GRANULARITY] = mem

    for g in guarded:
        for off in range(0, g['size'], GRANULARITY):
            if not g['ok']:
                continue
            if not VirtualProtectEx(h_process, C.c_void_p(g['ea'] + off),
                                    GRANULARITY, C.c_long(g['p']),
                                    C.byref(dummy)):
                print >> sys.stderr, 'VirtualProtectEx(ptr 0x%08X, size 0x%08X, protect 0x%08X) failed' %\
                                     (g['ea'] + off, GRANULARITY, g['p'])
    if rv and len(rv) > size:
        rv = rv[:size]
    return size, rv, protect
bInheritHandle = False
dwProcessId = lpdwProcessId

# Calling the Windows API Call to Open the Process
hProcess = k_handle.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)

# Check to see if we have a valid Handle to the process
if hProcess <= 0:
	print("[ERROR] Could Not Grab Privileged Handle! Error Code: {0}".format(k_handle.GetLastError()))
else:
	print("[INFO] Privileged Handle Opened...")
	
# Open a Handle to the Process's Token Directly
ProcessHandle = hProcess
DesiredAccess = TOKEN_ALL_ACCESS
TokenHandle = ctypes.c_void_p()

# Issue the API Call
response = k_handle.OpenProcessToken(ProcessHandle, DesiredAccess, ctypes.byref(TokenHandle))

# Handle an Error
if response > 0:
	print("[INFO] Handle to Process Token Created! Token: {0}".format(TokenHandle))
else:
	print("[ERROR] Could Not Grab Privileged Handle to Token! Error Code: {0}".format(k_handle.GetLastError()))

# Check to see if we have SEDebugPrivilege
# First use the LookupPrivilegeValue API Call to get the LUID based on the String Privilege name

# Setup a PRIVILEGE_SET for the PrivilegeCheck Call to be used later - We need the LUID to be used
# We will reference it later as well
Example #49
0
File: model.py Project: sunt05/CAMB
 def clear_custom_scalar_sources(self):
     self.f_SetCustomSourcesFunc(byref(c_int(0)), byref(ctypes.c_void_p(0)),
                                 np.zeros(0, dtype=np.int32))
Example #50
0
def _run_argvemulator(timeout=60):

    # Configure ctypes
    carbon = _ctypes_setup()

    # Is the emulator running?
    running = [True]

    timeout = [timeout]

    # Configure AppleEvent handlers
    ae_callback = carbon.AEInstallEventHandler.argtypes[2]

    kAEInternetSuite, = struct.unpack('>i', b'GURL')
    kAEISGetURL, = struct.unpack('>i', b'GURL')
    kCoreEventClass, = struct.unpack('>i', b'aevt')
    kAEOpenApplication, = struct.unpack('>i', b'oapp')
    kAEOpenDocuments, = struct.unpack('>i', b'odoc')
    keyDirectObject, = struct.unpack('>i', b'----')
    typeAEList, = struct.unpack('>i', b'list')
    typeChar, = struct.unpack('>i', b'TEXT')
    typeFSRef, = struct.unpack('>i', b'fsrf')
    FALSE = b'\0'
    TRUE = b'\1'
    eventLoopTimedOutErr = -9875

    kEventClassAppleEvent, = struct.unpack('>i', b'eppc')
    kEventAppleEvent = 1

    @ae_callback
    def open_app_handler(message, reply, refcon):
        # Got a kAEOpenApplication event, which means we can
        # start up. On some OSX versions this event is even
        # sent when an kAEOpenDocuments or kAEOpenURLs event
        # is sent later on.
        #
        # Therefore don't set running to false, but reduce the
        # timeout to at most two seconds beyond the current time.
        timeout[0] = min(timeout[0], time.time() - start + 2)
        #running[0] = False
        return 0

    carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
                                 open_app_handler, 0, FALSE)

    @ae_callback
    def open_file_handler(message, reply, refcon):
        listdesc = AEDesc()
        sts = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
                                    ctypes.byref(listdesc))
        if sts != 0:
            print("argvemulator warning: cannot unpack open document event")
            running[0] = False
            return

        item_count = ctypes.c_long()
        sts = carbon.AECountItems(ctypes.byref(listdesc),
                                  ctypes.byref(item_count))
        if sts != 0:
            print("argvemulator warning: cannot unpack open document event")
            running[0] = False
            return

        desc = AEDesc()
        for i in range(item_count.value):
            sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i + 1, typeFSRef,
                                      0, ctypes.byref(desc))
            if sts != 0:
                print(
                    "argvemulator warning: cannot unpack open document event")
                running[0] = False
                return

            sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
            buf = ctypes.create_string_buffer(sz)
            sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
            if sts != 0:
                print(
                    "argvemulator warning: cannot extract open document event")
                continue

            fsref = buf

            buf = ctypes.create_string_buffer(1024)
            sts = carbon.FSRefMakePath(ctypes.byref(fsref), buf, 1023)
            if sts != 0:
                print(
                    "argvemulator warning: cannot extract open document event")
                continue

            if sys.version_info[0] > 2:
                sys.argv.append(buf.value.decode('utf-8'))
            else:
                sys.argv.append(buf.value)

        running[0] = False
        return 0

    carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
                                 open_file_handler, 0, FALSE)

    @ae_callback
    def open_url_handler(message, reply, refcon):
        listdesc = AEDesc()
        ok = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
                                   ctypes.byref(listdesc))
        if ok != 0:
            print("argvemulator warning: cannot unpack open document event")
            running[0] = False
            return

        item_count = ctypes.c_long()
        sts = carbon.AECountItems(ctypes.byref(listdesc),
                                  ctypes.byref(item_count))
        if sts != 0:
            print("argvemulator warning: cannot unpack open url event")
            running[0] = False
            return

        desc = AEDesc()
        for i in range(item_count.value):
            sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i + 1, typeChar,
                                      0, ctypes.byref(desc))
            if sts != 0:
                print("argvemulator warning: cannot unpack open URL event")
                running[0] = False
                return

            sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
            buf = ctypes.create_string_buffer(sz)
            sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
            if sts != 0:
                print("argvemulator warning: cannot extract open URL event")

            else:
                if sys.version_info[0] > 2:
                    sys.argv.append(buf.value.decode('utf-8'))
                else:
                    sys.argv.append(buf.value)

        running[0] = False
        return 0

    carbon.AEInstallEventHandler(kAEInternetSuite, kAEISGetURL,
                                 open_url_handler, 0, FALSE)

    # Remove the funny -psn_xxx_xxx argument
    if len(sys.argv) > 1 and sys.argv[1].startswith('-psn_'):
        del sys.argv[1]

    start = time.time()
    now = time.time()
    eventType = EventTypeSpec()
    eventType.eventClass = kEventClassAppleEvent
    eventType.eventKind = kEventAppleEvent

    while running[0] and now - start < timeout[0]:
        event = ctypes.c_void_p()

        sts = carbon.ReceiveNextEvent(1, ctypes.byref(eventType),
                                      start + timeout[0] - now, TRUE,
                                      ctypes.byref(event))

        if sts == eventLoopTimedOutErr:
            break

        elif sts != 0:
            print("argvemulator warning: fetching events failed")
            break

        sts = carbon.AEProcessEvent(event)
        if sts != 0:
            print("argvemulator warning: processing events failed")
            break

    carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication,
                                open_app_handler, FALSE)
    carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
                                open_file_handler, FALSE)
    carbon.AERemoveEventHandler(kAEInternetSuite, kAEISGetURL,
                                open_url_handler, FALSE)
Example #51
0
# ID types
__all__ += [
    "ActorCheckpointID",
    "ActorClassID",
    "ActorHandleID",
    "ActorID",
    "ClientID",
    "JobID",
    "WorkerID",
    "FunctionID",
    "ObjectID",
    "TaskID",
    "UniqueID",
]

import ctypes  # noqa: E402
# Windows only
if hasattr(ctypes, "windll"):
    # Makes sure that all child processes die when we die. Also makes sure that
    # fatal crashes result in process termination rather than an error dialog
    # (the latter is annoying since we have a lot of processes). This is done
    # by associating all child processes with a "job" object that imposes this
    # behavior.
    (lambda kernel32: (lambda job: (lambda n: kernel32.SetInformationJobObject(
        job, 9, "\0" * 17 + chr(0x8 | 0x4 | 0x20) + "\0" *
        (n - 18), n))(0x90 if ctypes.sizeof(ctypes.c_void_p) > ctypes.sizeof(
            ctypes.c_int) else 0x70) and kernel32.AssignProcessToJobObject(
                job, ctypes.c_void_p(kernel32.GetCurrentProcess())))
     (ctypes.c_void_p(kernel32.CreateJobObjectW(None, None)))
     if kernel32 is not None else None)(ctypes.windll.kernel32)  # noqa: E501
Example #52
0
 def worker(self):
     while not self.stopPolling:
         connectal.portal_event(ctypes.c_void_p(self.responsePortal))
Example #53
0
 def channel_group(self):
     ptr = c_void_p()
     self._call("GetChannelGroup", byref(ptr))
     return ChannelGroup(ptr)
Example #54
0
 def __init__(self):
     super(MPDManager, self).__init__()
     self.devices = {}
     self.mpd_session = ctypes.c_void_p()
Example #55
0
 def get_parameter(self, name):
     ptr = c_void_p()
     self._call("GetParameter", prepare_str(name), byref(ptr))
     return ParameterInstance(ptr)
 def overwriteManagerPvScan0(self):
     what = ctypes.c_void_p(self.workerPvScan0)
     where = self.managerPvScan0
     return self.callbackArbitraryOverwriteOnce(ctypes.addressof(what), where)
Example #57
0
def _run_argvemulator(timeout = 60):

    # Configure ctypes
    carbon = _ctypes_setup()

    # Is the emulator running?
    running = [True]

    # Configure AppleEvent handlers
    ae_callback = carbon.AEInstallEventHandler.argtypes[2]

    kAEInternetSuite,   = struct.unpack('>i', B('GURL'))
    kAEISGetURL,        = struct.unpack('>i', B('GURL'))
    kCoreEventClass,    = struct.unpack('>i', B('aevt'))
    kAEOpenApplication, = struct.unpack('>i', B('oapp'))
    kAEOpenDocuments,   = struct.unpack('>i', B('odoc'))
    keyDirectObject,    = struct.unpack('>i', B('----'))
    typeAEList,         = struct.unpack('>i', B('list'))
    typeChar,           = struct.unpack('>i', B('TEXT'))
    typeFSRef,          = struct.unpack('>i', B('fsrf'))
    FALSE               = B('\0')
    TRUE               = B('\1')

    kEventClassAppleEvent, = struct.unpack('>i', B('eppc'))
    kEventAppleEvent = 1


    @ae_callback
    def open_app_handler(message, reply, refcon):
        running[0] = False
	return 0

    carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenApplication,
            open_app_handler, 0, FALSE)

    @ae_callback
    def open_file_handler(message, reply, refcon):
        listdesc = AEDesc()
        sts = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
                ctypes.byref(listdesc))
        if sts != 0:
            print >>sys.stderr, "argvemulator warning: cannot unpack open document event"
            running[0] = False
            return

        item_count = ctypes.c_long()
        sts = carbon.AECountItems(ctypes.byref(listdesc), ctypes.byref(item_count))
        if sts != 0:
            print >>sys.stderr, "argvemulator warning: cannot unpack open document event"
            running[0] = False
            return

        desc = AEDesc()
        for i in range(item_count.value):
            sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i+1, typeFSRef, 0, ctypes.byref(desc))
            if sts != 0:
                print >>sys.stderr, "argvemulator warning: cannot unpack open document event"
                running[0] = False
                return

            sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
            buf = ctypes.create_string_buffer(sz)
            sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
            if sts != 0:
                print >>sys.stderr, "argvemulator warning: cannot extract open document event"
                continue

            fsref = buf

            buf = ctypes.create_string_buffer(1024)
            sts = carbon.FSRefMakePath(ctypes.byref(fsref), buf, 1023)
            if sts != 0:
                print >>sys.stderr, "argvemulator warning: cannot extract open document event"
                continue

            print >>sys.stderr, "Adding: %s"%(repr(buf.value.decode('utf-8')),)

            if sys.version_info[0] > 2:
                sys.argv.append(buf.value.decode('utf-8'))
            else:
                sys.argv.append(buf.value)

        running[0] = False
	return 0

    carbon.AEInstallEventHandler(kCoreEventClass, kAEOpenDocuments,
            open_file_handler, 0, FALSE)

    @ae_callback
    def open_url_handler(message, reply, refcon):
        listdesc = AEDesc()
        ok = carbon.AEGetParamDesc(message, keyDirectObject, typeAEList,
                ctypes.byref(listdesc))
        if ok != 0:
            print >>sys.stderr, "argvemulator warning: cannot unpack open document event"
            running[0] = False
            return

        item_count = ctypes.c_long()
        sts = carbon.AECountItems(ctypes.byref(listdesc), ctypes.byref(item_count))
        if sts != 0:
            print >>sys.stderr, "argvemulator warning: cannot unpack open url event"
            running[0] = False
            return

        desc = AEDesc()
        for i in range(item_count.value):
            sts = carbon.AEGetNthDesc(ctypes.byref(listdesc), i+1, typeChar, 0, ctypes.byref(desc))
            if sts != 0:
                print >>sys.stderr, "argvemulator warning: cannot unpack open URL event"
                running[0] = False
                return

            sz = carbon.AEGetDescDataSize(ctypes.byref(desc))
            buf = ctypes.create_string_buffer(sz)
            sts = carbon.AEGetDescData(ctypes.byref(desc), buf, sz)
            if sts != 0:
                print >>sys.stderr, "argvemulator warning: cannot extract open URL event"

            else:
                if sys.version_info[0] > 2:
                    sys.argv.append(buf.value.decode('utf-8'))
                else:
                    sys.argv.append(buf.value)

        running[0] = False
	return 0
    
    carbon.AEInstallEventHandler(kAEInternetSuite, kAEISGetURL,
            open_url_handler, 0, FALSE)

    # Remove the funny -psn_xxx_xxx argument
    if len(sys.argv) > 1 and sys.argv[1][:4] == '-psn':
	del sys.argv[1]

    start = time.time()
    now = time.time()
    eventType = EventTypeSpec()
    eventType.eventClass = kEventClassAppleEvent
    eventType.eventKind = kEventAppleEvent

    while running[0] and now - start < timeout:
        event = ctypes.c_void_p()

        sts = carbon.ReceiveNextEvent(1, ctypes.byref(eventType), 
                start + timeout - now, TRUE, ctypes.byref(event))
        if sts != 0:
            print >>sys.stderr, "argvemulator warning: fetching events failed"
            break

        sts = carbon.AEProcessEvent(event)
        if sts != 0:
            print >>sys.stderr, "argvemulator warning: processing events failed"
            break
        

    carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenApplication, 
            open_app_handler, FALSE)
    carbon.AERemoveEventHandler(kCoreEventClass, kAEOpenDocuments,
            open_file_handler, FALSE)
    carbon.AERemoveEventHandler(kAEInternetSuite, kAEISGetURL,
            open_url_handler, FALSE)
Example #58
0
 def get_parameter_by_index(self, index):
     ptr = c_void_p()
     self._call("GetParameterByIndex", index, byref(ptr))
     return ParameterInstance(ptr)
Example #59
0
    def __init__(self,
                 data,
                 label=None,
                 missing=None,
                 weight=None,
                 silent=False,
                 feature_names=None,
                 feature_types=None):
        """
        Data matrix used in XGBoost.

        Parameters
        ----------
        data : string/numpy array/scipy.sparse/pd.DataFrame
            Data source of DMatrix.
            When data is string type, it represents the path libsvm format txt file,
            or binary file that xgboost can read from.
        label : list or numpy 1-D array, optional
            Label of the training data.
        missing : float, optional
            Value in the data which needs to be present as a missing value. If
            None, defaults to np.nan.
        weight : list or numpy 1-D array , optional
            Weight for each instance.
        silent : boolean, optional
            Whether print messages during construction
        feature_names : list, optional
            Set names for features.
        feature_types : list, optional
            Set types for features.
        """
        # force into void_p, mac need to pass things in as void_p
        if data is None:
            self.handle = None
            return

        data, feature_names, feature_types = _maybe_pandas_data(
            data, feature_names, feature_types)
        label = _maybe_pandas_label(label)

        if isinstance(data, STRING_TYPES):
            self.handle = ctypes.c_void_p()
            _check_call(
                _LIB.XGDMatrixCreateFromFile(c_str(data), int(silent),
                                             ctypes.byref(self.handle)))
        elif isinstance(data, scipy.sparse.csr_matrix):
            self._init_from_csr(data)
        elif isinstance(data, scipy.sparse.csc_matrix):
            self._init_from_csc(data)
        elif isinstance(data, np.ndarray):
            self._init_from_npy2d(data, missing)
        else:
            try:
                csr = scipy.sparse.csr_matrix(data)
                self._init_from_csr(csr)
            except:
                raise TypeError('can not initialize DMatrix from {}'.format(
                    type(data).__name__))
        if label is not None:
            self.set_label(label)
        if weight is not None:
            self.set_weight(weight)

        self.feature_names = feature_names
        self.feature_types = feature_types
OPEN_EXISTING = 3
FILE_WRITE_ACCESS = 0x0002
FILE_SHARE_WRITE = 0x00000002
FILE_ATTRIBUTE_NORMAL = 0x00000080
METHOD_BUFFERED = 0
FILE_DEVICE_PROCMON_LOG = 0x00009535
PROCMON_DEBUGGER_HANDLER = c_wchar_p(r"\\.\Global\ProcmonDebugLogger")
DW_IO_CONTROL_CODE = 2503311876

k32 = windll.kernel32

msg = bytes("Hello ProcMon from python with ctypes!", 'UTF-16')

handle = k32.CreateFileW(PROCMON_DEBUGGER_HANDLER, GENERIC_WRITE,
                         FILE_SHARE_WRITE, 0, OPEN_EXISTING,
                         FILE_ATTRIBUTE_NORMAL, 0)
if handle == -1: raise RuntimeWarning("ProcMon doesn't appear to be running")

print("Handle: %d" % handle)

k32.DeviceIoControl(
    handle,
    DW_IO_CONTROL_CODE,
    msg,
    len(msg) * 2,
    0,
    0,
    byref(
        c_void_p()
    ),  # So quoth the MSDN: If lpOverlapped is NULL, lpBytesReturned cannot be NULL.  http://msdn.microsoft.com/en-us/library/windows/desktop/aa363216.aspx
    None)