This article shares with you the main isPythonin thePYcfiles andCodeObjectRelated content, come together to look at it, hope to learn Python to help you. PythonThe result of compiling the source program is to generate a. PYcfile. Pythonthe. PYThe compilation result of the file is a byte code,in order to be reusable and not need to recompile to write. PYcfile.for the interpreter,Pycodeobjectobject is the actual compilation result, PYCThe file is just a representation of the object on the hard disk.. Pycodeobject [Code.h]typedefstruct{ Pyobject_head intCo_argcount; /* #arguments, except *args */ intCo_kwonlyargcount; /* #keyword only arguments */ intCo_nlocals; /* #local Variables */ intCo_stacksize; /* #entries needed for evaluation stack */ intCo_flags; /* Co_ ..., see below */ intCo_firstlineno; /* First Source line number */ Pyobject *co_code; /* Instruction Opcodes */ Pyobject *co_consts; /* List (constants used) */ Pyobject *co_names; /* List of strings (names used) */ Pyobject *co_varnames; /* Tuple of strings (local variable names) */ Pyobject *co_freevars; /* Tuple of strings (free variable names) */ Pyobject *co_cellvars; /* Tuple of strings (cell variable names) */ void*co_extra; } Pycodeobject;when the compiler compiles the source code,each oneCode Blockwill create aPycodeobjectobject corresponds to this code snippet.the range of code snippets can be large and small. can be the wholepyfile, can beclass,can be a function. accessing pycodeobject objects in thePythonin, have withClevel of thePycodeobjectSimple Packaging, CodeObject, can access thePycodeobjectthe various domains. >>> Source = open (' db.py '). Read () >>> CO = compile (source, ' db.py ', ' exec ') >>> type (CO) <class’Code' >>>> co.co_names (' pymysql ', ' config ', ' threading ', ' Rlock ', ' Lock ', ' create_table_template ', ' OB ' Ject ', ' Model ', ' str ', ' m ') Write file pymarshal_writeobjecttofiletoPYcThe file writes the data mainly to these several, there are limitations .: [marshal.c] typedef struct { FILE *FP; int depth; Pyobject *str; Char *ptr; Char *end; Char *buf; _py_hashtable_t *hashtable; int version; } wfile; #define W_byte (C, p) do { if ((P)->ptr = (P)->end | | W_reserve ((P), 1)) *(P)->ptr++ = (C); } while (0) static void W_flush (Wfile*P) { AssertP->FP! = NULL); FwriteP->buf, 1, P->ptr-p->buf, P->FP); P->ptr = p->buf; }This is the basic structure of the file write definitionFppoint to the last file to write, W_byte (c, p)It's a simple package.,copied in bytes to thep->ptrin advance area. W_flush (wfile *p)It is the bufferP->bufWrite to File. p->ptrThat is, the incremental portion of the file to be written.. Staticvoid W_long (long x, Wfile *p) { W_byte ((Char) (X & 0xFF), p); W_byte ((Char) ((x>> 8) & 0xff), p); W_byte ((Char) ((x>>16) & 0xff), p); W_byte ((Char) ((x>>24) & 0xff), p); } Staticvoid W_string (ConstChar*s, py_ssize_t N, Wfile *p) { py_ssize_t m; if(!n | | p->ptr = = NULL) return; m = p->end-p->ptr; if(P->FP! = NULL) { if(n <= m) { memcpy (p->ptr, S, N); P->ptr + = n; } Else{ W_flush (P); Fwrite (S, 1, N, P->FP); } } Else{ if(n <= m | | w_reserve (P, n-m)) { memcpy (p->ptr, S, N); P->ptr + = n; } } }as in callingPymarshal_writelongtofilewhen,is calledW_long,The data will be written in a byte byte to the file. and CallPymarshal_writeobjecttofilewill also be called W_object,This function is relatively long,it's not listed..to differentiate the type of write,an action is made before the file is written,is to write the type of object you want to write first.: [marshal.c] #define Type_null ' 0 ' #define Type_none ' N ' #define Type_false ' F ' #define Type_true ' T ' #define Type_stopiter ' S ' #define W_TYPE (T, p) do { W_byte ((T) | Flag, (P)); } while (0)The identity of this object type is readPYcfile is critical, because Object writesPYcfile after, all data becomes byte stream, type information is missing. with this logo,, When such an identity is read, indicates the end of the previous object, start of new object, and I know what kind of new object it is..internally there are mechanisms to handle shared objects,reduce redundant information in bytecode. the share type belongs totype_interned. load pyc file pymarshal_readobjectfromfile Check the loadPYcthe process of the file, LetPYcdocument understanding more deeply: Pyobject * Pymarshal_readobjectfromfile (FILE*FP) { Rfile RF; Pyobject *result; RF.FP = FP; rf.readable = NULL; Rf.current_filename = NULL; rf.depth = 0; Rf.ptr = Rf.end = NULL; Rf.buf = NULL; Rf.refs = pylist_new (0); if (RF. refs = = NULL) return NULL; result = R_object (&RF); Py_decref (RF. refs); if (RF. buf = NULL) Pymem_free (RF. buf); return result; }fromR_objectstart fromPYcread data into the file, and createPycodeobjectObject, thisR_objectis toW_objectthe inverse.when you read thetype_internedafter,The string that follows it will be read into the, This string isInternOperation.Source:perched later than a mound
Python Learning PYc files and code objects