No easy way to get an isolated, customized SafeLoader
See original GitHub issueIn all docs and example code I have seen, developers are instructed to register loaders and dumpers as:
import yaml
yaml.SafeLoader.add_constructor(...)
This works and is simple. However, it does not scale to the situation where a single process might want different loaders/dumpers , e.g. scenarios like
- one yaml tag set for a configuration file and another for saved state,
- a base project that uses
pyyaml
and has an in-process plugin system, and I’m writing a plugin that wants to usepyyaml
for something else
The only workaround I have found is to do
import importlib
import yaml.constructor as yamlConstructor
import yaml.loader as yamlLoader
import yaml.dumper as yamlDumper
import yaml.representer as yamlRepresenter
yamlConstructor = importlib.reload(yamlConstructor)
yamlRepresenter = importlib.reload(yamlRepresenter)
yamlLoader = importlib.reload(yamlLoader)
MyLoader = yamlLoader.SafeLoader
MyDumper = yamlDumper.SafeDumper
MyLoader.add_constructor(...)
which is pretty awful, and may break other code later it has been written to expect the static behaviour in accessing yaml.SafeLoader
directly.
My feature request is for to be able to do something like
import yaml
MyLoader = yaml.buildSafeLoader()
MyLoader.add_constructor(...)
which allows me to easily avoid the static configuration issues above.
Issue Analytics
- State:
- Created 5 years ago
- Reactions:5
- Comments:10
Top Results From Across the Web
TP-Link TL-WPA8630P v2 - OpenWRT
The problem for OpenWrt and other custom firmware is that there is no way to safeguard against a user accidentally uploading an image...
Read more >Difference between yaml.load and yaml.SafeLoader in PyYAML
import yaml env_variable_matcher = re.compile(r'<your custom pattern here>') def ... This is another way of using SafeLoader
Read more >82.0 LTS — Avocado 93.0 documentation
No issues were observed when doing an in-place upgrade from Python 2 based ... As an example, Avocado's safeloader is now able to...
Read more >Monofocal intraocular lens (IOL) | HumanOptics AG
XL diopter range, -20.0 to 60.0 D in 1.0 D steps 10.0 to 30.0 D in 0.5 D steps. Also preloaded in SAFELOADER®...
Read more >SigmaDSP control from a micro - is there an update to "Basic ...
I'm trying to help someone get a SigmaDSP integrated with a microcontroller. ... to see if the indirect method would make our life...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
For sure that sounds like something to avoid, but everyone I have seen using pyyaml is using it that way. The docs say to register constructors that way (using
yaml.add_constructor
, which is a top level module function that seems to work in this modify-built-in-classes way by default), and the top rated answers on stack overflow also recommend that developers do it that way. Maybe this could be considered a documentation bug then?It does! Thank you 😃