-
Notifications
You must be signed in to change notification settings - Fork 291
/
Copy pathP.cpp
2663 lines (2319 loc) · 70.5 KB
/
P.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
/*
A* -------------------------------------------------------------------
B* This file contains source code for the PyMOL computer program
C* copyright 1998-2000 by Warren Lyford Delano of DeLano Scientific.
D* -------------------------------------------------------------------
E* It is unlawful to modify or remove this copyright notice.
F* -------------------------------------------------------------------
G* Please see the accompanying LICENSE file for further information.
H* -------------------------------------------------------------------
I* Additional authors of this source file include:
-*
-*
-*
Z* -------------------------------------------------------------------
*/
/* meaning of defines
_PYMOL_MONOLITHIC: means that we're building PyMOL and its Python C
dependencies as one C library. That means we need to explicitly call
the initialization functions for these libraries on startup.
*/
#include"os_python.h"
#include"os_predef.h"
#include"os_std.h"
#include"Base.h"
/* BEGIN PROPRIETARY CODE SEGMENT (see disclaimer in "os_proprietary.h") */
#ifdef WIN32
#include"os_proprietary.h"
#include<process.h>
#endif
/* END PROPRIETARY CODE SEGMENT */
#ifdef _PYMOL_MINGW
#define putenv _putenv
#endif
#include"os_time.h"
#include"os_unix.h"
#include"MemoryDebug.h"
#include"Base.h"
#include"Err.h"
#include"P.h"
#include"PConv.h"
#include"Ortho.h"
#include"Cmd.h"
#include"main.h"
#include"AtomInfo.h"
#include"CoordSet.h"
#include"Util.h"
#include"Executive.h"
#include"PyMOLOptions.h"
#include"PyMOL.h"
#include "Lex.h"
#include "Seeker.h"
#include "Feedback.h"
#ifdef _PYMOL_IP_PROPERTIES
#include"Property.h"
#endif
#include <memory>
/**
* Use with functions which return a PyObject - if they return NULL, then an
* unexpected exception has occured.
*/
#define ASSERT_PYOBJECT_NOT_NULL(obj, return_value) \
if (!obj) { \
PyErr_Print(); \
return return_value; \
}
static int label_copy_text(char *dst, const char *src, int len, int max)
{
dst += len;
while(len < max) {
if(!*src)
break;
*(dst++) = *(src++);
len++;
}
*dst = 0;
return len;
}
static int label_next_token(WordType dst, const char **expr)
{
const char *p = *expr;
char *q = dst;
char ch;
int tok_len = 0;
int tok_max = sizeof(WordType) - 1;
/* skip leading whitespace (if any) */
while((ch = *p)) {
if(ch > 33)
break;
p++;
}
/* copy the token */
while((ch = *p)) {
if(((ch >= 'a') && (ch <= 'z')) ||
((ch >= 'A') && (ch <= 'Z')) || ((ch >= '0') && (ch <= '9')) || ((ch == '_'))) {
if(tok_len < tok_max) {
*(q++) = ch;
tok_len++;
}
} else {
break;
}
p++;
}
*q = 0;
if(p != *expr)
*expr = p;
else if(*p)
*expr = p + 1; /* always advance input by at least one character */
/* let caller know whether we read anything */
return (q != dst);
}
int PLabelExprUsesVariable(PyMOLGlobals * G, const char *expr, const char *var)
{
char ch, quote = 0;
int escaped = false;
while((ch = *(expr++))) {
if(!quote) {
if(ch == '\'') {
quote = ch;
} else if(ch == '"') {
quote = ch;
} else if((ch < 33) || (ch == '+') || (ch == '(') || (ch == ')')) {
/* nop */
} else if(ch > 32) {
WordType tok;
expr--;
if(label_next_token(tok, &expr)) {
if(!strcmp(tok, var)) {
return 1;
}
}
}
} else {
if(ch == quote) {
quote = 0;
} else if(ch == '\\') {
if(!escaped) {
escaped = true;
} else {
escaped = false;
}
}
}
}
return 0;
}
int PLabelAtomAlt(PyMOLGlobals * G, AtomInfoType * at, const char *model, const char *expr, int index)
{
/* alternate C implementation which bypasses Python expressions -- works
only for simple label formats "..."+property+... */
int result = false;
OrthoLineType label;
int label_len = 0;
int label_max = sizeof(OrthoLineType);
OrthoLineType buffer;
char ch, quote = 0;
int escaped = false;
const char *origexpr = expr;
label[0] = 0;
while((ch = *(expr++))) {
if(!quote) {
if(ch == '\'') {
quote = ch;
} else if(ch == '"') {
quote = ch;
} else if((ch < 33) || (ch == '+') || (ch == '(') || (ch == ')')) {
/* nop */
} else if(ch > 32) {
WordType tok;
int tokresult = true;
expr--;
if(label_next_token(tok, &expr)) {
/* brain-dead linear string matching */
buffer[0] = 0;
if(!strcmp(tok, "model")) {
label_len = label_copy_text(label, model, label_len, label_max);
} else if(!strcmp(tok, "index")) {
sprintf(buffer, "%d", index + 1);
} else if(!strcmp(tok, "type")) {
if(at->hetatm)
label_len = label_copy_text(label, "HETATM", label_len, label_max);
else
label_len = label_copy_text(label, "ATOM", label_len, label_max);
} else if(!strcmp(tok, "name")) {
label_len = label_copy_text(label, LexStr(G, at->name), label_len, label_max);
} else if(!strcmp(tok, "resn")) {
label_len = label_copy_text(label, LexStr(G, at->resn), label_len, label_max);
} else if(!strcmp(tok, "resi")) {
sprintf(buffer, "%d%c", at->resv, at->inscode);
} else if(!strcmp(tok, "resv")) {
sprintf(buffer, "%d", at->resv);
} else if(!strcmp(tok, "chain")) {
label_len = label_copy_text(label, LexStr(G, at->chain), label_len, label_max);
} else if(!strcmp(tok, "alt")) {
label_len = label_copy_text(label, at->alt, label_len, label_max);
} else if(!strcmp(tok, "segi")) {
label_len = label_copy_text(label, LexStr(G, at->segi), label_len, label_max);
} else if(!strcmp(tok, "ss")) {
label_len = label_copy_text(label, at->ssType, label_len, label_max);
} else if(!strcmp(tok, "vdw")) {
sprintf(buffer, "%1.2f", at->vdw);
} else if(!strcmp(tok, "elec_radius")) {
sprintf(buffer, "%1.2f", at->elec_radius);
} else if(!strcmp(tok, "text_type")) {
const char *st = LexStr(G, at->textType);
label_len = label_copy_text(label, st, label_len, label_max);
} else if(!strcmp(tok, "custom")) {
const char *st = LexStr(G, at->custom);
label_len = label_copy_text(label, st, label_len, label_max);
} else if(!strcmp(tok, "elem")) {
label_len = label_copy_text(label, at->elem, label_len, label_max);
} else if(!strcmp(tok, "geom")) {
sprintf(buffer, "%d", at->geom);
} else if(!strcmp(tok, "valence")) {
sprintf(buffer, "%d", at->valence);
} else if(!strcmp(tok, "rank")) {
sprintf(buffer, "%d", at->rank);
} else if(!strcmp(tok, "flags")) {
if(at->flags) {
sprintf(buffer, "%X", at->flags);
} else {
strcpy(buffer, "0");
}
} else if(!strcmp(tok, "q")) {
sprintf(buffer, "%1.2f", at->q);
} else if(!strcmp(tok, "b")) {
sprintf(buffer, "%1.2f", at->b);
} else if(!strcmp(tok, "numeric_type")) {
if(at->customType != cAtomInfoNoType)
sprintf(buffer, "%d", at->customType);
else {
strcpy(buffer, "?");
}
} else if(!strcmp(tok, "partial_charge")) {
sprintf(buffer, "%1.3f", at->partialCharge);
} else if(!strcmp(tok, "formal_charge")) {
sprintf(buffer, "%d", at->formalCharge);
} else if(!strcmp(tok, "stereo")) {
strcpy(buffer, AtomInfoGetStereoAsStr(at));
} else if(!strcmp(tok, "color")) {
sprintf(buffer, "%d", at->color);
} else if(!strcmp(tok, "cartoon")) {
sprintf(buffer, "%d", at->cartoon);
} else if(!strcmp(tok, "ID")) {
sprintf(buffer, "%d", at->id);
} else if(!strcmp(tok, "str")) {
/* nop */
} else {
tokresult = false;
}
if(buffer[0]) {
label_len = label_copy_text(label, buffer, label_len, label_max);
}
} else {
if (tok[0]){
label_len = label_copy_text(label, "?", label_len, label_max);
label_len = label_copy_text(label, tok, label_len, label_max);
} else {
tokresult = false;
}
}
result |= tokresult;
} else {
if(label_len < label_max) {
label[label_len] = '?';
label_len++;
result = true;
}
}
} else {
if(ch == quote) {
quote = 0;
result = true;
} else if(ch == '\\') {
if(!escaped) {
escaped = true;
} else {
if(label_len < label_max) {
label[label_len] = ch;
label_len++;
}
escaped = false;
}
} else {
if(label_len < label_max) {
label[label_len] = ch;
label_len++;
label[label_len] = 0;
}
}
}
}
if (!result && !label[0]){
// if label is not set, just use expression as a string for label
strncpy(label, origexpr, OrthoLineLength);
result = true;
}
LexDec(G, at->label);
at->label = result ? LexIdx(G, label) : 0;
return (result);
}
#ifndef _PYMOL_NOPY
/* all of the following Python objects must be invariant & global for the application */
/* these are module / module properties -- global and static for a given interpreter */
/* local to this C code module */
static PyObject *P_pymol = NULL;
static PyObject *P_pymol_dict = NULL; /* must be refomed into globals and instance properties */
static PyObject *P_cmd = NULL;
static PyObject *P_povray = NULL;
static PyObject *P_traceback = NULL;
static PyObject *P_parser = NULL;
static PyObject *P_vfont = NULL;
/* module import helper */
static
PyObject * PImportModuleOrFatal(const char * name) {
PyObject * mod = PyImport_ImportModule(name);
if(!mod) {
fprintf(stderr, "PyMOL-Error: can't find '%s'\n", name);
exit(EXIT_FAILURE);
}
return mod;
}
static
PyObject * PGetAttrOrFatal(PyObject * o, const char * name) {
PyObject * attr = PyObject_GetAttrString(o, name);
if(!attr) {
fprintf(stderr, "PyMOL-Error: can't find '%s'\n", name);
exit(EXIT_FAILURE);
}
return attr;
}
/* used elsewhere */
PyObject *P_menu = NULL; /* menu definitions are currently global */
PyObject *P_xray = NULL; /* okay as global */
PyObject *P_chempy = NULL; /* okay as global */
PyObject *P_models = NULL; /* okay as global */
PyObject *P_setting = NULL; /* okay as global -- just used for names */
PyObject *P_CmdException = nullptr;
PyObject *P_QuietException = nullptr;
PyObject *P_IncentiveOnlyException = nullptr;
static PyMappingMethods wrapperMappingMethods, settingMappingMethods;
static PyTypeObject Wrapper_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"wrapper.Wrapper", /* tp_name */
0, /* tp_basicsize */
};
static PyTypeObject settingWrapper_Type = {
PyVarObject_HEAD_INIT(NULL, 0)
"wrapper.SettingWrapper", /* tp_name */
0, /* tp_basicsize */
};
#ifdef _PYMOL_IP_PROPERTIES
#endif
/**
* If `wob` is not in a valid state (outside iterate-family context), raise
* an error and return false.
*/
static bool check_wrapper_scope(WrapperObject * wobj) {
if (wobj && wobj->obj)
return true;
PyErr_SetString(PyExc_RuntimeError,
"wrappers cannot be used outside the iterate-family commands");
return false;
}
/**
* key: Python int (setting index) or str (setting name)
*
* Return the setting index or -1 for unknown `key`
*
* Raise LookupError if `key` doesn't name a known setting
*/
static int get_and_check_setting_index(PyMOLGlobals * G, PyObject * key) {
int setting_id;
if(PyInt_Check(key)) {
setting_id = PyInt_AS_LONG(key);
} else {
key = PyObject_Str(key);
setting_id = SettingGetIndex(G, PyString_AS_STRING(key));
Py_DECREF(key);
}
if (setting_id < 0 || setting_id >= cSetting_INIT) {
PyErr_SetString(PyExc_LookupError, "unknown setting");
return -1;
}
return setting_id;
}
/**
* Access a setting with iterate et. al.
*
* s[key]
*
* obj: `s` object in iterate-family namespace
*
* Raise LookupError if `key` doesn't name a known setting
*/
static
PyObject *SettingWrapperObjectSubScript(PyObject *obj, PyObject *key){
auto& wobj = reinterpret_cast<SettingPropertyWrapperObject*>(obj)->wobj;
int setting_id;
PyObject *ret = NULL;
if (!check_wrapper_scope(wobj)) {
return NULL;
}
auto G = wobj->G;
if ((setting_id = get_and_check_setting_index(G, key)) == -1) {
return NULL;
}
if (wobj->idx >= 0){
// atom-state level
ret = SettingGetIfDefinedPyObject(G, wobj->cs, wobj->idx, setting_id);
}
if (!ret){
// atom level
ret = SettingGetIfDefinedPyObject(G, wobj->atomInfo, setting_id);
if (!ret) {
// object-state, object, or global
ret = SettingGetPyObject(G,
wobj->cs ? wobj->cs->Setting.get() : NULL,
wobj->obj->Setting.get(), setting_id);
}
}
return PConvAutoNone(ret);
}
/**
* Set an atom or atom-state level setting with alter or alter_state.
*
* s[key] = val
*
* obj: `s` object in cmd.alter/cmd.alter_state namespace
*
* Return 0 on success or -1 on failure.
*
* Raise TypeError if setting not modifiable in the current context, and
* LookupError if `key` doesn't name a known setting
*/
static
int SettingWrapperObjectAssignSubScript(PyObject *obj, PyObject *key, PyObject *val){
auto& wobj = reinterpret_cast<SettingPropertyWrapperObject*>(obj)->wobj;
if (!check_wrapper_scope(wobj)) {
return -1;
}
int setting_id;
auto G = wobj->G;
if (wobj->read_only){
PyErr_SetString(PyExc_TypeError, "Use alter/alter_state to modify settings");
return -1;
}
if ((setting_id = get_and_check_setting_index(G, key)) == -1) {
return -1;
}
if (wobj->idx >= 0) {
// atom-state level
if(!SettingLevelCheck(G, setting_id, cSettingLevel_astate)) {
PyErr_SetString(PyExc_TypeError,
"only atom-state level settings can be set in alter_state function");
return -1; // failure
} else if (CoordSetSetSettingFromPyObject(G, wobj->cs, wobj->idx, setting_id, val)) {
}
} else {
// atom level
if(!SettingLevelCheck(G, setting_id, cSettingLevel_atom)) {
PyErr_SetString(PyExc_TypeError,
"only atom-level settings can be set in alter function");
return -1; // failure
} else if (AtomInfoSetSettingFromPyObject(G, wobj->atomInfo, setting_id, val)) {
AtomInfoSettingGenerateSideEffects(G, wobj->obj, setting_id, wobj->atm);
}
}
return 0; // success
}
#ifdef _PYMOL_IP_PROPERTIES
#endif
/**
* Python iterator over atom or atom-state setting indices
*/
static PyObject* SettingWrapperObjectIter(PyObject *self)
{
auto& wobj = reinterpret_cast<SettingPropertyWrapperObject*>(self)->wobj;
if (!check_wrapper_scope(wobj)) {
return NULL;
}
int unique_id = wobj->atomInfo->unique_id;
if (wobj->idx >= 0) {
unique_id =
wobj->cs->atom_state_setting_id ?
wobj->cs->atom_state_setting_id[wobj->idx] : 0;
}
PyObject * items = SettingUniqueGetIndicesAsPyList(wobj->G, unique_id);
PyObject * iter = PyObject_GetIter(items);
Py_XDECREF(items);
return iter;
}
#ifdef _PYMOL_IP_PROPERTIES
#endif
/**
* Allows attribute-like syntax for item lookups
*
* o.key -> o[key] if `key` is not an attribute of `o`
*/
static PyObject* PyObject_GenericGetAttrOrItem(PyObject *o, PyObject *key) {
PyObject *ret = PyObject_GenericGetAttr(o, key);
if (!PyErr_Occurred())
return ret;
PyErr_Clear();
return PyObject_GetItem(o, key);
}
/**
* Allows attribute-like syntax for item assignment
*
* `o.key = value` -> `o[key] = value`
*/
static
int PyObject_GenericSetAttrAsItem(PyObject *o, PyObject *key, PyObject *value) {
return PyObject_SetItem(o, key, value);
}
/**
* Generic getter for member variable pointer at struct byte offset
*/
template <typename T, typename S>
static T* get_member_pointer(S* instance, size_t offset)
{
return reinterpret_cast<T*>(reinterpret_cast<char*>(instance) + offset);
}
template <typename T, typename S>
static T const* get_member_pointer(S const* instance, size_t offset)
{
return reinterpret_cast<T const*>(
reinterpret_cast<char const*>(instance) + offset);
}
/**
* Explicit valence of an atom, defined as the sum of bond orders.
*
* Delocalized/aromatic bonds count as order=1.5 (heuristic).
*
* Should be equivalent to:
* OBAtom::GetExplicitValence() [Open Babel 3.0]
*/
static int getExplicitValence(ObjectMolecule const* obj, size_t atm)
{
int value = 0;
for (auto const& item : AtomNeighbors(obj, atm)) {
int const order = obj->Bond[item.bond].order;
if (order == cBondOrderDeloc) {
// simple rule which gets all aromatic C atoms right, but can
// be wrong for example for neutral aromatic N atoms with degree 3 or
// for neutral carboxy O atoms which PyMOL also assigns bond oder 4.
value += 3;
} else {
value += 2 * order;
}
}
return value / 2;
}
/**
* Explicit degree of an Atom, defined as the number of directly-bonded
* neighbors in the graph.
*
* Should be equivalent to:
* RDKit::Atom::getDegree()
* OBAtom::GetExplicitDegree() [Open Babel 3.0]
*/
static unsigned getExplicitDegree(ObjectMolecule const* obj, size_t atm)
{
return AtomNeighbors(obj, atm).size();
}
/**
* iterate-family namespace implementation: lookup
*
* Raise NameError if state attributes are accessed outside of iterate_state
*/
static
PyObject * WrapperObjectSubScript(PyObject *obj, PyObject *key){
static PyObject * pystr_HETATM = PyString_InternFromString("HETATM");
static PyObject * pystr_ATOM = PyString_InternFromString("ATOM");
static PyObject * pystr_QuestionMark = PyString_InternFromString("?");
auto wobj = static_cast<WrapperObject*>(obj);
if (!check_wrapper_scope(wobj))
return NULL;
PyMOLGlobals* G = wobj->G;
PyObject* ret = nullptr;
auto const keyobj = unique_PyObject_ptr(PyObject_Str(key));
auto const aprop = PyString_AS_STRING(keyobj.get());
auto const ap = PyMOL_GetAtomPropertyInfo(G->PyMOL, aprop);
if (ap) {
#ifdef _PYMOL_IP_EXTRAS
switch (ap->id) {
case ATOM_PROP_STEREO:
if (ObjectMoleculeUpdateMMStereoInfoForState(G, wobj->obj, wobj->state - 1) < 0) {
PyErr_SetString(P_CmdException,
"please install rdkit or set SCHRODINGER variable");
return NULL;
}
break;
case ATOM_PROP_TEXT_TYPE:
#ifndef NO_MMLIBS
ObjectMoleculeUpdateAtomTypeInfoForState(G, wobj->obj, wobj->state - 1, 1, 0);
#endif
break;
}
#endif
switch (ap->Ptype){
case cPType_string:
ret = PyUnicode_FromString(
get_member_pointer<char>(wobj->atomInfo, ap->offset));
break;
case cPType_schar:
ret = PyLong_FromLong(
*get_member_pointer<signed char>(wobj->atomInfo, ap->offset));
break;
case cPType_int:
ret =
PyLong_FromLong(*get_member_pointer<int>(wobj->atomInfo, ap->offset));
break;
case cPType_uint32:
ret = PyLong_FromUnsignedLong(
*get_member_pointer<uint32_t>(wobj->atomInfo, ap->offset));
break;
case cPType_int_as_string:
ret = PyUnicode_FromString(LexStr(wobj->G,
*get_member_pointer<lexborrow_t>(wobj->atomInfo, ap->offset)));
break;
case cPType_float:
ret = PyFloat_FromDouble(
*get_member_pointer<float>(wobj->atomInfo, ap->offset));
break;
case cPType_char_as_type:
ret = PIncRef(wobj->atomInfo->hetatm ? pystr_HETATM : pystr_ATOM);
break;
case cPType_model:
ret = PyUnicode_FromString(wobj->obj->Name);
break;
case cPType_index:
ret = PyLong_FromLong(wobj->atm + 1);
break;
case cPType_int_custom_type: {
auto val = *get_member_pointer<int>(wobj->atomInfo, ap->offset);
if (val != cAtomInfoNoType) {
ret = PyLong_FromLong(val);
} else {
ret = PIncRef(pystr_QuestionMark);
}
} break;
case cPType_xyz_float:
if (wobj->idx < 0) {
PyErr_SetString(PyExc_NameError,
"x/y/z only available in iterate_state and alter_state");
} else {
ret = PyFloat_FromDouble(wobj->cs->coordPtr(wobj->idx)[ap->offset]);
}
break;
case cPType_settings:
if (!wobj->settingWrapperObject) {
wobj->settingWrapperObject = static_cast<SettingPropertyWrapperObject*>(
PyType_GenericNew(&settingWrapper_Type, Py_None, Py_None));
wobj->settingWrapperObject->wobj = wobj;
}
ret = PIncRef(wobj->settingWrapperObject);
break;
case cPType_properties:
#ifndef _PYMOL_IP_PROPERTIES
PyErr_SetString(P_IncentiveOnlyException,
"'properties/p' not supported in Open-Source PyMOL");
#else
static_assert(false, "");
#endif
break;
case cPType_state:
ret = PyLong_FromLong(wobj->state);
break;
default:
switch (ap->id) {
case ATOM_PROP_RESI: {
char resi[8];
AtomResiFromResv(resi, sizeof(resi), wobj->atomInfo);
ret = PyUnicode_FromString(resi);
} break;
case ATOM_PROP_STEREO: {
auto mmstereotype = AtomInfoGetStereoAsStr(wobj->atomInfo);
ret = PyUnicode_FromString(mmstereotype);
} break;
case ATOM_PROP_ONELETTER: {
const char* st = LexStr(G, wobj->atomInfo->resn);
char abbr[2] = {SeekerGetAbbr(G, st, 'O', 'X'), 0};
ret = PyUnicode_FromString(abbr);
} break;
case ATOM_PROP_EXPLICIT_DEGREE: {
ret = PyLong_FromLong(getExplicitDegree(wobj->obj, wobj->atm));
} break;
case ATOM_PROP_EXPLICIT_VALENCE: {
ret = PyLong_FromLong(getExplicitValence(wobj->obj, wobj->atm));
} break;
default:
PyErr_SetString(PyExc_SystemError, "unhandled atom property type");
}
}
} else {
/* if not an atom property, check if local variable in dict */
if (wobj->dict) {
ret = PyDict_GetItem(wobj->dict, key); // Borrowed reference
}
if (ret) {
Py_INCREF(ret);
} else {
PyErr_SetObject(PyExc_KeyError, key);
}
}
return ret;
}
/**
* Make IPython happy, which may call f_locals.get("__tracebackhide__", 0) and
* crash if there is no get() method (the wrapper object is f_locals).
*/
static PyObject* WrapperObject_get(PyObject* self, PyObject* args)
{
auto nargs = PyTuple_Size(args);
assert(0 < nargs && nargs < 3);
// Could call WrapperObjectSubScript here, but we don't really need that. To
// fix the IPython issue, it's sufficient to return default or None.
if (nargs == 2) {
return PIncRef(PyTuple_GET_ITEM(args, 1));
}
Py_RETURN_NONE;
}
static PyMethodDef wrapperMethods[] = {
{"get", WrapperObject_get, METH_VARARGS, nullptr},
{nullptr},
};
/**
* iterate-family namespace implementation: assignment
*
* Raise TypeError for read-only variables
*/
static
int WrapperObjectAssignSubScript(PyObject *obj, PyObject *key, PyObject *val){
auto wobj = static_cast<WrapperObject*>(obj);
if (!check_wrapper_scope(wobj)) {
return -1;
}
auto G = wobj->G;
const auto keyobj = unique_PyObject_ptr(PyObject_Str(key));
const char* const aprop = PyString_AS_STRING(keyobj.get());
const AtomPropertyInfo* ap = PyMOL_GetAtomPropertyInfo(G->PyMOL, aprop);
if (ap) {
if (wobj->read_only) {
PyErr_SetString(
PyExc_TypeError, "Use alter/alter_state to modify values");
return -1;
}
#ifdef _PYMOL_IP_EXTRAS
if (wobj->cs) {
switch (ap->id) {
case ATOM_PROP_STEREO:
wobj->cs->validMMStereo = MMPYMOLX_PROP_STATE_USER;
break;
case ATOM_PROP_TEXT_TYPE:
wobj->cs->validTextType = MMPYMOLX_PROP_STATE_USER;
break;
}
}
#endif
bool changed = false;
switch (ap->Ptype) {
case cPType_string: {
PyObject* valobj = PyObject_Str(val);
const char* valstr = PyString_AS_STRING(valobj);
char* dest = get_member_pointer<char>(wobj->atomInfo, ap->offset);
if (strlen(valstr) > ap->maxlen) {
strncpy(dest, valstr, ap->maxlen);
} else {
strcpy(dest, valstr);
}
Py_DECREF(valobj);
changed = true;
} break;
case cPType_schar: {
int valint = PyInt_AsLong(val);
if (valint == -1 && PyErr_Occurred())
return -1;
*get_member_pointer<signed char>(wobj->atomInfo, ap->offset) = valint;
changed = true;
} break;
case cPType_int: {
int valint = PyInt_AsLong(val);
if (valint == -1 && PyErr_Occurred())
return -1;
*get_member_pointer<int>(wobj->atomInfo, ap->offset) = valint;
changed = true;
} break;
case cPType_uint32: {
auto valint = PyLong_AsUnsignedLong(val);
if (valint == -1 && PyErr_Occurred())
return -1;
*get_member_pointer<uint32_t>(wobj->atomInfo, ap->offset) = valint;
changed = true;
} break;
case cPType_int_as_string: {
auto dest = get_member_pointer<lexidx_t>(wobj->atomInfo, ap->offset);
const auto valobj = unique_PyObject_ptr(PyObject_Str(val));
const char* valstr = PyString_AS_STRING(valobj.get());
LexAssign(G, *dest, valstr);
changed = true;
} break;
case cPType_float:
if (!PConvPyObjectToFloat(
val, get_member_pointer<float>(wobj->atomInfo, ap->offset))) {
return -1;
}
changed = true;
break;
case cPType_char_as_type: {
const auto valobj = unique_PyObject_ptr(PyObject_Str(val));
const char* valstr = PyString_AS_STRING(valobj.get());
wobj->atomInfo->hetatm = ((valstr[0] == 'h') || (valstr[0] == 'H'));
changed = true;
} break;
case cPType_int_custom_type: {
const auto valobj = unique_PyObject_ptr(PyObject_Str(val));
const char* valstr = PyString_AS_STRING(valobj.get());
auto* dest = get_member_pointer<int>(wobj->atomInfo, ap->offset);
if (valstr[0] == '?') {
*dest = cAtomInfoNoType;
} else {
int valint = PyInt_AS_LONG(val);
*dest = valint;
}
changed = true;
} break;
case cPType_xyz_float:
if (wobj->idx < 0) {
PyErr_SetString(PyExc_NameError, "x/y/z only available in alter_state");
return -1;
} else {
float* v = wobj->cs->coordPtr(wobj->idx) + ap->offset;
if (!PConvPyObjectToFloat(val, v)) {
return -1;
}
}
break;
default:
switch (ap->id) {
case ATOM_PROP_RESI:
if (PConvPyIntToInt(val, &wobj->atomInfo->resv)) {
wobj->atomInfo->inscode = '\0';
} else {
const auto valobj = unique_PyObject_ptr(PyObject_Str(val));
wobj->atomInfo->setResi(PyString_AS_STRING(valobj.get()));
}
break;
case ATOM_PROP_STEREO: {
const auto valobj = unique_PyObject_ptr(PyObject_Str(val));
const char* valstr = PyString_AS_STRING(valobj.get());
AtomInfoSetStereo(wobj->atomInfo, valstr);
} break;
default:
PyErr_Format(PyExc_TypeError, "'%s' is read-only", aprop);
return -1;
}
}
if (changed) {
switch (ap->id) {
case ATOM_PROP_ELEM:
wobj->atomInfo->protons = 0;
wobj->atomInfo->vdw = 0;
AtomInfoAssignParameters(wobj->G, wobj->atomInfo);
break;
case ATOM_PROP_RESV:
wobj->atomInfo->inscode = '\0';
break;
case ATOM_PROP_SS:
wobj->atomInfo->ssType[0] = toupper(wobj->atomInfo->ssType[0]);
break;
case ATOM_PROP_FORMAL_CHARGE:
wobj->atomInfo->chemFlag = false;
break;
}
}
} else {
/* if not an atom property, then its a local variable, store it */
if (!wobj->dict) {
wobj->dict = PyDict_New();
}
PyDict_SetItem(wobj->dict, key, val);
}
return 0; /* 0 success, -1 failure */
}
/* BEGIN PROPRIETARY CODE SEGMENT (see disclaimer in "os_proprietary.h") */
#ifdef WIN32
static PyObject *P_time = NULL;
static PyObject *P_sleep = NULL;
#endif