How do you pass kwargs to a boost-python wrapped function? - boost-python

I have a python function with this signature:
def post_message(self, message, *args, **kwargs):
I would like to call the function from c++ and pass to it some kwargs. Calling the function is not the problem. Knowing how to pass the kwargs is. Here is a non-working paraphrased sample:
std::string message("aMessage");
boost::python::list arguments;
arguments.append("1");
boost::python::dict options;
options["source"] = "cpp";
boost::python::object python_func = get_python_func_of_wrapped_object()
python_func(message, arguments, options)
When I exercise this code, in pdb I get (which is not what I would like):
messsage = aMessage
args = (['1'], {'source': 'cpp'})
kwargs = {}
How do you pass the options in my example in the **kwargs dictionary ?
I have seen one post suggesting to use the **options syntax (how cool is this!):
python_func(message, arguments, **options)
Unfortunately, this results in
TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy
Thank you for any help you can give.

After some investigation, it turns out that the object function call operator is overridden for two arguments of type args_proxy and kwds_proxy. So you have to use this specific call style of two arguments.
args_proxy and kwds_proxy are generated by the * overloads. This is really nice.
Additionally, the first argument must be a tuple type so that the python interpreter correctly handles the *args argument.
The resulting example works:
boost::python::list arguments;
arguments.append("aMessage");
arguments.append("1");
boost::python::dict options;
options["source"] = "cpp";
boost::python::object python_func = get_python_func_of_wrapped_object()
python_func(*boost::python::tuple(arguments), **options)
Hope this helps...

Related

Null item error when placing factory registration within a function

