Module: PyCall::LibPython::Helpers
- Defined in:
- ext/pycall/pycall.c
Class Method Summary collapse
- .call_object(*args) ⇒ Object
- .callable?(pyptr) ⇒ Boolean
- .compare(op, pyptr_a, pyptr_b) ⇒ Object
- .define_wrapper_method(wrapper, name) ⇒ Object
- .delattr(pyptr, name) ⇒ Object
- .delitem(pyptr, key) ⇒ Object
- .dict_contains(pyptr, key) ⇒ Object
- .dict_each(pyptr) ⇒ Object
- .getattr(*args) ⇒ Object
- .getitem(pyptr, key) ⇒ Object
- .hasattr?(pyptr, name) ⇒ Boolean
- .import_module(*args) ⇒ Object
- .sequence_contains(pyptr, key) ⇒ Object
- .sequence_each(pyptr) ⇒ Object
- .setattr(pyptr, name, val) ⇒ Object
- .setitem(pyptr, key, v) ⇒ Object
- .str(pyptr) ⇒ Object
-
.unicode_literals? ⇒ Boolean
PyCall::Helpers ====.
Class Method Details
.call_object(*args) ⇒ Object
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 |
# File 'ext/pycall/pycall.c', line 1013
static VALUE
pycall_libpython_helpers_m_call_object(int argc, VALUE *argv, VALUE mod)
{
VALUE pyptr;
PyObject *pyobj;
pycall_drain_pending_decrefs();
if (argc < 1) {
rb_raise(rb_eArgError, "too few arguments (%d for >=1)", argc);
}
pyptr = argv[0];
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
if (!Py_API(PyCallable_Check)(pyobj)) {
rb_raise(rb_eTypeError, "Non-callable Python object was given");
}
if (argc == 1) {
return pycall_call_python_callable(pyobj, 0, NULL);
}
else {
return pycall_call_python_callable(pyobj, argc - 1, argv + 1);
}
}
|
.callable?(pyptr) ⇒ Boolean
995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 |
# File 'ext/pycall/pycall.c', line 995
static VALUE
pycall_libpython_helpers_m_callable_p(VALUE mod, VALUE pyptr)
{
PyObject *pyobj;
int res;
pycall_drain_pending_decrefs();
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
res = Py_API(PyCallable_Check)(pyobj);
return res ? Qtrue : Qfalse;
}
|
.compare(op, pyptr_a, pyptr_b) ⇒ Object
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 |
# File 'ext/pycall/pycall.c', line 845
static VALUE
pycall_libpython_helpers_m_compare(VALUE mod, VALUE op, VALUE pyptr_a, VALUE pyptr_b)
{
PyObject *pyobj_a, *pyobj_b, *res;
int opid;
pycall_drain_pending_decrefs();
opid = pycall_rich_compare_opid(op);
if (!is_pycall_pyptr(pyptr_a)) {
rb_raise(rb_eTypeError, "unexpected 2nd argument type %s (expected PyCall::PyPtr)", rb_class2name(CLASS_OF(pyptr_a)));
}
if (!is_pycall_pyptr(pyptr_b)) {
rb_raise(rb_eTypeError, "unexpected 3rd argument type %s (expected PyCall::PyPtr)", rb_class2name(CLASS_OF(pyptr_b)));
}
pyobj_a = get_pyobj_ptr(pyptr_a);
pyobj_b = get_pyobj_ptr(pyptr_b);
res = Py_API(PyObject_RichCompare)(pyobj_a, pyobj_b, opid);
if (!res) {
pycall_pyerror_fetch_and_raise("PyObject_RichCompare in pycall_libpython_helpers_m_compare");
}
VALUE obj = pycall_pyobject_to_ruby(res);
pycall_Py_DecRef(res);
return obj;
}
|
.define_wrapper_method(wrapper, name) ⇒ Object
1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 |
# File 'ext/pycall/pycall.c', line 1220
static VALUE
pycall_libpython_helpers_m_define_wrapper_method(VALUE mod, VALUE wrapper, VALUE name)
{
VALUE pyptr;
PyObject *pyobj, *attr;
char *name_cstr;
pycall_drain_pending_decrefs();
pyptr = rb_attr_get(wrapper, rb_intern("@__pyptr__"));
if (NIL_P(pyptr) || !is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "Wrong wrapper object is given");
}
pyobj = get_pyobj_ptr(pyptr);
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
name_cstr = StringValueCStr(name);
if (name_cstr[RSTRING_LEN(name) - 1] == '=') {
name_cstr[RSTRING_LEN(name) - 1] = '\0';
attr = Py_API(PyObject_GetAttrString)(pyobj, name_cstr);
name_cstr[RSTRING_LEN(name) - 1] = '=';
}
else {
attr = Py_API(PyObject_GetAttrString)(pyobj, name_cstr);
}
if (!attr) {
pycall_pyerror_fetch_and_raise("PyObject_GetAttrString in pycall_libpython_helpers_m_define_wrapper_method");
}
pycall_Py_DecRef(attr);
rb_define_singleton_method(wrapper, name_cstr, pycall_pyobject_wrapper_wrapper_method, -1);
return Qnil;
}
|
.delattr(pyptr, name) ⇒ Object
971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 |
# File 'ext/pycall/pycall.c', line 971
static VALUE
pycall_libpython_helpers_m_delattr(VALUE mod, VALUE pyptr, VALUE name)
{
PyObject *pyobj;
pycall_drain_pending_decrefs();
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
if (Py_API(PyObject_DelAttrString)(pyobj, StringValueCStr(name)) == -1) {
pycall_pyerror_fetch_and_raise("PyObject_DelAttrString");
}
return Qnil;
}
|
.delitem(pyptr, key) ⇒ Object
1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 |
# File 'ext/pycall/pycall.c', line 1337
static VALUE
pycall_libpython_helpers_m_delitem(VALUE mod, VALUE pyptr, VALUE key)
{
PyObject *pyobj, *pyobj_key;
int res;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, NULL);
pyobj_key = pycall_convert_index(key);
res = Py_API(PyObject_DelItem)(pyobj, pyobj_key);
pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyObject_DelItem");
}
return Qnil;
}
|
.dict_contains(pyptr, key) ⇒ Object
1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 |
# File 'ext/pycall/pycall.c', line 1376
static VALUE
pycall_libpython_helpers_m_dict_contains(VALUE mod, VALUE pyptr, VALUE key)
{
PyObject *pyobj, *pyobj_key;
int res;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, Py_API(PyDict_Type));
pyobj_key = pycall_pyobject_from_ruby(key);
res = Py_API(PyDict_Contains)(pyobj, pyobj_key);
pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyDict_Contains");
}
return res ? Qtrue : Qfalse;
}
|
.dict_each(pyptr) ⇒ Object
1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 |
# File 'ext/pycall/pycall.c', line 1395
static VALUE
pycall_libpython_helpers_m_dict_each(VALUE mod, VALUE pyptr)
{
PyObject *pyobj, *pyobj_key, *pyobj_value;
Py_ssize_t pos;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, Py_API(PyDict_Type));
pos = 0;
while (Py_API(PyDict_Next)(pyobj, &pos, &pyobj_key, &pyobj_value)) {
VALUE key, value;
key = pycall_pyobject_to_ruby(pyobj_key);
value = pycall_pyobject_to_ruby(pyobj_value);
rb_yield(rb_assoc_new(key, value));
}
return Qnil;
}
|
.getattr(*args) ⇒ Object
902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 |
# File 'ext/pycall/pycall.c', line 902
static VALUE
pycall_libpython_helpers_m_getattr(int argc, VALUE *argv, VALUE mod)
{
VALUE pyptr, name, default_value;
pycall_drain_pending_decrefs();
if (rb_scan_args(argc, argv, "21", &pyptr, &name, &default_value) == 2) {
default_value = Qundef;
}
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
return pycall_getattr_default(pyptr, StringValueCStr(name), default_value);
}
|
.getitem(pyptr, key) ⇒ Object
1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 |
# File 'ext/pycall/pycall.c', line 1288
static VALUE
pycall_libpython_helpers_m_getitem(VALUE mod, VALUE pyptr, VALUE key)
{
PyObject *pyobj, *pyobj_key, *pyobj_v;
VALUE obj;
pycall_drain_pending_decrefs();
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
pyobj_key = pycall_convert_index(key);
pyobj_v = Py_API(PyObject_GetItem)(pyobj, pyobj_key);
pycall_Py_DecRef(pyobj_key);
if (!pyobj_v) {
pycall_pyerror_fetch_and_raise("PyObject_GetItem in pycall_libpython_helpers_m_getitem");
}
obj = pycall_pyobject_to_ruby(pyobj_v);
pycall_Py_DecRef(pyobj_v);
return obj;
}
|
.hasattr?(pyptr, name) ⇒ Boolean
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 |
# File 'ext/pycall/pycall.c', line 924
static VALUE
pycall_libpython_helpers_m_hasattr_p(VALUE mod, VALUE pyptr, VALUE name)
{
PyObject *pyobj;
int res;
pycall_drain_pending_decrefs();
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
res = Py_API(PyObject_HasAttrString)(pyobj, StringValueCStr(name));
return res ? Qtrue : Qfalse;
}
|
.import_module(*args) ⇒ Object
791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 |
# File 'ext/pycall/pycall.c', line 791
static VALUE
pycall_libpython_helpers_m_import_module(int argc, VALUE *argv, VALUE mod)
{
VALUE name, globals, locals, fromlist, level;
char const *name_cstr;
pycall_drain_pending_decrefs();
rb_scan_args(argc, argv, "14", &name, &globals, &locals, &fromlist, &level);
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
name_cstr = StringValueCStr(name);
if (argc == 1) {
return pycall_import_module(name_cstr);
}
if (argc == 5) {
level = rb_check_to_integer(level, "to_int");
}
else {
/* TODO: set the default level to 0 */
}
return pycall_import_module_level(name_cstr, globals, locals, fromlist, NUM2INT(level));
}
|
.sequence_contains(pyptr, key) ⇒ Object
1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 |
# File 'ext/pycall/pycall.c', line 1416
static VALUE
pycall_libpython_helpers_m_sequence_contains(VALUE mod, VALUE pyptr, VALUE key)
{
PyObject *pyobj, *pyobj_key;
int res;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, NULL);
if (!Py_API(PySequence_Check)(pyobj))
rb_raise(rb_eTypeError, "unexpected Python type %s (expected a Python sequence object)", Py_TYPE(pyobj)->tp_name);
pyobj_key = pycall_pyobject_from_ruby(key);
res = Py_API(PySequence_Contains)(pyobj, pyobj_key);
pycall_Py_DecRef(pyobj_key);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PySequence_Contains");
}
return res ? Qtrue : Qfalse;
}
|
.sequence_each(pyptr) ⇒ Object
1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 |
# File 'ext/pycall/pycall.c', line 1438
static VALUE
pycall_libpython_helpers_m_sequence_each(VALUE mod, VALUE pyptr)
{
PyObject *pyobj, *pyobj_iter, *pyobj_item;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, NULL);
if (!Py_API(PySequence_Check)(pyobj))
rb_raise(rb_eTypeError, "unexpected Python type %s (expected a Python sequence object)", Py_TYPE(pyobj)->tp_name);
pyobj_iter = Py_API(PyObject_GetIter)(pyobj);
if (!pyobj_iter) {
pycall_pyerror_fetch_and_raise("PyObject_GetIter in pycall_libpython_helpers_m_sequence_each");
}
while ((pyobj_item = Py_API(PyIter_Next)(pyobj_iter))) {
rb_yield(pycall_pyobject_to_ruby(pyobj_item));
pycall_Py_DecRef(pyobj_item);
}
pycall_Py_DecRef(pyobj_iter);
if (Py_API(PyErr_Occurred)() != NULL) {
pycall_pyerror_fetch_and_raise("checking error just in case at the end of pycall_libpython_helpers_m_sequence_each");
}
return Qnil;
}
|
.setattr(pyptr, name, val) ⇒ Object
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 |
# File 'ext/pycall/pycall.c', line 946
static VALUE
pycall_libpython_helpers_m_setattr(VALUE mod, VALUE pyptr, VALUE name, VALUE val)
{
PyObject *pyobj, *pyval;
pycall_drain_pending_decrefs();
if (!is_pycall_pyptr(pyptr)) {
rb_raise(rb_eTypeError, "PyCall::PyPtr is required");
}
pyobj = get_pyobj_ptr(pyptr);
if (RB_TYPE_P(name, T_SYMBOL)) {
name = rb_sym_to_s(name);
}
pyval = pycall_pyobject_from_ruby(val);
if (Py_API(PyObject_SetAttrString)(pyobj, StringValueCStr(name), pyval) == -1) {
pycall_pyerror_fetch_and_raise("PyObject_SetAttrString");
}
return Qnil;
}
|
.setitem(pyptr, key, v) ⇒ Object
1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 |
# File 'ext/pycall/pycall.c', line 1315
static VALUE
pycall_libpython_helpers_m_setitem(VALUE mod, VALUE pyptr, VALUE key, VALUE v)
{
PyObject *pyobj, *pyobj_key, *pyobj_value;
int res;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, NULL);
pyobj_key = pycall_convert_index(key);
pyobj_value = pycall_pyobject_from_ruby(v);
res = Py_API(PyObject_SetItem)(pyobj, pyobj_key, pyobj_value);
pycall_Py_DecRef(pyobj_key);
pycall_Py_DecRef(pyobj_value);
if (res == -1) {
pycall_pyerror_fetch_and_raise("PyObject_SetItem in pycall_libpython_helpers_m_setitem");
}
return v;
}
|
.str(pyptr) ⇒ Object
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 |
# File 'ext/pycall/pycall.c', line 1357
static VALUE
pycall_libpython_helpers_m_str(VALUE mod, VALUE pyptr)
{
PyObject *pyobj, *pyobj_str;
pycall_drain_pending_decrefs();
pyobj = check_get_pyobj_ptr(pyptr, NULL);
pyobj_str = Py_API(PyObject_Str)(pyobj);
if (!pyobj_str) {
pycall_pyerror_fetch_and_raise("PyObject_Str");
}
VALUE v = pycall_pyobject_to_ruby(pyobj_str);
pycall_Py_DecRef(pyobj_str);
return v;
}
|
.unicode_literals? ⇒ Boolean
PyCall::Helpers ====
748 749 750 751 752 |
# File 'ext/pycall/pycall.c', line 748
static VALUE
pycall_libpython_helpers_m_unicode_literals_p(VALUE mod)
{
return python_is_unicode_literals ? Qtrue : Qfalse;
}
|