2011-10-23 17:48:10 -07:00
|
|
|
#include "pyc_code.h"
|
|
|
|
#include "pyc_module.h"
|
2009-07-24 08:35:21 +00:00
|
|
|
#include "data.h"
|
|
|
|
|
2023-12-05 13:43:50 -08:00
|
|
|
/* == Marshal structure for Code object ==
|
|
|
|
1.0 1.3 1.5 2.1 2.3 3.0 3.8 3.11
|
|
|
|
argcount short short short long long long long
|
|
|
|
posonlyargc long long
|
|
|
|
kwonlyargc long long long
|
|
|
|
nlocals short short short long long long
|
|
|
|
stacksize short short long long long long
|
|
|
|
flags short short short long long long long
|
|
|
|
code Obj Obj Obj Obj Obj Obj Obj Obj
|
|
|
|
consts Obj Obj Obj Obj Obj Obj Obj Obj
|
|
|
|
names Obj Obj Obj Obj Obj Obj Obj Obj
|
|
|
|
varnames Obj Obj Obj Obj Obj Obj
|
|
|
|
freevars Obj Obj Obj Obj
|
|
|
|
cellvars Obj Obj Obj Obj
|
|
|
|
locals+names Obj
|
|
|
|
locals+kinds Obj
|
|
|
|
filename Obj Obj Obj Obj Obj Obj Obj Obj
|
|
|
|
name Obj Obj Obj Obj Obj Obj Obj Obj
|
|
|
|
qualname Obj
|
|
|
|
firstline short short long long long long
|
|
|
|
lntable Obj Obj Obj Obj Obj Obj
|
|
|
|
exceptiontable Obj
|
|
|
|
*/
|
|
|
|
|
2009-07-24 08:35:21 +00:00
|
|
|
void PycCode::load(PycData* stream, PycModule* mod)
|
|
|
|
{
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_argCount = stream->get16();
|
2012-05-26 14:03:10 -07:00
|
|
|
else if (mod->verCompare(2, 3) >= 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_argCount = stream->get32();
|
|
|
|
|
2019-02-08 16:06:55 -08:00
|
|
|
if (mod->verCompare(3, 8) >= 0)
|
|
|
|
m_posOnlyArgCount = stream->get32();
|
|
|
|
else
|
|
|
|
m_posOnlyArgCount = 0;
|
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->majorVer() >= 3)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_kwOnlyArgCount = stream->get32();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_kwOnlyArgCount = 0;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_numLocals = stream->get16();
|
2022-12-02 16:34:58 -08:00
|
|
|
else if (mod->verCompare(2, 3) >= 0 && mod->verCompare(3, 11) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_numLocals = stream->get32();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_numLocals = 0;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 5) >= 0 && mod->verCompare(2, 3) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_stackSize = stream->get16();
|
2012-05-26 14:03:10 -07:00
|
|
|
else if (mod->verCompare(2, 3) >= 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_stackSize = stream->get32();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_stackSize = 0;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 3) >= 0 && mod->verCompare(2, 3) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_flags = stream->get16();
|
2012-05-26 14:03:10 -07:00
|
|
|
else if (mod->verCompare(2, 3) >= 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_flags = stream->get32();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_flags = 0;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2024-08-04 12:43:23 -07:00
|
|
|
if (mod->verCompare(3, 8) < 0) {
|
|
|
|
// Remap flags to new values introduced in 3.8
|
|
|
|
if (m_flags & 0xF0000000)
|
|
|
|
throw std::runtime_error("Cannot remap unexpected flags");
|
|
|
|
m_flags = (m_flags & 0xFFFF) | ((m_flags & 0xFFF0000) << 4);
|
|
|
|
}
|
|
|
|
|
2022-12-01 16:13:31 -08:00
|
|
|
m_code = LoadObject(stream, mod).cast<PycString>();
|
|
|
|
m_consts = LoadObject(stream, mod).cast<PycSequence>();
|
|
|
|
m_names = LoadObject(stream, mod).cast<PycSequence>();
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2023-01-19 11:20:06 -08:00
|
|
|
if (mod->verCompare(1, 3) >= 0)
|
|
|
|
m_localNames = LoadObject(stream, mod).cast<PycSequence>();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
2023-01-19 11:20:06 -08:00
|
|
|
m_localNames = new PycTuple;
|
|
|
|
|
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
m_localKinds = LoadObject(stream, mod).cast<PycString>();
|
|
|
|
else
|
|
|
|
m_localKinds = new PycString;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-02 16:34:58 -08:00
|
|
|
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0)
|
2022-12-01 16:13:31 -08:00
|
|
|
m_freeVars = LoadObject(stream, mod).cast<PycSequence>();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_freeVars = new PycTuple;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-02 16:34:58 -08:00
|
|
|
if (mod->verCompare(2, 1) >= 0 && mod->verCompare(3, 11) < 0)
|
2022-12-01 16:13:31 -08:00
|
|
|
m_cellVars = LoadObject(stream, mod).cast<PycSequence>();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_cellVars = new PycTuple;
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-01 16:13:31 -08:00
|
|
|
m_fileName = LoadObject(stream, mod).cast<PycString>();
|
|
|
|
m_name = LoadObject(stream, mod).cast<PycString>();
|
2009-07-24 08:35:21 +00:00
|
|
|
|
2022-12-02 16:34:58 -08:00
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
m_qualName = LoadObject(stream, mod).cast<PycString>();
|
|
|
|
else
|
|
|
|
m_qualName = new PycString;
|
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 5) >= 0 && mod->verCompare(2, 3) < 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_firstLine = stream->get16();
|
2012-05-26 14:03:10 -07:00
|
|
|
else if (mod->verCompare(2, 3) >= 0)
|
2009-07-24 08:35:21 +00:00
|
|
|
m_firstLine = stream->get32();
|
|
|
|
|
2012-05-26 14:03:10 -07:00
|
|
|
if (mod->verCompare(1, 5) >= 0)
|
2022-12-01 16:13:31 -08:00
|
|
|
m_lnTable = LoadObject(stream, mod).cast<PycString>();
|
2018-01-28 14:33:26 -08:00
|
|
|
else
|
|
|
|
m_lnTable = new PycString;
|
2022-12-02 16:34:58 -08:00
|
|
|
|
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
m_exceptTable = LoadObject(stream, mod).cast<PycString>();
|
|
|
|
else
|
|
|
|
m_exceptTable = new PycString;
|
2009-07-24 08:35:21 +00:00
|
|
|
}
|
2023-01-19 13:05:06 -08:00
|
|
|
|
|
|
|
PycRef<PycString> PycCode::getCellVar(PycModule* mod, int idx) const
|
|
|
|
{
|
|
|
|
if (mod->verCompare(3, 11) >= 0)
|
|
|
|
return getLocal(idx);
|
|
|
|
|
|
|
|
return (idx >= m_cellVars->size())
|
|
|
|
? m_freeVars->get(idx - m_cellVars->size()).cast<PycString>()
|
|
|
|
: m_cellVars->get(idx).cast<PycString>();
|
|
|
|
}
|