github.com/goplus/llgo@v0.8.3/py/inspect/gen.go (about) 1 package inspect 2 3 import ( 4 _ "unsafe" 5 6 "github.com/goplus/llgo/py" 7 ) 8 9 const LLGoPackage = "py.inspect" 10 11 // Compute the annotations dict for an object. 12 // 13 // obj may be a callable, class, or module. 14 // Passing in an object of any other type raises TypeError. 15 // 16 // Returns a dict. get_annotations() returns a new dict every time 17 // it's called; calling it twice on the same object will return two 18 // different but equivalent dicts. 19 // 20 // This function handles several details for you: 21 // 22 // * If eval_str is true, values of type str will 23 // be un-stringized using eval(). This is intended 24 // for use with stringized annotations 25 // ("from __future__ import annotations"). 26 // * If obj doesn't have an annotations dict, returns an 27 // empty dict. (Functions and methods always have an 28 // annotations dict; classes, modules, and other types of 29 // callables may not.) 30 // * Ignores inherited annotations on classes. If a class 31 // doesn't have its own annotations dict, returns an empty dict. 32 // * All accesses to object members and dict values are done 33 // using getattr() and dict.get() for safety. 34 // * Always, always, always returns a freshly-created dict. 35 // 36 // eval_str controls whether or not values of type str are replaced 37 // with the result of calling eval() on those values: 38 // 39 // * If eval_str is true, eval() is called on values of type str. 40 // * If eval_str is false (the default), values of type str are unchanged. 41 // 42 // globals and locals are passed in to eval(); see the documentation 43 // for eval() for more information. If either globals or locals is 44 // None, this function may replace that value with a context-specific 45 // default, contingent on type(obj): 46 // 47 // * If obj is a module, globals defaults to obj.__dict__. 48 // * If obj is a class, globals defaults to 49 // sys.modules[obj.__module__].__dict__ and locals 50 // defaults to the obj class namespace. 51 // * If obj is a callable, globals defaults to obj.__globals__, 52 // although if obj is a wrapped function (using 53 // functools.update_wrapper()) it is first unwrapped. 54 // 55 //go:linkname GetAnnotations py.get_annotations 56 func GetAnnotations(obj *py.Object) *py.Object 57 58 // Return true if the object is a module. 59 // 60 //go:linkname Ismodule py.ismodule 61 func Ismodule(object *py.Object) *py.Object 62 63 // Return true if the object is a class. 64 // 65 //go:linkname Isclass py.isclass 66 func Isclass(object *py.Object) *py.Object 67 68 // Return true if the object is an instance method. 69 // 70 //go:linkname Ismethod py.ismethod 71 func Ismethod(object *py.Object) *py.Object 72 73 // Return true if the object is a method descriptor. 74 // 75 // But not if ismethod() or isclass() or isfunction() are true. 76 // 77 // This is new in Python 2.2, and, for example, is true of int.__add__. 78 // An object passing this test has a __get__ attribute but not a __set__ 79 // attribute, but beyond that the set of attributes varies. __name__ is 80 // usually sensible, and __doc__ often is. 81 // 82 // Methods implemented via descriptors that also pass one of the other 83 // tests return false from the ismethoddescriptor() test, simply because 84 // the other tests promise more -- you can, e.g., count on having the 85 // __func__ attribute (etc) when an object passes ismethod(). 86 // 87 //go:linkname Ismethoddescriptor py.ismethoddescriptor 88 func Ismethoddescriptor(object *py.Object) *py.Object 89 90 // Return true if the object is a data descriptor. 91 // 92 // Data descriptors have a __set__ or a __delete__ attribute. Examples are 93 // properties (defined in Python) and getsets and members (defined in C). 94 // Typically, data descriptors will also have __name__ and __doc__ attributes 95 // (properties, getsets, and members have both of these attributes), but this 96 // is not guaranteed. 97 // 98 //go:linkname Isdatadescriptor py.isdatadescriptor 99 func Isdatadescriptor(object *py.Object) *py.Object 100 101 // Return true if the object is a member descriptor. 102 // 103 // Member descriptors are specialized descriptors defined in extension 104 // modules. 105 // 106 //go:linkname Ismemberdescriptor py.ismemberdescriptor 107 func Ismemberdescriptor(object *py.Object) *py.Object 108 109 // Return true if the object is a getset descriptor. 110 // 111 // getset descriptors are specialized descriptors defined in extension 112 // modules. 113 // 114 //go:linkname Isgetsetdescriptor py.isgetsetdescriptor 115 func Isgetsetdescriptor(object *py.Object) *py.Object 116 117 // Return true if the object is a user-defined function. 118 // 119 // Function objects provide these attributes: 120 // __doc__ documentation string 121 // __name__ name with which this function was defined 122 // __code__ code object containing compiled function bytecode 123 // __defaults__ tuple of any default values for arguments 124 // __globals__ global namespace in which this function was defined 125 // __annotations__ dict of parameter annotations 126 // __kwdefaults__ dict of keyword only parameters with defaults 127 // 128 //go:linkname Isfunction py.isfunction 129 func Isfunction(object *py.Object) *py.Object 130 131 // Return true if the object is a user-defined generator function. 132 // 133 // Generator function objects provide the same attributes as functions. 134 // See help(isfunction) for a list of attributes. 135 // 136 //go:linkname Isgeneratorfunction py.isgeneratorfunction 137 func Isgeneratorfunction(obj *py.Object) *py.Object 138 139 // Decorator to ensure callable is recognised as a coroutine function. 140 // 141 //go:linkname Markcoroutinefunction py.markcoroutinefunction 142 func Markcoroutinefunction(func_ *py.Object) *py.Object 143 144 // Return true if the object is a coroutine function. 145 // 146 // Coroutine functions are normally defined with "async def" syntax, but may 147 // be marked via markcoroutinefunction. 148 // 149 //go:linkname Iscoroutinefunction py.iscoroutinefunction 150 func Iscoroutinefunction(obj *py.Object) *py.Object 151 152 // Return true if the object is an asynchronous generator function. 153 // 154 // Asynchronous generator functions are defined with "async def" 155 // syntax and have "yield" expressions in their body. 156 // 157 //go:linkname Isasyncgenfunction py.isasyncgenfunction 158 func Isasyncgenfunction(obj *py.Object) *py.Object 159 160 // Return true if the object is an asynchronous generator. 161 // 162 //go:linkname Isasyncgen py.isasyncgen 163 func Isasyncgen(object *py.Object) *py.Object 164 165 // Return true if the object is a generator. 166 // 167 // Generator objects provide these attributes: 168 // __iter__ defined to support iteration over container 169 // close raises a new GeneratorExit exception inside the 170 // generator to terminate the iteration 171 // gi_code code object 172 // gi_frame frame object or possibly None once the generator has 173 // been exhausted 174 // gi_running set to 1 when generator is executing, 0 otherwise 175 // next return the next item from the container 176 // send resumes the generator and "sends" a value that becomes 177 // the result of the current yield-expression 178 // throw used to raise an exception inside the generator 179 // 180 //go:linkname Isgenerator py.isgenerator 181 func Isgenerator(object *py.Object) *py.Object 182 183 // Return true if the object is a coroutine. 184 // 185 //go:linkname Iscoroutine py.iscoroutine 186 func Iscoroutine(object *py.Object) *py.Object 187 188 // Return true if object can be passed to an “await“ expression. 189 // 190 //go:linkname Isawaitable py.isawaitable 191 func Isawaitable(object *py.Object) *py.Object 192 193 // Return true if the object is a traceback. 194 // 195 // Traceback objects provide these attributes: 196 // tb_frame frame object at this level 197 // tb_lasti index of last attempted instruction in bytecode 198 // tb_lineno current line number in Python source code 199 // tb_next next inner traceback object (called by this level) 200 // 201 //go:linkname Istraceback py.istraceback 202 func Istraceback(object *py.Object) *py.Object 203 204 // Return true if the object is a frame object. 205 // 206 // Frame objects provide these attributes: 207 // f_back next outer frame object (this frame's caller) 208 // f_builtins built-in namespace seen by this frame 209 // f_code code object being executed in this frame 210 // f_globals global namespace seen by this frame 211 // f_lasti index of last attempted instruction in bytecode 212 // f_lineno current line number in Python source code 213 // f_locals local namespace seen by this frame 214 // f_trace tracing function for this frame, or None 215 // 216 //go:linkname Isframe py.isframe 217 func Isframe(object *py.Object) *py.Object 218 219 // Return true if the object is a code object. 220 // 221 // Code objects provide these attributes: 222 // co_argcount number of arguments (not including *, ** args 223 // or keyword only arguments) 224 // co_code string of raw compiled bytecode 225 // co_cellvars tuple of names of cell variables 226 // co_consts tuple of constants used in the bytecode 227 // co_filename name of file in which this code object was created 228 // co_firstlineno number of first line in Python source code 229 // co_flags bitmap: 1=optimized | 2=newlocals | 4=*arg | 8=**arg 230 // | 16=nested | 32=generator | 64=nofree | 128=coroutine 231 // | 256=iterable_coroutine | 512=async_generator 232 // co_freevars tuple of names of free variables 233 // co_posonlyargcount number of positional only arguments 234 // co_kwonlyargcount number of keyword only arguments (not including ** arg) 235 // co_lnotab encoded mapping of line numbers to bytecode indices 236 // co_name name with which this code object was defined 237 // co_names tuple of names other than arguments and function locals 238 // co_nlocals number of local variables 239 // co_stacksize virtual machine stack space required 240 // co_varnames tuple of names of arguments and local variables 241 // 242 //go:linkname Iscode py.iscode 243 func Iscode(object *py.Object) *py.Object 244 245 // Return true if the object is a built-in function or method. 246 // 247 // Built-in functions and methods provide these attributes: 248 // __doc__ documentation string 249 // __name__ original name of this function or method 250 // __self__ instance to which a method is bound, or None 251 // 252 //go:linkname Isbuiltin py.isbuiltin 253 func Isbuiltin(object *py.Object) *py.Object 254 255 // Return true if the object is a method wrapper. 256 // 257 //go:linkname Ismethodwrapper py.ismethodwrapper 258 func Ismethodwrapper(object *py.Object) *py.Object 259 260 // Return true if the object is any kind of function or method. 261 // 262 //go:linkname Isroutine py.isroutine 263 func Isroutine(object *py.Object) *py.Object 264 265 // Return true if the object is an abstract base class (ABC). 266 // 267 //go:linkname Isabstract py.isabstract 268 func Isabstract(object *py.Object) *py.Object 269 270 // Return all members of an object as (name, value) pairs sorted by name. 271 // 272 // Optionally, only return members that satisfy a given predicate. 273 // 274 //go:linkname Getmembers py.getmembers 275 func Getmembers(object *py.Object, predicate *py.Object) *py.Object 276 277 // Return all members of an object as (name, value) pairs sorted by name 278 // 279 // without triggering dynamic lookup via the descriptor protocol, 280 // __getattr__ or __getattribute__. Optionally, only return members that 281 // satisfy a given predicate. 282 // 283 // Note: this function may not be able to retrieve all members 284 // that getmembers can fetch (like dynamically created attributes) 285 // and may find members that getmembers can't (like descriptors 286 // that raise AttributeError). It can also return descriptor objects 287 // instead of instance members in some cases. 288 // 289 //go:linkname GetmembersStatic py.getmembers_static 290 func GetmembersStatic(object *py.Object, predicate *py.Object) *py.Object 291 292 // Return list of attribute-descriptor tuples. 293 // 294 // For each name in dir(cls), the return list contains a 4-tuple 295 // with these elements: 296 // 297 // 0. The name (a string). 298 // 299 // 1. The kind of attribute this is, one of these strings: 300 // 'class method' created via classmethod() 301 // 'static method' created via staticmethod() 302 // 'property' created via property() 303 // 'method' any other flavor of method or descriptor 304 // 'data' not a method 305 // 306 // 2. The class which defined this attribute (a class). 307 // 308 // 3. The object as obtained by calling getattr; if this fails, or if the 309 // resulting object does not live anywhere in the class' mro (including 310 // metaclasses) then the object is looked up in the defining class's 311 // dict (found by walking the mro). 312 // 313 // If one of the items in dir(cls) is stored in the metaclass it will now 314 // be discovered and not have None be listed as the class in which it was 315 // defined. Any items whose home class cannot be discovered are skipped. 316 // 317 //go:linkname ClassifyClassAttrs py.classify_class_attrs 318 func ClassifyClassAttrs(cls *py.Object) *py.Object 319 320 // Return tuple of base classes (including cls) in method resolution order. 321 // 322 //go:linkname Getmro py.getmro 323 func Getmro(cls *py.Object) *py.Object 324 325 // Get the object wrapped by *func*. 326 // 327 // Follows the chain of :attr:`__wrapped__` attributes returning the last 328 // object in the chain. 329 // 330 // *stop* is an optional callback accepting an object in the wrapper chain 331 // as its sole argument that allows the unwrapping to be terminated early if 332 // the callback returns a true value. If the callback never returns a true 333 // value, the last object in the chain is returned as usual. For example, 334 // :func:`signature` uses this to stop unwrapping if any object in the 335 // chain has a ``__signature__`` attribute defined. 336 // 337 // :exc:`ValueError` is raised if a cycle is encountered. 338 // 339 //go:linkname Unwrap py.unwrap 340 func Unwrap(func_ *py.Object) *py.Object 341 342 // Return the indent size, in spaces, at the start of a line of text. 343 // 344 //go:linkname Indentsize py.indentsize 345 func Indentsize(line *py.Object) *py.Object 346 347 // Get the documentation string for an object. 348 // 349 // All tabs are expanded to spaces. To clean up docstrings that are 350 // indented to line up with blocks of code, any whitespace than can be 351 // uniformly removed from the second line onwards is removed. 352 // 353 //go:linkname Getdoc py.getdoc 354 func Getdoc(object *py.Object) *py.Object 355 356 // Clean up indentation from docstrings. 357 // 358 // Any whitespace that can be uniformly removed from the second line 359 // onwards is removed. 360 // 361 //go:linkname Cleandoc py.cleandoc 362 func Cleandoc(doc *py.Object) *py.Object 363 364 // Work out which source or compiled file an object was defined in. 365 // 366 //go:linkname Getfile py.getfile 367 func Getfile(object *py.Object) *py.Object 368 369 // Return the module name for a given file, or None. 370 // 371 //go:linkname Getmodulename py.getmodulename 372 func Getmodulename(path *py.Object) *py.Object 373 374 // Return the filename that can be used to locate an object's source. 375 // 376 // Return None if no way can be identified to get the source. 377 // 378 //go:linkname Getsourcefile py.getsourcefile 379 func Getsourcefile(object *py.Object) *py.Object 380 381 // Return an absolute path to the source or compiled file for an object. 382 // 383 // The idea is for each object to have a unique origin, so this routine 384 // normalizes the result as much as possible. 385 // 386 //go:linkname Getabsfile py.getabsfile 387 func Getabsfile(object *py.Object, Filename *py.Object) *py.Object 388 389 // Return the module an object was defined in, or None if not found. 390 // 391 //go:linkname Getmodule py.getmodule 392 func Getmodule(object *py.Object, Filename *py.Object) *py.Object 393 394 // Return the entire source file and starting line number for an object. 395 // 396 // The argument may be a module, class, method, function, traceback, frame, 397 // or code object. The source code is returned as a list of all the lines 398 // in the file and the line number indexes a line in that list. An OSError 399 // is raised if the source code cannot be retrieved. 400 // 401 //go:linkname Findsource py.findsource 402 func Findsource(object *py.Object) *py.Object 403 404 // Get lines of comments immediately preceding an object's source code. 405 // 406 // Returns None when source can't be found. 407 // 408 //go:linkname Getcomments py.getcomments 409 func Getcomments(object *py.Object) *py.Object 410 411 // Extract the block of code at the top of the given list of lines. 412 // 413 //go:linkname Getblock py.getblock 414 func Getblock(lines *py.Object) *py.Object 415 416 // Return a list of source lines and starting line number for an object. 417 // 418 // The argument may be a module, class, method, function, traceback, frame, 419 // or code object. The source code is returned as a list of the lines 420 // corresponding to the object and the line number indicates where in the 421 // original source file the first line of code was found. An OSError is 422 // raised if the source code cannot be retrieved. 423 // 424 //go:linkname Getsourcelines py.getsourcelines 425 func Getsourcelines(object *py.Object) *py.Object 426 427 // Return the text of the source code for an object. 428 // 429 // The argument may be a module, class, method, function, traceback, frame, 430 // or code object. The source code is returned as a single string. An 431 // OSError is raised if the source code cannot be retrieved. 432 // 433 //go:linkname Getsource py.getsource 434 func Getsource(object *py.Object) *py.Object 435 436 // Recursive helper function for getclasstree(). 437 // 438 //go:linkname Walktree py.walktree 439 func Walktree(classes *py.Object, children *py.Object, parent *py.Object) *py.Object 440 441 // Arrange the given list of classes into a hierarchy of nested lists. 442 // 443 // Where a nested list appears, it contains classes derived from the class 444 // whose entry immediately precedes the list. Each entry is a 2-tuple 445 // containing a class and a tuple of its base classes. If the 'unique' 446 // argument is true, exactly one entry appears in the returned structure 447 // for each class in the given list. Otherwise, classes using multiple 448 // inheritance and their descendants will appear multiple times. 449 // 450 //go:linkname Getclasstree py.getclasstree 451 func Getclasstree(classes *py.Object, unique *py.Object) *py.Object 452 453 // Get information about the arguments accepted by a code object. 454 // 455 // Three things are returned: (args, varargs, varkw), where 456 // 'args' is the list of argument names. Keyword-only arguments are 457 // appended. 'varargs' and 'varkw' are the names of the * and ** 458 // arguments or None. 459 // 460 //go:linkname Getargs py.getargs 461 func Getargs(co *py.Object) *py.Object 462 463 // Get the names and default values of a callable object's parameters. 464 // 465 // A tuple of seven things is returned: 466 // (args, varargs, varkw, defaults, kwonlyargs, kwonlydefaults, annotations). 467 // 'args' is a list of the parameter names. 468 // 'varargs' and 'varkw' are the names of the * and ** parameters or None. 469 // 'defaults' is an n-tuple of the default values of the last n parameters. 470 // 'kwonlyargs' is a list of keyword-only parameter names. 471 // 'kwonlydefaults' is a dictionary mapping names from kwonlyargs to defaults. 472 // 'annotations' is a dictionary mapping parameter names to annotations. 473 // 474 // Notable differences from inspect.signature(): 475 // - the "self" parameter is always reported, even for bound methods 476 // - wrapper chains defined by __wrapped__ *not* unwrapped automatically 477 // 478 //go:linkname Getfullargspec py.getfullargspec 479 func Getfullargspec(func_ *py.Object) *py.Object 480 481 // Get information about arguments passed into a particular frame. 482 // 483 // A tuple of four things is returned: (args, varargs, varkw, locals). 484 // 'args' is a list of the argument names. 485 // 'varargs' and 'varkw' are the names of the * and ** arguments or None. 486 // 'locals' is the locals dictionary of the given frame. 487 // 488 //go:linkname Getargvalues py.getargvalues 489 func Getargvalues(frame *py.Object) *py.Object 490 491 // Get the mapping of arguments to values. 492 // 493 // A dict is returned, with keys the function argument names (including the 494 // names of the * and ** arguments, if any), and values the respective bound 495 // values from 'positional' and 'named'. 496 // 497 //go:linkname Getcallargs py.getcallargs 498 func Getcallargs(func_ *py.Object, __llgo_va_list ...interface{}) *py.Object 499 500 // Get the mapping of free variables to their current values. 501 // 502 // Returns a named tuple of dicts mapping the current nonlocal, global 503 // and builtin references as seen by the body of the function. A final 504 // set of unbound names that could not be resolved is also provided. 505 // 506 //go:linkname Getclosurevars py.getclosurevars 507 func Getclosurevars(func_ *py.Object) *py.Object 508 509 // Get information about a frame or traceback object. 510 // 511 // A tuple of five things is returned: the filename, the line number of 512 // the current line, the function name, a list of lines of context from 513 // the source code, and the index of the current line within that list. 514 // The optional second argument specifies the number of lines of context 515 // to return, which are centered around the current line. 516 // 517 //go:linkname Getframeinfo py.getframeinfo 518 func Getframeinfo(frame *py.Object, context *py.Object) *py.Object 519 520 // Get the line number from a frame object, allowing for optimization. 521 // 522 //go:linkname Getlineno py.getlineno 523 func Getlineno(frame *py.Object) *py.Object 524 525 // Get a list of records for a frame and all higher (calling) frames. 526 // 527 // Each record contains a frame object, filename, line number, function 528 // name, a list of lines of context, and index within the context. 529 // 530 //go:linkname Getouterframes py.getouterframes 531 func Getouterframes(frame *py.Object, context *py.Object) *py.Object 532 533 // Get a list of records for a traceback's frame and all lower frames. 534 // 535 // Each record contains a frame object, filename, line number, function 536 // name, a list of lines of context, and index within the context. 537 // 538 //go:linkname Getinnerframes py.getinnerframes 539 func Getinnerframes(tb *py.Object, context *py.Object) *py.Object 540 541 // Return the frame of the caller or None if this is not possible. 542 // 543 //go:linkname Currentframe py.currentframe 544 func Currentframe() *py.Object 545 546 // Return a list of records for the stack above the caller's frame. 547 // 548 //go:linkname Stack py.stack 549 func Stack(context *py.Object) *py.Object 550 551 // Return a list of records for the stack below the current exception. 552 // 553 //go:linkname Trace py.trace 554 func Trace(context *py.Object) *py.Object 555 556 // Get current state of a generator-iterator. 557 // 558 // Possible states are: 559 // GEN_CREATED: Waiting to start execution. 560 // GEN_RUNNING: Currently being executed by the interpreter. 561 // GEN_SUSPENDED: Currently suspended at a yield expression. 562 // GEN_CLOSED: Execution has completed. 563 // 564 //go:linkname Getgeneratorstate py.getgeneratorstate 565 func Getgeneratorstate(generator *py.Object) *py.Object 566 567 // Get the mapping of generator local variables to their current values. 568 // 569 // A dict is returned, with the keys the local variable names and values the 570 // bound values. 571 // 572 //go:linkname Getgeneratorlocals py.getgeneratorlocals 573 func Getgeneratorlocals(generator *py.Object) *py.Object 574 575 // Get current state of a coroutine object. 576 // 577 // Possible states are: 578 // CORO_CREATED: Waiting to start execution. 579 // CORO_RUNNING: Currently being executed by the interpreter. 580 // CORO_SUSPENDED: Currently suspended at an await expression. 581 // CORO_CLOSED: Execution has completed. 582 // 583 //go:linkname Getcoroutinestate py.getcoroutinestate 584 func Getcoroutinestate(coroutine *py.Object) *py.Object 585 586 // Get the mapping of coroutine local variables to their current values. 587 // 588 // A dict is returned, with the keys the local variable names and values the 589 // bound values. 590 // 591 //go:linkname Getcoroutinelocals py.getcoroutinelocals 592 func Getcoroutinelocals(coroutine *py.Object) *py.Object 593 594 // Get current state of an asynchronous generator object. 595 // 596 // Possible states are: 597 // AGEN_CREATED: Waiting to start execution. 598 // AGEN_RUNNING: Currently being executed by the interpreter. 599 // AGEN_SUSPENDED: Currently suspended at a yield expression. 600 // AGEN_CLOSED: Execution has completed. 601 // 602 //go:linkname Getasyncgenstate py.getasyncgenstate 603 func Getasyncgenstate(agen *py.Object) *py.Object 604 605 // Get the mapping of asynchronous generator local variables to their current 606 // values. 607 // 608 // A dict is returned, with the keys the local variable names and values the 609 // bound values. 610 // 611 //go:linkname Getasyncgenlocals py.getasyncgenlocals 612 func Getasyncgenlocals(agen *py.Object) *py.Object 613 614 // Get a signature object for the passed callable. 615 // 616 //go:linkname Signature py.signature 617 func Signature(obj *py.Object) *py.Object