@@ -47,14 +47,20 @@ typedef long Py_hash_t;
47
47
48
48
#if PY_MAJOR_VERSION >= 3
49
49
#define PY_STRING (x ) PyUnicode_DecodeUTF8(x, strlen(x), "ignore" )
50
+ #define PY_STRING_FORMAT (...) PyUnicode_FromFormat(__VA_ARGS__)
50
51
#define PY_STRING_TO_C (x ) PyUnicode_AsUTF8(x)
51
52
#define PY_STRING_CHECK (x ) PyUnicode_Check(x)
52
53
#else
53
54
#define PY_STRING (x ) PyString_FromString(x)
55
+ #define PY_STRING_FORMAT (...) PyString_FromFormat(__VA_ARGS__)
54
56
#define PY_STRING_TO_C (x ) PyString_AsString(x)
55
57
#define PY_STRING_CHECK (x ) (PyString_Check(x) || PyUnicode_Check(x))
56
58
#endif
57
59
60
+ #if PY_VERSION_HEX < 0x03020000
61
+ #define PyDescr_NAME (x ) (((PyDescrObject*)x)->d_name)
62
+ #endif
63
+
58
64
/* Module globals */
59
65
60
66
static PyObject * YaraError = NULL ;
@@ -1694,33 +1700,6 @@ void raise_exception_on_error(
1694
1700
const YR_RULE * rule ,
1695
1701
const char * message ,
1696
1702
void * user_data )
1697
- {
1698
- if (error_level == YARA_ERROR_LEVEL_ERROR )
1699
- {
1700
- if (file_name != NULL )
1701
- PyErr_Format (
1702
- YaraSyntaxError ,
1703
- "%s(%d): %s" ,
1704
- file_name ,
1705
- line_number ,
1706
- message );
1707
- else
1708
- PyErr_Format (
1709
- YaraSyntaxError ,
1710
- "line %d: %s" ,
1711
- line_number ,
1712
- message );
1713
- }
1714
- }
1715
-
1716
-
1717
- void raise_exception_on_error_or_warning (
1718
- int error_level ,
1719
- const char * file_name ,
1720
- int line_number ,
1721
- const YR_RULE * rule ,
1722
- const char * message ,
1723
- void * user_data )
1724
1703
{
1725
1704
if (error_level == YARA_ERROR_LEVEL_ERROR )
1726
1705
{
@@ -1740,22 +1719,25 @@ void raise_exception_on_error_or_warning(
1740
1719
}
1741
1720
else
1742
1721
{
1722
+ PyObject * warnings = (PyObject * )user_data ;
1723
+ PyObject * warning_msg ;
1743
1724
if (file_name != NULL )
1744
- PyErr_Format (
1745
- YaraWarningError ,
1725
+ warning_msg = PY_STRING_FORMAT (
1746
1726
"%s(%d): %s" ,
1747
1727
file_name ,
1748
1728
line_number ,
1749
1729
message );
1750
1730
else
1751
- PyErr_Format (
1752
- YaraWarningError ,
1731
+ warning_msg = PY_STRING_FORMAT (
1753
1732
"line %d: %s" ,
1754
1733
line_number ,
1755
1734
message );
1735
+ PyList_Append (warnings , warning_msg );
1736
+ Py_DECREF (warning_msg );
1756
1737
}
1757
1738
}
1758
1739
1740
+
1759
1741
////////////////////////////////////////////////////////////////////////////////
1760
1742
1761
1743
const char * yara_include_callback (
@@ -1951,6 +1933,8 @@ static PyObject* yara_compile(
1951
1933
char * filepath = NULL ;
1952
1934
char * source = NULL ;
1953
1935
char * ns = NULL ;
1936
+ PyObject * warnings = PyList_New (0 );
1937
+ bool warning_error = false;
1954
1938
1955
1939
if (PyArg_ParseTupleAndKeywords (
1956
1940
args ,
@@ -1973,18 +1957,15 @@ static PyObject* yara_compile(
1973
1957
if (error != ERROR_SUCCESS )
1974
1958
return handle_error (error , NULL );
1975
1959
1976
- yr_compiler_set_callback (compiler , raise_exception_on_error , NULL );
1960
+ yr_compiler_set_callback (compiler , raise_exception_on_error , warnings );
1977
1961
1978
1962
if (error_on_warning != NULL )
1979
1963
{
1980
1964
if (PyBool_Check (error_on_warning ))
1981
1965
{
1982
1966
if (PyObject_IsTrue (error_on_warning ) == 1 )
1983
1967
{
1984
- yr_compiler_set_callback (
1985
- compiler ,
1986
- raise_exception_on_error_or_warning ,
1987
- NULL );
1968
+ warning_error = true;
1988
1969
}
1989
1970
}
1990
1971
else
@@ -2170,6 +2151,13 @@ static PyObject* yara_compile(
2170
2151
"compile() takes 1 argument" );
2171
2152
}
2172
2153
2154
+ if (warning_error & PyList_Size (warnings ) > 0 )
2155
+ {
2156
+ PyErr_SetObject (YaraWarningError , warnings );
2157
+ }
2158
+
2159
+ Py_DECREF (warnings );
2160
+
2173
2161
if (PyErr_Occurred () == NULL )
2174
2162
{
2175
2163
rules = Rules_NEW ();
@@ -2371,6 +2359,24 @@ static PyMethodDef yara_methods[] = {
2371
2359
ob = Py_InitModule3(name, methods, doc);
2372
2360
#endif
2373
2361
2362
+ static PyObject * YaraWarningError_getwarnings (PyObject * self , void * closure )
2363
+ {
2364
+ PyObject * args = PyObject_GetAttrString (self , "args" );
2365
+ if (!args ) {
2366
+ return NULL ;
2367
+ }
2368
+
2369
+ PyObject * ret = PyTuple_GetItem (args , 0 );
2370
+ Py_XINCREF (ret );
2371
+ Py_XDECREF (args );
2372
+ return ret ;
2373
+ }
2374
+
2375
+ static PyGetSetDef YaraWarningError_getsetters [] = {
2376
+ {"warnings" , YaraWarningError_getwarnings , NULL , NULL , NULL },
2377
+ {NULL }
2378
+ };
2379
+
2374
2380
2375
2381
MOD_INIT (yara )
2376
2382
{
@@ -2397,6 +2403,14 @@ MOD_INIT(yara)
2397
2403
YaraSyntaxError = PyErr_NewException ("yara.SyntaxError" , YaraError , NULL );
2398
2404
YaraTimeoutError = PyErr_NewException ("yara.TimeoutError" , YaraError , NULL );
2399
2405
YaraWarningError = PyErr_NewException ("yara.WarningError" , YaraError , NULL );
2406
+
2407
+ PyTypeObject * YaraWarningError_type = (PyTypeObject * )YaraWarningError ;
2408
+ PyObject * descr = PyDescr_NewGetSet (YaraWarningError_type , YaraWarningError_getsetters );
2409
+ if (PyDict_SetItem (YaraWarningError_type -> tp_dict , PyDescr_NAME (descr ), descr ) < 0 ) {
2410
+ Py_DECREF (m );
2411
+ Py_DECREF (descr );
2412
+ }
2413
+ Py_DECREF (descr );
2400
2414
#else
2401
2415
YaraError = Py_BuildValue ("s" , "yara.Error" );
2402
2416
YaraSyntaxError = Py_BuildValue ("s" , "yara.SyntaxError" );
0 commit comments