Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
bpo-34213: __builtins__ bug
  • Loading branch information
VadimPushtaev committed Jul 26, 2018
commit 4b0ea3efb09f13e69e0736b9d40c15699ffec915
8 changes: 7 additions & 1 deletion Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import types
import inspect
import keyword
import builtins

__all__ = ['dataclass',
'field',
Expand Down Expand Up @@ -343,6 +344,11 @@ def _create_fn(name, args, body, *, globals=None, locals=None,
# worries about external callers.
if locals is None:
locals = {}
# __builtins__ may be the "builtins" module or
# the value of its "__dict__",
# so make sure "__builtins" is the module.
if globals is not None and '__builtins__' not in globals:
globals['__builtins__'] = builtins
return_annotation = ''
if return_type is not MISSING:
locals['_return_type'] = return_type
Expand All @@ -365,7 +371,7 @@ def _field_assign(frozen, name, value, self_name):
# self_name is what "self" is called in this function: don't
# hard-code "self", since that might be a field name.
if frozen:
return f'__builtins__["object"].__setattr__({self_name},{name!r},{value})'
return f'__builtins__.object.__setattr__({self_name},{name!r},{value})'
return f'{self_name}.{name}={value}'


Expand Down
23 changes: 13 additions & 10 deletions Lib/test/test_dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import pickle
import inspect
import builtins
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional
Expand Down Expand Up @@ -211,15 +212,16 @@ def test_field_named_like_builtin(self):
# since code generation is used.
# Ensure that this is not happening.
exclusions = {'None', 'True', 'False'}
builtins = sorted(
b for b in __builtins__.__dict__.keys()
builtins_names = sorted(
b for b in builtins.__dict__.keys()
if not b.startswith('__') and b not in exclusions
)
C = make_dataclass('C', [(name, str) for name in builtins])
attributes = [(name, str) for name in builtins_names]
C = make_dataclass('C', attributes)

c = C(*[name for name in builtins])
c = C(*[name for name in builtins_names])

for name in builtins:
for name in builtins_names:
self.assertEqual(getattr(c, name), name)

def test_field_named_like_builtin_frozen(self):
Expand All @@ -228,15 +230,16 @@ def test_field_named_like_builtin_frozen(self):
# Ensure that this is not happening
# for frozen data classes.
exclusions = {'None', 'True', 'False'}
builtins = sorted(
b for b in __builtins__.__dict__.keys()
builtins_names = sorted(
b for b in builtins.__dict__.keys()
if not b.startswith('__') and b not in exclusions
)
C = make_dataclass('C', [(name, str) for name in builtins], frozen=True)
attributes = [(name, str) for name in builtins_names]
C = make_dataclass('C', attributes, frozen=True)

c = C(*[name for name in builtins])
c = C(*[name for name in builtins_names])

for name in builtins:
for name in builtins_names:
self.assertEqual(getattr(c, name), name)

def test_0_field_compare(self):
Expand Down