github.com/goplus/llgo@v0.8.3/py/python.go (about)

     1  /*
     2   * Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package py
    18  
    19  import (
    20  	_ "unsafe"
    21  
    22  	"github.com/goplus/llgo/c"
    23  )
    24  
    25  const (
    26  	LLGoPackage = "link: $LLGO_LIB_PYTHON"
    27  )
    28  
    29  // -----------------------------------------------------------------------------
    30  
    31  //go:linkname SetProgramName C.Py_SetProgramName
    32  func SetProgramName(name *c.Char)
    33  
    34  //go:linkname Initialize C.Py_Initialize
    35  func Initialize()
    36  
    37  // This function works like Initialize() if initsigs is 1.
    38  // If initsigs is 0, it skips initialization registration of signal handlers,
    39  // which might be useful when Python is embedded.
    40  //
    41  //go:linkname InitializeEx C.Py_InitializeEx
    42  func InitializeEx(initsigs c.Int)
    43  
    44  //go:linkname Finalize C.Py_Finalize
    45  func Finalize()
    46  
    47  // -----------------------------------------------------------------------------
    48  
    49  //go:linkname RunSimpleString C.PyRun_SimpleString
    50  func RunSimpleString(command *c.Char) c.Int
    51  
    52  //go:linkname RunSimpleStringFlags C.PyRun_SimpleStringFlags
    53  func RunSimpleStringFlags(command *c.Char, flags *CompilerFlags) c.Int
    54  
    55  //go:linkname RunSimpleFile C.PyRun_SimpleFile
    56  func RunSimpleFile(fp c.FilePtr, filename *c.Char) c.Int
    57  
    58  //go:linkname RunSimpleFileFlags C.PyRun_SimpleFileFlags
    59  func RunSimpleFileFlags(fp c.FilePtr, filename *c.Char, flags *CompilerFlags) c.Int
    60  
    61  // -----------------------------------------------------------------------------
    62  
    63  type InputType c.Int
    64  
    65  const (
    66  	SingleInput InputType = 256 // read code from i/o
    67  	FileInput   InputType = 257 // read code from filename
    68  	EvalInput   InputType = 258 // read code from string
    69  	// FuncTypeInput InputType = 345
    70  )
    71  
    72  // llgo:type C
    73  type CompilerFlags struct {
    74  	CfFlags c.Int
    75  }
    76  
    77  //go:linkname CompileString C.Py_CompileString
    78  func CompileString(str, filename *c.Char, start InputType) *Object
    79  
    80  //go:linkname CompileStringFlags C.Py_CompileStringFlags
    81  func CompileStringFlags(str, filename *c.Char, start InputType, flags *CompilerFlags) *Object
    82  
    83  //go:linkname CompileStringExFlags C.Py_CompileStringExFlags
    84  func CompileStringExFlags(str, filename *c.Char, start InputType, flags *CompilerFlags, optimize c.Int) *Object
    85  
    86  // Parse and compile the Python source code in str, returning the resulting code object.
    87  // The start token is given by start; this can be used to constrain the code which can be
    88  // compiled and should be py.EvalInput, py.FileInput, or py.SingleInput. The filename
    89  // specified by filename is used to construct the code object and may appear in tracebacks
    90  // or SyntaxError exception messages. This returns NULL if the code cannot be parsed or
    91  // compiled.
    92  //
    93  // The integer optimize specifies the optimization level of the compiler; a value of -1
    94  // selects the optimization level of the interpreter as given by -O options. Explicit levels
    95  // are 0 (no optimization; __debug__ is true), 1 (asserts are removed, __debug__ is false) or
    96  // 2 (docstrings are removed too).
    97  //
    98  //go:linkname CompileStringObject C.Py_CompileStringObject
    99  func CompileStringObject(str *c.Char, filename *Object, start InputType, flags *CompilerFlags, optimize c.Int) *Object
   100  
   101  // -----------------------------------------------------------------------------
   102  
   103  // This is a simplified interface to EvalCodeEx, with just the code object, and global and
   104  // local variables. The other arguments are set to nil.
   105  //
   106  //go:linkname EvalCode C.PyEval_EvalCode
   107  func EvalCode(code, globals, locals *Object) *Object
   108  
   109  // Evaluate a precompiled code object, given a particular environment for its evaluation.
   110  // This environment consists of a dictionary of global variables, a mapping object of local
   111  // variables, arrays of arguments, keywords and defaults, a dictionary of default values for
   112  // keyword-only arguments and a closure tuple of cells.
   113  //
   114  //go:linkname EvalCodeEx C.PyEval_EvalCodeEx
   115  func EvalCodeEx(
   116  	code, globals, locals *Object,
   117  	args *Object, argcount c.Int, kws *Object, kwcount c.Int,
   118  	defs *Object, defcount c.Int, kwdefs, closure *Object) *Object
   119  
   120  // -----------------------------------------------------------------------------