• 0

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

What is the best way to pass from python 3.6.9 to C-API struct
I have a C library, which I'm trying to create a Python interface for it,
but the library expects to initialize a struct beforehand, the main problem is that one element of a struct is a pointer to another struct.

The part of the C header with the structs:

enum filter_type {     BLACK,      /* Black list */     WHITE       /* White list */ };  struct message {         uint32_t    mid;       /* the message id */         uint8_t     interface; /* the interface */ } __attribute__ ((__packed__));  struct filtering {     struct message      *filter;     uint32_t            arr_len;    /* length of the array     enum filter_type    mf_type;    /* filter type - black or white list */ } __attribute__ ((__packed__)); 

Python code:

from ctypes import Structure, c_uint32, c_uint8, c_bool from myC_Module import test_struct   class Message(Structure):       _fields_ = [('mid', c_uint32),                   ('interface', c_uint8)]   def gen_filter(mids, mf_type):       class Filtering(Structure):             _fields_ = [('filter', Message*len(mids)),                         ('arr_len', c_uint32),                         ('mf_type', c_bool)]        c_array = Message * len(mids)       return Filtering(c_array(*mids), len(mids), mf_type)   messages = [  # for testing       Message(int("1af", 16), 3),       Message(int("aaaaaaaa", 16), 100),       Message(int("bbbbbbbb", 16), 200), ]  print(test_struct(gen_filter(messages, True))) 

C-API test_struct function code:

static PyObject *test_struct(PyObject *self, PyObject *args) {     struct filtering *filtering = NULL;     Py_buffer buffer;     PyObject *result;      if (!PyArg_ParseTuple(args, "w*:getargs_w_star", &buffer))         return NULL;      printf("buffer.len: %ld
", buffer.len);     filtering = buffer.buf;      printf("recived results: arr_len[%d]  mf_type[%d]
", filtering->arr_len, filtering->mf_type);     printf("filter: %d
", filtering->int_list);      result = PyBytes_FromStringAndSize(buffer.buf, buffer.len);     PyBuffer_Release(&buffer);     return result; } 


buffer.len: 32 recived results: arr_len[431]  mf_type[3] filter: -1442840576 b'd' 

With the following approach, in case of a single value of message (not array and by changing the filtering struct accordingly), it works.
Any idea what I'm doing wrong, or What is the right way to this?

Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]

Wouldn't it be nice to have Server.RelativePath(path)?

well, you just need to extend it ;-)

public static class ExtensionMethods {     public static string RelativePath(this HttpServerUtility srv, string path, HttpRequest context)     {         return path.Replace(context.ServerVariables["APPL_PHYSICAL_PATH"], "~/").Replace(@"", "/");     } } 

With this you can simply call

Server.RelativePath(path, Request); 
  • 353
Reply Report

I know this is old but I needed to account for virtual directories (per @Costo's comment). This seems to help:

static string RelativeFromAbsolutePath(string path) {     if(HttpContext.Current != null)     {         var request = HttpContext.Current.Request;         var applicationPath = request.PhysicalApplicationPath;         var virtualDir = request.ApplicationPath;         virtualDir = virtualDir == "/" ? virtualDir : (virtualDir + "/");         return path.Replace(applicationPath, virtualDir).Replace(@"", "/");     }      throw new InvalidOperationException("We can only map an absolute back to a relative path if an HttpContext is available."); } 
  • 218
Reply Report

I like the idea from Canoas. Unfortunately I had not "HttpContext.Current.Request" available (BundleConfig.cs).

I changed the methode like this:

public static string RelativePath(this HttpServerUtility srv, string path) {      return path.Replace(HttpContext.Current.Server.MapPath("~/"), "~/").Replace(@"", "/"); } 
  • 148
Reply Report

If you used Server.MapPath, then you should already have the relative web path. According to the MSDN documentation, this method takes one variable, path, which is the virtual path of the Web server. So if you were able to call the method, you should already have the relative web path immediately accessible.

  • 211
Reply Report