[CRIU] [PATCH 1/3] p.haul: minor style changes in p_haul_vz.py

Nikita Spiridonov nspiridonov at odin.com
Mon Nov 30 01:35:39 PST 2015


On Mon, 2015-11-30 at 12:16 +0300, Pavel Emelyanov wrote:
> On 11/26/2015 03:52 PM, Nikita Spiridonov wrote:
> > Rename parse_vz_config and expand_veid_var functions of p_haul_vz.py
> > (add leading underscore) to make it formally private module
> > function.
> 
> Single underbar doesn't make a method private.
> 
> [pavel at xemulnb ~]$ cat x.py 
> #!/bin/env python
> 
> class foo:
> 	def one():
> 		pass
> 
> 	def _two():
> 		pass
> 
> 	def __three():
> 		pass
> 
> 
> f = foo()
> print dir(f)
> [pavel at xemulnb ~]$ ./x.py 
> ['__doc__', '__module__', '_foo__three', '_two', 'one']
> 
> See? Both one() and _two() preserved their names, but in order to
> call __tree() you have to use "some other name" for it.

Your example is about class methods and I rename module global functions
in current patch. For module functions this name mangling don't work at
all:

[root:hwc:[v]]# cat bar.py
def one():
	pass

def _two():
	pass

def __three():
	pass

[root:hwc:[v]]# cat foo.py 
#! /usr/bin/python
import bar
print(dir(bar))

[root:hwc:[v]]# ./foo.py 
['__builtins__', '__doc__', '__file__', '__name__', '__package__',
'__three', '_two', 'one']

Moreover, it is impossible to call global function starting with double
underscore inside class method cause python parser handle it wrong:

[root:hwc:[v]]# cat buz.py 
#! /usr/bin/python

def __some_echo():
	print("some echo called!")

class foo:
	def __init__(self):
		__some_echo()

f = foo()

[root:hwc:[v]]# ./buz.py 
Traceback (most recent call last):
  File "./buz.py", line 10, in <module>
    f = foo()
  File "./buz.py", line 8, in __init__
    __some_echo()
NameError: global name '_foo__some_echo' is not defined

So I think such naming scheme (single starting underscore for internal
module methods) is a good idea.

> 
> > Signed-off-by: Nikita Spiridonov <nspiridonov at odin.com>
> > ---
> >  phaul/p_haul_vz.py |   16 ++++++++--------
> >  1 files changed, 8 insertions(+), 8 deletions(-)
> > 
> > diff --git a/phaul/p_haul_vz.py b/phaul/p_haul_vz.py
> > index 70a4620..b9ca5b7 100644
> > --- a/phaul/p_haul_vz.py
> > +++ b/phaul/p_haul_vz.py
> > @@ -36,11 +36,11 @@ class p_haul_type:
> >  
> >  		# Read container config
> >  		with open(self.__ct_config_path(path)) as ifd:
> > -			config = parse_vz_config(ifd.read())
> > +			config = _parse_vz_config(ifd.read())
> >  
> >  		# Read global config
> >  		with open(vz_global_conf) as ifd:
> > -			global_config = parse_vz_config(ifd.read())
> > +			global_config = _parse_vz_config(ifd.read())
> >  
> >  		# Extract veth pairs, later we will equip restore request with this
> >  		# data and will use it while (un)locking the network
> > @@ -60,16 +60,16 @@ class p_haul_type:
> >  
> >  		# Extract private path from config
> >  		if "VE_PRIVATE" in config:
> > -			self._ct_priv = expand_veid_var(config["VE_PRIVATE"], self._ctid)
> > +			self._ct_priv = _expand_veid_var(config["VE_PRIVATE"], self._ctid)
> >  		else:
> > -			self._ct_priv = expand_veid_var(global_config["VE_PRIVATE"],
> > +			self._ct_priv = _expand_veid_var(global_config["VE_PRIVATE"],
> >  				self._ctid)
> >  
> >  		# Extract root path from config
> >  		if "VE_ROOT" in config:
> > -			self._ct_root = expand_veid_var(config["VE_ROOT"], self._ctid)
> > +			self._ct_root = _expand_veid_var(config["VE_ROOT"], self._ctid)
> >  		else:
> > -			self._ct_root = expand_veid_var(global_config["VE_ROOT"],
> > +			self._ct_root = _expand_veid_var(global_config["VE_ROOT"],
> >  				self._ctid)
> >  
> >  	def __load_ct_config_dst(self, path):
> > @@ -240,7 +240,7 @@ class p_haul_type:
> >  		return True
> >  
> >  
> > -def parse_vz_config(body):
> > +def _parse_vz_config(body):
> >  	"""Parse shell-like virtuozzo config file"""
> >  
> >  	config_values = dict()
> > @@ -250,6 +250,6 @@ def parse_vz_config(body):
> >  	return config_values
> >  
> >  
> > -def expand_veid_var(value, ctid):
> > +def _expand_veid_var(value, ctid):
> >  	"""Replace shell-like VEID variable with actual container id"""
> >  	return value.replace("$VEID", ctid).replace("${VEID}", ctid)
> > 
> 




More information about the CRIU mailing list