-
Notifications
You must be signed in to change notification settings - Fork 2
/
lp_creation_python_policy.cc
1443 lines (1303 loc) · 40.6 KB
/
lp_creation_python_policy.cc
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
/*
* Copyright (C) 2024 SUSE Software Solutions Germany GmbH
*
* This file is part of klp-ccp.
*
* klp-ccp is free software: you can redistribute it and/or modify it
* under the terms of version 2 of the GNU General Public License as
* published by the Free Software Foundation.
*
* klp-ccp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with klp-ccp. If not, see <https://www.gnu.org/licenses/>.
*/
// Must come first.
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <osdefs.h>
#include "lp_creation_python_policy.hh"
#include "ast.hh"
#include "pp_result.hh"
#include "code_remark.hh"
#include "code_remarks.hh"
#include "raw_pp_token.hh"
#include "lp_except.hh"
#include <string>
#include <new>
#include <functional>
using namespace klp::ccp;
python_except::python_except(const char *what)
: std::runtime_error{what}
{}
python_except::~python_except() noexcept = default;
void klp::ccp::handle_python_except()
{
if (PyErr_Occurred())
PyErr_Print();
}
namespace
{
struct allocated_ids
{
using allocated_ids_type = lp_creation_policy::allocated_ids_type;
static PyObject* create(const allocated_ids_type &ids);
static void py_dealloc(PyObject *self);
static PyObject* contains(PyObject *self, PyObject *args,
PyObject *kwargs);
static PyMethodDef py_methods[];
static PyTypeObject py_type;
PyObject_HEAD;
const allocated_ids_type *_wrapped_ids;
};
}
extern "C" {
static void allocated_ids_py_dealloc(PyObject *self);
static PyObject* allocated_ids_py_contains(PyObject *self, PyObject *args,
PyObject *kwargs);
}
PyObject* allocated_ids::create(const allocated_ids_type &ids)
{
PyObject *o = PyType_GenericAlloc(&allocated_ids::py_type, 0);
if (!o)
return nullptr;
reinterpret_cast<allocated_ids*>(o)->_wrapped_ids = &ids;
return o;
}
void allocated_ids_py_dealloc(PyObject *self)
{
Py_TYPE(self)->tp_free(self);
}
PyObject*
allocated_ids_py_contains(PyObject *self, PyObject *args, PyObject *kwargs)
{
const allocated_ids *me = reinterpret_cast<const allocated_ids *>(self);
if (!me->_wrapped_ids) {
PyErr_SetString(PyExc_ReferenceError,
"Accessed AllocatedIds instance out of scope.");
return nullptr;
}
const char *query_id = nullptr;
// Beginning with Python 3.13 this can be constified.
char kw_id[] = "id";
char * kwlist[] = { kw_id, nullptr };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", &kwlist[0],
&query_id)) {
return nullptr;
}
if (me->_wrapped_ids->find(query_id) != me->_wrapped_ids->end())
Py_RETURN_TRUE;
Py_RETURN_FALSE;
}
PyMethodDef allocated_ids::py_methods[] {
{
"contains",
reinterpret_cast<PyCFunction>(allocated_ids_py_contains),
METH_VARARGS | METH_KEYWORDS,
"Check whether a given identifier has been used somewhere already."
},
{ nullptr }
};
// Note: some members are initialized outside the initializer list
// from pyinit_ccp_interface_mod().
PyTypeObject allocated_ids::py_type = {
.ob_base = PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "_ccp.AllocatedIds",
.tp_basicsize = sizeof(allocated_ids),
};
static PyModuleDef py_ccp_interface_mod_def = {
.m_base = PyModuleDef_HEAD_INIT,
.m_name = "_ccp",
.m_doc = "Provide interfacing functionality between ccp and Python.",
.m_size = -1,
};
static PyObject *pyinit_ccp_interface_mod(void)
{
// Install the ccp <-> Python interface extension.
// g++ 7.5 does not support initialization of these members with a
// designated initializer list yet and would bail out with. "sorry,
// unimplemented: non-trivial designated initializers not
// supported". For the moment being, do it from here then :/
allocated_ids::py_type.tp_dealloc =
static_cast<destructor>(allocated_ids_py_dealloc);
allocated_ids::py_type.tp_flags =
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_DISALLOW_INSTANTIATION;
allocated_ids::py_type.tp_doc =
PyDoc_STR("Registry of id tokens used somewhere.");
allocated_ids::py_type.tp_methods = allocated_ids::py_methods;
if (PyType_Ready(&allocated_ids::py_type))
return nullptr;
PyObject *ccp_interface_mod = PyModule_Create(&py_ccp_interface_mod_def);
if (!ccp_interface_mod)
return nullptr;
if ((PyModule_AddObjectRef
(ccp_interface_mod,
"AllocatedIds",
reinterpret_cast<PyObject *>(&allocated_ids::py_type))) < 0) {
Py_DECREF(ccp_interface_mod);
return nullptr;
}
return ccp_interface_mod;
}
struct lp_creation_python_policy::_data
{
_data() noexcept;
~_data() noexcept;
using instantiate_remark_type =
std::function<code_remark(const code_remark::severity,
std::string&&)>;
bool is_py_lp_creation_policy_warning(PyObject *o) const noexcept;
bool is_py_lp_creation_policy_error(PyObject *o) const noexcept;
code_remark convert_py_lp_creation_policy_warning
(PyObject *o,
const instantiate_remark_type &instantiate) const;
void convert_py_lp_creation_policy_warnings
(PyObject *o,
const instantiate_remark_type &instantiate,
code_remarks &remarks) const;
code_remark convert_py_lp_creation_policy_error
(PyObject *o,
const instantiate_remark_type &instantiate,
code_remarks &remarks) const;
PyObject* unwrap_py_lp_creation_policy_call_result
(PyObject *r,
const instantiate_remark_type &instantiate_remark,
code_remarks &remarks) const;
PyObject* build_py_loc(const pp_token_index id_tok,
const pp_result &pp_result) const;
PyObject*
get_py_linkage_enum_member(const ast::linkage::linkage_kind &linkage)
const noexcept;
PyObject *wrap_allocated_ids(const allocated_ids::allocated_ids_type &ids)
const;
lp_creation_policy::symbol_modification
convert_symbol_modification(PyObject *o);
lp_creation_policy::externalized_symbol_modification
convert_externalized_symbol_modification(PyObject *o);
PyObject *linkage_enum_member_none;
PyObject *linkage_enum_member_internal;
PyObject *linkage_enum_member_external;
PyObject *linkage_enum_member_nested_fun_auto;
PyObject *symbol_modification_cls;
long linkage_change_enum_member_none_val = -1;
long linkage_change_enum_member_make_static_val = -1;
long linkage_change_enum_member_make_extern_val = -1;
PyObject *externalized_symbol_modification_cls;
PyObject *lp_creation_policy_warning_cls;
PyObject *lp_creation_policy_error_cls;
PyObject *pol_obj;
};
lp_creation_python_policy::_data::_data() noexcept
: linkage_enum_member_none{nullptr},
linkage_enum_member_internal{nullptr},
linkage_enum_member_external{nullptr},
linkage_enum_member_nested_fun_auto{nullptr},
symbol_modification_cls{nullptr},
linkage_change_enum_member_none_val{-1},
linkage_change_enum_member_make_static_val{-1},
linkage_change_enum_member_make_extern_val{-1},
externalized_symbol_modification_cls{nullptr},
lp_creation_policy_warning_cls{nullptr},
lp_creation_policy_error_cls{nullptr},
pol_obj{nullptr}
{}
lp_creation_python_policy::_data::~_data() noexcept
{
Py_XDECREF(linkage_enum_member_none);
Py_XDECREF(linkage_enum_member_internal);
Py_XDECREF(linkage_enum_member_external);
Py_XDECREF(linkage_enum_member_nested_fun_auto);
Py_XDECREF(symbol_modification_cls);
Py_XDECREF(externalized_symbol_modification_cls);
Py_XDECREF(lp_creation_policy_warning_cls);
Py_XDECREF(lp_creation_policy_error_cls);
Py_XDECREF(pol_obj);
}
bool lp_creation_python_policy::_data::
is_py_lp_creation_policy_warning(PyObject *o) const noexcept
{
return PyObject_IsInstance(o, lp_creation_policy_warning_cls);
}
bool lp_creation_python_policy::_data::
is_py_lp_creation_policy_error(PyObject *o) const noexcept
{
return o && PyObject_IsInstance(o, lp_creation_policy_error_cls);
}
code_remark lp_creation_python_policy::_data::
convert_py_lp_creation_policy_warning
(PyObject *o,
const instantiate_remark_type &instantiate) const
{
if (!is_py_lp_creation_policy_warning(o)) {
throw python_except {
"expected instance of python class \"ccp.LpCreationPolicyWarning\""
};
}
PyObject *msg_member = PyObject_GetAttrString(o, "_msg");
if (!msg_member) {
throw python_except {
"expected \"_msg\" member in python class \"ccp.LpCreationPolicyWarning\""
};
}
if (!PyUnicode_Check(msg_member)) {
Py_DECREF(msg_member);
throw python_except {
("expected string value in "
"python member \"ccp.LpCreationPolicyWarning._msg\"")
};
}
const char *pmsg = PyUnicode_AsUTF8(msg_member);
if (!pmsg) {
Py_DECREF(msg_member);
throw python_except {
("couldn't obtain string value from "
"python member \"ccp.LpCreationPolicyWarning._msg\"")
};
}
std::string msg;
try {
msg = pmsg;
Py_DECREF(msg_member);
} catch (...) {
Py_DECREF(msg_member);
throw;
}
return instantiate(code_remark::severity::warning,
std::move(msg));
}
void lp_creation_python_policy::_data::convert_py_lp_creation_policy_warnings
(PyObject *o,
const instantiate_remark_type &instantiate,
code_remarks &remarks) const
{
if (!o || Py_IsNone(o))
return;
// A single warning.
if (is_py_lp_creation_policy_warning(o)) {
remarks.add
(convert_py_lp_creation_policy_warning(o, instantiate));
return;
}
// Otherwiseit is expected that the passed object is an iterable
// over warning instances.
if (Py_TYPE(o)->tp_iter == NULL && !PySequence_Check(o)) {
throw python_except {
"expected iterable over python \"ccp.LpCreationPolicyWarning\" instances"
};
}
PyObject *iter = PyObject_GetIter(o);
if (!iter) {
throw python_except {
("failed to obtain iterator over python"
" \"ccp.LpCreationPolicyWarning\" instances")
};
}
PyObject *e = PyIter_Next(iter);
while (e) {
if (!is_py_lp_creation_policy_warning(e)) {
throw python_except {
("unexpected non-\"ccp.LpCreationPolicyWarning\" instance"
" in python iterable")
};
Py_DECREF(e);
Py_DECREF(iter);
}
try {
remarks.add
(convert_py_lp_creation_policy_warning(e, instantiate));
} catch (...) {
Py_DECREF(e);
Py_DECREF(iter);
throw;
}
Py_DECREF(e);
e = PyIter_Next(iter);
}
Py_DECREF(iter);
if (PyErr_Occurred()) {
throw python_except {
"failed to iterate over \"ccp.LpCreationPolicyWarning\" instances"
};
}
}
code_remark lp_creation_python_policy::_data::
convert_py_lp_creation_policy_error
(PyObject *o,
const instantiate_remark_type &instantiate,
code_remarks &remarks) const
{
if (!is_py_lp_creation_policy_error(o)) {
throw python_except {
"expected instance of python class \"ccp.LpCreationPolicyError\""
};
} else if (!PyExceptionInstance_Check(o)) {
throw python_except {"expected python exception instance"};
}
// PyException_GetArgs() is available only from Python 3.12 onwards.
// In the meanwhile, grab the msg from the _msg member duplicated in
// from the inheriting class for this purpose..
PyObject *msg_member = PyObject_GetAttrString(o, "_msg");
if (!msg_member) {
throw python_except {
"failed to retrieve \"ccp.LpCreationPolicyError\" python exception args"
};
}
if (!PyUnicode_Check(msg_member)) {
Py_DECREF(msg_member);
throw python_except {
("expected string value in "
"\"ccp.LpCreationPolicyError\" python exception")
};
}
const char *pmsg = PyUnicode_AsUTF8(msg_member);
if (!pmsg) {
Py_DECREF(msg_member);
throw python_except {
("couldn't obtain string value from "
"\"ccp.LpCreationPolicyError\" python exception")
};
}
std::string msg;
try {
msg = pmsg;
Py_DECREF(msg_member);
} catch (...) {
Py_DECREF(msg_member);
throw;
}
// Extract and store any warnings preceeding the fatal error.
PyObject *warnings_member = PyObject_GetAttrString(o, "_warnings");
if (!warnings_member) {
throw python_except {
("expected \"_warnings\" member in python class"
" \"ccp.LpCreationPolicyError\"")
};
}
try {
convert_py_lp_creation_policy_warnings(warnings_member,
instantiate,
remarks);
Py_DECREF(warnings_member);
} catch (...) {
Py_DECREF(warnings_member);
throw;
}
code_remark err_remark = instantiate(code_remark::severity::fatal,
std::move(msg));
remarks.add(err_remark);
return err_remark;
}
PyObject* lp_creation_python_policy::_data::
unwrap_py_lp_creation_policy_call_result
(PyObject *r,
const instantiate_remark_type &instantiate_remark,
code_remarks &remarks) const
{
// The accepted return values from python policy calls are
// result, (result, ) or (result, warnings). Extract the warnings
// and unwrap the result.
if (!r) {
if (is_py_lp_creation_policy_error(PyErr_Occurred())) {
PyObject *exc = PyErr_Occurred();
Py_INCREF(exc);
PyErr_Clear();
code_remark remark =
[&]() {
try {
return (convert_py_lp_creation_policy_error
(exc, instantiate_remark, remarks));
} catch (...) {
Py_DECREF(exc);
throw;
}
}();
Py_DECREF(exc);
throw lp_except(std::move(remark));
} else {
throw python_except{
("failed to invoke python \"LpCreationPolicyAbc\""
" method implementation")
};
}
} else if (!PyTuple_Check(r)) {
return r;
} else if (PyTuple_Size(r) == 1) {
PyObject *_r = PyTuple_GetItem(r, 0);
if (_r) {
Py_INCREF(_r);
Py_DECREF(r);
} else {
Py_DECREF(r);
throw python_except {
"failed to retrieve result tuple entry returned from python policy call"
};
}
return _r;
} else if (PyTuple_Size(r) != 2) {
Py_DECREF(r);
throw python_except {
"unexpected number of entries in tuple returned from python policy call"
};
}
PyObject *warnings = PyTuple_GetItem(r, 1);
if (!warnings) {
Py_DECREF(r);
throw python_except {
"failed to retrieve warnings tuple entry returned from python policy call"
};
}
try {
convert_py_lp_creation_policy_warnings(warnings,
instantiate_remark,
remarks);
} catch (...) {
Py_DECREF(r);
throw;
}
PyObject *_r = PyTuple_GetItem(r, 0);
if (_r) {
Py_INCREF(_r);
Py_DECREF(r);
} else {
Py_DECREF(r);
throw python_except {
"failed to retrieve result tuple entry returned from python policy call"
};
}
return _r;
}
PyObject* lp_creation_python_policy::_data::
build_py_loc(const pp_token_index id_tok, const pp_result &pp_result) const
{
const raw_pp_token_index &raw_id_tok
= (pp_result.pp_tokens_range_to_raw(pp_tokens_range{id_tok, id_tok + 1})
.begin);
const auto id_tok_source =
pp_result.intersecting_sources_begin(raw_pp_tokens_range{raw_id_tok});
assert(id_tok_source !=
(pp_result.intersecting_sources_end
(raw_pp_tokens_range{raw_id_tok})));
const auto id_tok_line_col
= (id_tok_source->offset_to_line_col
(pp_result.get_raw_tokens()[raw_id_tok].get_range_in_file().begin));
PyObject *loc =
Py_BuildValue("sKK",
id_tok_source->get_filename().c_str(),
static_cast<unsigned long long>(id_tok_line_col.first),
static_cast<unsigned long long>(id_tok_line_col.second));
if (!loc) {
throw python_except{
"failed to build source location information python tuple"
};
}
return loc;
}
PyObject* lp_creation_python_policy::_data::
get_py_linkage_enum_member(const ast::linkage::linkage_kind &linkage)
const noexcept
{
PyObject *linkage_enum_member = nullptr;
switch (linkage) {
case ast::linkage::linkage_kind::none:
linkage_enum_member = linkage_enum_member_none;
break;
case ast::linkage::linkage_kind::internal:
linkage_enum_member = linkage_enum_member_internal;
break;
case ast::linkage::linkage_kind::external:
linkage_enum_member = linkage_enum_member_external;
break;
case ast::linkage::linkage_kind::nested_fun_auto:
linkage_enum_member = linkage_enum_member_nested_fun_auto;
break;
}
return linkage_enum_member;
}
PyObject* lp_creation_python_policy::_data::
wrap_allocated_ids(const allocated_ids::allocated_ids_type &ids)
const
{
PyObject *o = allocated_ids::create(ids);
if (!o) {
throw python_except{
"failed to instantiate \"_ccp.AllocatedIds\" interface wrapper"
};
}
return o;
}
lp_creation_policy::symbol_modification lp_creation_python_policy::_data::
convert_symbol_modification(PyObject *o)
{
if (!PyObject_IsInstance(o, symbol_modification_cls)) {
Py_DECREF(o);
throw python_except{
"expected instance of python class \"ccp.SymbolModification\""
};
}
PyObject *new_name_member =
PyObject_GetAttrString(o, "_new_name");
if (!new_name_member) {
Py_DECREF(o);
throw python_except {
"expected \"_new_name\" member in python class \"ccp.SymbolModification\""
};
}
std::string new_name;
if (!Py_IsNone(new_name_member)) {
if (!PyUnicode_Check(new_name_member)) {
Py_DECREF(new_name_member);
Py_DECREF(o);
throw python_except {
("expected string value in "
"python member \"ccp.SymbolModification._new_name\"")
};
}
const char *pnew_name = PyUnicode_AsUTF8(new_name_member);
if (!pnew_name) {
Py_DECREF(new_name_member);
Py_DECREF(o);
throw python_except {
("couldn't obtain string value from "
"python member \"ccp.SymbolModification._new_name\"")
};
}
try {
new_name = pnew_name;
} catch (...) {
Py_DECREF(new_name_member);
Py_DECREF(o);
throw;
}
}
Py_DECREF(new_name_member);
PyObject *linkage_change_member =
PyObject_GetAttrString(o, "_linkage_change");
if (!linkage_change_member) {
Py_DECREF(o);
throw python_except {
("expected \"_linkage_change\" member"
" in python class \"ccp.SymbolModification\"")
};
}
Py_DECREF(o);
long linkage_change_val = PyLong_AsLong(linkage_change_member);
Py_DECREF(linkage_change_member);
if (linkage_change_val == -1 && PyErr_Occurred()) {
throw python_except{
("could not convert \"ccp.SymbolModification._linkage_change\""
" python member to int")
};
}
lp_creation_policy::symbol_modification::linkage_change linkage_change;
if (linkage_change_val == linkage_change_enum_member_none_val) {
linkage_change =
lp_creation_policy::symbol_modification::linkage_change::lc_none;
} else if (linkage_change_val == linkage_change_enum_member_make_static_val) {
linkage_change =
lp_creation_policy::symbol_modification::linkage_change::lc_make_static;
} else if (linkage_change_val == linkage_change_enum_member_make_extern_val) {
linkage_change =
lp_creation_policy::symbol_modification::linkage_change::lc_make_extern;
} else {
throw python_except{
("unrecognized value in \"ccp.SymbolModification._linkage_change\""
" python member")
};
}
return lp_creation_policy::symbol_modification{
std::move(new_name), linkage_change
};
}
lp_creation_policy::externalized_symbol_modification
lp_creation_python_policy::_data::
convert_externalized_symbol_modification(PyObject *o)
{
if (!PyObject_IsInstance(o, externalized_symbol_modification_cls)) {
Py_DECREF(o);
throw python_except{
"expected instance of python class \"ccp.ExternalizedSymbolModification\""
};
}
PyObject *make_pointer_member =
PyObject_GetAttrString(o, "_make_pointer");
if (!make_pointer_member) {
Py_DECREF(o);
throw python_except {
("expected \"_make_pointer\" member"
" in python class \"ccp.ExternalizedSymbolModification\"")
};
}
if (!PyBool_Check(make_pointer_member)) {
Py_DECREF(o);
throw python_except {
("expected Bool type for \"_make_pointer\" member"
" in python class \"ccp.ExternalizedSymbolModification\"")
};
}
bool make_pointer = Py_IsTrue(make_pointer_member);
Py_DECREF(make_pointer_member);
PyObject *symbol_mod_member =
PyObject_GetAttrString(o, "_symbol_mod");
if (!symbol_mod_member) {
Py_DECREF(o);
throw python_except {
("expected \"_symbol_mod\" member"
" in python class \"ccp.ExternalizedSymbolModification\"")
};
}
try {
// convert_symbol_modification() consumes the reference.
lp_creation_policy::symbol_modification sym_mod =
convert_symbol_modification(symbol_mod_member);
Py_DECREF(o);
return lp_creation_policy::externalized_symbol_modification{
std::move(sym_mod.new_name),
sym_mod.new_linkage,
make_pointer
};
} catch (...) {
Py_DECREF(o);
throw;
}
}
lp_creation_python_policy::lp_creation_python_policy
(const pp_result &pp_result,
std::vector<std::string> &&patched_functions,
const char * const python_policy_mod,
const char * const python_policy_cls)
: _pp_result{pp_result}, _patched_functions{std::move(patched_functions)},
_d{new _data{}}
{
// Construct a PYTHONPATH for the embedded interpreter.
std::string python_path;
const char *env_pypol_path = getenv("KLP_CCP_PYTHONPOLICY_PATH");
if (env_pypol_path) {
python_path.append(env_pypol_path);
python_path.push_back(':');
}
python_path.append(KLP_CCP_PYPOL_DIR);
const char *env_pythonpath = getenv("PYTHONPATH");
if (env_pythonpath) {
python_path.push_back(':');
python_path.append(env_pythonpath);
}
PyStatus status;
PyConfig config;
PyConfig_InitPythonConfig(&config);
config.configure_c_stdio = 0;
config.install_signal_handlers = 0;
status = PyConfig_SetBytesString(&config,
&config.pythonpath_env,
python_path.c_str());
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
// It would be better to obtain the error message as a string and
// print that later, without exiting, but AFAICT that's not
// possible.
Py_ExitStatusException(status);
}
if (PyImport_AppendInittab("_ccp", pyinit_ccp_interface_mod)) {
throw python_except{"could not extend python's inittab"};
}
status = Py_InitializeFromConfig(&config);
if (PyStatus_Exception(status)) {
PyConfig_Clear(&config);
// It would be better to obtain the error message as a string and
// print that later, without exiting, but AFAICT that's not
// possible.
Py_ExitStatusException(status);
}
PyConfig_Clear(&config);
// Load the distributed ccp.py mod and inspect it further below.
PyObject *ccp_mod_name = PyUnicode_DecodeFSDefault("ccp");
PyObject *ccp_mod = PyImport_Import(ccp_mod_name);
Py_XDECREF(ccp_mod_name);
if (!ccp_mod)
throw python_except{"could not import \"ccp\" python module"};
// Lookup the Linkage enum member values and store them away for
// comparison later on.
PyObject *linkage_enum_cls =
PyObject_GetAttrString(ccp_mod, "Linkage");
if (!linkage_enum_cls) {
Py_DECREF(ccp_mod);
throw python_except{
"could not locate \"ccp.Linkage\" python enum class"
};
}
for (auto &m : {
std::make_pair(&_d->linkage_enum_member_none,
"NONE"),
std::make_pair(&_d->linkage_enum_member_internal,
"INTERNAL"),
std::make_pair(&_d->linkage_enum_member_external,
"EXTERNAL"),
std::make_pair(&_d->linkage_enum_member_nested_fun_auto,
"NESTED_FUN_AUTO"),
}) {
*std::get<0>(m) = PyObject_GetAttrString(linkage_enum_cls, std::get<1>(m));
if (!*std::get<0>(m)) {
Py_DECREF(linkage_enum_cls);
Py_DECREF(ccp_mod);
throw python_except{
"could not locate expected \"ccp.Linkage\" python enum member"
};
}
}
Py_DECREF(linkage_enum_cls);
// Lookup the SymbolModification.LinkageChange enum member values
// and store them away for comparison later on.
_d->symbol_modification_cls =
PyObject_GetAttrString(ccp_mod, "SymbolModification");
if (!_d->symbol_modification_cls) {
Py_DECREF(ccp_mod);
throw python_except{
"could not locate \"ccp.SymbolModification\" python class"
};
}
PyObject *linkage_change_enum_cls =
PyObject_GetAttrString(_d->symbol_modification_cls, "LinkageChange");
if (!linkage_change_enum_cls) {
Py_DECREF(ccp_mod);
throw python_except{
("could not locate \"ccp.SymbolModification.LinkageChange\""
" python enum class")
};
}
for (auto &m : {
std::make_pair(&_d->linkage_change_enum_member_none_val,
"NONE"),
std::make_pair(&_d->linkage_change_enum_member_make_static_val,
"MAKE_STATIC"),
std::make_pair(&_d->linkage_change_enum_member_make_extern_val,
"MAKE_EXTERN"),
}) {
PyObject *enum_member=
PyObject_GetAttrString(linkage_change_enum_cls, std::get<1>(m));
if (!enum_member) {
Py_DECREF(linkage_change_enum_cls);
Py_DECREF(ccp_mod);
throw python_except{
("could not locate expected \"ccp.SymbolModification.LinkageChange\""
" python enum member")
};
}
*std::get<0>(m) = PyLong_AsLong(enum_member);
Py_DECREF(enum_member);
if (*std::get<0>(m) == -1 && PyErr_Occurred()) {
Py_DECREF(linkage_change_enum_cls);
Py_DECREF(ccp_mod);
throw python_except{
("could not convert \"ccp.SymbolModification.LinkageChange\""
" python enum member to int")
};
}
}
Py_DECREF(linkage_change_enum_cls);
// Lookup a couple more classes needed for type inspection over the
// course of the run.
_d->externalized_symbol_modification_cls =
PyObject_GetAttrString(ccp_mod, "ExternalizedSymbolModification");
if (!_d->externalized_symbol_modification_cls) {
Py_DECREF(ccp_mod);
throw python_except{
"could not locate \"ccp.ExternalizedSymbolModification\" python class"
};
}
_d->lp_creation_policy_warning_cls =
PyObject_GetAttrString(ccp_mod, "LpCreationPolicyWarning");
if (!_d->lp_creation_policy_warning_cls) {
Py_DECREF(ccp_mod);
throw python_except{
"could not locate \"ccp.LpCreationPolicyWarning\" python class"
};
}
_d->lp_creation_policy_error_cls =
PyObject_GetAttrString(ccp_mod, "LpCreationPolicyError");
if (!_d->lp_creation_policy_error_cls) {
Py_DECREF(ccp_mod);
throw python_except{
"could not locate \"ccp.LpCreationPolicyError\" python class"
};
}
// Finally, lookup the "CcpLpCreationPolicy" Abstract Base Class
// (ABC) defined in the cls.py mod -- it will be used below
// for verifying the user specified policy implements it.
PyObject *ccp_lp_creation_policy_abc_cls =
PyObject_GetAttrString(ccp_mod, "LpCreationPolicyAbc");
Py_DECREF(ccp_mod);
if (!ccp_lp_creation_policy_abc_cls) {
throw python_except{
"could not locate \"ccp.LpCreationPolicyAbc\" python class"
};
}
// Load the module providing the user specified policy class.
PyObject *pol_mod_name = PyUnicode_DecodeFSDefault(python_policy_mod);
PyObject *pol_mod = PyImport_Import(pol_mod_name);
Py_XDECREF(pol_mod_name);
if (!pol_mod) {
Py_DECREF(ccp_lp_creation_policy_abc_cls);
throw python_except{"could not import the specified python policy module"};
}
// And locate the class.
PyObject *pol_cls = PyObject_GetAttrString(pol_mod, python_policy_cls);
Py_DECREF(pol_mod);
if (!pol_cls) {
Py_DECREF(ccp_lp_creation_policy_abc_cls);
throw python_except{"could not locate the specified python policy class"};
}
// Verify that the specified class implementing policy is a subclass
// of the CcpLpCreationPolicy ABC.
if (!PyObject_IsSubclass(pol_cls, ccp_lp_creation_policy_abc_cls)) {
Py_DECREF(ccp_lp_creation_policy_abc_cls);
Py_DECREF(pol_cls);
throw python_except{
"specified python policy class isn't a subclass of \"CcpLpCreationPolicy\""
};
}
Py_DECREF(ccp_lp_creation_policy_abc_cls);
// Instantiate. That's being done by calling the cls object
// c.f. type_call() in the Python sources.
if (!PyCallable_Check(pol_cls)) {
throw python_except{
"specified python policy class can't get instantiated"
};
}
PyObject *args = PyTuple_New(0);
if (!args) {
Py_DECREF(pol_cls);
throw python_except{
"failed to prepare python policy class constructor invocation"
};
}
_d->pol_obj = PyObject_CallObject(pol_cls, args);
Py_DECREF(pol_cls);
Py_DECREF(args);
if (!_d->pol_obj)
throw python_except{"python policy class construction failed"};
}
lp_creation_python_policy::~lp_creation_python_policy() noexcept
{
_d.reset();
}
void lp_creation_python_policy::teardown_python() noexcept
{
Py_FinalizeEx();
}
bool lp_creation_python_policy::
is_patched(const ast::function_definition &fd, code_remarks &remarks) const
{
const pp_token_index id_tok = _get_id_tok(fd);
if (std::find(_patched_functions.cbegin(),
_patched_functions.cend(),
_pp_result.get_pp_tokens()[id_tok].get_value())
!= _patched_functions.cend()) {
return true;
}
const auto &instantiate_remark =
[&](const code_remark::severity severity,