diff --git a/tests.py b/tests.py index 3a694bd..e34717c 100644 --- a/tests.py +++ b/tests.py @@ -843,7 +843,7 @@ def testTooManyArguments(self): def testCompileFile(self): - f = tempfile.TemporaryFile('wt') + f = tempfile.TemporaryFile('w+t') f.write('rule test { condition: true }') f.flush() diff --git a/tests/test_compile.py b/tests/test_compile.py new file mode 100644 index 0000000..7a7a06c --- /dev/null +++ b/tests/test_compile.py @@ -0,0 +1,67 @@ +# +# Copyright (c) 2007-2026. The YARA Authors. All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +# + +import io + +import pytest +import yara + + +def test_compile_from_advanced_file_object_small(tmp_path): + """Rules should not be lost when the file object was read from before compile().""" + rule_file = tmp_path / "rules.yar" + rule_file.write_text( + "// header\n" + "rule rule_alpha { condition: true }\n" + "rule rule_beta { condition: true }\n" + ) + + with open(rule_file, "r") as fp: + fp.readline() + rules = yara.compile(file=fp) + + identifiers = [r.identifier for r in rules] + assert identifiers == ["rule_alpha", "rule_beta"], ( + f"expected all rules to compile; got {identifiers}" + ) + + +def test_compile_from_advanced_file_object_large(tmp_path): + """Partial rule loss should not occur for files larger than the buffer size.""" + header = "// header\n" + rule_a = "rule rule_alpha { condition: true }\n" + padding_len = 8192 - len(header) - len(rule_a) + padding = " " * padding_len + rule_b = "rule rule_beta { condition: true }\n" + rule_file = tmp_path / "rules.yar" + rule_file.write_text(header + rule_a + padding + rule_b) + + with open(rule_file, "r") as fp: + fp.readline() + rules = yara.compile(file=fp) + + identifiers = [r.identifier for r in rules] + assert identifiers == ["rule_alpha", "rule_beta"], ( + f"expected all rules to compile; got {identifiers}" + ) + + +def test_compile_from_bytesio(): + """file= should accept any object with a .read() method.""" + import io + source = b"rule r { condition: true }" + rules = yara.compile(file=io.BytesIO(source)) + assert [r.identifier for r in rules] == ["r"] diff --git a/yara-python.c b/yara-python.c index 25566e0..d86e07b 100644 --- a/yara-python.c +++ b/yara-python.c @@ -2679,7 +2679,6 @@ static PyObject* yara_compile( Py_ssize_t pos = 0; - int fd; int error = 0; char* filepath = NULL; @@ -2845,21 +2844,43 @@ static PyObject* yara_compile( } else if (file != NULL) { - fd = PyObject_AsFileDescriptor(file); + PyObject* read_result = PyObject_CallMethod(file, "read", NULL); - if (fd != -1) + if (read_result != NULL) { - Py_BEGIN_ALLOW_THREADS - fh = fdopen(dup(fd), "r"); - error = yr_compiler_add_file(compiler, fh, NULL, NULL); - fclose(fh); - Py_END_ALLOW_THREADS + const char* buffer = NULL; + Py_ssize_t size = 0; + + if (PyUnicode_Check(read_result)) + { + buffer = PyUnicode_AsUTF8AndSize(read_result, &size); + } + else if (PyBytes_Check(read_result)) + { + size = PyBytes_GET_SIZE(read_result); + buffer = PyBytes_AS_STRING(read_result); + } + else + { + result = PyErr_Format( + PyExc_TypeError, + "'file' read() must return string or bytes"); + } + + (void) size; + + if (buffer != NULL) + { + Py_BEGIN_ALLOW_THREADS + error = yr_compiler_add_string(compiler, buffer, NULL); + Py_END_ALLOW_THREADS + } + + Py_DECREF(read_result); } else { - result = PyErr_Format( - PyExc_TypeError, - "'file' is not a file object"); + result = NULL; } } else if (sources_dict != NULL)