【发布时间】:2013-04-11 22:49:06
【问题描述】:
由于作品版权,我无法发布我的实际代码,所以我将尝试用简单的示例代码来展示我的问题。
我有一个 C 扩展,其简化版本如下所示:
#include <ruby.h>
#include <termios.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
VALUE test(VALUE self, VALUE string);
void Init_module_name() {
module_name = rb_define_module("Modulename");
c_modulename = rb_define_class_under(modulename, "Class", rb_cObject);
rb_define_method(c_modulename, "test", test, 1);
e_ModuleNameError = rb_define_class_under(modulename, "Error", rb_eStandardError);
}
VALUE test(VALUE self, VALUE string) {
char *c_string = StringValueCStr(string);
int fd = open(c_string, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (fd == -1) {
rb_raise(e_ModuleNameError, "Failed to open file");
}
if (!isatty(fd)) {
rb_raise(e_ModuleNameError, "File is not a tty");
}
struct termios config;
int termios_ret = init_termios(config, fd)
if (termios_ret != OK) { // OK defined by enum in modulename's header
close(fd);
rb_raise(e_ModuleNameError, "Termios init failed.");
}
int success = write(fd, "I'm a string", str_length);
if (success < str_length) {
close(fd);
rb_raise(e_ModuleNameError, "Failed to write to file.");
}
close(fd);
return rb_str_new2("Success");
}
然后,需要这个的 ruby 代码如下所示:
require 'modulename'
class ModuleName
attr_acessor :file
def initialize(file)
@file = file
@object = Modulename::Class.new
end
def test
@object.test @file
end
end
然后在我的生产项目中调用,例如:
require "modulename_ruby_file"
x = ModuleName "/dev/pts/1"
x.test
这是有趣的事情。当我在生产环境中运行此代码时,上面 x.test 的返回值为 false(如字面意义上的值 false,而不是字符串)。此外,对文件的写入永远不会发生。但是,如果我在一些简化的测试代码中执行此操作,它会像预期的那样返回字符串“Success”,并且确实完成了写入。
有谁知道会导致这个函数不执行写入并返回 false 的任何情况?我已经尝试在它周围进行救援,以防它抛出一个 rb_raises,但它似乎没有。
我和我团队的其他 3 名成员已经看了整个下午,但没有找到答案。
【问题讨论】:
-
我认为示例代码需要一些编辑。例如,
@console是什么?真的是@object吗? -
据我所知,您的示例 C 扩展方法
test将返回字符串“成功”,或引发错误。您真的能够使用此代码复制故障吗?我怀疑您选择提取的部分很有可能实际上并不能证明您的问题。 -
事实上,我预计会出现环境问题,或者一些非常简单的问题,例如在部署到生产环境时没有重新构建扩展。
-
这可能是您没有运行您认为的代码的情况之一。例如,gem 构建或部署失败可能会让您在生产中调用以前的版本。如果您的生产代码在任何地方都有自动加载器,它们也可能隐藏破损。
-
@mordocal。在这种情况下,您解决了自己的问题。我只是给出了一些模糊的指示,而不是答案,而且我没有想到命名空间冲突 - 我想我会因为对评论的支持而获得 +2,所以请随意发表评论,为您指明正确的方向。跨度>
标签: c ruby ruby-c-extension