From af7586e1dde0b9ce4f36d86f5483bcaf924a2f41 Mon Sep 17 00:00:00 2001 From: hinus_fan Date: Sun, 18 Aug 2019 00:05:11 +0800 Subject: [PATCH] Summary: bug fix Issue: https://gitee.com/hinus/pythonvm/issues/I10T5J Description: set lib path to absolute path, avoid finding builtin.pyc from the current path. LLT: NA --- src/runtime/stringTable.cpp | 21 ++++++++++++++++++++- src/runtime/stringTable.hpp | 2 ++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/src/runtime/stringTable.cpp b/src/runtime/stringTable.cpp index 6961617..8fcfd0f 100644 --- a/src/runtime/stringTable.cpp +++ b/src/runtime/stringTable.cpp @@ -1,6 +1,8 @@ #include "runtime/stringTable.hpp" #include "object/hiString.hpp" #include "memory/oopClosure.hpp" +#include +#include StringTable* StringTable::instance = NULL; @@ -11,6 +13,17 @@ StringTable* StringTable::get_instance() { return instance; } +bool StringTable::get_absolute_path(char* path) { + int result = readlink("/proc/self/exe", path, MAX_PATH_LENGTH); + if(result == -1) return false; + //now path = /home/user/pythonvm/src/build/railgun + //modify path = /home/user/pythonvm/src/build/lib/ + int len = strlen(path); + int base_name = 7; //strlen("railgun") + strcpy(path + len - base_name, "lib/"); + return true; +} + StringTable::StringTable() { next_str = new HiString("next"); mod_str = new HiString("__module__"); @@ -27,7 +40,13 @@ StringTable::StringTable() { setattr_str = new HiString("__setattr__"); so_pre_str = new HiString("lib"); - libdir_pre_str = new HiString("./lib/"); + char* path = new char[MAX_PATH_LENGTH]; + if (get_absolute_path(path)) { + libdir_pre_str = new HiString(path); + } else { + libdir_pre_str = new HiString("./lib/"); + } + delete[] path; empty_str = new HiString(""); so_suf_str = new HiString(".so"); pyc_suf_str = new HiString(".pyc"); diff --git a/src/runtime/stringTable.hpp b/src/runtime/stringTable.hpp index 1921ca3..07ba1f0 100644 --- a/src/runtime/stringTable.hpp +++ b/src/runtime/stringTable.hpp @@ -6,6 +6,8 @@ class OopClosure; class StringTable { private: + static const int MAX_PATH_LENGTH = 128; + static bool get_absolute_path(char* path); static StringTable* instance; StringTable(); -- Gitee