Skip to content

Commit

Permalink
python 3.12 module load support
Browse files Browse the repository at this point in the history
  • Loading branch information
goblinhack committed Feb 6, 2025
1 parent 37d2ce0 commit da0ec5d
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 11 deletions.
69 changes: 62 additions & 7 deletions python/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
import os
import fnmatch
from os.path import dirname, basename
import imp
import sys

if sys.version_info[0] == 3:
if sys.version_info[1] >= 5:
import importlib.util
elif sys.version_info[1] < 5:
import importlib.machinery
elif sys.version_info[0] == 2:
import imp

# import sys
import console
Expand Down Expand Up @@ -85,24 +93,71 @@ def load_one_plugin(filename):
if basename(filename) == "tp.py":
return

# my.log("PYC: - loading init plugin: " + filename)
my.log("PYC: - loading init plugin: " + filename)

mod_name, file_ext = os.path.splitext(os.path.split(filename)[-1])

if file_ext.lower() == ".py":
imp.load_source(mod_name, filename)
# py_mod = imp.load_source(mod_name, filename)
# for attr_name in dir(py_mod):
# attr_value = getattr(py_mod, attr_name)
# print(attr_name, attr_value, callable(attr_value))
if sys.version_info[0] == 3:
if sys.version_info[1] >= 5:
spec = importlib.util.spec_from_file_location(mod_name, filename)
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)
elif sys.version_info[1] < 5:
loader = importlib.machinery.SourceFileLoader(mod_name, filename)
loader.load_module()
elif sys.version_info[0] == 2:
imp.load_source(mod_name, filename)
# load_source(mod_name, filename)

elif file_ext.lower() == ".pyc":
imp.load_compiled(mod_name, filename)


@timeme
def load_all_plugins():

paths = [
'things/keys',
'things/mobs',
'things/staffs',
'things/armor',
'things/monsters',
'things/amulets',
'things/traps',
'things/player',
'things/lasers',
'things/bodyparts',
'things/spells',
'things/skills',
'things/projectiles',
'things/weapons',
'things/debuffs',
'things/food',
'things/treasure',
'things/buffs',
'things/totem',
'things/shield',
'things/internal',
'things/gauntlets',
'things/effects',
'things/boots',
'things/items',
'things/dungeon',
'things/rings',
'things/containers',
'things/fungus',
'things/cloaks',
'things/potions',
'things/doors',
]

plug_path = os.path.normcase(os.path.join(dirname(__file__), ""))

for path in paths:
my.log("PYC: Add path " + os.path.join(plug_path, path))
sys.path.append(os.path.join(plug_path, path))

my.log("PYC: Init module, load all plugins from " + plug_path)
for filename in find_plugins(plug_path, "*.py"):
load_one_plugin(filename)
Expand Down
1 change: 1 addition & 0 deletions src/python_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ void py_init(char *argv[])

my_mod = PyImport_ImportModule("init");
if (! my_mod) {
PyErr_Print();
py_err();
PY_ERR("Module init import failed");
return;
Expand Down
8 changes: 5 additions & 3 deletions src/thing_template.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -417,11 +417,13 @@ void tp_fini(void)
Tpp tp_load(int id, std::string const &name, const std::string &text_long_name, const std::string &text_short_name)
{
TRACE_NO_INDENT();
if (tp_find(name)) {
DIE("Thing template name [%s] already loaded", name.c_str());
Tpp tp = tp_find(name);
if (tp) {
LOG("Thing template name [%s] already loaded", name.c_str());
return tp;
}

auto tp = new Tp();
tp = new Tp();
tp->name_set(name);
tp->text_long_name_set(text_long_name);
tp->text_short_name_set(text_short_name);
Expand Down
1 change: 0 additions & 1 deletion src/wid.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5883,7 +5883,6 @@ void wid_key_down(const struct SDL_Keysym *key, int x, int y)
if (wid_focus && ! wid_is_hidden(wid_focus) && (wid_focus->on_key_down)) {
if ((wid_focus->on_key_down)(wid_focus, key)) {
DBG("WID: key grabbed by focused wid: %s at (%d,%d)", wid_focus->name.c_str(), ascii_mouse_x, ascii_mouse_y);
sound_play("keypress");
//
// Do not raise, gets in the way of popups the callback creates.
//
Expand Down

0 comments on commit da0ec5d

Please sign in to comment.