@@ -456,6 +456,84 @@ PyDoc_STRVAR(set___main___attrs_doc,
456
456
\n\
457
457
Bind the given attributes in the interpreter's __main__ module." );
458
458
459
+ static PyObject *
460
+ interp_get___main___attrs (PyObject * self , PyObject * args )
461
+ {
462
+ PyObject * id , * names ;
463
+ PyObject * dflt = Py_None ;
464
+ if (!PyArg_ParseTuple (args , "OO|O:" MODULE_NAME ".get___main___attrs" ,
465
+ & id , & names , & dflt ))
466
+ {
467
+ return NULL ;
468
+ }
469
+
470
+ // Look up the interpreter.
471
+ PyInterpreterState * interp = PyInterpreterID_LookUp (id );
472
+ if (interp == NULL ) {
473
+ return NULL
8000
;
474
+ }
475
+
476
+ // Prep the result.
477
+ PyObject * found = PyDict_New ();
478
+ if (found == NULL ) {
479
+ return NULL ;
480
+ }
481
+
482
+ // Set up the shared ns.
483
+ _PyXI_namespace * shared = _PyXI_NamespaceFromNames (names );
484
+ if (shared == NULL ) {
485
+ if (!PyErr_Occurred ()) {
486
+ PyErr_SetString (PyExc_ValueError , "expected non-empty list of names" );
487
+ }
488
+ Py_DECREF (found );
489
+ return NULL ;
490
+ }
491
+
492
+ _PyXI_session session = {0 };
493
+
494
+ // Prep and switch interpreters, including apply the updates.
495
+ if (_PyXI_Enter (& session , interp , NULL ) < 0 ) {
496
+ Py_DECREF (found );
497
+ assert (!PyErr_Occurred ());
498
+ _PyXI_ApplyCapturedException (& session , NULL );
499
+ assert (PyErr_Occurred ());
500
+ return NULL ;
501
+ }
502
+
503
+ // Extract the requested attrs from __main__.
504
+ int res = _PyXI_FillNamespaceFromDict (shared , session .main_ns , & session );
505
+
506
+ // Clean up and switch back.
507
+ _PyXI_Exit (& session );
508
+
509
+ if (res == 0 ) {
510
+ assert (!PyErr_Occurred ());
511
+ // Copy the objects into the result dict.
512
+ if (_PyXI_ApplyNamespace (shared , found , dflt ) < 0 ) {
513
+ Py_CLEAR (found );
514
+ }
515
+ }
516
+ else {
517
+ if (!PyErr_Occurred ()) {
518
+ _PyXI_ApplyCapturedException (& session , NULL );
519
+ assert (PyErr_Occurred ());
520
+ }
521
+ else {
522
+ assert (!_PyXI_HasCapturedException (& session ));
523
+ }
524
+ Py_CLEAR (found );
525
+ }
526
+
527
+ _PyXI_FreeNamespace (shared );
528
+ return found ;
529
+ }
530
+
531
+ PyDoc_STRVAR (get___main___attrs_doc ,
532
+ "get___main___attrs(id, names, default=None, /)\n\
533
+ \n\
534
+ Look up the given attributes in the interpreter's __main__ module.\n\
535
+ Return the default if not found." );
536
+
459
537
static PyUnicodeObject *
460
538
convert_script_arg (PyObject * arg , const char * fname , const char * displayname ,
461
539
const char * expected )
@@ -754,6 +832,8 @@ static PyMethodDef module_functions[] = {
754
832
755
833
{"set___main___attrs" , _PyCFunction_CAST (interp_set___main___attrs ),
756
834
METH_VARARGS , set___main___attrs_doc },
835
+ {"get___main___attrs" , _PyCFunction_CAST (interp_get___main___attrs ),
836
+ METH_VARARGS , get___main___attrs_doc },
757
837
{"is_shareable" , _PyCFunction_CAST (object_is_shareable ),
758
838
METH_VARARGS | METH_KEYWORDS , is_shareable_doc },
759
839
0 commit comments