Skip to content

Commit

Permalink
centralize python12 backwards compatibility function
Browse files Browse the repository at this point in the history
  • Loading branch information
ahnitz committed Jul 29, 2024
1 parent 2b25d21 commit b9d35b2
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 39 deletions.
14 changes: 1 addition & 13 deletions bin/all_sky_search/pycbc_cut_merge_triggers_to_tmpltbank
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,7 @@ import numpy
import h5py
import pycbc
from pycbc.io import HFile

import importlib.util
import importlib.machinery

def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module
from pycbc import load_source

parser = argparse.ArgumentParser(description=__doc__)
pycbc.add_common_pycbc_options(parser)
Expand Down
14 changes: 1 addition & 13 deletions bin/all_sky_search/pycbc_reduce_template_bank
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,7 @@ import logging
import argparse
import pycbc
from pycbc.io import HFile

import importlib.util
import importlib.machinery

def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module
from pycbc import load_source

parser = argparse.ArgumentParser(description=__doc__)
pycbc.add_common_pycbc_options(parser)
Expand Down
14 changes: 1 addition & 13 deletions bin/pycbc_banksim_match_combine
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,7 @@ from pycbc import pnutils
from pycbc.waveform import TemplateBank
from pycbc.io.ligolw import LIGOLWContentHandler
from pycbc.io.hdf import HFile

import importlib.util
import importlib.machinery

def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module
from pycbc import load_source

__author__ = "Ian Harry <[email protected]>"
__version__ = pycbc.version.git_verbose_msg
Expand Down
14 changes: 14 additions & 0 deletions pycbc/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import logging
import random
import string
import importlib.util
import importlib.machinery
from datetime import datetime as dt

try:
Expand Down Expand Up @@ -227,3 +229,15 @@ def gps_now():
from astropy.time import Time

return float(Time.now().gps)

# This is needed as a backwards compatibility. The function was removed in
# python 3.12.
def load_source(modname, filename):
loader = importlib.machinery.SourceFileLoader(modname, filename)
spec = importlib.util.spec_from_file_location(modname, filename, loader=loader)
module = importlib.util.module_from_spec(spec)
# The module is always executed and not cached in sys.modules.
# Uncomment the following line to cache the module.
# sys.modules[module.__name__] = module
loader.exec_module(module)
return module

0 comments on commit b9d35b2

Please sign in to comment.