@@ -4429,6 +4429,55 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
4429
4429
// Implemented in BodyVisitor
4430
4430
}
4431
4431
4432
+ void import_cpython (const AST::FunctionDef_t &x, SymbolTable *parent_scope) {
4433
+ std::vector<std::string> pybind_funcs = {
4434
+ " Py_Initialize" ,
4435
+ " Py_IsInitialized" ,
4436
+ " PyRun_SimpleString" ,
4437
+ " Py_DecodeLocale" ,
4438
+ " PySys_SetArgv" ,
4439
+ " Py_FinalizeEx" ,
4440
+ " PyUnicode_FromString" ,
4441
+ " PyUnicode_AsUTF8AndSize" ,
4442
+ " PyImport_Import" ,
4443
+ " Py_DecRef" ,
4444
+ " Py_IncRef" ,
4445
+ " PyObject_GetAttrString" ,
4446
+ " PyTuple_New" ,
4447
+ " PyTuple_SetItem" ,
4448
+ " PyObject_CallObject" ,
4449
+ " PyLong_AsLongLong" ,
4450
+ " PyLong_AsUnsignedLongLong" ,
4451
+ " PyLong_FromLongLong" ,
4452
+ " PyLong_FromUnsignedLongLong" ,
4453
+ " PyFloat_FromDouble" ,
4454
+ " PyFloat_AsDouble" ,
4455
+ " PyBool_FromLong" ,
4456
+ " PyObject_IsTrue" ,
4457
+ };
4458
+ Str s;
4459
+ AST::alias_t *module_symbols =
4460
+ al.allocate <AST::alias_t >(pybind_funcs.size ());
4461
+
4462
+ for (size_t i = 0 ; i < pybind_funcs.size (); i++) {
4463
+ s.from_str (al, pybind_funcs.at (i));
4464
+ (module_symbols + i)->loc = x.base .base .loc ;
4465
+ (module_symbols + i)->m_name = s.c_str (al);
4466
+ (module_symbols + i)->m_asname = nullptr ;
4467
+ }
4468
+
4469
+ AST::ImportFrom_t *imports =
4470
+ (AST::ImportFrom_t*)AST::make_ImportFrom_t (
4471
+ al, x.base .base .loc ,
4472
+ (char *)" cpython_bindings" , module_symbols,
4473
+ pybind_funcs.size (), 0 );
4474
+
4475
+ SymbolTable *function_scope = current_scope;
4476
+ current_scope = parent_scope;
4477
+ visit_ImportFrom (*imports);
4478
+ current_scope = function_scope;
4479
+ }
4480
+
4432
4481
void visit_FunctionDef (const AST::FunctionDef_t &x) {
4433
4482
dependencies.clear (al);
4434
4483
SymbolTable *parent_scope = current_scope;
@@ -4492,52 +4541,8 @@ class SymbolTableVisitor : public CommonVisitor<SymbolTableVisitor> {
4492
4541
module_file = extract_keyword_val_from_decorator (call_d, " module" );
4493
4542
4494
4543
if (!contains_bindpython) {
4495
- std::vector<std::string> pybind_funcs = {
4496
- " Py_Initialize" ,
4497
- " Py_IsInitialized" ,
4498
- " PyRun_SimpleString" ,
4499
- " Py_DecodeLocale" ,
4500
- " PySys_SetArgv" ,
4501
- " Py_FinalizeEx" ,
4502
- " PyUnicode_FromString" ,
4503
- " PyUnicode_AsUTF8AndSize" ,
4504
- " PyImport_Import" ,
4505
- " Py_DecRef" ,
4506
- " Py_IncRef" ,
4507
- " PyObject_GetAttrString" ,
4508
- " PyTuple_New" ,
4509
- " PyTuple_SetItem" ,
4510
- " PyObject_CallObject" ,
4511
- " PyLong_AsLongLong" ,
4512
- " PyLong_AsUnsignedLongLong" ,
4513
- " PyLong_FromLongLong" ,
4514
- " PyLong_FromUnsignedLongLong" ,
4515
- " PyFloat_FromDouble" ,
4516
- " PyFloat_AsDouble" ,
4517
- " PyBool_FromLong" ,
4518
- " PyObject_IsTrue" ,
4519
- };
4520
- Str s;
4521
- AST::alias_t *module_symbols =
4522
- al.allocate <AST::alias_t >(pybind_funcs.size ());
4523
-
4524
- for (size_t i = 0 ; i < pybind_funcs.size (); i++) {
4525
- s.from_str (al, pybind_funcs.at (i));
4526
- (module_symbols + i)->loc = x.base .base .loc ;
4527
- (module_symbols + i)->m_name = s.c_str (al);
4528
- (module_symbols + i)->m_asname = nullptr ;
4529
- }
4530
-
4531
- AST::ImportFrom_t *imports =
4532
- (AST::ImportFrom_t*)AST::make_ImportFrom_t (
4533
- al, x.base .base .loc ,
4534
- (char *)" cpython_bindings" , module_symbols,
4535
- pybind_funcs.size (), 0 );
4536
4544
4537
- SymbolTable *function_scope = current_scope;
4538
- current_scope = parent_scope;
4539
- visit_ImportFrom (*imports);
4540
- current_scope = function_scope;
4545
+ import_cpython (x, parent_scope);
4541
4546
}
4542
4547
contains_bindpython = true ;
4543
4548
0 commit comments