question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

No easy way to get an isolated, customized SafeLoader

See original GitHub issue

In 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 use pyyaml 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:open
  • Created 5 years ago
  • Reactions:5
  • Comments:10

github_iconTop GitHub Comments

3reactions
akdor1154commented, Oct 28, 2018

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?

0reactions
timcharpercommented, Mar 16, 2021

It does! Thank you 😃

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found