forked from rstudio/reticulate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
python.cpp
3470 lines (2711 loc) · 94.4 KB
/
python.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
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
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
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
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
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
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
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
#include "libpython.h"
#define RCPP_NO_MODULES
#define RCPP_NO_SUGAR
#include <Rcpp.h>
using namespace Rcpp;
#include "signals.h"
#include "reticulate_types.h"
#include "common.h"
#include "event_loop.h"
#include "tinythread.h"
#include <fstream>
#include <time.h>
#ifndef _WIN32
#include <dlfcn.h>
#else
#define WIN32_LEAN_AND_MEAN 1
#include <windows.h>
#endif
using namespace reticulate::libpython;
// track whether we are using python 3 (set during py_initialize)
bool s_isPython3 = false;
// [[Rcpp::export]]
bool is_python3() {
return s_isPython3;
}
// track whether this is an interactive session
bool s_isInteractive = false;
bool is_interactive() {
return s_isInteractive;
}
// a simplified version of loadSymbol adopted from libpython.cpp
void loadSymbol(void* pLib, const std::string& name, void** ppSymbol)
{
*ppSymbol = NULL;
#ifdef _WIN32
*ppSymbol = (void*) ::GetProcAddress((HINSTANCE)pLib, name.c_str());
#else
*ppSymbol = ::dlsym(pLib, name.c_str());
#endif
}
// track whether we have required numpy
std::string s_numpy_load_error;
bool haveNumPy() {
return s_numpy_load_error.empty();
}
bool requireNumPy() {
if (!haveNumPy())
stop("Required version of NumPy not available: " + s_numpy_load_error);
return true;
}
bool isPyArray(PyObject* object) {
if (!haveNumPy()) return false;
return PyArray_Check(object);
}
bool isPyArrayScalar(PyObject* object) {
if (!haveNumPy()) return false;
return PyArray_CheckScalar(object);
}
// static buffers for Py_SetProgramName / Py_SetPythonHome
std::string s_python;
std::wstring s_python_v3;
std::string s_pythonhome;
std::wstring s_pythonhome_v3;
// helper to convert std::string to std::wstring
std::wstring to_wstring(const std::string& str) {
std::wstring ws = std::wstring(str.size(), L' ');
ws.resize(std::mbstowcs(&ws[0], str.c_str(), str.size()));
return ws;
}
// helper to convert std::wstring to std::string
std::string to_string(const std::wstring& ws) {
int maxnchar = ws.size() * 4;
char *buffer = (char*) malloc(sizeof(char) * maxnchar);
int nchar = wcstombs(buffer, ws.c_str(), maxnchar);
std::string s(buffer, nchar);
free(buffer);
return s;
}
// forward declare error handling utility
SEXP py_fetch_error(bool maybe_reuse_cached_r_trace = false);
const char *r_object_string = "r_object";
// wrap an R object in a longer-lived python object "capsule"
SEXP py_capsule_read(PyObject* capsule) {
SEXP object = (SEXP) PyCapsule_GetPointer(capsule, r_object_string);
if (object == NULL)
throw PythonException(py_fetch_error());
// Rcpp_precious_preserve() returns a cell of a doubly linked list
// with the original object preserved in the cell TAG().
return TAG(object);
}
tthread::thread::id s_main_thread = 0;
bool is_main_thread() {
if (s_main_thread == 0)
return true;
return s_main_thread == tthread::this_thread::get_id();
}
int free_sexp(void* sexp) {
// wrap Rcpp_precious_remove() to satisfy
// Py_AddPendingCall() signature and return value requirements
Rcpp_precious_remove((SEXP) sexp);
return 0;
}
void Rcpp_precious_remove_main_thread(SEXP object) {
if (is_main_thread()) {
return Rcpp_precious_remove(object);
}
// #Py_AddPendingCall can fail sometimes, so we retry a few times
const size_t wait_ms = 100;
size_t waited_ms = 0;
while (Py_AddPendingCall(free_sexp, object) != 0) {
tthread::this_thread::sleep_for(tthread::chrono::milliseconds(wait_ms));
// increment total wait time and print a warning every 60 seconds
waited_ms += wait_ms;
if ((waited_ms % 60000) == 0)
PySys_WriteStderr("Waiting to schedule object finalizer on main R interpeter thread...\n");
else if (waited_ms > 60000 * 2) {
// if we've waited more than 2 minutes, something is wrong
PySys_WriteStderr("Error: unable to register R object finalizer on main thread\n");
return;
}
}
}
void py_capsule_free(PyObject* capsule) {
SEXP object = (SEXP)PyCapsule_GetPointer(capsule, r_object_string);
if (object == NULL)
throw PythonException(py_fetch_error());
// the R api access must be from the main thread
Rcpp_precious_remove_main_thread(object);
}
PyObject* py_capsule_new(SEXP object) {
// if object == R_NilValue, this is a no-op, R_NilValue is reflected back.
object = Rcpp_precious_preserve(object);
return PyCapsule_New((void *)object, r_object_string, py_capsule_free);
}
PyObject* py_get_attr(PyObject* object, const std::string& name) {
if (PyObject_HasAttrString(object, name.c_str()))
return PyObject_GetAttrString(object, name.c_str());
else
return NULL;
}
bool is_r_object_capsule(PyObject* capsule) {
return PyCapsule_IsValid(capsule, r_object_string);
}
// helper class for ensuring decref of PyObject in the current scope
template <typename T>
class PyPtr {
public:
// attach on creation, decref on destruction
PyPtr()
: object_(NULL)
{
}
explicit PyPtr(T* object)
: object_(object)
{
}
virtual ~PyPtr()
{
if (object_ != NULL) {
Py_DecRef((PyObject*) object_);
}
}
operator T*() const
{
return object_;
}
T* get() const
{
return object_;
}
void assign(T* object)
{
object_ = object;
}
T* detach()
{
T* object = object_;
object_ = NULL;
return object;
}
bool is_null() const
{
return object_ == NULL;
}
private:
// prevent copying
PyPtr(const PyPtr&);
PyPtr& operator=(const PyPtr&);
// underlying object
T* object_;
};
typedef PyPtr<PyObject> PyObjectPtr;
typedef PyPtr<PyArray_Descr> PyArray_DescrPtr;
inline PyObject* PyUnicode_AsBytes(PyObject* str) {
return PyUnicode_AsEncodedString(str, /* encoding = */ NULL, /* errors = */ "ignore");
// encoding = NULL is fastpath to "utf-8"
}
PyObject* as_python_str(const std::string& str);
std::string as_std_string(PyObject* str) {
// conver to bytes if its unicode
PyObjectPtr pStr;
if (PyUnicode_Check(str) || isPyArrayScalar(str)) {
str = PyUnicode_AsBytes(str);
pStr.assign(str);
}
char* buffer;
Py_ssize_t length;
int res = is_python3() ?
PyBytes_AsStringAndSize(str, &buffer, &length) :
PyString_AsStringAndSize(str, &buffer, &length);
if (res == -1)
throw PythonException(py_fetch_error());
return std::string(buffer, length);
}
#define as_utf8_r_string(str) Rcpp::String(as_std_string(str))
PyObject* as_python_str(SEXP strSEXP) {
if (is_python3()) {
// python3 doesn't have PyString and all strings are unicode so
// make sure we get a unicode representation from R
const char * value = Rf_translateCharUTF8(strSEXP);
return PyUnicode_FromString(value);
} else {
const char * value = Rf_translateChar(strSEXP);
return PyString_FromString(value);
}
}
PyObject* as_python_str(const std::string& str) {
if (is_python3()) {
return PyUnicode_FromString(str.c_str());
} else {
return PyString_FromString(str.c_str());
}
}
bool has_null_bytes(PyObject* str) {
char* buffer;
int res = PyString_AsStringAndSize(str, &buffer, NULL);
if (res == -1) {
py_fetch_error();
return true;
} else {
return false;
}
}
// helpers to narrow python array type to something convertable from R,
// guaranteed to return NPY_BOOL, NPY_LONG, NPY_DOUBLE, or NPY_CDOUBLE
// (throws an exception if it's unable to return one of these types)
int narrow_array_typenum(int typenum) {
switch(typenum) {
// logical
case NPY_BOOL:
typenum = NPY_BOOL;
break;
// integer
case NPY_BYTE:
case NPY_UBYTE:
case NPY_SHORT:
case NPY_USHORT:
case NPY_INT:
typenum = NPY_LONG;
break;
// double
case NPY_UINT:
case NPY_ULONG:
case NPY_ULONGLONG:
case NPY_LONG:
case NPY_LONGLONG:
case NPY_HALF:
case NPY_FLOAT:
case NPY_DOUBLE:
typenum = NPY_DOUBLE;
break;
// complex
case NPY_CFLOAT:
case NPY_CDOUBLE:
typenum = NPY_CDOUBLE;
break;
// string/object (leave these alone)
case NPY_STRING:
case NPY_UNICODE:
case NPY_OBJECT:
break;
// unsupported
default:
stop("Conversion from numpy array type %d is not supported", typenum);
break;
}
return typenum;
}
int narrow_array_typenum(PyArrayObject* array) {
return narrow_array_typenum(PyArray_TYPE(array));
}
int narrow_array_typenum(PyArray_Descr* descr) {
return narrow_array_typenum(descr->type_num);
}
bool is_numpy_str(PyObject* x) {
if (!isPyArrayScalar(x))
return false; // ndarray or other, not string
PyArray_DescrPtr descrPtr(PyArray_DescrFromScalar(x));
int typenum = narrow_array_typenum(descrPtr);
return (typenum == NPY_STRING || typenum == NPY_UNICODE);
}
bool is_python_str(PyObject* x) {
if (PyUnicode_Check(x))
return true;
// python3 doesn't have PyString_* so mask it out (all strings in
// python3 will get caught by PyUnicode_Check, we'll ignore
// PyBytes entirely and let it remain a python object)
else if (!is_python3() && PyString_Check(x) && !has_null_bytes(x))
return true;
else if (is_numpy_str(x))
return true;
else
return false;
}
// check whether a PyObject is None
bool py_is_none(PyObject* object) {
return object == Py_None;
}
// convenience wrapper for PyImport_Import
PyObject* py_import(const std::string& module) {
PyObjectPtr module_str(as_python_str(module));
return PyImport_Import(module_str);
}
std::string as_r_class(PyObject* classPtr) {
PyObjectPtr namePtr(PyObject_GetAttrString(classPtr, "__name__"));
std::ostringstream ostr;
std::string module;
if (PyObject_HasAttrString(classPtr, "__module__")) {
PyObjectPtr modulePtr(PyObject_GetAttrString(classPtr, "__module__"));
module = as_std_string(modulePtr) + ".";
std::string builtin("__builtin__");
if (module.find(builtin) == 0)
module.replace(0, builtin.length(), "python.builtin");
std::string builtins("builtins");
if (module.find(builtins) == 0)
module.replace(0, builtins.length(), "python.builtin");
} else {
module = "python.builtin.";
}
ostr << module << as_std_string(namePtr);
return ostr.str();
}
std::vector<std::string> py_class_names(PyObject* object) {
// class
PyObjectPtr classPtr(PyObject_GetAttrString(object, "__class__"));
if (classPtr.is_null())
throw PythonException(py_fetch_error());
// call inspect.getmro to get the class and it's bases in
// method resolution order
static PyObject* getmro = NULL;
if (getmro == NULL) {
PyObjectPtr inspect(py_import("inspect"));
if (inspect.is_null())
throw PythonException(py_fetch_error());
getmro = PyObject_GetAttrString(inspect, "getmro");
if (getmro == NULL)
throw PythonException(py_fetch_error());
}
PyObjectPtr classes(PyObject_CallFunctionObjArgs(getmro, classPtr.get(), NULL));
if (classes.is_null())
throw PythonException(py_fetch_error());
// start adding class names
std::vector<std::string> classNames;
// add the bases to the R class attribute
Py_ssize_t len = PyTuple_Size(classes);
for (Py_ssize_t i = 0; i < len; i++) {
PyObject* base = PyTuple_GetItem(classes, i); // borrowed
classNames.push_back(as_r_class(base));
}
// return constructed class names
return classNames;
}
// wrap a PyObject
PyObjectRef py_ref(PyObject* object,
bool convert,
const std::string& extraClass = "")
{
// wrap
PyObjectRef ref(object, convert);
// class attribute
std::vector<std::string> attrClass;
// add extra class if requested
if (!extraClass.empty() &&
std::find(attrClass.begin(),
attrClass.end(),
extraClass) == attrClass.end()) {
attrClass.push_back(extraClass);
}
// register R classes
if (PyObject_HasAttrString(object, "__class__")) {
std::vector<std::string> classNames = py_class_names(object);
attrClass.insert(attrClass.end(), classNames.begin(), classNames.end());
}
// add python.builtin.object if we don't already have it
if (std::find(attrClass.begin(), attrClass.end(), "python.builtin.object") == attrClass.end()) {
attrClass.push_back("python.builtin.object");
}
// apply class filter
Rcpp::Environment pkgEnv = Rcpp::Environment::namespace_env("reticulate");
Rcpp::Function py_filter_classes = pkgEnv["py_filter_classes"];
attrClass = as< std::vector<std::string> >(py_filter_classes(attrClass));
// set classes
ref.attr("class") = attrClass;
// return ref
return ref;
}
//' Check if a Python object is a null externalptr
//'
//' @param x Python object
//'
//' @return Logical indicating whether the object is a null externalptr
//'
//' @details When Python objects are serialized within a persisted R
//' environment (e.g. .RData file) they are deserialized into null
//' externalptr objects (since the Python session they were originally
//' connected to no longer exists). This function allows you to safely
//' check whether whether a Python object is a null externalptr.
//'
//' The `py_validate` function is a convenience function which calls
//' `py_is_null_xptr` and throws an error in the case that the xptr
//' is `NULL`.
//'
//' @export
// [[Rcpp::export]]
bool py_is_null_xptr(PyObjectRef x) {
return x.is_null_xptr();
}
//' @rdname py_is_null_xptr
//' @export
// [[Rcpp::export]]
void py_validate_xptr(PyObjectRef x) {
if (py_is_null_xptr(x)) {
stop("Object is a null externalptr (it may have been disconnected from "
" the session where it was created)");
}
}
bool option_is_true(const std::string& name) {
SEXP valueSEXP = Rf_GetOption(Rf_install(name.c_str()), R_BaseEnv);
return Rf_isLogical(valueSEXP) && (as<bool>(valueSEXP) == true);
}
bool traceback_enabled() {
Environment pkgEnv = Environment::namespace_env("reticulate");
Function func = pkgEnv["traceback_enabled"];
return as<bool>(func());
}
// copied directly from purrr; used to call rlang::trace_back() in
// py_fetch_error() in such a way that it doesn't introduce a new
// frame in returned traceback
SEXP current_env(void) {
static SEXP call = NULL;
if (!call) {
// `sys.frame(sys.nframe())` doesn't work because `sys.nframe()`
// returns the number of the frame in which evaluation occurs. It
// doesn't return the number of frames on the stack. So we'd need
// to evaluate it in the last frame on the stack which is what we
// are looking for to begin with. We use instead this workaround:
// Call `sys.frame()` from a closure to push a new frame on the
// stack, and use negative indexing to get the previous frame.
ParseStatus status;
SEXP code = PROTECT(Rf_mkString("sys.frame(-1)"));
SEXP parsed = PROTECT(R_ParseVector(code, -1, &status, R_NilValue));
SEXP body = VECTOR_ELT(parsed, 0);
SEXP fn = PROTECT(Rf_allocSExp(CLOSXP));
SET_FORMALS(fn, R_NilValue);
SET_BODY(fn, body);
SET_CLOENV(fn, R_BaseEnv);
call = Rf_lang1(fn);
R_PreserveObject(call);
UNPROTECT(3);
}
return Rf_eval(call, R_BaseEnv);
}
SEXP get_current_call(void) {
static SEXP call = NULL;
if (!call) {
ParseStatus status;
SEXP code = PROTECT(Rf_mkString("sys.call(-1)"));
SEXP parsed = PROTECT(R_ParseVector(code, -1, &status, R_NilValue));
SEXP body = VECTOR_ELT(parsed, 0);
SEXP fn = PROTECT(Rf_allocSExp(CLOSXP));
SET_FORMALS(fn, R_NilValue);
SET_BODY(fn, body);
SET_CLOENV(fn, R_BaseEnv);
call = Rf_lang1(fn);
R_PreserveObject(call);
UNPROTECT(3);
}
return Rf_eval(call, R_BaseEnv);
}
SEXP get_r_trace(bool maybe_use_cached = false) {
static SEXP get_r_trace_s = NULL;
static SEXP reticulate_ns = NULL;
if (!get_r_trace_s) {
reticulate_ns = R_FindNamespace(Rf_mkString("reticulate"));
get_r_trace_s = Rf_install("get_r_trace");
}
SEXP maybe_use_cached_ = PROTECT(Rf_ScalarLogical(maybe_use_cached));
SEXP trim_tail_ = PROTECT(Rf_ScalarInteger(1));
SEXP call = PROTECT(Rf_lang3(get_r_trace_s, maybe_use_cached_, trim_tail_));
SEXP result = PROTECT(Rf_eval(call, reticulate_ns));
UNPROTECT(4);
return result;
}
SEXP py_fetch_error(bool maybe_reuse_cached_r_trace) {
// TODO: we need to add a guardrail to catch cases when
// this is being invoked from not the main thread
// check whether this error was signaled via an interrupt.
// the intention here is to catch cases where reticulate is running
// Python code, an interrupt is signaled and caught by that code,
// and then the associated error is returned. in such a case, we
// want to forward that interrupt back to R so that the user is then
// returned back to the top level.
if (reticulate::signals::getPythonInterruptsPending()) {
PyErr_Clear();
reticulate::signals::setInterruptsPending(false);
reticulate::signals::setPythonInterruptsPending(false);
throw Rcpp::internal::InterruptedException();
}
PyObject *excType, *excValue, *excTraceback;
PyErr_Fetch(&excType, &excValue, &excTraceback); // we now own the PyObjects
if (!excType) {
Rcpp::stop("Unknown Python error.");
}
PyErr_NormalizeException(&excType, &excValue, &excTraceback);
if (excTraceback != NULL && excValue != NULL && s_isPython3) {
PyException_SetTraceback(excValue, excTraceback);
Py_DecRef(excTraceback);
}
PyObjectPtr pExcType(excType); // decref on exit
if (!PyObject_HasAttrString(excValue, "call")) {
// check if this exception originated in python using the `raise from`
// statement with an exception that we've already augmented with the full
// r_trace. (or similarly, raised a new exception inside an `except:` block
// while it is catching an Exception that contains an r_trace). If we find
// r_trace/r_call in a __context__ Exception, pull them forward to this
// topmost exception.
PyObject *context = NULL, *r_call = NULL, *r_trace = NULL;
PyObject *excValue_tmp = excValue;
while ((context = PyObject_GetAttrString(excValue_tmp, "__context__"))) {
if ((r_call = PyObject_GetAttrString(context, "call"))) {
PyObject_SetAttrString(excValue, "call", r_call);
Py_DecRef(r_call);
}
if ((r_trace = PyObject_GetAttrString(context, "trace"))) {
PyObject_SetAttrString(excValue, "trace", r_trace);
Py_DecRef(r_trace);
}
excValue_tmp = context;
Py_DecRef(context);
if(r_call || r_trace) {
break;
}
}
}
// make sure the exception object has some some attrs: call, trace
if (!PyObject_HasAttrString(excValue, "trace")) {
SEXP r_trace = PROTECT(get_r_trace(maybe_reuse_cached_r_trace));
PyObject* r_trace_capsule(py_capsule_new(r_trace));
PyObject_SetAttrString(excValue, "trace", r_trace_capsule);
Py_DecRef(r_trace_capsule);
UNPROTECT(1);
}
// Otherwise, try to capture the current call.
// A first draft of this tried using: SEXP r_call = get_last_call();
// with get_last_call() defined in Rcpp headers. Unfortunately, that would
// skip over the actual call of interest, and frequently return NULL
// for shallow call stacks. So we fetch the call directly
// using the R API.
if (!PyObject_HasAttrString(excValue, "call")) {
SEXP r_call = get_current_call();
PyObject *r_call_capsule(py_capsule_new(r_call));
PyObject_SetAttrString(excValue, "call", r_call_capsule);
Py_DecRef(r_call_capsule);
UNPROTECT(1);
}
// get the cppstack, r_cppstack
// FIXME: this doesn't seem to work, always returns NULL
// SEXP r_cppstack = PROTECT(rcpp_get_stack_trace());
// PyObject* r_cppstack_capsule(py_capsule_new(r_cppstack));
// UNPROTECT(1);
// PyObject_SetAttrString(excValue, "r_cppstack", r_cppstack_capsule);
// Py_DecRef(r_cppstack_capsule);
PyObjectRef cond(py_ref(excValue, true));
Environment pkg_globals(
Environment::namespace_env("reticulate").get(".globals"));
pkg_globals.assign("py_last_exception", cond);
if (flush_std_buffers() == -1)
warning(
"Error encountered when flushing python buffers sys.stderr and "
"sys.stdout");
return cond;
}
// [[Rcpp::export]]
std::string conditionMessage_from_py_exception(PyObjectRef exc) {
// invoke 'traceback.format_exception_only(<traceback>)'
PyObjectPtr tb_module(py_import("traceback"));
if (tb_module.is_null())
return "<unknown python exception, traceback module not found>";
PyObjectPtr format_exception_only(
PyObject_GetAttrString(tb_module, "format_exception_only"));
if (format_exception_only.is_null())
return "<unknown python exception, traceback format fn not found>";
PyObjectPtr formatted(PyObject_CallFunctionObjArgs(
format_exception_only, Py_TYPE(exc.get()), exc.get(), NULL));
if (formatted.is_null())
return "<unknown python exception, traceback format fn returned NULL>";
// build error text
std::ostringstream oss;
// PyList_GetItem() returns a borrowed reference, no need to decref.
for (Py_ssize_t i = 0, n = PyList_Size(formatted); i < n; i++)
oss << as_std_string(PyList_GetItem(formatted, i));
static std::string hint;
if (hint.empty()) {
Environment pkg_env(Environment::namespace_env("reticulate"));
Function hint_fn = pkg_env[".py_last_error_hint"];
CharacterVector r_result = hint_fn();
hint = Rcpp::as<std::string>(r_result[0]);
}
oss << hint;
std::string error = oss.str();
SEXP max_msg_len_s = PROTECT(Rf_GetOption1(Rf_install("warning.length")));
std::size_t max_msg_len(Rf_asInteger(max_msg_len_s));
UNPROTECT(1);
if (error.size() > max_msg_len) {
// R has a modest byte size limit for error messages, default 1000, user
// adjustable up to 8170. Error messages beyond the limit are silently
// truncated. If the message will be truncated, we truncate it a little
// better here and include a useful hint in the error message.
std::string trunc("<...truncated...>");
// Tensorflow since ~2.6 has been including a currated traceback as part of
// the formatted exception message, with the most user-actionable content
// towards the tail. Since the tail is the most useful part of the message,
// truncate from the middle of the exception by default, after including the
// first two lines.
int over(error.size() - max_msg_len);
int first_line_end_pos(error.find("\n"));
int second_line_start_pos(error.find("\n", first_line_end_pos + 1));
std::string head(error.substr(0, second_line_start_pos + 1));
std::string tail(
error.substr(over + head.size() + trunc.size() + 20,
std::string::npos));
// +20 to accommodate "Error: " and similar accruals from R signal handlers.
error = head + trunc + tail;
}
return error;
}
// check whether the PyObject can be mapped to an R scalar type
int r_scalar_type(PyObject* x) {
if (PyBool_Check(x))
return LGLSXP;
// integer
else if (PyInt_Check(x) || PyLong_Check(x))
return INTSXP;
// double
else if (PyFloat_Check(x))
return REALSXP;
// complex
else if (PyComplex_Check(x))
return CPLXSXP;
else if (is_python_str(x))
return STRSXP;
// not a scalar
else
return NILSXP;
}
// check whether the PyObject is a list of a single R scalar type
int scalar_list_type(PyObject* x) {
Py_ssize_t len = PyList_Size(x);
if (len == 0)
return NILSXP;
PyObject* first = PyList_GetItem(x, 0);
int scalarType = r_scalar_type(first);
if (scalarType == NILSXP)
return NILSXP;
for (Py_ssize_t i = 1; i<len; i++) {
PyObject* next = PyList_GetItem(x, i);
if (r_scalar_type(next) != scalarType)
return NILSXP;
}
return scalarType;
}
bool py_equal(PyObject* x, const std::string& str) {
PyObjectPtr pyStr(as_python_str(str));
if (pyStr.is_null())
throw PythonException(py_fetch_error());
return PyObject_RichCompareBool(x, pyStr, Py_EQ) == 1;
}
bool is_pandas_na(PyObject* x) {
// retrieve class object
PyObjectPtr pyClass(py_get_attr(x, "__class__"));
if (pyClass.is_null())
return false;
PyObjectPtr pyModule(py_get_attr(pyClass, "__module__"));
if (pyModule.is_null())
return false;
// check for expected module name
if (!py_equal(pyModule, "pandas._libs.missing"))
return false;
// retrieve class name
PyObjectPtr pyName(py_get_attr(pyClass, "__name__"));
if (pyName.is_null())
return false;
// check for expected names
return py_equal(pyName, "NAType") ||
py_equal(pyName, "C_NAType");
}
PyObject* numpy () {
const static PyObjectPtr numpy(PyImport_ImportModule("numpy"));
if (numpy.is_null()) {
throw PythonException(py_fetch_error());
}
return numpy;
}
bool is_pandas_na_like(PyObject* x) {
const static PyObjectPtr np_nan(PyObject_GetAttrString(numpy(), "nan"));
return is_pandas_na(x) || (x == Py_None) || (x == (PyObject*)np_nan);
}
void set_string_element(SEXP rArray, int i, PyObject* pyStr) {
if (is_pandas_na_like(pyStr)) {
SET_STRING_ELT(rArray, i, NA_STRING);
return;
}
std::string str = as_std_string(pyStr);
cetype_t ce = PyUnicode_Check(pyStr) ? CE_UTF8 : CE_NATIVE;
SEXP strSEXP = Rf_mkCharCE(str.c_str(), ce);
SET_STRING_ELT(rArray, i, strSEXP);
}
bool py_is_callable(PyObject* x) {
return PyCallable_Check(x) == 1 || PyObject_HasAttrString(x, "__call__");
}
// [[Rcpp::export]]
PyObjectRef py_none_impl() {
Py_IncRef(Py_None);
return py_ref(Py_None, false);
}
// [[Rcpp::export]]
bool py_is_callable(PyObjectRef x) {
if (x.is_null_xptr())
return false;
else
return py_is_callable(x.get());
}
// caches np.nditer function so we don't need to obtain it everytime we want to
// cast numpy string arrays into R objects.
PyObject* get_np_nditer () {
const static PyObjectPtr np_nditer(PyObject_GetAttrString(numpy(), "nditer"));
if (np_nditer.is_null()) {
throw PythonException(py_fetch_error());
}
return np_nditer;
}
// convert a python object to an R object
SEXP py_to_r(PyObject* x, bool convert) {
// NULL for Python None
if (py_is_none(x))
return R_NilValue;
// check for scalars
int scalarType = r_scalar_type(x);
if (scalarType != NILSXP) {
// logical
if (scalarType == LGLSXP)
return LogicalVector::create(x == Py_True);
// integer
else if (scalarType == INTSXP)
return IntegerVector::create(PyInt_AsLong(x));
// double
else if (scalarType == REALSXP)
return NumericVector::create(PyFloat_AsDouble(x));
// complex
else if (scalarType == CPLXSXP) {
Rcomplex cplx;
cplx.r = PyComplex_RealAsDouble(x);
cplx.i = PyComplex_ImagAsDouble(x);
return ComplexVector::create(cplx);
}
// string
else if (scalarType == STRSXP)
return CharacterVector::create(as_utf8_r_string(x));
else
return R_NilValue; // keep compiler happy
}
// list
else if (PyList_CheckExact(x)) {
Py_ssize_t len = PyList_Size(x);
int scalarType = scalar_list_type(x);