Skip to content

Commit d1c34c3

Browse files
committed
interrogate: support static properties
1 parent 65ae1e1 commit d1c34c3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+2630
-2272
lines changed

dtool/src/cppparser/cppBison.cxx.prebuilt

Lines changed: 2245 additions & 2227 deletions
Large diffs are not rendered by default.

dtool/src/cppparser/cppBison.yxx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4051,6 +4051,10 @@ name:
40514051
| KW_STATIC
40524052
{
40534053
$$ = new CPPIdentifier("static", @1);
4054+
}
4055+
| KW_DEFAULT
4056+
{
4057+
$$ = new CPPIdentifier("default", @1);
40544058
}
40554059
;
40564060

dtool/src/dtoolutil/executionEnvironment.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ class EXPCL_DTOOL ExecutionEnvironment {
5151

5252
static Filename get_cwd();
5353

54+
MAKE_SEQ_PROPERTY(args, get_num_args, get_arg);
55+
MAKE_PROPERTY(binary_name, get_binary_name, set_binary_name);
56+
MAKE_PROPERTY(dtool_name, get_dtool_name, set_dtool_name);
57+
MAKE_PROPERTY(cwd, get_cwd);
58+
5459
private:
5560
bool ns_has_environment_variable(const string &var) const;
5661
string ns_get_environment_variable(const string &var) const;

dtool/src/dtoolutil/pandaSystem.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ class EXPCL_DTOOL PandaSystem {
4848

4949
static string get_platform();
5050

51+
MAKE_PROPERTY(version_string, get_version_string);
52+
MAKE_PROPERTY(major_version, get_major_version);
53+
MAKE_PROPERTY(minor_version, get_minor_version);
54+
MAKE_PROPERTY(sequence_version, get_sequence_version);
55+
MAKE_PROPERTY(official_version, is_official_version);
56+
57+
MAKE_PROPERTY(memory_alignment, get_memory_alignment);
58+
59+
MAKE_PROPERTY(distributor, get_distributor);
60+
MAKE_PROPERTY(compiler, get_compiler);
61+
MAKE_PROPERTY(build_date, get_build_date);
62+
MAKE_PROPERTY(git_commit, get_git_commit);
63+
64+
MAKE_PROPERTY(platform, get_platform);
65+
5166
bool has_system(const string &system) const;
5267
size_t get_num_systems() const;
5368
string get_system(size_t n) const;

dtool/src/dtoolutil/textEncoder.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ class EXPCL_DTOOL TextEncoder {
4646

4747
INLINE static void set_default_encoding(Encoding encoding);
4848
INLINE static Encoding get_default_encoding();
49+
MAKE_PROPERTY(default_encoding, get_default_encoding, set_default_encoding);
4950

5051
INLINE void set_text(const string &text);
5152
INLINE void set_text(const string &text, Encoding encoding);

dtool/src/interrogate/interfaceMaker.cxx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,8 @@ Property(const InterrogateElement &ielement) :
9191
_setter(NULL),
9292
_has_function(NULL),
9393
_clear_function(NULL),
94-
_deleter(NULL)
94+
_deleter(NULL),
95+
_has_this(false)
9596
{
9697
}
9798

dtool/src/interrogate/interfaceMaker.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ class InterfaceMaker {
132132
Function *_has_function;
133133
Function *_clear_function;
134134
Function *_deleter;
135+
bool _has_this;
135136
};
136137
typedef vector<Property *> Properties;
137138

dtool/src/interrogate/interfaceMakerPythonNative.cxx

Lines changed: 127 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2623,7 +2623,8 @@ write_module_class(ostream &out, Object *obj) {
26232623
for (pit = obj->_properties.begin(); pit != obj->_properties.end(); ++pit) {
26242624
Property *property = (*pit);
26252625
const InterrogateElement &ielem = property->_ielement;
2626-
if (property->_getter == NULL || !is_function_legal(property->_getter)) {
2626+
if (!property->_has_this ||
2627+
property->_getter == NULL || !is_function_legal(property->_getter)) {
26272628
continue;
26282629
}
26292630

@@ -3169,6 +3170,45 @@ write_module_class(ostream &out, Object *obj) {
31693170
}
31703171
}
31713172

3173+
// Also add the static properties, which can't be added via getset.
3174+
Properties::const_iterator pit;
3175+
for (pit = obj->_properties.begin(); pit != obj->_properties.end(); ++pit) {
3176+
Property *property = (*pit);
3177+
const InterrogateElement &ielem = property->_ielement;
3178+
if (property->_has_this ||
3179+
property->_getter == NULL || !is_function_legal(property->_getter)) {
3180+
continue;
3181+
}
3182+
3183+
string name1 = methodNameFromCppName(ielem.get_name(), "", false);
3184+
// string name2 = methodNameFromCppName(ielem.get_name(), "", true);
3185+
3186+
string getter = "&Dtool_" + ClassName + "_" + ielem.get_name() + "_Getter";
3187+
string setter = "NULL";
3188+
if (property->_length_function == NULL &&
3189+
property->_setter != NULL && is_function_legal(property->_setter)) {
3190+
setter = "&Dtool_" + ClassName + "_" + ielem.get_name() + "_Setter";
3191+
}
3192+
3193+
out << " static const PyGetSetDef def_" << name1 << " = {(char *)\"" << name1 << "\", " << getter << ", " << setter;
3194+
3195+
if (ielem.has_comment()) {
3196+
out << ", (char *)\n";
3197+
output_quoted(out, 4, ielem.get_comment());
3198+
out << ",\n ";
3199+
} else {
3200+
out << ", NULL, ";
3201+
}
3202+
3203+
// Extra void* argument; we don't make use of it.
3204+
out << "NULL};\n";
3205+
3206+
out << " PyDict_SetItemString(dict, \"" << name1 << "\", Dtool_NewStaticProperty(&Dtool_" << ClassName << "._PyType, &def_" << name1 << "));\n";
3207+
/* Alternative spelling:
3208+
out << " PyDict_SetItemString(\"" << name2 << "\", &def_" << name1 << ");\n";
3209+
*/
3210+
}
3211+
31723212
out << " if (PyType_Ready((PyTypeObject *)&Dtool_" << ClassName << ") < 0) {\n"
31733213
" Dtool_Raise_TypeError(\"PyType_Ready(" << ClassName << ")\");\n"
31743214
" return;\n"
@@ -6443,11 +6483,15 @@ write_getset(ostream &out, Object *obj, Property *property) {
64436483
"/**\n"
64446484
" * sequence getter for property " << cClassName << "::" << ielem.get_name() << "\n"
64456485
" */\n"
6446-
"static PyObject *Dtool_" + ClassName + "_" + ielem.get_name() + "_Getitem(PyObject *self, Py_ssize_t index) {\n"
6447-
" " << cClassName << " *local_this = NULL;\n"
6448-
" if (!Dtool_Call_ExtractThisPointer(self, Dtool_" << ClassName << ", (void **)&local_this)) {\n"
6449-
" return NULL;\n"
6450-
" }\n";
6486+
"static PyObject *Dtool_" + ClassName + "_" + ielem.get_name() + "_Getitem(PyObject *self, Py_ssize_t index) {\n";
6487+
if (property->_getter->_has_this ||
6488+
(property->_has_function && property->_has_function->_has_this)) {
6489+
out <<
6490+
" " << cClassName << " *local_this = NULL;\n"
6491+
" if (!Dtool_Call_ExtractThisPointer(self, Dtool_" << ClassName << ", (void **)&local_this)) {\n"
6492+
" return NULL;\n"
6493+
" }\n";
6494+
}
64516495

64526496
// This is a getitem of a sequence type. This means we *need* to raise
64536497
// IndexError if we're out of bounds.
@@ -6458,8 +6502,12 @@ write_getset(ostream &out, Object *obj, Property *property) {
64586502
out << " }\n";
64596503

64606504
if (property->_has_function != NULL) {
6461-
out << " if (!local_this->" << property->_has_function->_ifunc.get_name() << "(index)) {\n"
6462-
<< " Py_INCREF(Py_None);\n"
6505+
if (property->_has_function->_has_this) {
6506+
out << " if (!local_this->" << property->_has_function->_ifunc.get_name() << "(index)) {\n";
6507+
} else {
6508+
out << " if (!" << cClassName << "::" << property->_has_function->_ifunc.get_name() << "(index)) {\n";
6509+
}
6510+
out << " Py_INCREF(Py_None);\n"
64636511
<< " return Py_None;\n"
64646512
<< " }\n";
64656513
}
@@ -6494,26 +6542,36 @@ write_getset(ostream &out, Object *obj, Property *property) {
64946542
// Write out a setitem if this is not a read-only property.
64956543
if (property->_setter != NULL) {
64966544
out << "static int Dtool_" + ClassName + "_" + ielem.get_name() + "_Setitem(PyObject *self, Py_ssize_t index, PyObject *arg) {\n";
6497-
out << " " << cClassName << " *local_this = NULL;\n";
6498-
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6499-
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6500-
out << " return -1;\n";
6501-
out << " }\n\n";
6545+
if (property->_has_this) {
6546+
out << " " << cClassName << " *local_this = NULL;\n";
6547+
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6548+
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6549+
out << " return -1;\n";
6550+
out << " }\n\n";
6551+
}
65026552

65036553
out << " if (arg == (PyObject *)NULL) {\n";
65046554
if (property->_deleter != NULL) {
6505-
out << " local_this->" << property->_deleter->_ifunc.get_name() << "(index);\n"
6506-
<< " return 0;\n";
6555+
if (property->_deleter->_has_this) {
6556+
out << " local_this->" << property->_deleter->_ifunc.get_name() << "(index);\n";
6557+
} else {
6558+
out << " " << cClassName << "::" << property->_deleter->_ifunc.get_name() << "(index);\n";
6559+
}
6560+
out << " return 0;\n";
65076561
} else {
65086562
out << " Dtool_Raise_TypeError(\"can't delete " << ielem.get_name() << "[] attribute\");\n"
65096563
" return -1;\n";
65106564
}
65116565
out << " }\n";
65126566

65136567
if (property->_clear_function != NULL) {
6514-
out << " if (arg == Py_None) {\n"
6515-
<< " local_this->" << property->_clear_function->_ifunc.get_name() << "(index);\n"
6516-
<< " return 0;\n"
6568+
out << " if (arg == Py_None) {\n";
6569+
if (property->_clear_function->_has_this) {
6570+
out << " local_this->" << property->_clear_function->_ifunc.get_name() << "(index);\n";
6571+
} else {
6572+
out << " " << cClassName << "::" << property->_clear_function->_ifunc.get_name() << "(index);\n";
6573+
}
6574+
out << " return 0;\n"
65176575
<< " }\n";
65186576
}
65196577

@@ -6547,9 +6605,14 @@ write_getset(ostream &out, Object *obj, Property *property) {
65476605
}
65486606

65496607
// Now write the getter, which returns a special wrapper object.
6550-
out << "static PyObject *Dtool_" + ClassName + "_" + ielem.get_name() + "_Getter(PyObject *self, void *) {\n"
6551-
" Py_INCREF(self);\n"
6552-
" Dtool_SequenceWrapper *wrap = PyObject_New(Dtool_SequenceWrapper, &Dtool_SequenceWrapper_Type);\n"
6608+
out << "static PyObject *Dtool_" + ClassName + "_" + ielem.get_name() + "_Getter(PyObject *self, void *) {\n";
6609+
if (property->_has_this) {
6610+
out << " nassertr(self != NULL, NULL);\n"
6611+
" Py_INCREF(self);\n";
6612+
} else {
6613+
out << " Py_XINCREF(self);\n";
6614+
}
6615+
out << " Dtool_SequenceWrapper *wrap = PyObject_New(Dtool_SequenceWrapper, &Dtool_SequenceWrapper_Type);\n"
65536616
" wrap->_base = self;\n"
65546617
" wrap->_len_func = &Dtool_" << ClassName << "_" << ielem.get_name() << "_Len;\n"
65556618
" wrap->_getitem_func = &Dtool_" << ClassName << "_" << ielem.get_name() << "_Getitem;\n";
@@ -6566,20 +6629,26 @@ write_getset(ostream &out, Object *obj, Property *property) {
65666629
out << "static PyObject *Dtool_" + ClassName + "_" + ielem.get_name() + "_Getter(PyObject *self, void *) {\n";
65676630
FunctionRemap *remap = property->_getter->_remaps.front();
65686631

6569-
if (remap->_const_method) {
6570-
out << " const " << cClassName << " *local_this = NULL;\n";
6571-
out << " if (!Dtool_Call_ExtractThisPointer(self, Dtool_" << ClassName << ", (void **)&local_this)) {\n";
6572-
} else {
6573-
out << " " << cClassName << " *local_this = NULL;\n";
6574-
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6575-
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6632+
if (remap->_has_this) {
6633+
if (remap->_const_method) {
6634+
out << " const " << cClassName << " *local_this = NULL;\n";
6635+
out << " if (!Dtool_Call_ExtractThisPointer(self, Dtool_" << ClassName << ", (void **)&local_this)) {\n";
6636+
} else {
6637+
out << " " << cClassName << " *local_this = NULL;\n";
6638+
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6639+
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6640+
}
6641+
out << " return NULL;\n";
6642+
out << " }\n\n";
65766643
}
6577-
out << " return NULL;\n";
6578-
out << " }\n\n";
65796644

65806645
if (property->_has_function != NULL) {
6581-
out << " if (!local_this->" << property->_has_function->_ifunc.get_name() << "()) {\n"
6582-
<< " Py_INCREF(Py_None);\n"
6646+
if (remap->_has_this) {
6647+
out << " if (!local_this->" << property->_has_function->_ifunc.get_name() << "()) {\n";
6648+
} else {
6649+
out << " if (!" << cClassName << "::" << property->_has_function->_ifunc.get_name() << "()) {\n";
6650+
}
6651+
out << " Py_INCREF(Py_None);\n"
65836652
<< " return Py_None;\n"
65846653
<< " }\n";
65856654
}
@@ -6596,26 +6665,35 @@ write_getset(ostream &out, Object *obj, Property *property) {
65966665
// Write out a setter if this is not a read-only property.
65976666
if (property->_setter != NULL) {
65986667
out << "static int Dtool_" + ClassName + "_" + ielem.get_name() + "_Setter(PyObject *self, PyObject *arg, void *) {\n";
6599-
out << " " << cClassName << " *local_this = NULL;\n";
6600-
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6601-
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6602-
out << " return -1;\n";
6603-
out << " }\n\n";
6668+
if (remap->_has_this) {
6669+
out << " " << cClassName << " *local_this = NULL;\n";
6670+
out << " if (!Dtool_Call_ExtractThisPointer_NonConst(self, Dtool_" << ClassName << ", (void **)&local_this, \""
6671+
<< classNameFromCppName(cClassName, false) << "." << ielem.get_name() << "\")) {\n";
6672+
out << " return -1;\n";
6673+
out << " }\n\n";
6674+
}
66046675

66056676
out << " if (arg == (PyObject *)NULL) {\n";
6606-
if (property->_deleter != NULL) {
6677+
if (property->_deleter != NULL && remap->_has_this) {
66076678
out << " local_this->" << property->_deleter->_ifunc.get_name() << "();\n"
66086679
<< " return 0;\n";
6680+
} else if (property->_deleter != NULL) {
6681+
out << " " << cClassName << "::" << property->_deleter->_ifunc.get_name() << "();\n"
6682+
<< " return 0;\n";
66096683
} else {
66106684
out << " Dtool_Raise_TypeError(\"can't delete " << ielem.get_name() << " attribute\");\n"
66116685
" return -1;\n";
66126686
}
66136687
out << " }\n";
66146688

66156689
if (property->_clear_function != NULL) {
6616-
out << " if (arg == Py_None) {\n"
6617-
<< " local_this->" << property->_clear_function->_ifunc.get_name() << "();\n"
6618-
<< " return 0;\n"
6690+
out << " if (arg == Py_None) {\n";
6691+
if (remap->_has_this) {
6692+
out << " local_this->" << property->_clear_function->_ifunc.get_name() << "();\n";
6693+
} else {
6694+
out << " " << cClassName << "::" << property->_clear_function->_ifunc.get_name() << "();\n";
6695+
}
6696+
out << " return 0;\n"
66196697
<< " }\n";
66206698
}
66216699

@@ -6744,6 +6822,7 @@ record_object(TypeIndex type_index) {
67446822
Function *setter = record_function(itype, func_index);
67456823
if (is_function_legal(setter)) {
67466824
property->_setter = setter;
6825+
property->_has_this |= setter->_has_this;
67476826
}
67486827
}
67496828

@@ -6752,6 +6831,7 @@ record_object(TypeIndex type_index) {
67526831
Function *getter = record_function(itype, func_index);
67536832
if (is_function_legal(getter)) {
67546833
property->_getter = getter;
6834+
property->_has_this |= getter->_has_this;
67556835
}
67566836
}
67576837

@@ -6760,6 +6840,7 @@ record_object(TypeIndex type_index) {
67606840
Function *has_function = record_function(itype, func_index);
67616841
if (is_function_legal(has_function)) {
67626842
property->_has_function = has_function;
6843+
property->_has_this |= has_function->_has_this;
67636844
}
67646845
}
67656846

@@ -6768,6 +6849,7 @@ record_object(TypeIndex type_index) {
67686849
Function *clear_function = record_function(itype, func_index);
67696850
if (is_function_legal(clear_function)) {
67706851
property->_clear_function = clear_function;
6852+
property->_has_this |= clear_function->_has_this;
67716853
}
67726854
}
67736855

@@ -6776,12 +6858,16 @@ record_object(TypeIndex type_index) {
67766858
Function *del_function = record_function(itype, func_index);
67776859
if (is_function_legal(del_function)) {
67786860
property->_deleter = del_function;
6861+
property->_has_this |= del_function->_has_this;
67796862
}
67806863
}
67816864

67826865
if (ielement.is_sequence()) {
67836866
FunctionIndex func_index = ielement.get_length_function();
67846867
property->_length_function = record_function(itype, func_index);
6868+
if (property->_length_function != nullptr) {
6869+
property->_has_this |= property->_length_function->_has_this;
6870+
}
67856871
}
67866872

67876873
if (property->_getter != NULL) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy