[Python] _ZSt28__throw_bad_array_new_lengthv trying to import C++ code in Python after...

Discussão em 'Python' iniciado por Stack, Outubro 1, 2024.

  1. Stack

    Stack Membro Participativo

    Run into a frustrating issue trying to import a Python library that itself calls some code I wrote in C++ and compiled into a .so

    The C++ code is as follows:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <algorithm>
    #include <numeric>
    #include <cmath>

    std::vector<std::string> bigram(std::string initial_str) {
    int len = initial_str.size();
    std::vector<std::string> tokens;
    for (int i = 0; i < len-1; i += 1){
    tokens.push_back(initial_str.substr(i, 2));
    }
    return tokens;
    }

    std::vector<std::string> vsunion(std::vector<std::string> s1, std::vector<std::string> s2) {
    std::vector<std::string> union_str(s1);
    union_str.insert(union_str.end(), s2.begin(), s2.end());
    std::sort(union_str.begin(), union_str.end());
    union_str.erase(std::unique(union_str.begin(), union_str.end()), union_str.end());
    return union_str;
    }

    std::vector<int> ufreq(std::vector<std::string> u, std::vector<std::string> s) {
    int len = u.size();
    std::vector<int> vfreq;
    for (int i = 0; i < len; i += 1){
    int freq = std::count(s.begin(), s.end(), u);
    vfreq.push_back(freq);
    }
    return vfreq;
    }

    float similarity(std::vector<int> f1, std::vector<int> f2) {
    float num = std::inner_product(f1.begin(), f1.end(), f2.begin(), 0.0);
    float den1 = std::inner_product(f1.begin(), f1.end(), f1.begin(), 0.0);
    float den2 = std::inner_product(f2.begin(), f2.end(), f2.begin(), 0.0);
    float similarity = num / std::sqrt(den1 * den2);
    return similarity;
    }

    float similarity(std::string string1, std::string string2) {
    std::vector<std::string> new_str = bigram(string1);
    std::vector<std::string> new_str2 = bigram(string2);
    std::vector<std::string> union_str = vsunion(new_str, new_str2);
    std::vector<int> freq1 = ufreq(union_str, new_str);
    std::vector<int> freq2 = ufreq(union_str, new_str2);
    float score = similarity(freq1, freq2);
    return score;
    }

    extern "C" {
    float gram(std::string str1, std::string str2)
    {
    return similarity(str1, str2);
    }
    }


    Which I compiled using:

    g++ gram.cpp -shared -o gram.so


    and finally I'm trying to import the below script that's throwing the error in the title "_ZSt28__throw_bad_array_new_lengthv could not be located in the dynamic link library":

    import ctypes
    import sys
    import os

    dir_path = os.path.dirname(os.path.realpath(__file__))
    handle = ctypes.CDLL(dir_path + "/gram.so")

    handle.My_Function.argtypes = [ctypes.c_wchar_p]

    def gram(string1, string2):
    return handle.gram(string1, string2)


    Any idea where I might have gone wrong? I can compile a test case instead of the "extern" bit:

    int main() {
    float score = similarity("John Smith", "Johnny John Smith");
    std::cout << score << " ";
    std::cin.get();
    }


    and run as an exe, seemingly without issue; something seems to be going wrong at the

    handle = ctypes.CDLL(dir_path + "/gram.so")


    stage in the gram library.

    Continue reading...

Compartilhe esta Página