Skip to content

Commit 0311164

Browse files
committed
CRuby: File file not found case.
1 parent 06a97eb commit 0311164

3 files changed

Lines changed: 25 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- CRuby: Fix file not found case.
6+
57
## CRuby version 0.1.2 and mruby version 0.1.0 - 2026-03-11
68

79
- Add mruby version.

ext/gettextpo/gettextpo.c

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
*/
2525

2626
#include "gettextpo.h"
27+
#include <gettext-po.h>
2728
#include <ruby/internal/core/rdata.h>
2829
#include <ruby/internal/intern/variable.h>
2930

@@ -551,10 +552,17 @@ gettextpo_po_message_m_check_format (int argc, VALUE *argv, VALUE self)
551552

552553
/* ********** file ********** */
553554

555+
void
556+
gettextpo_file_free (void *file)
557+
{
558+
if (file)
559+
po_file_free (file);
560+
}
561+
554562
static const rb_data_type_t gettextpo_po_file_type = {
555563
.wrap_struct_name = "gettextpo PO file",
556564
.function = {
557-
.dfree = (void (*)(void *)) po_file_free,
565+
.dfree = gettextpo_file_free,
558566
},
559567
.flags = RUBY_TYPED_FREE_IMMEDIATELY,
560568
};
@@ -602,13 +610,17 @@ gettextpo_po_file_m_read (int argc, VALUE *argv, VALUE klass)
602610
gettextpo_xerror_context.user_xerror = &kwargs_vals[0];
603611
if (kwargs_vals[1] != Qundef)
604612
gettextpo_xerror_context.user_xerror2 = &kwargs_vals[1];
605-
/* TODO: po_file_read can return NULL when not found? Also check
606-
null at file free. */
607-
DATA_PTR (self)
613+
po_file_t file
608614
= po_file_read (StringValueCStr (filename), &gettextpo_xerror_handler);
609-
if (gettextpo_xerror_context.error)
610-
rb_raise (ERROR, "failed to read");
611-
return self;
615+
if (file)
616+
{
617+
DATA_PTR (self) = file;
618+
if (gettextpo_xerror_context.error)
619+
rb_raise (ERROR, "failed to read");
620+
return self;
621+
}
622+
else
623+
rb_raise (ERROR, "failed to read file, maybe file not found?");
612624
}
613625

614626
/**

test/file.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@
2222
path = File.expand_path(File.join(__FILE__, "../../test.cruby/resources/ok.po"))
2323
assert_kind_of GettextPO::File, GettextPO::File.read(path)
2424

25+
assert_raise(GettextPO::Error) do
26+
GettextPO::File.read(File.join(__FILE__, '../not-there'))
27+
end
28+
2529
xerrors = []
2630
assert_raise(GettextPO::Error) do
2731
path = File.expand_path(File.join(__FILE__, "../../test.cruby/resources/a.po"))

0 commit comments

Comments
 (0)