Module: PyCall::LibPython::Helpers

Defined in:
ext/pycall/pycall.c

Class Method Summary collapse

Class Method Details

.call_object(*args) ⇒ Object



813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
# File 'ext/pycall/pycall.c', line 813

static VALUE
pycall_libpython_helpers_m_call_object(int argc, VALUE *argv, VALUE mod)
{
  VALUE pyptr;
  PyObject *pyobj;

  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

Returns:

  • (Boolean)


797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
# File 'ext/pycall/pycall.c', line 797

static VALUE
pycall_libpython_helpers_m_callable_p(VALUE mod, VALUE pyptr)
{
  PyObject *pyobj;
  int res;

  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



702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
# File 'ext/pycall/pycall.c', line 702

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;

  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");
  }

  return pycall_pyobject_to_ruby(res);
}

.define_wrapper_method(wrapper, name) ⇒ Object



969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
# File 'ext/pycall/pycall.c', line 969

static VALUE
pycall_libpython_helpers_m_define_wrapper_method(VALUE mod, VALUE wrapper, VALUE name)
{
  VALUE pyptr, name_sym;
  PyObject *pyobj, *attr;
  char *name_cstr;

  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_sym = name;
    name = rb_sym_to_s(name);
  }
  else if (RB_TYPE_P(name, T_STRING)) {
    name_sym = rb_str_intern(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;
}

.delitem(pyptr, key, v) ⇒ Object



1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
# File 'ext/pycall/pycall.c', line 1083

static VALUE
pycall_libpython_helpers_m_delitem(VALUE mod, VALUE pyptr, VALUE key, VALUE v)
{
  PyObject *pyobj, *pyobj_key;
  int res;

  pyobj = check_get_pyobj_ptr(pyptr, NULL);
  pyobj_key = pycall_convert_index(key);

  res = Py_API(PyObject_DelItem)(pyobj, pyobj_key);
  if (res == -1) {
    pycall_pyerror_fetch_and_raise("PyObject_DelItem");
  }

  return v;
}

.dict_contains(pyptr, key) ⇒ Object



1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
# File 'ext/pycall/pycall.c', line 1115

static VALUE
pycall_libpython_helpers_m_dict_contains(VALUE mod, VALUE pyptr, VALUE key)
{
  PyObject *pyobj, *pyobj_key;
  int res;

  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);
  if (res == -1) {
    pycall_pyerror_fetch_and_raise("PyDict_Contains");
  }

  return res ? Qtrue : Qfalse;
}

.dict_each(pyptr) ⇒ Object



1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
# File 'ext/pycall/pycall.c', line 1131

static VALUE
pycall_libpython_helpers_m_dict_each(VALUE mod, VALUE pyptr)
{
  PyObject *pyobj, *pyobj_key, *pyobj_value;
  Py_ssize_t pos;

  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



757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'ext/pycall/pycall.c', line 757

static VALUE
pycall_libpython_helpers_m_getattr(int argc, VALUE *argv, VALUE mod)
{
  VALUE pyptr, name, default_value;

  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



1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
# File 'ext/pycall/pycall.c', line 1039

static VALUE
pycall_libpython_helpers_m_getitem(VALUE mod, VALUE pyptr, VALUE key)
{
  PyObject *pyobj, *pyobj_key, *pyobj_v;
  VALUE obj;

  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);
  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

Returns:

  • (Boolean)


777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'ext/pycall/pycall.c', line 777

static VALUE
pycall_libpython_helpers_m_hasattr_p(VALUE mod, VALUE pyptr, VALUE name)
{
  PyObject *pyobj;
  int res;

  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



650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'ext/pycall/pycall.c', line 650

static VALUE
pycall_libpython_helpers_m_import_module(int argc, VALUE *argv, VALUE mod)
{
  VALUE name, globals, locals, fromlist, level;
  char const *name_cstr;

  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



1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
# File 'ext/pycall/pycall.c', line 1150

static VALUE
pycall_libpython_helpers_m_sequence_contains(VALUE mod, VALUE pyptr, VALUE key)
{
  PyObject *pyobj, *pyobj_key;
  int res;

  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);
  if (res == -1) {
    pycall_pyerror_fetch_and_raise("PySequence_Contains");
  }

  return res ? Qtrue : Qfalse;
}

.sequence_each(pyptr) ⇒ Object



1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
# File 'ext/pycall/pycall.c', line 1169

static VALUE
pycall_libpython_helpers_m_sequence_each(VALUE mod, VALUE pyptr)
{
  PyObject *pyobj, *pyobj_iter, *pyobj_item;

  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;
}

.setitem(pyptr, key, v) ⇒ Object



1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
# File 'ext/pycall/pycall.c', line 1063

static VALUE
pycall_libpython_helpers_m_setitem(VALUE mod, VALUE pyptr, VALUE key, VALUE v)
{
  PyObject *pyobj, *pyobj_key, *pyobj_value;
  int res;

  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);
  if (res == -1) {
    pycall_pyerror_fetch_and_raise("PyObject_SetItem in pycall_libpython_helpers_m_setitem");
  }
  Py_API(Py_DecRef(pyobj_key));
  Py_API(Py_DecRef(pyobj_value));

  return v;
}

.str(pyptr) ⇒ Object



1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
# File 'ext/pycall/pycall.c', line 1100

static VALUE
pycall_libpython_helpers_m_str(VALUE mod, VALUE pyptr)
{
  PyObject *pyobj, *pyobj_str;

  pyobj = check_get_pyobj_ptr(pyptr, NULL);

  pyobj_str = Py_API(PyObject_Str)(pyobj);
  if (!pyobj_str) {
    pycall_pyerror_fetch_and_raise("PyObject_Str");
  }

  return pycall_pyobject_to_ruby(pyobj_str);
}

.unicode_literals?Boolean

PyCall::Helpers ====

Returns:

  • (Boolean)


607
608
609
610
611
# File 'ext/pycall/pycall.c', line 607

static VALUE
pycall_libpython_helpers_m_unicode_literals_p(VALUE mod)
{
  return python_is_unicode_literals ? Qtrue : Qfalse;
}