小编Nic*_*k S的帖子

使用Python ctypes的C函数名依赖段错误

在Python中使用ctypes时,我遇到了一个非常奇怪的崩溃,但我不确定问题是来自Python还是C.

这是C源(in test.c):

#include <stdio.h>

void compress(char *a, int b) {
  printf("inside\n");
}

void run() {
  printf("before\n");
  compress("hi", 2);
  printf("after\n");
}
Run Code Online (Sandbox Code Playgroud)

然后这是我run()用ctypes 调用时会发生什么:

$ python -c 'import ctypes; ctypes.cdll.LoadLibrary("./test.so").run()'
before
Segmentation fault (core dumped)
Run Code Online (Sandbox Code Playgroud)

最奇怪的是,当我重命名compress()为其他任何东西时,崩溃不会发生.

阻止它崩溃的其他因素:

  • compress()直接打电话
  • 调用run()compress()由C直接(如果我加入main(),直接编译它,执行它)
  • 从签名中删除任一参数compress()(但是根据缺少" inside"打印,该函数似乎不执行.

我对C很新,所以我假设我在这里缺少一些东西.可能是什么导致了这个?

系统信息:
Python 2.7.6
gcc版本4.8.4(Ubuntu 4.8.4-2ubuntu1~14.04)
Ubuntu 14.04
uname -r:3.13.0-58-generic

c python ctypes

3
推荐指数
2
解决办法
662
查看次数

有没有办法在导入或需要节点模块后检查节点模块的版本?

有没有办法从脚本检查模块的版本?

根据我的Python经验,我尝试了以下方法:

const someModule = require('some-module');
someModule.version
Run Code Online (Sandbox Code Playgroud)

但没有骰子。还有其他方法可以做到这一点吗?我怀疑我没有得到我认为的版本,我想检查一下。

当我搜索时,我得到的只是人们反复指示如何在命令行上获取模块的版本。

javascript node.js

2
推荐指数
1
解决办法
1401
查看次数

标签 统计

c ×1

ctypes ×1

javascript ×1

node.js ×1

python ×1