00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #ifndef __PY_CompiledCode_h__
00019 #define __PY_CompiledCode_h__
00020
00021 #include "PY_API.h"
00022 #include "PY_Result.h"
00023 #include "PY_InterpreterAutoLock.h"
00024 #include "PY_EvaluationContext.h"
00025 #include <UT/UT_String.h>
00026 class PY_EvaluationCache;
00027 class UT_IStream;
00028
00029 class PY_API PY_CompiledCode
00030 {
00031 public:
00032 enum CodeType { STATEMENTS, EXPRESSION };
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 PY_CompiledCode(
00054 const char *python_code,
00055 CodeType code_type,
00056 const char *as_file = NULL,
00057 bool allow_function_bodies = false);
00058
00059
00060 PY_CompiledCode(
00061 UT_IStream &is,
00062 CodeType code_type);
00063
00064 ~PY_CompiledCode();
00065
00066
00067 bool hasSyntaxErrors() const;
00068 const UT_String &syntaxErrors() const;
00069
00070
00071 const UT_String &code() const;
00072
00073
00074
00075
00076 void compiledCodeAsString(UT_WorkBuffer &result) const;
00077
00078
00079 bool saveCompiledCode(ostream &os);
00080
00081
00082
00083
00084
00085
00086 void evaluate(
00087 PY_Result::Type desired_result_type, PY_Result &result) const;
00088
00089
00090
00091
00092
00093 void evaluateInContext(
00094 PY_Result::Type desired_result_type,
00095 PY_EvaluationContext &context,
00096 PY_Result &result) const
00097 {
00098 PY_InterpreterAutoLock auto_lock;
00099 evaluateUsingDicts(desired_result_type,
00100 context.myOpaqueGlobalsDict, context.myOpaqueGlobalsDict, result);
00101 }
00102
00103
00104
00105
00106 PY_Result evaluate(PY_Result::Type desired_result_type) const
00107 {
00108 PY_Result result;
00109 evaluate(desired_result_type, result);
00110 return result;
00111 }
00112
00113 PY_Result evaluateInContext(
00114 PY_Result::Type desired_result_type,
00115 PY_EvaluationContext &context) const
00116 {
00117 PY_Result result;
00118 evaluateInContext(desired_result_type, context, result);
00119 return result;
00120 }
00121
00122 private:
00123 PY_CompiledCode(const PY_CompiledCode &);
00124
00125 void initializeThreadInfoAndEvalCache();
00126
00127 PY_Result evaluateUsingDicts(
00128 PY_Result::Type desired_result_type,
00129 void *opaque_globals_dict,
00130 void *opaque_locals_dict) const;
00131 void evaluateUsingDicts(
00132 PY_Result::Type desired_result_type,
00133 void *opaque_globals_dict,
00134 void *opaque_locals_dict,
00135 PY_Result &result) const;
00136
00137 void startInterruptableEvaluation() const;
00138 void endInterruptableEvaluation() const;
00139
00140 void *myOpaqueCodeObject;
00141 CodeType myCodeType;
00142 UT_String myCode;
00143 UT_String mySyntaxErrors;
00144
00145 #ifndef ALLOW_DIFFERENT_CONSTRUCTOR_AND_EVAL_THREADS
00146 UT_PtrArray<PY_EvaluationCache *> *myEvaluationCacheStack;
00147 #endif
00148 mutable PY_EvaluationCache *myEvaluationCache;
00149 };
00150
00151 #endif