I'm trying to write a function that creates registers an item with the factory then does some basic operations to that item. The problem I'm having is that when I try to execute this code, I get a null item error.
An example excerpt of the code I'd like to have would be:
modified_sequence_item example_msg_item
function new (string name = ex_sequence);
super.new(name);
create_message(example_msg_item, "example_msg_item", 32'hDEADBEEF);
endfunction
function create_message(modified_sequence_item msg_item, string msg_name, bit[31:0] data);
msg_item = modified_sequence_item::type_id::create(msg_name);
msg_item.data_field = data;
endfunction
Unfortunately, this doesn't work. I get the following error:
UVM_FATAL # 5710: reporter [NullITM] attempting to start a null item from sequence 'main'
However, the following code does work:
modified_sequence_item example_msg_item
function new (string name = ex_sequence);
super.new(name);
example_msg_item = modified_sequence_item::type_id_create("example_msg_item");
example_msg_item.data_field = 32'hDEADBEEF;
endfunction
Looking at these two bits of code, to me they are nearly identical aside from the actions being nested inside a function in the first bit of code. This leads me to believe the issue is most likely an issue with passing data being the functions.
Does anyone have any recommendations on how I could modify the first code example so that it does not have a null item error?
Two problems with your function declaration:
The handle that you are creating inside your function needs to be copied out when exiting the function, so msg_item needs to be declared as an output argument.
You forgot to declare the return type as void. Otherwise the default is a 1-bit 4-state result (IEEE Std 1800-2017, section 13.4 Functions: implicit_data_type)
function void create_message(
output modified_sequence_item msg_item,
input string msg_name, bit[31:0] data);
msg_item = modified_sequence_item::type_id::create(msg_name);
msg_item.data_field = data;
endfunction

Adding classes to micropython module

In reference to adding module in micropython, I was trying to create a class which has a local method. In the documentation it is given how to add local methods and that the first argument should be of mp_obj_t type which is the data struct itself. However, I was asking how can I pass extra parameters like other methods? I tried using mp_obj_t * args as second argument but STATIC MP_DEFINE_CONST_FUN_OBJ_1 gives error. I tried the same with STATIC MP_DEFINE_CONST_FUN_OBJ_VAR but it does not support passing mp_obt_t as first argument as STATIC MP_DEFINE_CONST_FUN_OBJ_VAR needs an int. I am quite new, so I was asking how to add methods to classes which can accept arguments?
You need MP_DEFINE_CONST_FUN_OBJ_2, since you have 2 arguments.
Something like
STATIC mp_obj_t my_class_func(mp_obj_t self, mp_obj_t arg) {
if (MP_OBJ_IS_SMALL_INT(lhs)) {
const mp_int_t lhs_val = MP_OBJ_SMALL_INT_VALUE(arg);
//...
} else {
//oops, not an int
}
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_2(my_class_func_obj, my_class_func);
The best source of samples like this is the source code btw.
To eleaborate on #stijn answer ~ when creating a class, all the MP_DEFINE_CONST_FUN_OBJ_XXXXXX defines work the exact same as they would if you weren't creating a class. The only difference is the first argument of ACTUAL arguments will always refer to self
Here's an example:
mp_obj_t Class_method(mp_uint_t n_args, const mp_obj_t *args) { ... }
That is the standard candidate for:
MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(Class_method_obj, 1, 3, Class_method);
However, in this case args[0] will be self.
Let's have another example.
mp_obj_t Class_method(mp_uint_t n_args, const mp_obj_t *args, mp_map_t *kw_args) { ... }
That's a prime candidate for this define
MP_DEFINE_CONST_FUN_OBJ_KW(Class_method_obj, 2, Class_method);
The only difference in this case is that the first index of allowed_args needs to automatically be handled as self. Nothing about how you do these things changes, except now the first ACTUAL argument (ie not including n_args or any other "helper" argument) needs to automatically be considered as self. That being said, you will NEVER use MP_DEFINE_CONST_FUN_OBJ_0 with a class method. '_0' means "zero arguments" and a class method will never have zero arguments because it will ALWAYS at least have self. This also means that you have to add one to however many expected arguments you have on the python end. If your python version accepts 3 arguments ~
(red, green, blue)
then your C_MODULE define has to start at 4 because it's going to get
(self, red, green, blue)

Can you pass by reference in PeopleCode?

I'm new to PeopleCode and as I'm learning functions, I noticed that in PeopleCode, we'd normally pass value using %PATIENT_ID. A friend told me that you can also pass by reference in PeopleCode but how?
PeopleCode passes by reference for functions.
Function addOne(&num As integer)
&num = &num + 1
End-Function;
Local integer &val = 9;
addOne(&val);
MessageBox(0, "", 0, 0,String(&val));
Results in 10
If you are using App Classes it behaves differently
for methods:
Pass by value for simple types (string, int, number,etc)
Pass by reference for objects (rowsets, records, app classes)
Can pass by reference for simple types using the OUT keyword in the parameter list
method addOne(&num as integer out)
Functions which are defined in the same context as the executing code, e.g. page/component/record/field event PeopleCode, always consider parameters as refernces.
Within Application Classes, parameters of simple types on methods can be defined with the 'out' key word to state that they are a references. Methods also automatically pass parameters as references for complex types. Think: "If there is a lot of data, it is a reference"
This documentation will be very helpful for you.
https://docs.oracle.com/cd/E26239_01/pt851h3/eng/psbooks/tpcr/chapter.htm?File=tpcr/htm/tpcr07.htm
Passing Parameters with Object Data Types
Parameters with object data types are always passed by reference:
/* argument passed by reference */
method storeInfo(&f as File);
If you specify the out modifier for a method parameter with an object
data type, it becomes a reference parameter. This means that the
parameter variable is passed by reference instead of the object that
it is pointing at when passed.
For example, if you pass an object parameter with the out modifier:
method myMethod(&arg as MyObjectClass);
Local MyObjectClass &o1 = create MyObjectClass("A");
Local MyOtherObjectClass &o2 = create MyOtherObjectClass();
&o2.myMethod(&o1);
And inside myMethod this occurs:
Method myMethod
&arg = create MyObjectClass("B");
end-method;
Since the method argument is reassigned within the body of myMethod,
&o1 does not point at the new instance of MyObjectClass (initialized
with "B") after the method call completes. This is because &o1 still
references the original instance of MyObjectClass.
However, if &o1 had been passed with the out modifier, after the
method call completes, &o1 points at whatever the parameter was last
assigned to; in this case, the new instance of MyObjectClass. The
parameter, rather than the object, is passed by reference. Using
the Out Specification for a Parameter
In the following example, a class, AddStuff, has a single public
method, DoAdd. This adds two numbers together, then assigns them as
different numbers. In the signature of the method declaration, the
first parameter is not declared with an out statement, while the
second one is.
class AddStuff
​method DoAdd(&P1 as number, &P2 as number out);
​end-class;
method DoAdd
&X = &P1 + &P2;
&P1 = 1;
&P2 = 2;
end-method;
In the following PeopleCode example, an object named &Aref is
instantiated from the class AddStuff. Two parameters, &I and &J are
also defined.
local AddStuff &Aref = Create AddStuff();
local number &I = 10;
local number &J = 20;
The following code example is correct. &J is changed, because of the
outstatement in the method signature, and because the value is being
passed by reference. The value of &I is not updated.
&Aref.DoAdd(&I, &J); /* changes &J but not &I */
The following code example causes a design time error. The second
parameter must be passed by reference, not by value.
&Aref.DoAdd(10, 20); /* error - second argument not variable */

How to define a default argument value for a method in as2?

look at this code :
function a2j(trusted:Boolean=true):String
{
...
}
compiler will not accept this code in flash actionscript 2.
It looks like AS2 doesn't force you to supply the all the arguments that a function declares. At the bottom of this help page, they state that arguments you do not supply are undefined ... and that any extra arguments you supply are ignored.
Also, the answer to this question shows that you can use the arguments keyword (an Array) to work with the parameters that are passed into the function.
So for a default value, as in your example above, you could do something like this:
function methodThatHasADefault(value:Boolean):void
{
if (arguments.length == 0)
value = true;
// do something
}

How can I specify the value of a named argument in boost.python?

i want to embed a function written in python into c++ code.
My python code is:test.py
def func(x=None, y=None, z=None):
print x,y,z
My c++ code is:
module = import("test");
namespace = module.attr("__dict__");
//then i want to know how to pass value 'y' only.
module.attr("func")("y=1") // is that right?
I'm not sure Boost.Python implements the ** dereference operator as claimed, but you can still use the Python C-API to execute the method you are intested on, as described here.
Here is a prototype of the solution:
//I'm starting from where you should change
boost::python::object callable = module.attr("func");
//Build your keyword argument dictionary using boost.python
boost::python::dict kw;
kw["x"] = 1;
kw["y"] = 3.14;
kw["z"] = "hello, world!";
//Note: This will return a **new** reference
PyObject* c_retval = PyObject_Call(callable.ptr(), NULL, kw.ptr());
//Converts a new (C) reference to a formal boost::python::object
boost::python::object retval(boost::python::handle<>(c_retval));
After you have converted the return value from PyObject_Call to a formal boost::python::object, you can either return it from your function or you can just forget it and the new reference returned by PyObject_Call will be auto-deleted.
For more information about wrapping PyObject* as boost::python::object, have a look at the Boost.Python tutorial. More precisely, at this link, end of the page.
a theoretical answer (no time to try myself :-| ):
boost::python::dict kw;
kw["y"]=1;
module.attr("func")(**kw);