Skip to content

Commit

Permalink
Redefinition checks2 (#2462)
Browse files Browse the repository at this point in the history
  • Loading branch information
pepone authored Jul 11, 2024
1 parent 5c1163f commit bf36cc6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
47 changes: 47 additions & 0 deletions cpp/src/slice2py/PythonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,21 @@ lookupKwd(const string& name)
return found ? "_" + name : name;
}

static string
getDictLookup(const ContainedPtr& cont, const string& suffix = "", const string& prefix = "")
{
string scope = Slice::Python::scopedToName(cont->scope());
assert(!scope.empty());

string package = Slice::Python::getPackageMetadata(cont);
if (!package.empty())
{
scope = package + "." + scope;
}

return "'" + suffix + Slice::Python::fixIdent(cont->name() + prefix) + "' not in _M_" + scope + "__dict__";
}

//
// ModuleVisitor implementation.
//
Expand Down Expand Up @@ -362,7 +377,10 @@ Slice::Python::CodeVisitor::visitClassDecl(const ClassDeclPtr& p)
string scoped = p->scoped();
if (_classHistory.count(scoped) == 0)
{
_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.declareValue('" << scoped << "')";
_out.dec();
_classHistory.insert(scoped); // Avoid redundant declarations.
}
}
Expand All @@ -376,8 +394,11 @@ Slice::Python::CodeVisitor::visitInterfaceDecl(const InterfaceDeclPtr& p)
string scoped = p->scoped();
if (_classHistory.count(scoped) == 0)
{
_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_", "Disp") << " = IcePy.declareClass('" << scoped << "')";
_out << nl << "_M_" << getAbsolute(p, "_t_", "Prx") << " = IcePy.declareProxy('" << scoped << "')";
_out.dec();
_classHistory.insert(scoped); // Avoid redundant declarations.
}
}
Expand Down Expand Up @@ -450,6 +471,8 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)
string valueName = fixIdent(p->name());
ClassDefPtr base = p->base();

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << valueName << '(';
if (!base)
Expand Down Expand Up @@ -598,6 +621,8 @@ Slice::Python::CodeVisitor::visitClassDefStart(const ClassDefPtr& p)

registerName(valueName);

_out.dec();

if (_classHistory.count(scoped) == 0)
{
_classHistory.insert(scoped); // Avoid redundant declarations.
Expand Down Expand Up @@ -625,6 +650,9 @@ Slice::Python::CodeVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
writeMetaData(p->getMetaData());
_out << ", True, None, ())";

_out << sp << nl << "if " << getDictLookup(p, "", "Prx") << ':';
_out.inc();

// Define the proxy class
_out << nl << "_M_" << prxAbs << " = None";
_out << nl << "class " << prxName << '(';
Expand Down Expand Up @@ -997,6 +1025,7 @@ Slice::Python::CodeVisitor::visitInterfaceDefStart(const InterfaceDefPtr& p)
}

registerName(className);
_out.dec();

if (_classHistory.count(scoped) == 0)
{
Expand All @@ -1013,6 +1042,8 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p)
string abs = getAbsolute(p);
string name = fixIdent(p->name());

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << '(';
ExceptionPtr base = p->base();
Expand Down Expand Up @@ -1141,6 +1172,8 @@ Slice::Python::CodeVisitor::visitExceptionStart(const ExceptionPtr& p)

registerName(name);

_out.dec();

return false;
}

Expand All @@ -1163,6 +1196,8 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p)
}
}

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << "(object):";
_out.inc();
Expand Down Expand Up @@ -1410,6 +1445,8 @@ Slice::Python::CodeVisitor::visitStructStart(const StructPtr& p)

registerName(name);

_out.dec();

return false;
}

Expand All @@ -1419,25 +1456,31 @@ Slice::Python::CodeVisitor::visitSequence(const SequencePtr& p)
// Emit the type information.
StringList metaData = p->getMetaData();
string scoped = p->scoped();
_out << sp << nl << "if " << getDictLookup(p, "_t_") << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineSequence('" << scoped << "', ";
writeMetaData(metaData);
_out << ", ";
writeType(p->type());
_out << ")";
_out.dec();
}

void
Slice::Python::CodeVisitor::visitDictionary(const DictionaryPtr& p)
{
// Emit the type information.
string scoped = p->scoped();
_out << sp << nl << "if " << getDictLookup(p, "_t_") << ':';
_out.inc();
_out << nl << "_M_" << getAbsolute(p, "_t_") << " = IcePy.defineDictionary('" << scoped << "', ";
writeMetaData(p->getMetaData());
_out << ", ";
writeType(p->keyType());
_out << ", ";
writeType(p->valueType());
_out << ")";
_out.dec();
}

void
Expand All @@ -1449,6 +1492,8 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p)
EnumeratorList enums = p->enumerators();
EnumeratorList::iterator q;

_out << sp << nl << "if " << getDictLookup(p) << ':';
_out.inc();
_out << nl << "_M_" << abs << " = None";
_out << nl << "class " << name << "(Ice.EnumBase):";
_out.inc();
Expand Down Expand Up @@ -1499,6 +1544,8 @@ Slice::Python::CodeVisitor::visitEnum(const EnumPtr& p)
_out << ", " << name << "._enumerators)";

registerName(name);

_out.dec();
}

void
Expand Down
13 changes: 0 additions & 13 deletions python/modules/IcePy/Slice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include "Util.h"
#include "slice2py/PythonUtil.h"

#include <set>
#include <string>

//
// Python headers needed for PyEval_EvalCode.
//
Expand All @@ -25,11 +22,6 @@ using namespace Slice;
using namespace Slice::Python;
using namespace IceInternal;

namespace
{
set<string> loadedSliceFiles;
}

extern "C" PyObject*
IcePy_loadSlice(PyObject* /*self*/, PyObject* args)
{
Expand Down Expand Up @@ -128,11 +120,6 @@ IcePy_loadSlice(PyObject* /*self*/, PyObject* args)

for (const auto& file : files)
{
if (!loadedSliceFiles.insert(Slice::fullPath(file)).second)
{
continue;
}

Slice::PreprocessorPtr icecpp = Slice::Preprocessor::create("icecpp", file, cppArgs);
FILE* cppHandle = icecpp->preprocess(keepComments, "-D__SLICE2PY__");

Expand Down

0 comments on commit bf36cc6

Please sign in to comment.