Repository navigation
Conversation
pkgutil.get_loader was deprecated in Python 3.12 and removed in 3.14, so importing postgresql.python.command (and with it pg_python) fails with ImportError on 3.14. Resolve the loader with importlib.util.find_spec instead, which is available on every supported version.
project.py no longer defines author or date, so the combined import from .project raised ImportError after binding version and before __version__ was assigned. The error was swallowed, leaving the package without __version__ and breaking "from postgresql import *" (__all__ lists __author__ and __date__) and postgresql.bin.pg_dotconf, which does "from .. import __version__". Import only version_info and version, assign __version__ outside the try, and drop __author__ and __date__ from __all__.
The settings-file loop referenced the undefined names comment and unquote (NameError) instead of configfile.comment and configfile.unquote, and it tested the first character of the key rather than the text before it, so commented-out settings in the file would have been applied. Check line[:start], as alter_config() does.
Contributor
|
Thanks! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three commits.
1. Replace pkgutil.get_loader for 3.14
postgresql/python/command.pydoesfrom pkgutil import get_loader as module_loader.pkgutil.get_loaderwas deprecated in Python 3.12 and removed in 3.14, so on 3.14which also takes down
postgresql.bin.pg_python(thepg_pythonscript). On 3.12/3.13 it emits a DeprecationWarning on every-m/-C module:use.The change defines
module_loader(name)on top ofimportlib.util.find_spec()(available since 3.4, so fine forrequires-python >= 3.7), returning the spec's loader or None, which is whatExecutionexpects.2. Always set version; fixes pg_dotconf import
postgresql/__init__.pydoesinside
try/except ImportError.project.pyno longer definesauthorordate, so the import fails after bindingversionand before__version__is set; the error is swallowed. As a result, on any Python version:The change imports only
version_infoandversion, sets__version__after thetry, and drops__author__/__date__from__all__.3. Fix pg_dotconf -f/--file
With the import fixed,
pg_dotconf -f settings.conf postgresql.confhitsNameError: name 'comment' is not defined: the loop uses barecommentandunquoteinstead ofconfigfile.comment/configfile.unquote. It also testedline[pl[0].start](the first character of the key) for the comment marker, so a commented-out setting in the settings file would have been applied. It now checksline[:pl[0].start], asconfigfile.alter_config()does.Testing
Driving
postgresql.python.command.command()directly with['pg_python', '-C', 'module:pgcmd_ctx', '-m', 'pgcmd_main', 'a1', 'a2'](two small throwaway modules on sys.path), plus-m no_such_module_xyz, withDeprecationWarningturned into errors:DeprecationWarning: 'pkgutil.get_loader' is deprecated and slated for removal in Python 3.14.__main__with argv['a1', 'a2']and the context module's globals, returns 0; the missing module raisesImportError: Python module 'no_such_module_xyz' does not exist or cannot be readas before.python -m unittest postgresql.test.test_python: OK on all three.Commits 2 and 3:
import postgresql,from postgresql import *, thenpg_dotconf.command(['pg_dotconf', '--stdout', '-f', 'settings.conf', 'postgresql.conf'])with a settings file holdingport = 5433and#work_mem = 64MB, and a postgresql.conf holding#port = 5432,max_connections = 100,#work_mem = 4MB:__version__missing, star import AttributeError,import postgresql.bin.pg_dotconfImportError (CPython 3.12.13 and 3.13.14).commentincommand().__version__ == '1.3.1', star import OK, outputport = '5433',max_connections = 100,#work_mem = 4MB(the commented work_mem line in the settings file is skipped).python -m unittest postgresql.test.test_configfile: OK on all three.